Skip to content

Commit

Permalink
feat(ci): pass pr number as cache
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxMustermann2 committed Nov 21, 2024
1 parent dddebd7 commit 5f7363a
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 46 deletions.
98 changes: 53 additions & 45 deletions .github/actions/composite-action-pr-status-comment/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ inputs:
github-token:
description: 'The GitHub token to use for making API requests.'
required: true
pr-number:
description: 'The pull request number to comment on.'
required: true
runs:
using: "composite"
steps:
Expand All @@ -21,7 +24,8 @@ runs:
run: |
echo "Workflow conclusion: ${{ inputs.workflow-conclusion }}"
echo "Workflow name: ${{ inputs.workflow-name }}"
echo "Workflow URL: ::add-mask::${{ inputs.workflow-url }}"
echo "Workflow URL: ${{ inputs.workflow-url }}"
echo "PR number: ${{ inputs.pr-number }}"
- name: Dump GitHub context
shell: bash
env:
Expand All @@ -35,53 +39,57 @@ runs:
const workflowConclusion = "${{ inputs.workflow-conclusion }}";
const workflowRunUrl = "${{ inputs.workflow-url }}";
const workflowName = "${{ inputs.workflow-name }}";
const pullRequests = context.payload.workflow_run.pull_requests;
const prNumber = parseInt("${{ inputs.pr-number }}", 10);
console.log(`Processing pull request #${prNumber}`);
console.log(`Commenting on the ${workflowName} workflow with status: ${workflowConclusion}`);
if (pullRequests.length === 0) {
console.log('No pull requests associated with this workflow run.');
} else {
for (const pr of pullRequests) {
try {
const existingComments = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
});
if (!prNumber) {
console.log("No valid pull request number provided. Skipping comment.");
return;
}
const existingComment = existingComments.data.find(comment =>
comment.body.includes(`The ${workflowName} workflow`)
);
try {
const existingComments = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
let commentBody;
const existingComment = existingComments.data.find(comment =>
comment.body.includes(`The ${workflowName} workflow`)
);
if (workflowConclusion === 'running') {
commentBody = `🔄 The ${workflowName} workflow is currently running. Check the [workflow run](${workflowRunUrl}) for progress.`;
} else if (workflowConclusion === 'failure') {
commentBody = `⚠️ The ${workflowName} workflow has failed! Check the [workflow run](${workflowRunUrl}) for details.`;
} else if (workflowConclusion === 'success') {
commentBody = `✅ The ${workflowName} workflow has completed successfully. Check the [workflow run](${workflowRunUrl}) for details.`;
} else if (workflowConclusion === 'skipped') {
commentBody = `⏭️ The ${workflowName} workflow was skipped. Check the [workflow run](${workflowRunUrl}) for details.`;
} else {
commentBody = `❓ The ${workflowName} workflow has completed with an unknown status. Check the [workflow run](${workflowRunUrl}) for details.`;
}
if (existingComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existingComment.id,
body: commentBody,
});
} else {
await github.rest.issues.createComment({
issue_number: pr.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: commentBody,
});
}
} catch (error) {
console.error(`Failed to comment on PR #${pr.number}: ${error.message}`);
}
let commentBody;
if (workflowConclusion === 'running') {
commentBody = `🔄 The ${workflowName} workflow is currently running. Check the [workflow run](${workflowRunUrl}) for progress.`;
} else if (workflowConclusion === 'failure') {
commentBody = `⚠️ The ${workflowName} workflow has failed! Check the [workflow run](${workflowRunUrl}) for details.`;
} else if (workflowConclusion === 'success') {
commentBody = `✅ The ${workflowName} workflow has completed successfully. Check the [workflow run](${workflowRunUrl}) for details.`;
} else if (workflowConclusion === 'skipped') {
commentBody = `⏭️ The ${workflowName} workflow was skipped. Check the [workflow run](${workflowRunUrl}) for details.`;
} else {
console.log(`Unknown workflow conclusion: ${workflowConclusion}`);
commentBody = `❓ The ${workflowName} workflow has completed with an unknown status. Check the [workflow run](${workflowRunUrl}) for details.`;
}
if (existingComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existingComment.id,
body: commentBody,
});
} else {
await github.rest.issues.createComment({
issue_number: prNumber,
owner: context.repo.owner,
repo: context.repo.repo,
body: commentBody,
});
}
} catch (error) {
console.error(`Failed to comment on PR #${prNumber} for workflow ${workflowName}: ${error.message}`);
throw error;
}
10 changes: 10 additions & 0 deletions .github/workflows/compare-layouts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@ on:
- completed

jobs:
restore:
name: Restore the cached PR number, if available
uses: ./.github/workflows/reusable-pr-number-restore.yml
with:
commit-hash: ${{ github.event.workflow_run.head_commit.id }}

# The actual job to compare the storage layouts.
compare_storage_layouts:
needs: restore
runs-on: ubuntu-latest

