Skip to content

Commit

Permalink
feat: add a /ready command (#160)
Browse files Browse the repository at this point in the history
# Description

Adds the `/ready` slash ops command, which

- fails if at least one item in the check list is still open
- removes the check list from the PR body if all items are checked.

Fixes #11 

# Verification

Manually verified in this PR.
  • Loading branch information
kayman-mk authored Jan 7, 2025
1 parent 49bdaff commit 2326aab
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
name: Execute ChatOps command
name: Execute ChatOps help command

# yamllint disable-line rule:truthy
on:
Expand Down
46 changes: 46 additions & 0 deletions .github/workflows/default_slash_ops_command_ready_callable.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
name: Execute ChatOps ready command

# yamllint disable-line rule:truthy
on:
# USE_WORKFLOW
workflow_call:
# /USE_WORKFLOW
# USE_REPOSITORY
# repository_dispatch:
# types:
# - ready-command
# /USE_REPOSITORY

jobs:
ready-command:
name: "ChatOps: /ready"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

- name: Create comment
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
// get the PR text and make sure that all items are checked
const prText = context.payload.pull_request.body;
prText.split('\n').forEach(line => {
if (line.startsWith('- [ ]')) {
throw new Error('Please check all items in the checklist before marking the PR as ready! Unchecked item: ' + line);
}
});
// remove the checklist paragraph from "# Checklist" to the end of the PR text
const prTextWithoutChecklist = prText.split('\n').slice(0, prText.split('\n').
findIndex(line => line.startsWith('# Checklist'))).join('\n');
github.rest.pulls.update({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number,
body: prTextWithoutChecklist,
});
// set the PR to ready --> this is not possible with the GitHub API, use GraphQL instead
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ jobs:
reactions: true
commands: |
help
ready
30 changes: 27 additions & 3 deletions .github/workflows/default_welcome_message_callable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,37 @@ jobs:
steps:
- uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
# yamllint disable rule:line-length
script: |
message = `Hey @${{ github.event.pull_request.user.login }}! 👋
Thank you for your contribution to the project. Please refer to the
[contribution rules](../blob/main/.github/CONTRIBUTING.md) for a quick overview of the process.
Make sure that this PR clearly explains:
- the problem being solved
- the best way a reviewer and you can test your changes
With submitting this PR you confirm that you hold the rights of the code added and agree that it will
published under this [LICENSE](../blob/main/LICENSE).
The following ChatOps commands are supported:
- \`/help\`: notifies a maintainer to help you out
- \`/ready\`: marks the PR as ready for review and removes the checklist
Simply add a comment with the command in the first line. If you need to pass more information, separate it
with a blank line from the command.
_This message was generated automatically. You are welcome to
[improve it](../blob/main/.github/workflows/default_welcome_message_callable.yml)._`
// dedent the message
message = message.replace(/^ +/gm, '')
// adds a comment to the PR (there is the issue API only which works work PRs too)
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'Hey @${{ github.event.pull_request.user.login }}! 👋\n\nThank you for your contribution to the project. Please refer to the [contribution rules](../blob/main/.github/CONTRIBUTING.md) for a quick overview of the process.\n\nMake sure that this PR clearly explains:\n\n- the problem being solved\n- the best way a reviewer and you can test your changes\n\nWith submitting this PR you confirm that you hold the rights of the code added and agree that it will published under this [LICENSE](../blob/main/LICENSE).\n\nThe following ChatOps commands are supported:\n- `/help`: notifies a maintainer to help you out\n\nSimply add a comment with the command in the first line. If you need to pass more information, separate it with a blank line from the command.\n\n_This message was generated automatically. You are welcome to [improve it](https://github.com/Hapag-Lloyd/Workflow-Templates/blob/main/.github/workflows/default_welcome_message_callable.yml)._'
body: message,
})
# yamllint enable rule:line-length
13 changes: 13 additions & 0 deletions .github/workflows/this_slash_ops_command_ready.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
name: Execute ChatOps command

# yamllint disable-line rule:truthy
on:
repository_dispatch:
types:
- ready-command

jobs:
default:
uses: Hapag-Lloyd/Workflow-Templates/.github/workflows/default_slash_ops_command_ready_callable.yml@main
secrets: inherit

0 comments on commit 2326aab

Please sign in to comment.