From a839397bf3e326c41fa3825938c061fcb779a18e Mon Sep 17 00:00:00 2001 From: Andrea Frittoli Date: Thu, 5 Dec 2024 12:51:40 +0000 Subject: [PATCH] Add /retest chatops command Add a /retest chatops command that reruns failed GitHub actions. This uses github.com/peter-evans/slash-command-dispatch which intercepts the comment on the PR, filters based on the role of the commenter and dispatches an event with the original payload to run the command. The chatops_retest workflow receives the event, extracts the details about the run_ids from the GitHub API and runs failed jobs, thanks for the gh CLI. Signed-off-by: Andrea Frittoli --- .github/workflows/chatops_retest.yaml | 68 +++++++++++++++++++++++++++ .github/workflows/slash.yml | 41 ++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 .github/workflows/chatops_retest.yaml create mode 100644 .github/workflows/slash.yml diff --git a/.github/workflows/chatops_retest.yaml b/.github/workflows/chatops_retest.yaml new file mode 100644 index 00000000000..01f3388c6b4 --- /dev/null +++ b/.github/workflows/chatops_retest.yaml @@ -0,0 +1,68 @@ +# The _chatops_retest workflow reruns failed GHA for a PR +# +# This workflow is triggered by leaving a "/retest" comment on +# a pull request. If the required preconditions are met, it will +# rerun failed GitHub actions checks on that PR +# +# Condition for the "/retest" command are: +# - either the issuer is a maintainer +# - or the issuer is the owner the PR + +name: Rerun Failed Actions +on: + repository_dispatch: + types: [retest-command] + +jobs: + retest: + name: Rerun Failed Actions + runs-on: ubuntu-latest + steps: + - name: Show Environment Variables + run: env + - name: Show Github Object + run: | + cat <<'EOF' + ${{ toJson(github) }} + EOF + - name: Show Github Event Path Json + run: 'cat $GITHUB_EVENT_PATH || true' + - name: Rerun Failed Actions + run: | + # Get a list of run IDs + RUN_IDS=$(gh api repos/${GITHUB_REPO}/commits/${GITHUB_COMMIT_SHA}/check-runs | \ + jq -r '.check_runs[] | select(.name != "Rerun Failed Actions") | .html_url | capture("/runs/(?[0-9]+)/job") | .number' | \ + sort -u) + + # For each run, retrigger faild jobs + for runid in ${RUN_IDS}; do + gh run \ + --repo ${GITHUB_REPO} \ + rerun ${runid} \ + --failed + done + env: + GITHUB_TOKEN: ${{ secrets.CHATOPS_TOKEN }} + GITHUB_REPO: ${{ github.event.client_payload.github.payload.repository.full_name }} + GITHUB_COMMIT_SHA: ${{ github.event.client_payload.github.payload.commit.sha }} + + - name: Create comment + if: ${{ failure() && steps.landStack.outcome == 'failure' }} + uses: peter-evans/create-or-update-comment@v4 + with: + token: ${{ secrets.CHATOPS_TOKEN }} + repository: ${{ github.event.client_payload.github.payload.repository.full_name }} + issue-number: ${{ github.event.client_payload.github.payload.issue.number }} + body: | + Something went wrong with your `/${{ github.event.client_payload.slash_command.command }}` command: [please check the logs][1]. + + [1]: ${{ steps.vars.outputs.run-url }} + + - name: Add reaction + if: ${{ success() }} + uses: peter-evans/create-or-update-comment@v4 + with: + token: ${{ secrets.CHATOPS_TOKEN }} + repository: ${{ github.event.client_payload.github.payload.repository.full_name }} + comment-id: ${{ github.event.client_payload.github.payload.comment.id }} + reaction-type: hooray \ No newline at end of file diff --git a/.github/workflows/slash.yml b/.github/workflows/slash.yml new file mode 100644 index 00000000000..d8b7ae2657c --- /dev/null +++ b/.github/workflows/slash.yml @@ -0,0 +1,41 @@ +# The slash workflow handles slash commands +# +# Slash commands are given through comments on pull requests +# and may be used only by individuals with "write" access to +# the repository (i.e. maintainers). +# +# Slash commands must be placed at the very beginning of the +# first line of a comment. More details are available in the +# action docs: https://github.com/peter-evans/slash-command-dispatch/tree/main?tab=readme-ov-file#how-comments-are-parsed-for-slash-commands +# +# The workflow looks for and dispatches to another workflow +# named -command which must exist in the repository. +# +# Supported commands: +# - /land: invokes the land-command workflow, to land (merge) PRs +# stacked through ghstack +# +# When a command is recognised, the rocket and eyes emojis are added + +name: Slash Command Routing +on: + issue_comment: + types: [created] + +jobs: + check_comments: + runs-on: ubuntu-latest + steps: + - name: route-land + uses: peter-evans/slash-command-dispatch@v4 + with: + token: ${{ secrets.CHATOPS_TOKEN }} + config: > + [ + { + "command": "retest", + "permission": "write", + "issue_type": "pull-request", + "repository": "tektoncd/pipeline" + } + ] \ No newline at end of file