Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
chrstph-dvx committed Sep 20, 2024
1 parent 0b9ea94 commit 883ebec
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 48 deletions.
46 changes: 21 additions & 25 deletions .github/actions/update-check-run/index.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,38 @@
const core = require("@actions/core");
const github = require("@actions/github");

module.exports = async ({ github, context, core }, inputs) => {
async function run() {
try {
const token = inputs.token;
const status = inputs.status;
const conclusion = inputs.conclusion;
const detailsUrl = inputs["details-url"];
const token = core.getInput("github-token", { required: true });
const status = core.getInput("status", { required: true });
const conclusion = core.getInput("conclusion");
const detailsUrl = core.getInput("details-url");
const checkName = "CCTP E2E Tests";

const octokit = github.getOctokit(token);
const context = github.context;
const { owner, repo } = context.repo;

let pull_number, head_sha;

if (context.eventName === "issue_comment") {
pull_number = context.issue.number;
} else if (context.eventName === "pull_request_review") {
pull_number = context.payload.pull_request.number;
} else if (
context.eventName === "push" ||
context.eventName === "merge_group"
) {
head_sha = context.sha;
} else {
core.setFailed("Unexpected event type");
return;
}

if (!head_sha) {
const { data: pr } = await octokit.rest.pulls.get({
owner,
repo,
pull_number,
});
head_sha = pr.head.sha;
} else if (context.eventName === "pull_request_review") {
pull_number = context.payload.pull_request.number;
head_sha = context.payload.pull_request.head.sha;
} else {
core.setFailed("Unexpected event type");
return;
}

const checkName = "CCTP E2E Tests";

// Check if a check run already exists
const { data: existingChecks } = await octokit.rest.checks.listForRef({
// Get all check runs for the current SHA
const { data: allCheckRuns } = await octokit.rest.checks.listForRef({
owner,
repo,
ref: head_sha,
Expand Down Expand Up @@ -67,12 +60,13 @@ module.exports = async ({ github, context, core }, inputs) => {
? "Tests are currently in progress. Results will be updated upon completion."
: `For detailed information, please check the [workflow run](${detailsUrl}).`,
},
details_url: detailsUrl,
};

if (existingChecks.check_runs.length > 0) {
if (allCheckRuns[0]) {
// Update existing check run
await octokit.rest.checks.update({
check_run_id: existingChecks.check_runs[0].id,
check_run_id: allCheckRuns[0].id,
...checkRunData,
});
} else {
Expand All @@ -82,4 +76,6 @@ module.exports = async ({ github, context, core }, inputs) => {
} catch (error) {
core.setFailed(error.message);
}
};
}

run();
48 changes: 25 additions & 23 deletions .github/workflows/run-cctp-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

jobs:
initialize-check:
name: Initialize Check
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set check to in-progress
uses: actions/github-script@v7
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const script = require('./.github/actions/update-check-run/index.js')
await script({github, context, core}, {
token: process.env.GITHUB_TOKEN,
status: 'in_progress',
'details-url': 'https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}',
})
check-files:
name: Check files
runs-on: ubuntu-latest
Expand Down Expand Up @@ -60,23 +78,6 @@ jobs:
echo "should_run=false" >> $GITHUB_OUTPUT
fi
set-check-in-progress:
runs-on: ubuntu-latest
if: >-
github.event_name == 'issue_comment' &&
github.event.comment.body == '/run-cctp-tests' &&
contains(fromJson('["CONTRIBUTOR", "OWNER", "MEMBER"]'), github.event.comment.author_association)
steps:
- name: Set check run to in-progress
uses: actions/github-script@v7
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const script = require('./.github/actions/update-check-run/index.js')
await script({github, context, core})
result-encoding: string
status: 'in_progress'

build:
name: "Build"
runs-on: ubuntu-latest
Expand Down Expand Up @@ -110,7 +111,7 @@ jobs:

update-pr-status:
name: "Update PR Status"
needs: [cctp-e2e-tests, set-check-in-progress]
needs: [cctp-e2e-tests, initialize-check]
runs-on: ubuntu-latest
if: always()
steps:
Expand All @@ -120,8 +121,9 @@ jobs:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const script = require('./.github/actions/update-check-run/index.js')
await script({github, context, core})
result-encoding: string
status: 'completed'
conclusion: ${{ needs.cctp-e2e-tests.result }}
details-url: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
await script({github, context, core}, {
token: process.env.GITHUB_TOKEN,
status: 'completed',
conclusion: '${{ needs.cctp-e2e-tests.result }}',
'details-url': 'https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}',
})

0 comments on commit 883ebec

Please sign in to comment.