Skip to content

Commit 607c4d4

Browse files
ci: PLT-104: Add git merge chatops command
1 parent 0e67f83 commit 607c4d4

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed

.github/workflows/git-command.yml

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
name: "/git command"
2+
3+
on:
4+
repository_dispatch:
5+
types: [ git-command ]
6+
7+
concurrency:
8+
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 }}
9+
10+
jobs:
11+
merge:
12+
if: ${{ github.event.client_payload.slash_command.args.unnamed.arg1 == 'merge' }}
13+
runs-on: ubuntu-latest
14+
timeout-minutes: 10
15+
steps:
16+
- uses: hmarr/[email protected]
17+
18+
- name: Add Workflow link to command comment
19+
uses: peter-evans/create-or-update-comment@v4
20+
with:
21+
token: ${{ secrets.GIT_PAT }}
22+
repository: ${{ github.event.client_payload.github.payload.repository.full_name }}
23+
comment-id: ${{ github.event.client_payload.github.payload.comment.id }}
24+
body: |
25+
> [Workflow run](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})
26+
27+
- name: Checkout
28+
uses: actions/checkout@v4
29+
with:
30+
token: ${{ secrets.GIT_PAT }}
31+
repository: ${{ github.event.client_payload.pull_request.head.repo.full_name }}
32+
ref: ${{ github.event.client_payload.pull_request.head.ref }}
33+
34+
- name: Checkout Actions Hub
35+
uses: actions/checkout@v4
36+
with:
37+
token: ${{ secrets.GIT_PAT }}
38+
repository: HumanSignal/actions-hub
39+
path: ./.github/actions-hub
40+
41+
- name: Git Configure
42+
uses: ./.github/actions-hub/actions/git-configure
43+
with:
44+
username: ${{ github.event.client_payload.github.actor }}
45+
46+
- name: Git Merge
47+
id: merge
48+
uses: ./.github/actions-hub/actions/git-merge
49+
with:
50+
base_branch: ${{ github.event.client_payload.slash_command.args.unnamed.arg2 || github.event.client_payload.pull_request.base.ref }}
51+
our_files: ""
52+
53+
- name: Git Push
54+
if: steps.merge.outputs.result == 'true'
55+
uses: ./.github/actions-hub/actions/git-push
56+
57+
- name: Add reaction to command comment
58+
uses: peter-evans/create-or-update-comment@v4
59+
if: always()
60+
with:
61+
token: ${{ secrets.GIT_PAT }}
62+
repository: ${{ github.event.client_payload.github.payload.repository.full_name }}
63+
comment-id: ${{ github.event.client_payload.github.payload.comment.id }}
64+
body: |
65+
> ${{ steps.merge.outputs.message || '**Error**: Workflow failed' }}
66+
reactions: ${{ steps.merge.outputs.reaction || '-1' }}
67+
68+
help:
69+
if: ${{ github.event.client_payload.slash_command.args.unnamed.arg1 == 'help' || !contains(fromJson('["merge"]'), github.event.client_payload.slash_command.args.unnamed.arg1) }}
70+
runs-on: ubuntu-latest
71+
timeout-minutes: 1
72+
steps:
73+
- name: Update comment
74+
uses: peter-evans/create-or-update-comment@v4
75+
with:
76+
token: ${{ secrets.GIT_PAT }}
77+
repository: ${{ github.event.client_payload.github.payload.repository.full_name }}
78+
comment-id: ${{ github.event.client_payload.github.payload.comment.id }}
79+
body: |
80+
> Command | Description
81+
> --- | ---
82+
> /git merge `branch` | Merge branch `branch` into current branch
83+
reactions: hooray
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
name: Slash Command Dispatch
2+
on:
3+
issue_comment:
4+
types: [created]
5+
6+
env:
7+
pull_request_commands_list: |
8+
git
9+
10+
jobs:
11+
slashCommandDispatch:
12+
if: startsWith(github.event.comment.body, '/')
13+
timeout-minutes: 1
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: hmarr/[email protected]
17+
18+
- name: "React to comment"
19+
uses: peter-evans/create-or-update-comment@v4
20+
with:
21+
token: ${{ secrets.GIT_PAT }}
22+
comment-id: ${{ github.event.comment.id }}
23+
reactions: eyes
24+
25+
- name: "Validate command"
26+
id: determine_command
27+
uses: actions/github-script@v7
28+
env:
29+
COMMANDS_LIST: ${{ github.event.issue.pull_request && env.pull_request_commands_list }}
30+
with:
31+
github-token: ${{ secrets.GIT_PAT }}
32+
script: |
33+
const body = context.payload.comment.body.toLowerCase().trim();
34+
const commands_list = process.env.COMMANDS_LIST.split("\n");
35+
console.log(commands_list);
36+
const command = body.replace(/^\/+/, '').split(/\s+/)[0];
37+
core.setOutput('command', command);
38+
core.setOutput('command-state', commands_list.includes(command) ? 'known' : 'unknown');
39+
40+
- name: "Slash Command Dispatch"
41+
id: scd_issues
42+
if: steps.determine_command.outputs.command-state == 'known'
43+
uses: peter-evans/slash-command-dispatch@v4
44+
with:
45+
token: ${{ secrets.GIT_PAT }}
46+
reaction-token: ${{ secrets.GIT_PAT }}
47+
issue-type: ${{ github.event.issue.pull_request && 'pull-request' || 'issue' }}
48+
reactions: true
49+
commands: ${{ github.event.issue.pull_request && env.pull_request_commands_list }}
50+
51+
- name: "Edit comment with error message"
52+
if: steps.determine_command.outputs.command-state != 'known'
53+
uses: peter-evans/create-or-update-comment@v4
54+
with:
55+
token: ${{ secrets.GIT_PAT }}
56+
comment-id: ${{ github.event.comment.id }}
57+
body: |
58+
> '/${{ steps.determine_command.outputs.command }}' is an unknown ${{ github.event.issue.pull_request && 'pull-request' || 'issue' }} command.
59+
> See '/help'
60+
reactions: confused

0 commit comments

Comments
 (0)