Non-fork version of #28 #104
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this is adapted from openff-docs/cookbook_pr_trigger.yaml | |
name: Trigger benchmark run | |
# comment on a PR with "/run-optimization-benchmark" or "/run-torsion-benchmark" | |
on: | |
issue_comment: | |
types: [created] | |
jobs: | |
trigger: | |
permissions: write-all | |
if: ${{ github.event.issue.pull_request }} | |
runs-on: ubuntu-latest | |
steps: | |
- name: Dispatch benchmark | |
if: ${{ github.event.issue.pull_request }} | |
uses: actions/github-script@v6 | |
with: | |
script: | | |
const owner = context.repo.owner | |
const repo = context.repo.repo | |
const pr_number = context.issue.number | |
const comment = context.payload.comment | |
// Return early if comment is not one of the trigger phrases. Otherwise | |
// set the type of workflow to dispatch | |
let benchmark_workflow; | |
if (comment.body.startsWith('/run-optimization-benchmarks')) { | |
benchmark_workflow = "opt.yaml" | |
} else if (comment.body.startsWith('/run-torsion-benchmarks')) { | |
benchmark_workflow = "torsions.yaml" | |
} else { | |
console.log("Comment is not trigger phrase") | |
return | |
} | |
// This will 404 if the user is not a collaborator (and cause an | |
// exception below), so this check prevents random users from | |
// triggering the workflow. | |
// https://docs.github.com/en/rest/collaborators/collaborators | |
// ?apiVersion=2022-11-28#check-if-a-user-is-a-repository-collaborator | |
const commenter = await github.request( | |
'GET /repos/{owner}/{repo}/collaborators/{username}/permission', | |
{ | |
owner: owner, | |
repo: repo, | |
username: comment.user.login, | |
headers: { | |
'X-GitHub-Api-Version': '2022-11-28' | |
} | |
}, | |
) | |
const commenter_can_push = commenter.data.user.permissions.push | |
// Return early if comment sender doesn't have push rights | |
if (!commenter_can_push) { | |
console.log("User does not have push permissions") | |
return | |
} | |
const pr = await github.rest.pulls.get({ | |
pull_number: pr_number, | |
owner: owner, | |
repo: repo, | |
}) | |
const ref = pr.data.head.ref | |
// treat everything after the command name as an argument | |
const args = comment.body.split(" ").slice(1); | |
const path = args[0]; // path to submission dir | |
if (!path) { | |
console.log("No submission path provided"); | |
return; | |
} | |
const env = args[1] ? args[1] : "devtools/env.yaml"; // conda env | |
// We didn't return early, so dispatch the workflow | |
const dispatch = await github.request( | |
'POST /repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches', | |
{ | |
owner: owner, | |
repo: repo, | |
workflow_id: benchmark_workflow, | |
ref: ref, | |
inputs: { | |
pr_number: pr_number.toString(), | |
env: env, | |
path: path | |
}, | |
headers: { | |
'X-GitHub-Api-Version': '2022-11-28' | |
} | |
}, | |
) | |
console.log(dispatch) |