Skip to content

Commit

Permalink
Merge pull request #1 from iomarmochtar/fix-non-expires-token
Browse files Browse the repository at this point in the history
fix(expires_token): handler once expires token is not set
  • Loading branch information
iomarmochtar authored Oct 26, 2024
2 parents 6405361 + 61a1a5c commit 1446d9d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 0.1.1

### Bug Fixes

- handling once the returned access token is without expires

# 0.1.0

### Features and enhancements
Expand Down
6 changes: 3 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,10 @@ func (g *GitlabTokenUpdater) Do() error {

befDur, _ := at.cfgAccessToken.RenewBeforeDuration()
addMe := g.now.Add(befDur)
expiredAt := *at.glAccessToken.ExpiresAt
expiresAt := at.glAccessToken.ExpiresAt
validToRenew := false
if addMe.After(expiredAt) {
logTkn.Warn().Msgf("reach renew time. expired: %v, renew before: %s", expiredAt, at.cfgAccessToken.RenewBefore)
if expiresAt != nil && addMe.After(*expiresAt) {
logTkn.Warn().Msgf("reach renew time. expired: %v, renew before: %s", expiresAt, at.cfgAccessToken.RenewBefore)
validToRenew = true
}

Expand Down
48 changes: 47 additions & 1 deletion app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,27 @@ func TestGitlabTokenUpdater_Do(t *testing.T) {
currentTime *time.Time
expectedErrMsg string
}{
"success: simple scenario": {
config: func() *cfg.Config {
c := t_helper.GenConfig(nil, nil, nil)
return c
},
currentTime: t_helper.GenTime("2024-04-05"),
mockGitlab: func(ctrl *gomock.Controller) *gm.MockGitlabAPI {
newToken := "glpat-newnew"
g := gm.NewMockGitlabAPI(ctrl)

accessTokens := []gl.GitlabAccessToken{t_helper.SampleRepoAccessToken}
g.EXPECT().ListRepoAccessToken(t_helper.SampleRepoPath).Return(accessTokens, nil)
g.EXPECT().RotateRepoToken(t_helper.SampleRepoPath, 123, *t_helper.GenTime("2024-07-04")).Return(newToken, nil)
g.EXPECT().UpdateRepoVar(t_helper.SampleRepoPath, t_helper.SampleCICDVar, newToken).Return(nil)

return g
},
mockShell: func(ctrl *gomock.Controller) *sm.MockShell {
return nil
},
},
"update access token that will expired in 1 month ahead and execute all hooks": {
config: func() *cfg.Config {
anotherManageTokens := t_helper.GenManageTokens(nil, nil, nil)
Expand Down Expand Up @@ -59,7 +80,7 @@ func TestGitlabTokenUpdater_Do(t *testing.T) {
return s
},
},
"no access token will be expired and no hooks executed": {
"skip: no access token will be expired and no hooks executed": {
config: func() *cfg.Config {
return t_helper.GenConfig(nil, nil, nil)
},
Expand All @@ -74,6 +95,31 @@ func TestGitlabTokenUpdater_Do(t *testing.T) {
return nil
},
},
"skip: access token without expiry excluded from the execution": {
config: func() *cfg.Config {
return t_helper.GenConfig(nil, nil, nil)
},
currentTime: t_helper.GenTime("2024-01-01"),
mockGitlab: func(ctrl *gomock.Controller) *gm.MockGitlabAPI {
accessTokens := []gl.GitlabAccessToken{
{
Name: "MR Handler",
Type: gl.GitlabTargetTypeRepo,
ID: 123,
Path: t_helper.SampleRepoPath,
Active: true,
Revoked: false,
ExpiresAt: nil,
},
}
g := gm.NewMockGitlabAPI(ctrl)
g.EXPECT().ListRepoAccessToken(t_helper.SampleRepoPath).Return(accessTokens, nil)
return g
},
mockShell: func(*gomock.Controller) *sm.MockShell {
return nil
},
},
"any errors occured in the middle execution will not break the iterrations but will be raised in the end": {
config: func() *cfg.Config {
// there are 4 managed
Expand Down

0 comments on commit 1446d9d

Please sign in to comment.