Skip to content

Commit 474a9db

Browse files
committed
Don't error out when checking for token expiration on 404
When checking for token expiration the rate_limit api may not enabled on that token. Don't error out and just print a log message. Fixes #1107 Signed-off-by: Chmouel Boudjnah <[email protected]>
1 parent d8ad628 commit 474a9db

File tree

2 files changed

+44
-23
lines changed

2 files changed

+44
-23
lines changed

pkg/provider/github/github.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/base64"
66
"fmt"
7+
"net/http"
78
"net/url"
89
"strings"
910
"sync"
@@ -180,20 +181,25 @@ func makeClient(ctx context.Context, apiURL, token string) (*github.Client, stri
180181
func (v *Provider) checkWebhookSecretValidity(ctx context.Context, cw clockwork.Clock) error {
181182
rl, resp, err := v.Client.RateLimits(ctx)
182183
if resp.Header.Get("GitHub-Authentication-Token-Expiration") != "" && cw.Now().After(resp.TokenExpiration.Time) {
183-
errm := fmt.Sprintf("token has expired at %s", resp.TokenExpiration.Time.Format(time.RFC1123))
184+
errm := fmt.Sprintf("token has expired at %s", resp.TokenExpiration.Format(time.RFC1123))
184185
if err != nil {
185186
errm += fmt.Sprintf(" err: %s", err.Error())
186187
}
187188
return fmt.Errorf(errm)
188189
}
189190

191+
if resp.StatusCode == http.StatusNotFound {
192+
v.Logger.Info("skipping checking if token has expired, rate_limit api is not enabled on token")
193+
return nil
194+
}
195+
190196
// some other error happened that is not rate limited related
191197
if err != nil {
192198
return fmt.Errorf("error using token to access API: %w", err)
193199
}
194200

195201
if rl.SCIM.Remaining == 0 {
196-
return fmt.Errorf("token is ratelimited, it will be available again at %s", rl.SCIM.Reset.Time.Format(time.RFC1123))
202+
return fmt.Errorf("token is ratelimited, it will be available again at %s", rl.SCIM.Reset.Format(time.RFC1123))
197203
}
198204
return nil
199205
}

pkg/provider/github/github_test.go

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -623,11 +623,13 @@ func TestGetFiles(t *testing.T) {
623623
func TestProvider_checkWebhookSecretValidity(t *testing.T) {
624624
cw := clockwork.NewFakeClock()
625625
tests := []struct {
626-
name string
627-
wantSubErr string
628-
remaining int
629-
expTime time.Time
630-
expHeaderSet bool
626+
name string
627+
wantSubErr string
628+
remaining int
629+
expTime time.Time
630+
expHeaderSet bool
631+
apiNotEnabled bool
632+
wantLogSnippet string
631633
}{
632634
{
633635
name: "remaining scim calls",
@@ -657,39 +659,52 @@ func TestProvider_checkWebhookSecretValidity(t *testing.T) {
657659
remaining: 0,
658660
wantSubErr: "token is ratelimited",
659661
},
662+
{
663+
name: "not enabled",
664+
apiNotEnabled: true,
665+
wantLogSnippet: "skipping checking",
666+
},
660667
}
661668
for _, tt := range tests {
662669
t.Run(tt.name, func(t *testing.T) {
663670
ctx, _ := rtesting.SetupFakeContext(t)
664671
fakeclient, mux, _, teardown := ghtesthelper.SetupGH()
672+
logger, observer := logger.GetLogger()
665673

666-
mux.HandleFunc("/rate_limit", func(rw http.ResponseWriter, r *http.Request) {
667-
s := &github.RateLimits{
668-
SCIM: &github.Rate{
669-
Remaining: tt.remaining,
670-
},
671-
}
672-
st := new(struct {
673-
Resources *github.RateLimits `json:"resources"`
674+
if !tt.apiNotEnabled {
675+
mux.HandleFunc("/rate_limit", func(rw http.ResponseWriter, r *http.Request) {
676+
s := &github.RateLimits{
677+
SCIM: &github.Rate{
678+
Remaining: tt.remaining,
679+
},
680+
}
681+
st := new(struct {
682+
Resources *github.RateLimits `json:"resources"`
683+
})
684+
st.Resources = s
685+
b, _ := json.Marshal(st)
686+
rw.Header().Set("Content-Type", "application/json")
687+
if tt.expHeaderSet {
688+
rw.Header().Set("GitHub-Authentication-Token-Expiration", tt.expTime.Format("2006-01-02 03:04:05 MST"))
689+
}
690+
fmt.Fprint(rw, string(b))
674691
})
675-
st.Resources = s
676-
b, _ := json.Marshal(st)
677-
rw.Header().Set("Content-Type", "application/json")
678-
if tt.expHeaderSet {
679-
rw.Header().Set("GitHub-Authentication-Token-Expiration", tt.expTime.Format("2006-01-02 03:04:05 MST"))
680-
}
681-
fmt.Fprint(rw, string(b))
682-
})
692+
}
683693
defer teardown()
684694
v := &Provider{
685695
Client: fakeclient,
696+
Logger: logger,
686697
}
687698
err := v.checkWebhookSecretValidity(ctx, cw)
688699
if tt.wantSubErr != "" {
689700
assert.ErrorContains(t, err, tt.wantSubErr)
690701
} else {
691702
assert.NilError(t, err)
692703
}
704+
705+
if tt.wantLogSnippet != "" {
706+
assert.Assert(t, observer.FilterMessageSnippet(tt.wantLogSnippet).Len() > 0)
707+
}
693708
})
694709
}
695710
}

0 commit comments

Comments
 (0)