Refresh token before executing a cron pipeline (#1432)

Co-authored-by: Lauris BH <lauris@nix.lv>
Co-authored-by: Anbraten <anton@ju60.de>
This commit is contained in:
smainz 2022-11-19 21:47:47 +01:00 committed by GitHub
parent dfd4622ba2
commit 47faff3eff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -105,7 +105,7 @@ func runCron(store store.Store, forge forge.Forge, cron *model.Cron, now time.Ti
return err return err
} }
func CreatePipeline(ctx context.Context, store store.Store, forge forge.Forge, cron *model.Cron) (*model.Repo, *model.Pipeline, error) { func CreatePipeline(ctx context.Context, store store.Store, f forge.Forge, cron *model.Cron) (*model.Repo, *model.Pipeline, error) {
repo, err := store.GetRepo(cron.RepoID) repo, err := store.GetRepo(cron.RepoID)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
@ -121,7 +121,23 @@ func CreatePipeline(ctx context.Context, store store.Store, forge forge.Forge, c
return nil, nil, err return nil, nil, err
} }
commit, err := forge.BranchHead(ctx, creator, repo, cron.Branch) // if the forge has a refresh token, the current access token
// may be stale. Therefore, we should refresh prior to dispatching
// the pipeline.
if refresher, ok := f.(forge.Refresher); ok {
refreshed, err := refresher.Refresh(ctx, creator)
log.Debug().Msgf("token refreshed: %t", refreshed)
if err != nil {
log.Error().Err(err).Msgf("failed to refresh oauth2 token for creator: %s", creator.Login)
} else if refreshed {
if err := store.UpdateUser(creator); err != nil {
log.Error().Err(err).Msgf("error while updating creator: %s", creator.Login)
// move forward
}
}
}
commit, err := f.BranchHead(ctx, creator, repo, cron.Branch)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }