Skip to content

Commit

Permalink
feat(github): validate-comment (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
v1v authored Sep 16, 2024
1 parent 5dc5897 commit 1e369e6
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
39 changes: 39 additions & 0 deletions github/validate-comment/README.md
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-->
33 changes: 33 additions & 0 deletions github/validate-comment/action.yml
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;
}

0 comments on commit 1e369e6

Please sign in to comment.