Skip to content

Commit

Permalink
ci: PLT-462: add chatops commands
Browse files Browse the repository at this point in the history
  • Loading branch information
farioas committed Aug 14, 2024
1 parent ea653ae commit 2a16d91
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 45 deletions.
1 change: 0 additions & 1 deletion .github/workflows/docker-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ jobs:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: 'recursive'
token: ${{ github.token }}
ref: ${{ inputs.sha }}
fetch-depth: 0
Expand Down
87 changes: 87 additions & 0 deletions .github/workflows/git-command.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
name: "/git command"

on:
repository_dispatch:
types: [ git-command ]

concurrency:
group: ${{ github.workflow }}-${{ github.event.client_payload.github.payload.issue.number }}-${{ github.event.client_payload.slash_command.command }}-${{ github.event.client_payload.slash_command.args.unnamed.arg1 || github.event.client_payload.slash_command.args.all }}

jobs:
merge:
if: ${{ github.event.client_payload.slash_command.args.unnamed.arg1 == 'merge' }}
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- uses: hmarr/[email protected]

- name: Add Workflow link to command comment
uses: peter-evans/create-or-update-comment@v4
with:
token: ${{ secrets.GIT_PAT }}
repository: ${{ github.event.client_payload.github.payload.repository.full_name }}
comment-id: ${{ github.event.client_payload.github.payload.comment.id }}
body: |
> [Workflow run](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})
- name: Checkout
uses: actions/checkout@v4
with:
token: ${{ secrets.GIT_PAT }}
repository: ${{ github.event.client_payload.pull_request.head.repo.full_name }}
ref: ${{ github.event.client_payload.pull_request.head.ref }}
fetch-depth: 0

- name: Checkout Actions Hub
uses: actions/checkout@v4
with:
token: ${{ secrets.GIT_PAT }}
repository: HumanSignal/actions-hub
path: ./.github/actions-hub

- name: Git Configure
uses: ./.github/actions-hub/actions/git-configure
with:
username: ${{ github.event.client_payload.github.actor }}

# Merge

- name: Git Merge
id: merge
uses: ./.github/actions-hub/actions/git-merge
with:
base_branch: ${{ github.event.client_payload.slash_command.args.unnamed.arg2 || github.event.client_payload.pull_request.base.ref }}
head_branch: ${{ github.event.client_payload.pull_request.head.ref }}
our_files: "pyproject.toml poetry.lock"

- name: Git Push
if: steps.merge.outputs.result == 'true'
uses: ./.github/actions-hub/actions/git-push

- name: Add reaction to command comment
uses: peter-evans/create-or-update-comment@v4
if: always()
with:
token: ${{ secrets.GIT_PAT }}
repository: ${{ github.event.client_payload.github.payload.repository.full_name }}
comment-id: ${{ github.event.client_payload.github.payload.comment.id }}
body: |
> ${{ steps.merge.outputs.message || '**Error**: Workflow failed' }}
reactions: ${{ steps.merge.outputs.reaction || '-1' }}

help:
if: ${{ github.event.client_payload.slash_command.args.unnamed.arg1 == 'help' || !contains(fromJson('["merge"]'), github.event.client_payload.slash_command.args.unnamed.arg1) }}
runs-on: ubuntu-latest
timeout-minutes: 1
steps:
- name: Update comment
uses: peter-evans/create-or-update-comment@v4
with:
token: ${{ secrets.GIT_PAT }}
repository: ${{ github.event.client_payload.github.payload.repository.full_name }}
comment-id: ${{ github.event.client_payload.github.payload.comment.id }}
body: |
> Command | Description
> --- | ---
> /git merge `branch` | Merge branch `branch` into current branch
reaction-type: hooray
23 changes: 23 additions & 0 deletions .github/workflows/help-command.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: "/help command"

on:
repository_dispatch:
types: [ help-command ]
jobs:
help:
runs-on: ubuntu-latest
timeout-minutes: 1
steps:
- name: Update comment if empty
if: ${{ github.event.client_payload.slash_command.args.all == '' }}
uses: peter-evans/create-or-update-comment@v4
with:
token: ${{ secrets.GIT_PAT }}
repository: ${{ github.event.client_payload.github.payload.repository.full_name }}
comment-id: ${{ github.event.client_payload.github.payload.comment.id }}
body: |
> Command | Description
> --- | ---
> /git [\<args\> ...] | Actions with git. Type `/git help` for an additional help.
> /jira [\<args\> ...] | Actions with JIRA. Type `/jira help` for an additional help.
reaction-type: hooray
71 changes: 27 additions & 44 deletions .github/workflows/slash-command-dispatch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ on:
types: [created]

