-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(github): validate-comment (#120)
- Loading branch information
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,39 @@ | ||
# <!--name-->github/validate-comment<!--/name--> | ||
|
||
[![usages](https://img.shields.io/badge/usages-white?logo=githubactions&logoColor=blue)](https://github.com/search?q=elastic%2Foblt-actions%2Fgithub%2Fvalidate-comment+%28path%3A.github%2Fworkflows+OR+path%3A**%2Faction.yml+OR+path%3A**%2Faction.yaml%29&type=code) | ||
|
||
<!--description--> | ||
Check whether the GitHub comment was triggered by a user with write permissions | ||
<!--/description--> | ||
|
||
## Inputs | ||
<!--inputs--> | ||
| Name | Description | Required | Default | | ||
|------|-------------|----------|---------| | ||
<!--/inputs--> | ||
|
||
## Exported Environment Variables | ||
|
||
| name | description | | ||
|------------|-------------------------| | ||
| `GIT_USER` | <p>Git username</p> | | ||
| `GIT_EMAIL`| <p>Git email</p> | | ||
|
||
## Usage | ||
|
||
<!--usage action="elastic/oblt-actions/**" version="env:VERSION"--> | ||
```yaml | ||
--- | ||
name: Is GitHub comment allowed | ||
on: | ||
issue_comment: | ||
types: [created] | ||
jobs: | ||
run-action-if-comment: | ||
if: github.event.issue.pull_request && startsWith(github.event.comment.body, '/run-test') | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: elastic/oblt-actions/github/validate-comment@v1 | ||
# ... | ||
``` | ||
<!--/usage--> |
This file contains 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,33 @@ | ||
name: 'github/validate-comment' | ||
|
||
description: Check whether the GitHub comment was triggered by a user with write permissions | ||
|
||
runs: | ||
using: "composite" | ||
steps: | ||
- name: Is comment allowed? | ||
uses: actions/github-script@v7 | ||
with: | ||
script: | | ||
// Report with a reaction that the event has been listened | ||
await github.rest.reactions.createForIssueComment({ | ||
...context.repo, | ||
comment_id: context.payload.comment.id, | ||
content: '+1', | ||
}) | ||
const actorPermission = (await github.rest.repos.getCollaboratorPermissionLevel({ | ||
...context.repo, | ||
username: context.actor | ||
})).data.permission | ||
const isPermitted = ['write', 'admin'].includes(actorPermission) | ||
if (!isPermitted) { | ||
const errorMessage = 'Only users with write permission to the repository can run GitHub commands' | ||
await github.rest.issues.createComment({ | ||
...context.repo, | ||
issue_number: context.issue.number, | ||
body: errorMessage, | ||
}) | ||
core.setFailed(errorMessage) | ||
return; | ||
} |