diff --git a/server/forge/bitbucket/bitbucket.go b/server/forge/bitbucket/bitbucket.go index 3d4505fc91..d005ccf223 100644 --- a/server/forge/bitbucket/bitbucket.go +++ b/server/forge/bitbucket/bitbucket.go @@ -32,6 +32,7 @@ import ( "go.woodpecker-ci.org/woodpecker/v3/server/forge/common" forge_types "go.woodpecker-ci.org/woodpecker/v3/server/forge/types" "go.woodpecker-ci.org/woodpecker/v3/server/model" + "go.woodpecker-ci.org/woodpecker/v3/server/store" "go.woodpecker-ci.org/woodpecker/v3/shared/httputil" shared_utils "go.woodpecker-ci.org/woodpecker/v3/shared/utils" ) @@ -423,6 +424,14 @@ func (c *config) Hook(ctx context.Context, req *http.Request) (*model.Repo, *mod return nil, nil, err } + // Refresh the OAuth token before making API calls. + // The token may be expired, and without this refresh the API calls below + // would fail with "OAuth2 access token expired" error. + _store, ok := store.TryFromContext(ctx) + if ok { + forge.Refresh(ctx, c, _store, u) + } + switch pl.Event { case model.EventPush: // List only the latest push changes diff --git a/server/forge/github/github.go b/server/forge/github/github.go index a22722e530..94330329f3 100644 --- a/server/forge/github/github.go +++ b/server/forge/github/github.go @@ -692,6 +692,11 @@ func (c *client) loadChangedFilesFromPullRequest(ctx context.Context, pull *gith return nil, err } + // Refresh the OAuth token before making API calls. + // The token may be expired, and without this refresh the API calls below + // would fail with an authentication error. + forge.Refresh(ctx, c, _store, user) + gh := c.newClientToken(ctx, user.AccessToken) fileList := make([]string, 0, 16) @@ -730,6 +735,11 @@ func (c *client) getTagCommitSHA(ctx context.Context, repo *model.Repo, tagName return "", err } + // Refresh the OAuth token before making API calls. + // The token may be expired, and without this refresh the API calls below + // would fail with an authentication error. + forge.Refresh(ctx, c, _store, user) + gh := c.newClientToken(ctx, user.AccessToken) page := 1 @@ -785,6 +795,11 @@ func (c *client) loadChangedFilesFromCommits(ctx context.Context, tmpRepo *model return nil, err } + // Refresh the OAuth token before making API calls. + // The token may be expired, and without this refresh the API calls below + // would fail with an authentication error. + forge.Refresh(ctx, c, _store, user) + gh := c.newClientToken(ctx, user.AccessToken) fileList := make([]string, 0, 16)