-
-
Notifications
You must be signed in to change notification settings - Fork 7
feat: add authorize-commenter action #286
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
Draft
cryptodev-2s
wants to merge
4
commits into
main
Choose a base branch
from
feat/authorize-commenter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+106
−0
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ecfe105
feat: add authorize-commenter action
cryptodev-2s a91e4e9
docs: link the changelog entry to the pull request
cryptodev-2s f0291a4
feat: fail on unauthorized instead of returning an output
cryptodev-2s d30ca1c
test: keep the action test and record the verified token behavior
cryptodev-2s File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| name: Authorize commenter | ||
| description: >- | ||
| Check that the author of a comment has write access to the repository, and | ||
| fail if they do not. | ||
|
|
||
| Workflows triggered by `issue_comment` run from the default branch with | ||
| access to secrets, so gating them on the comment body alone lets anyone who | ||
| can comment start them. Run this action as the first job of such a workflow | ||
| and give every privileged job `needs` on that job. A failed dependency skips | ||
| the jobs that need it, so there is no condition to forget. | ||
|
|
||
| This authorizes the person who wrote the comment, not the code they wrote it | ||
| on. A workflow that passes this gate and then checks out the pull request | ||
| head or merge ref runs untrusted code with the privileges the gate was | ||
| protecting. | ||
|
|
||
| The default `GITHUB_TOKEN` can read collaborator permissions with no more | ||
| than `contents: read`. If a token ever cannot, this action fails, so an | ||
| underpowered token denies everyone rather than quietly authorizing them. | ||
|
|
||
| inputs: | ||
| username: | ||
| description: The login to authorize. Defaults to the author of the comment | ||
| that triggered the workflow. | ||
| required: true | ||
| default: ${{ github.event.comment.user.login }} | ||
|
|
||
| github-token: | ||
| description: The GitHub token to use for authentication. Defaults to the | ||
| standard GITHUB_TOKEN. | ||
| required: true | ||
| default: ${{ github.token }} | ||
|
|
||
| runs: | ||
| using: composite | ||
| steps: | ||
| - name: Check the commenter's repository permission | ||
| uses: actions/github-script@v9 | ||
| env: | ||
| USERNAME: ${{ inputs.username }} | ||
| with: | ||
| github-token: ${{ inputs.github-token }} | ||
| script: | | ||
| const { USERNAME } = process.env; | ||
|
|
||
| if (!USERNAME) { | ||
| return core.setFailed('No username to authorize. Pass `username`, or run this action on an event that has a comment author.'); | ||
| } | ||
|
|
||
| // `permission` collapses custom roles onto admin, write, read and | ||
| // none, so admin and write are exactly the logins that can push. | ||
| // Everyone else reads back as `read` on a public repository and | ||
| // `none` on a private one. A login that no longer resolves, such as | ||
| // `ghost`, raises instead, which fails this step rather than | ||
| // authorizing. | ||
| const { data } = await github.rest.repos.getCollaboratorPermissionLevel({ | ||
| ...context.repo, | ||
| username: USERNAME, | ||
| }); | ||
|
|
||
| if (!['admin', 'write'].includes(data.permission)) { | ||
| return core.setFailed(`@${USERNAME} has ${data.permission} permission on this repository, which is not enough to run this command.`); | ||
| } | ||
|
|
||
| core.info(`@${USERNAME} has ${data.permission} permission.`); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| name: Test "Authorize Commenter" Action | ||
|
|
||
| # Unlike the other test-*.yml workflows, this one triggers directly rather than | ||
| # through workflow_call, so that it actually runs on every pull request. | ||
|
|
||
| on: pull_request | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| test: | ||
| name: Test | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout repository | ||
| uses: actions/checkout@v6 | ||
|
|
||
| # Skipped on forks, where the author has read access and would be refused. | ||
| - name: A login with write access is authorized | ||
| if: github.event.pull_request.head.repo.fork == false | ||
| uses: ./.github/actions/authorize-commenter | ||
| with: | ||
| username: ${{ github.actor }} | ||
|
|
||
| - name: A login without write access is refused | ||
| id: refused | ||
| continue-on-error: true | ||
| uses: ./.github/actions/authorize-commenter | ||
| with: | ||
| username: octocat | ||
|
|
||
| - name: Fail if the refusal did not happen | ||
| if: steps.refused.outcome != 'failure' | ||
| run: | | ||
| echo "Expected octocat to be refused, but the action succeeded." | ||
| exit 1 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bot PRs fail write-access test
Low Severity
The write-access step treats
github.actoras a collaborator with push rights on every non-fork pull request. Same-repository bot PRs, including Dependabot, usually getnonefrom the permission API, so that step fails and the new check goes red.Reviewed by Cursor Bugbot for commit d30ca1c. Configure here.