Skip to content

Commit e11e24c

Browse files
committed
feat(github): update token refresh logic to handle old token comparison
1 parent b3fa376 commit e11e24c

File tree

4 files changed

+11
-4
lines changed

4 files changed

+11
-4
lines changed

backend/plugins/github/models/connection_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ limitations under the License.
1818
package models
1919

2020
import (
21-
"github.com/apache/incubator-devlake/helpers/pluginhelper/api"
2221
"testing"
2322

23+
"github.com/apache/incubator-devlake/helpers/pluginhelper/api"
2424
"github.com/go-playground/validator/v10"
2525
"github.com/stretchr/testify/assert"
2626
)

backend/plugins/github/token/round_tripper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (rt *RefreshRoundTripper) RoundTrip(req *http.Request) (*http.Response, err
5656
resp.Body.Close()
5757

5858
// Force refresh
59-
if err := rt.tokenProvider.ForceRefresh(); err != nil {
59+
if err := rt.tokenProvider.ForceRefresh(token); err != nil {
6060
return nil, err
6161
}
6262

backend/plugins/github/token/token_provider.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,15 @@ func (tp *TokenProvider) refreshToken() errors.Error {
157157
return nil
158158
}
159159

160-
func (tp *TokenProvider) ForceRefresh() errors.Error {
160+
func (tp *TokenProvider) ForceRefresh(oldToken string) errors.Error {
161161
tp.mu.Lock()
162162
defer tp.mu.Unlock()
163+
164+
// If the token has changed since the request was made, it means another thread
165+
// has already refreshed it. We don't need to do anything.
166+
if tp.conn.Token != oldToken {
167+
return nil
168+
}
169+
163170
return tp.refreshToken()
164171
}

backend/plugins/github/token/token_provider_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func TestPersistenceFailure(t *testing.T) {
167167

168168
// Mock DAL failure
169169
mockDal.On("UpdateColumns", mock.Anything, mock.Anything, mock.Anything).Return(errors.Default.New("db error")) // Force refresh
170-
err := tp.ForceRefresh()
170+
err := tp.ForceRefresh("")
171171
assert.NoError(t, err) // Should not return error even if persistence fails
172172

173173
mockRT.AssertExpectations(t)

0 commit comments

Comments
 (0)