Skip to content

Commit 993f67a

Browse files
Add an error message for when you don't have write access to the repository you are updating.
Co-authored-by: Simon Engledew <[email protected]>
1 parent ad4a06f commit 993f67a

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

internal/push/push.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,29 @@ func (pushService *pushService) createRepository() (*github.Repository, error) {
111111
return nil, errors.Wrap(err, "Error creating destination repository.")
112112
}
113113
} else {
114+
if githubapiutil.HasAnyScope(response, "site_admin") {
115+
impersonationToken, _, err := pushService.githubEnterpriseClient.Admin.CreateUserImpersonation(pushService.ctx, "actions-admin", &github.ImpersonateUserOptions{Scopes: []string{"repo"}})
116+
if err != nil {
117+
return nil, errors.Wrap(err, "Failed to impersonate Actions admin user.")
118+
}
119+
tokenSource := oauth2.StaticTokenSource(
120+
&oauth2.Token{AccessToken: impersonationToken.GetToken()},
121+
)
122+
tokenClient := oauth2.NewClient(pushService.ctx, tokenSource)
123+
pushService.destinationToken = impersonationToken.GetToken()
124+
pushService.githubEnterpriseClient, err = github.NewEnterpriseClient(pushService.githubEnterpriseClient.BaseURL.String(), pushService.githubEnterpriseClient.UploadURL.String(), tokenClient)
125+
if err != nil {
126+
return nil, errors.Wrap(err, "Error creating GitHub Enterprise client.")
127+
}
128+
}
114129
repository, response, err = pushService.githubEnterpriseClient.Repositories.Edit(pushService.ctx, pushService.destinationRepositoryOwner, pushService.destinationRepositoryName, &desiredRepositoryProperties)
115130
if err != nil {
116-
if response.StatusCode == http.StatusNotFound && !githubapiutil.HasAnyScope(response, "public_repo", "repo") {
117-
return nil, usererrors.New("The destination token you have provided does not have the `public_repo` scope.")
131+
if response.StatusCode == http.StatusNotFound {
132+
if !githubapiutil.HasAnyScope(response, "public_repo", "repo") {
133+
return nil, usererrors.New("The destination token you have provided does not have the `public_repo` scope.")
134+
} else {
135+
return nil, fmt.Errorf("You don't have permission to update the repository at %s/%s.", pushService.destinationRepositoryOwner, pushService.destinationRepositoryName)
136+
}
118137
}
119138
return nil, errors.Wrap(err, "Error updating destination repository.")
120139
}

0 commit comments

Comments
 (0)