Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run cross-version tests by comment #11765

Merged
merged 2 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions .github/workflows/cross-version-test-runner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
async function main({ context, github }) {
const { comment } = context.payload;
const { owner, repo } = context.repo;
const pull_number = context.issue.number;

const pr = await github.rest.pulls.get({ owner, repo, pull_number });
const flavorsMatch = comment.body.match(/\/(?:cross-version-test|crt)\s+([^\n]+)\n?/);
if (!flavorsMatch) {
return;
}

// Reject non-maintainers
if (!["OWNER", "MEMBER", "COLLABORATOR"].includes(comment.author_association)) {
await github.rest.issues.createComment({
owner,
repo,
issue_number: pull_number,
body: "Only maintainers are allowed to use this command.",
});
return;
}

// Run the workflow
const flavors = flavorsMatch[1];
const uuid = Array.from({ length: 16 }, () => Math.floor(Math.random() * 16).toString(16)).join(
""
);
const workflow_id = "cross-version-tests.yml";
await github.rest.actions.createWorkflowDispatch({
owner,
repo,
workflow_id,
ref: pr.data.base.ref,
inputs: {
repository: `${owner}/${repo}`,
ref: pr.merge_commit_sha,
flavors,
// The response of create-workflow-dispatch request doesn't contain the ID of the triggered
// workflow run. We need to pass a unique identifier to the workflow run and find the run by
// the identifier. See https://github.com/orgs/community/discussions/9752 for more details.
uuid,
},
});

// Find the triggered workflow run
let run;
const maxAttempts = 5;
for (let i = 0; i < maxAttempts; i++) {
await new Promise((resolve) => setTimeout(resolve, 5000));

const { data: runs } = await github.rest.actions.listWorkflowRunsForRepo({
owner,
repo,
workflow_id,
event: "workflow_dispatch",
});
run = runs.workflow_runs.find((run) => run.name.includes(uuid));
if (run) {
break;
}
}

if (!run) {
await github.rest.issues.createComment({
owner,
repo,
issue_number: pull_number,
body: "Failed to find the triggered workflow run.",
});
return;
}

await github.rest.issues.createComment({
owner,
repo,
issue_number: pull_number,
body: `Cross-version test run started: ${run.html_url}`,
});
}
Comment on lines +73 to +79
Copy link
Member Author

@harupy harupy Apr 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be improved by:

  • Waiting for the workflow run to finish.
  • Reporting the result as a commit status.


module.exports = {
main,
};
30 changes: 30 additions & 0 deletions .github/workflows/cross-version-test-runner.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Cross version test runner
on:
issue_comment:
types: [created]

permissions:
contents: read

defaults:
run:
shell: bash --noprofile --norc -exo pipefail {0}

jobs:
run:
runs-on: ubuntu-latest
timeout-minutes: 10
if: ${{ github.event.issue.pull_request && github.event.comment.body }}
permissions:
pull-requests: write
actions: write
steps:
- uses: actions/checkout@v4
- uses: actions/github-script@v7
id: get-ref
with:
result-encoding: string
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const runner = require('./.github/workflows/cross-version-test-runner.js');
await runner.main({ context, github });
5 changes: 5 additions & 0 deletions .github/workflows/cross-version-tests.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
name: Cross version tests
run-name: ${{ inputs.uuid }}

on:
pull_request:
Expand Down Expand Up @@ -30,6 +31,10 @@ on:
description: "[Optional] Comma-separated string specifying which versions to test (e.g. '1.2.3, 4.5.6'). If unspecified, all versions are tested."
required: false
default: ""
uuid:
description: "[Optional] A unique identifier for this run."
required: false
default: ""
schedule:
# Run this workflow daily at 13:00 UTC
- cron: "0 13 * * *"
Expand Down
Loading