Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: skip permission verification exception when Github Apps #272

Merged
merged 1 commit into from
May 22, 2020

Conversation

psalaberria002
Copy link
Contributor

@psalaberria002 psalaberria002 commented May 19, 2020

Similar to Github Actions, when using installation tokens from Github Apps the verification should be skipped.

Should we use a more generic flag for skipping the permission validation? e.g. SKIP_PERMISSIONS?

I will add the relevant docs to the Readme once I get the ok.

@psalaberria002 psalaberria002 changed the title Skip permission verification when using Github Apps fix: skip permission verification when using Github Apps May 19, 2020
lib/verify.js Outdated
// have all permissions required for @semantic-release/github to work
if (env.GITHUB_ACTION) {
if (env.GITHUB_ACTION || env.GITHUB_APP) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where does the GITHUB_APP environment variable come from? If it's a custom environment variable that you set in your particular case, then I don't think we should add a check for it to semantic-release

@gr2m
Copy link
Member

gr2m commented May 19, 2020

Can you share your semantic-release setup? I wonder how you create an installation access token and pass it to semantic release :)

@psalaberria002
Copy link
Contributor Author

psalaberria002 commented May 19, 2020

This is running in Jenkins with the new auth method for the Github plugin. See https://www.jenkins.io/blog/2020/04/16/github-app-authentication/

Example:

withCredentials([usernamePassword(credentialsId: scm.userRemoteConfigs[0].credentialsId, passwordVariable: 'GH_TOKEN', usernameVariable: 'GH_USER')]) {
              sh("GH_TOKEN=${GH_TOKEN} npx semantic-release")
            }

Github Apps create installation tokens the same way Github Actions do. These tokens have short expiration time.

The flag will need to be provided as an extra environment variable. Something like the following:

withCredentials([usernamePassword(credentialsId: scm.userRemoteConfigs[0].credentialsId, passwordVariable: 'GH_TOKEN', usernameVariable: 'GH_USER')]) {
              sh("SKIP_PERMISSION_VERIFICATION="true" GH_TOKEN=${GH_TOKEN} npx semantic-release")
            }

Github Apps have lots of advantages over personal access tokens. Higher rate limits, scopes permissions,...

@gr2m
Copy link
Member

gr2m commented May 19, 2020

Is see.

I wish there was a way to check if the current installation has access and the required permissions, but I don't think there is right now.

I suggest to add a skipVerify option to address your use case. Would that work for you?

@psalaberria002
Copy link
Contributor Author

That should work. I will try to add that flag.

@gr2m
Copy link
Member

gr2m commented May 20, 2020

Good point, I think we shouldn't skip all the verifications, only the permissions verifications 🤔

I have another idea. We could send a request that is guaranteed to succeed when authenticated as an installation, but fail otherwise. I would suggest we send a HEAD /installation/repositories. We are only interested in the response status, and sending a HEAD is faster than a GET.

What I would like to try is the following:

If push is false here, send the following request

// If authenticated as GitHub App installation, `push` will always be false.
// We send another request to check if current authentication is an installation.
// Note: we cannot check if the installation has all required permissions, it's
// up to the user to make sure it has
if (await github.request('HEAD /installation/repositories', { per_page: 1 }).catch(() => return false) {
  return
}

I think I'd prefer doing that check to address your specific use case, over adding a new configuration option. What do you think?

For testing: HEAD /installation/repositories responds with a 200 status without a body if it succeeds, and with a 403 status if it fails

@psalaberria002 psalaberria002 changed the title fix: skip permission verification when using Github Apps fix: skip permission verification exception when Github Apps May 22, 2020
Copy link
Member

@gr2m gr2m left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good 👍

@gr2m gr2m merged commit 32654fb into semantic-release:master May 22, 2020
@semantic-release-bot
Copy link
Collaborator

🎉 This PR is included in version 7.0.7 🎉

The release is available on:

Your semantic-release bot 📦🚀

@psalaberria002
Copy link
Contributor Author

@gr2m I don't think this is working as expected. "Accept: application/vnd.github.machine-man-preview+json" is needed in installation/repositories. How do you handle that?

@psalaberria002
Copy link
Contributor Author

And there is an issue in the semantic-release repo aswell, which needs to be solved like semantic-release/semantic-release@cb2c506

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants