From 381e59156d5072aa99ff4804241fffebfaaad995 Mon Sep 17 00:00:00 2001 From: Gem Lamont <106068376+gem-neo4j@users.noreply.github.com> Date: Fri, 15 Nov 2024 11:01:36 +0100 Subject: [PATCH] Add new trello action (#687) --- .../traceability-comments-trigger.yaml | 126 ++++++++++++++++++ ...ability.yml => traceability-comments.yaml} | 7 +- 2 files changed, 129 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/traceability-comments-trigger.yaml rename .github/workflows/{traceability.yml => traceability-comments.yaml} (59%) diff --git a/.github/workflows/traceability-comments-trigger.yaml b/.github/workflows/traceability-comments-trigger.yaml new file mode 100644 index 000000000..38b5c7f17 --- /dev/null +++ b/.github/workflows/traceability-comments-trigger.yaml @@ -0,0 +1,126 @@ +name: traceability-trigger + +permissions: + checks: write + pull-requests: read + +on: + issue_comment: + types: + - created + - edited + - deleted + +jobs: + traceability-trigger: + runs-on: ubuntu-latest + if: github.event.issue.pull_request + steps: + - name: "Get Pull Request for Comment" + uses: actions/github-script@v6 + id: get-commit + with: + script: | + const request = { + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: context.issue.number + } + core.info(`Getting PR #${request.pull_number} from ${request.owner}/${request.repo}`) + try { + const result = await github.rest.pulls.get(request) + core.debug(result) + return result.data + } catch (err) { + core.setFailed(`Request failed with error ${err}`) + } + - name: "Get Check Run For Pull Request" + uses: actions/github-script@v6 + id: get-check-runs + with: + script: | + const request = { + owner: context.repo.owner, + repo: context.repo.repo, + ref: `${{ fromJSON(steps.get-commit.outputs.result).head.sha }}`, + check_name: 'traceability', + filter: 'latest' + } + core.info(`Getting check-runs for ${request.ref} from ${request.owner}/${request.repo}`) + try { + const result = await github.rest.checks.listForRef(request) + core.debug(result) + if (result.data.check_runs.length > 0) { + return { + runs: JSON.stringify(result.data.check_runs), + continue: true + } + } + else { + return { + continue: false + } + } + } catch (err) { + core.setFailed(`Request failed with error ${err}`) + } + - name: "Cancel Check Runs" + uses: actions/github-script@v6 + id: cancel-check-run + if: ${{ fromJSON(steps.get-check-runs.outputs.result).continue }} + with: + script: | + const checkRuns = ${{ fromJSON(steps.get-check-runs.outputs.result).runs }} + for (const checkRun of checkRuns) { + const checkRunId = checkRun.id + const checkRunStatus = checkRun.status + const request = { + owner: context.repo.owner, + repo: context.repo.repo, + check_run_id: checkRunId, + conclusion: 'cancelled', + output: { + title: 'Cancelled.', + summary: 'Check Run cancelled by "traceability-trigger.yaml".', + } + } + if (checkRunStatus !== 'completed') { + core.info(`Cancelling check-run ${checkRunId} from ${request.owner}/${request.repo}`) + try { + const result = await github.rest.checks.update(request) + core.debug(result) + } catch (err) { + core.setFailed(`Request failed with error ${err}`) + } + } else { + core.info(`No need to cancel check-run ${checkRunId}`) + } + } + - name: "Re-request Check Suite" + uses: actions/github-script@v6 + id: rerequest-check-suite + if: ${{ fromJSON(steps.get-check-runs.outputs.result).continue }} + with: + script: | + const checkRuns = ${{ fromJSON(steps.get-check-runs.outputs.result).runs }} + for (const checkRun of checkRuns) { + const checkSuiteId = checkRun.check_suite.id + const request = { + owner: context.repo.owner, + repo: context.repo.repo, + check_suite_id: checkSuiteId + } + try { + core.info(`Re-requesting check-suite ${request.check_suite_id} from ${request.owner}/${request.repo}`) + const result = await github.rest.checks.rerequestSuite(request) + core.debug(result) + } catch (err) { + // we cancelled any running check-suite in the step before this one, so if we get an error here saying + // a check-suite is already re-running it means something else triggered it to rerun between this step + // and the last. That is what we wanted to do anyways, so we can ignore these errors. + if (err.response.data.message === 'This check suite is already re-running.') { + return + } + core.setFailed(`Request failed with error ${err}`) + } + } diff --git a/.github/workflows/traceability.yml b/.github/workflows/traceability-comments.yaml similarity index 59% rename from .github/workflows/traceability.yml rename to .github/workflows/traceability-comments.yaml index 6b099a7bf..0c9a76b0e 100644 --- a/.github/workflows/traceability.yml +++ b/.github/workflows/traceability-comments.yaml @@ -14,10 +14,9 @@ jobs: validate-pr: runs-on: ubuntu-latest steps: - - uses: neo4j/github-action-traceability@v1 + - uses: neo4j/github-action-traceability@v2 with: - global_verification_strategy: commits_and_pr_title - short_link_verification_strategy: trello_or_noid + global_verification_strategy: comments trello_api_key: ${{ secrets.TRELLO_API_KEY }} trello_api_token: ${{ secrets.TRELLO_API_TOKEN }} - github_api_token: ${{ secrets.GITHUB_TOKEN }} + github_api_token: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file