diff --git a/CHANGELOG.md b/CHANGELOG.md index 3eae1a2..5ef14f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# 0.1.1 + +### Bug Fixes + +- handling once the returned access token is without expires + # 0.1.0 ### Features and enhancements diff --git a/app/app.go b/app/app.go index d67f223..cc2800e 100644 --- a/app/app.go +++ b/app/app.go @@ -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 } diff --git a/app/app_test.go b/app/app_test.go index 96d1e35..b75ac3c 100644 --- a/app/app_test.go +++ b/app/app_test.go @@ -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) @@ -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) }, @@ -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