env:
commands_list: |
jira
issue_commands_list: |
pull_request_commands_list: |
help
git
jira
jobs:
Expand All @@ -18,62 +17,46 @@ jobs:
steps:
- uses: hmarr/[email protected]

- name: 'Validate command'
- name: "React to comment"
uses: peter-evans/create-or-update-comment@v4
with:
token: ${{ secrets.GIT_PAT }}
comment-id: ${{ github.event.comment.id }}
reactions: eyes

- name: "Validate command"
id: determine_command
uses: actions/github-script@v7
env:
COMMANDS_LIST: ${{ env.commands_list }}
ISSUE_COMMANDS_LIST: ${{ env.issue_commands_list }}
COMMANDS_LIST: ${{ github.event.issue.pull_request && env.pull_request_commands_list }}
with:
github-token: ${{ secrets.GIT_PAT }}
script: |
const body = context.payload.comment.body.toLowerCase().trim()
const commands_list = process.env.COMMANDS_LIST.split("\n")
const issue_commands_list = process.env.ISSUE_COMMANDS_LIST.split("\n")
console.log("Detected PR comment: " + body)
console.log("Commands list: " + commands_list)
console.log("Issue commands list: " + issue_commands_list)
commandArray = body.split(/\s+/)
const contextCommand = commandArray[0].split('/')[1].trim();
console.log("contextCommand: " + contextCommand)
core.setOutput('command_state', 'known')
core.setOutput('is_issue_command', 'false')
if (! commands_list.includes(contextCommand)) {
core.setOutput('command_state', 'unknown')
core.setOutput('command', contextCommand)
}
if (issue_commands_list.includes(contextCommand)) {
core.setOutput('is_issue_command', 'true')
}
const body = context.payload.comment.body.toLowerCase().trim();
const commands_list = process.env.COMMANDS_LIST.split("\n");
console.log(commands_list);
const command = body.replace(/^\/+/, '').split(/\s+/)[0];
core.setOutput('command', command);
core.setOutput('command-state', commands_list.includes(command) ? 'known' : 'unknown');
- name: Slash Command Dispatch for Issues
- name: "Slash Command Dispatch"
id: scd_issues
if: ${{ steps.determine_command.outputs.command_state != 'unknown' && steps.determine_command.outputs.is_issue_command == 'true' }}
if: steps.determine_command.outputs.command-state == 'known'
uses: peter-evans/slash-command-dispatch@v4
with:
token: ${{ secrets.GIT_PAT }}
reaction-token: ${{ secrets.GIT_PAT }}
issue-type: "issue"
issue-type: ${{ github.event.issue.pull_request && 'pull-request' || 'issue' }}
reactions: true
commands: ${{ env.issue_commands_list }}
commands: ${{ github.event.issue.pull_request && env.pull_request_commands_list }}

- name: Slash Command Dispatch for PRs
id: scd_prs
if: ${{ steps.determine_command.outputs.command_state != 'unknown' && steps.determine_command.outputs.is_issue_command != 'true' }}
uses: peter-evans/slash-command-dispatch@v4
with:
token: ${{ secrets.GIT_PAT }}
reaction-token: ${{ secrets.GIT_PAT }}
issue-type: "pull-request"
reactions: true
commands: ${{ env.commands_list }}

- name: Edit comment with error message
if: ${{ steps.determine_command.outputs.command_state == 'unknown' }}
- name: "Edit comment with error message"
if: steps.determine_command.outputs.command-state != 'known'
uses: peter-evans/create-or-update-comment@v4
with:
token: ${{ secrets.GIT_PAT }}
comment-id: ${{ github.event.comment.id }}
body: |
> '/${{ steps.determine_command.outputs.command }}' is an unknown command.
> '/${{ steps.determine_command.outputs.command }}' is an unknown ${{ github.event.issue.pull_request && 'pull-request' || 'issue' }} command.
> See '/help'
reactions: eyes, confused
reactions: confused

0 comments on commit 2a16d91

Please sign in to comment.