permissions:
Expand All @@ -33,6 +40,7 @@ jobs:
echo "Workflow run URL: ${{ github.event.workflow_run.html_url }}"
echo "Commit SHA: ${{ github.event.workflow_run.head_commit.id }}"
echo "Workflow Run ID: ${{ github.event.workflow_run.id }}"
echo "PR number: ${{ needs.restore.outputs.pr-number }}"
# For the composite action to be available, we must checkout the repository.
# Later, the repository is used to run the compareLayouts.js script.
- name: Checkout repository
Expand All @@ -48,6 +56,7 @@ jobs:
workflow-url: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
workflow-name: "Storage layout comparison"
github-token: ${{ secrets.GITHUB_TOKEN }}
pr-number: ${{ needs.restore.outputs.pr-number }}
- name: Restore the cached output file
if: ${{ github.event.workflow_run.conclusion == 'success' }}
uses: actions/cache/restore@v3
Expand Down Expand Up @@ -145,6 +154,7 @@ jobs:
workflow-url: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
workflow-name: "Storage layout comparison"
github-token: ${{ secrets.GITHUB_TOKEN }}
pr-number: ${{ needs.restore.outputs.pr-number }}
- name: Update parent commit status
if: always()
# if the outcome is not set, it will post failure
Expand Down
7 changes: 7 additions & 0 deletions .github/workflows/forge-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ on:
- "*"

jobs:
cache:
name: Cache the PR number, if available
uses: ./.github/workflows/reusable-pr-number-cache.yml
with:
commit-hash: ${{ github.sha }}
pr-number: ${{ github.event.pull_request.number || 0 }}

setup:
# A full job can be used as a reusable workflow.
# However, individual steps cannot call a reusable workflow.
Expand Down
7 changes: 7 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ on:
- "*"

jobs:
cache:
name: Cache the PR number, if available
uses: ./.github/workflows/reusable-pr-number-cache.yml
with:
commit-hash: ${{ github.sha }}
pr-number: ${{ github.event.pull_request.number || 0 }}

lint:
strategy:
fail-fast: true
Expand Down
28 changes: 28 additions & 0 deletions .github/workflows/reusable-pr-number-cache.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
name: Cache the PR number

on:
workflow_call:
inputs:
commit-hash:
required: true
type: string
pr-number:
required: true
type: number

jobs:
cache:
runs-on: ubuntu-latest
steps:
- name: Print the inputs
run: |
echo "Commit hash: ${{ inputs.commit-hash }}"
echo "PR number: ${{ inputs.pr-number }}"
- name: Echo the PR number to a file
run: echo "${{ inputs.pr-number }}" > res.txt
- name: Cache the PR number
uses: actions/cache/save@v3
with:
key: pr-number-cache-${{ inputs.commit-hash }}
path: res.txt
28 changes: 28 additions & 0 deletions .github/workflows/reusable-pr-number-restore.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
name: Restore the cached PR number

on:
workflow_call:
outputs:
pr-number:
description: The restored PR number from the cache
value: ${{ jobs.uncache.outputs.pr-number }}
inputs:
commit-hash:
required: true
type: string

jobs:
uncache:
runs-on: ubuntu-latest
outputs:
pr-number: ${{ steps.restore.outputs.pr-number }}
steps:
- name: Restore the PR number cache
uses: actions/cache/restore@v3
with:
key: pr-number-cache-${{ inputs.commit-hash }}
path: res.txt
- name: Save the PR number to the outputs
id: restore
run: echo "pr-number=$(cat res.txt)" >> "$GITHUB_OUTPUT"
7 changes: 7 additions & 0 deletions .github/workflows/slither.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ on:
- "*"

jobs:
cache:
name: Cache the PR number, if available
uses: ./.github/workflows/reusable-pr-number-cache.yml
with:
commit-hash: ${{ github.sha }}
pr-number: ${{ github.event.pull_request.number || 0 }}

analyze:
runs-on: ubuntu-latest
steps:
Expand Down
11 changes: 10 additions & 1 deletion .github/workflows/status-comment.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Comment the status of the CI run on the pull request
name: Comment the triggering CI status on the PR

on:
workflow_run:
Expand All @@ -11,7 +11,14 @@ permissions:
issues: read

jobs:
restore:
name: Restore the cached PR number, if available
uses: ./.github/workflows/reusable-pr-number-restore.yml
with:
commit-hash: ${{ github.event.workflow_run.head_commit.id }}

comment_status:
needs: restore
runs-on: ubuntu-latest
steps:
# Log the workflow trigger details for debugging.
Expand All @@ -23,6 +30,7 @@ jobs:
echo "Workflow run URL: ${{ github.event.workflow_run.html_url }}"
echo "Commit SHA: ${{ github.event.workflow_run.head_commit.id }}"
echo "Workflow Run ID: ${{ github.event.workflow_run.id }}"
echo "PR number: ${{ needs.restore.outputs.pr-number }}"
# Checkout the repository to make the composite action available.
- name: Checkout repository
uses: actions/checkout@v3
Expand All @@ -36,3 +44,4 @@ jobs:
workflow-url: ${{ github.event.workflow_run.html_url }}
workflow-name: ${{ github.event.workflow_run.name }}
github-token: ${{ secrets.GITHUB_TOKEN }}
pr-number: ${{ needs.restore.outputs.pr-number }}

0 comments on commit 5f7363a

Please sign in to comment.