Update README.md #236
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
name: Command | |
on: | |
issue_comment: # listen for comments on issues | |
types: [created] | |
permissions: # allow the action to comment on the PR | |
contents: write | |
issues: write | |
pull-requests: write | |
actions: write | |
jobs: | |
acknowledge: | |
if: ${{ startsWith(github.event.comment.body, '/cmd') }} | |
runs-on: ubuntu-latest | |
steps: | |
- name: Add reaction to triggered comment | |
uses: actions/github-script@v7 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
github.rest.reactions.createForIssueComment({ | |
comment_id: ${{ github.event.comment.id }}, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
content: 'eyes' | |
}) | |
clean: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Clean previous comments | |
uses: actions/github-script@v7 | |
if: ${{ startsWith(github.event.comment.body, '/cmd') && contains(github.event.comment.body, '--clean') }} | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
github.rest.issues.listComments({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo | |
}).then(comments => { | |
for (let comment of comments.data) { | |
console.log(comment) | |
if ( | |
${{ github.event.comment.id }} !== comment.id && | |
( | |
( | |
( | |
comment.body.startsWith('Command') || | |
comment.body.startsWith('<details><summary>Command') || | |
comment.body.startsWith('Sorry, only ') | |
) && comment.user.type === 'Bot' | |
) || | |
(comment.body.startsWith('/cmd') && comment.user.login === context.actor) | |
) | |
) { | |
github.rest.issues.deleteComment({ | |
comment_id: comment.id, | |
owner: context.repo.owner, | |
repo: context.repo.repo | |
}) | |
} | |
} | |
}) | |
get-pr-info: | |
if: ${{ startsWith(github.event.comment.body, '/cmd') }} | |
runs-on: ubuntu-latest | |
outputs: | |
CMD: ${{ steps.get-comment.outputs.group2 }} | |
pr-branch: ${{ steps.get-pr.outputs.pr_branch }} | |
repo: ${{ steps.get-pr.outputs.repo }} | |
steps: | |
- name: Get command | |
uses: actions-ecosystem/action-regex-match@v2 | |
id: get-comment | |
with: | |
text: ${{ github.event.comment.body }} | |
regex: "^(\\/cmd )([-\\/\\s\\w.=:]+)$" # see explanation in docs/contributor/commands-readme.md#examples | |
# Get PR branch name, because the issue_comment event does not contain the PR branch name | |
- name: Check if the issue is a PR | |
id: check-pr | |
run: | | |
if [ -n "${{ github.event.issue.pull_request.url }}" ]; then | |
echo "This is a pull request comment" | |
else | |
echo "This is not a pull request comment" | |
exit 1 | |
fi | |
- name: Get PR Branch Name and Repo | |
if: steps.check-pr.outcome == 'success' | |
id: get-pr | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const pr = await github.rest.pulls.get({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
pull_number: context.issue.number, | |
}); | |
const prBranch = pr.data.head.ref; | |
const repo = pr.data.head.repo.full_name; | |
console.log(prBranch, repo) | |
core.setOutput('pr_branch', prBranch); | |
core.setOutput('repo', repo); | |
- name: Use PR Branch Name and Repo | |
env: | |
PR_BRANCH: ${{ steps.get-pr.outputs.pr_branch }} | |
REPO: ${{ steps.get-pr.outputs.repo }} | |
CMD: ${{ steps.get-comment.outputs.group2 }} | |
run: | | |
echo "The PR branch is $PR_BRANCH" | |
echo "The repository is $REPO" | |
echo "The CMD is $CMD" | |
help: | |
needs: [clean, get-pr-info] | |
if: ${{ startsWith(github.event.comment.body, '/cmd') && contains(github.event.comment.body, '--help') }} | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Save output of help | |
id: help | |
env: | |
CMD: ${{ needs.get-pr-info.outputs.CMD }} # to avoid "" around the command | |
run: | | |
python3 -m pip install -r .github/scripts/generate-prdoc.requirements.txt | |
echo 'help<<EOF' >> $GITHUB_OUTPUT | |
python3 .github/scripts/cmd/cmd.py $CMD >> $GITHUB_OUTPUT | |
echo 'EOF' >> $GITHUB_OUTPUT | |
- name: Comment PR (Help) | |
uses: actions/github-script@v7 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
github.rest.issues.createComment({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: `<details><summary>Command help:</summary> | |
\`\`\` | |
${{ steps.help.outputs.help }} | |
\`\`\` | |
</details>` | |
}) | |
- name: Add confused reaction on failure | |
uses: actions/github-script@v7 | |
if: ${{ failure() }} | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
github.rest.reactions.createForIssueComment({ | |
comment_id: ${{ github.event.comment.id }}, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
content: 'confused' | |
}) | |
- name: Add π reaction on success | |
uses: actions/github-script@v7 | |
if: ${{ !failure() }} | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
github.rest.reactions.createForIssueComment({ | |
comment_id: ${{ github.event.comment.id }}, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
content: '+1' | |
}) | |
set-image: | |
needs: [clean, get-pr-info] | |
if: ${{ startsWith(github.event.comment.body, '/cmd') && !contains(github.event.comment.body, '--help') }} | |
runs-on: ubuntu-latest | |
outputs: | |
IMAGE: ${{ steps.set-image.outputs.IMAGE }} | |
RUNNER: ${{ steps.set-image.outputs.RUNNER }} | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- id: set-image | |
run: | | |
BODY=$(echo "${{ github.event.comment.body }}" | xargs) | |
IMAGE_OVERRIDE=$(echo $BODY | grep -oe 'docker.io/paritytech/ci-unified:.*\s' | xargs) | |
cat .github/env >> $GITHUB_OUTPUT | |
if [ -n "$IMAGE_OVERRIDE" ]; then | |
IMAGE=$IMAGE_OVERRIDE | |
echo "IMAGE=$IMAGE" >> $GITHUB_OUTPUT | |
fi | |
if [[ $BODY == "/cmd bench"* ]]; then | |
echo "RUNNER=parity-weights" >> $GITHUB_OUTPUT | |
elif [[ $BODY == "/cmd update-ui"* ]]; then | |
echo "RUNNER=parity-large" >> $GITHUB_OUTPUT | |
else | |
echo "RUNNER=ubuntu-latest" >> $GITHUB_OUTPUT | |
fi | |
- name: Print outputs | |
run: | | |
echo "RUNNER=${{ steps.set-image.outputs.RUNNER }}" | |
echo "IMAGE=${{ steps.set-image.outputs.IMAGE }}" | |
cmd: | |
needs: [set-image, get-pr-info] | |
env: | |
JOB_NAME: "cmd" | |
CMD: ${{ needs.get-pr-info.outputs.CMD }} | |
PR_BRANCH: ${{ needs.get-pr-info.outputs.pr-branch }} | |
runs-on: ${{ needs.set-image.outputs.RUNNER }} | |
container: | |
image: ${{ needs.set-image.outputs.IMAGE }} | |
timeout-minutes: 1440 # 24 hours per runtime | |
outputs: | |
cmd_output: ${{ steps.cmd.outputs.cmd_output }} | |
job_url: ${{ steps.build-link.outputs.job_url }} | |
run_url: ${{ steps.build-link.outputs.run_url }} | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
with: | |
repository: ${{ needs.get-pr-info.outputs.repo }} | |
ref: ${{ needs.get-pr-info.outputs.pr-branch }} | |
# In order to run prdoc without specifying the PR number, we need to add the PR number as an argument automatically | |
- name: Prepare PR Number argument | |
id: pr-arg | |
run: | | |
CMD="${{ needs.get-pr-info.outputs.CMD }}" | |
if echo "$CMD" | grep -q "prdoc" && ! echo "$CMD" | grep -qE "\-\-pr[[:space:]=][0-9]+"; then | |
echo "arg=--pr ${{ github.event.issue.number }}" >> $GITHUB_OUTPUT | |
else | |
echo "arg=" >> $GITHUB_OUTPUT | |
fi | |
- name: Build workflow link | |
if: ${{ !contains(github.event.comment.body, '--quiet') }} | |
id: build-link | |
run: | | |
# Get exactly the CMD job link, filtering out the other jobs | |
jobLink=$(curl -s \ | |
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
-H "Accept: application/vnd.github.v3+json" \ | |
https://api.github.com/repos/${{ github.repository }}/actions/runs/${{ github.run_id }}/jobs | jq '.jobs[] | select(.name | contains("${{ env.JOB_NAME }}")) | .html_url') | |
runLink=$(curl -s \ | |
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
-H "Accept: application/vnd.github.v3+json" \ | |
https://api.github.com/repos/${{ github.repository }}/actions/runs/${{ github.run_id }} | jq '.html_url') | |
echo "job_url=${jobLink}" | |
echo "run_url=${runLink}" | |
echo "job_url=$jobLink" >> $GITHUB_OUTPUT | |
echo "run_url=$runLink" >> $GITHUB_OUTPUT | |
- name: Comment PR (Start) | |
# No need to comment on prdoc start or if --quiet | |
if: ${{ !contains(github.event.comment.body, '--quiet') && !startsWith(needs.get-pr-info.outputs.CMD, 'prdoc') && !startsWith(needs.get-pr-info.outputs.CMD, 'fmt')}} | |
uses: actions/github-script@v7 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
let job_url = ${{ steps.build-link.outputs.job_url }} | |
let cmd = process.env.CMD; | |
github.rest.issues.createComment({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: `Command "${cmd}" has started π [See logs here](${job_url})` | |
}) | |
- name: Install dependencies for bench | |
if: startsWith(needs.get-pr-info.outputs.CMD, 'bench') | |
run: | | |
cargo install --path substrate/utils/frame/omni-bencher --locked | |
- name: Run cmd | |
id: cmd | |
env: | |
PR_ARG: ${{ steps.pr-arg.outputs.arg }} | |
run: | | |
echo "Running command: '$CMD $PR_ARG' on '${{ needs.set-image.outputs.RUNNER }}' runner, container: '${{ needs.set-image.outputs.IMAGE }}'" | |
echo "RUST_NIGHTLY_VERSION: $RUST_NIGHTLY_VERSION" | |
# Fixes "detected dubious ownership" error in the ci | |
git config --global --add safe.directory '*' | |
git remote -v | |
cat /proc/cpuinfo | |
python3 -m pip install -r .github/scripts/generate-prdoc.requirements.txt | |
python3 .github/scripts/cmd/cmd.py $CMD $PR_ARG | |
git status | |
git diff | |
if [ -f /tmp/cmd/command_output.log ]; then | |
CMD_OUTPUT=$(cat /tmp/cmd/command_output.log) | |
# export to summary to display in the PR | |
echo "$CMD_OUTPUT" >> $GITHUB_STEP_SUMMARY | |
# should be multiline, otherwise it captures the first line only | |
echo 'cmd_output<<EOF' >> $GITHUB_OUTPUT | |
echo "$CMD_OUTPUT" >> $GITHUB_OUTPUT | |
echo 'EOF' >> $GITHUB_OUTPUT | |
fi | |
git status | |
if [ -n "$(git status --porcelain)" ]; then | |
git config user.name "cmd[bot]" | |
git config user.email "41898282+github-actions[bot]@users.noreply.github.com" | |
git config --global pull.rebase false | |
# Push the results to the target branch | |
git remote add \ | |
github \ | |
"https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ needs.get-pr-info.outputs.repo }}.git" || : | |
push_changes() { | |
git push github "HEAD:$PR_BRANCH" | |
} | |
git add . | |
git restore --staged Cargo.lock # ignore changes in Cargo.lock | |
git commit -m "Update from ${{ github.actor }} running command '$CMD'" || true | |
# Attempt to push changes | |
if ! push_changes; then | |
echo "Push failed, trying to rebase..." | |
git pull --rebase github $PR_BRANCH | |
# After successful rebase, try pushing again | |
push_changes | |
fi | |
else | |
echo "Nothing to commit"; | |
fi | |
- name: Upload command output | |
if: ${{ always() }} | |
uses: actions/upload-artifact@v4 | |
with: | |
name: command-output | |
path: /tmp/cmd/command_output.log | |
- name: Install subweight for bench | |
if: startsWith(needs.get-pr-info.outputs.CMD, 'bench') | |
run: cargo install subweight | |
- name: Run Subweight for bench | |
id: subweight | |
if: startsWith(needs.get-pr-info.outputs.CMD, 'bench') | |
shell: bash | |
run: | | |
git fetch | |
result=$(subweight compare commits \ | |
--path-pattern "./**/weights/**/*.rs,./**/weights.rs" \ | |
--method asymptotic \ | |
--format markdown \ | |
--no-color \ | |
--change added changed \ | |
--ignore-errors \ | |
refs/remotes/origin/master refs/heads/$PR_BRANCH) | |
# Save the multiline result to the output | |
{ | |
echo "result<<EOF" | |
echo "$result" | |
echo "EOF" | |
} >> $GITHUB_OUTPUT | |
- name: Comment PR (End) | |
# No need to comment on prdoc success or --quiet | |
if: ${{ !failure() && !contains(github.event.comment.body, '--quiet') && !startsWith(needs.get-pr-info.outputs.CMD, 'prdoc') && !startsWith(needs.get-pr-info.outputs.CMD, 'fmt') }} | |
uses: actions/github-script@v7 | |
env: | |
SUBWEIGHT: "${{ steps.subweight.outputs.result }}" | |
CMD_OUTPUT: "${{ steps.cmd.outputs.cmd_output }}" | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
let runUrl = ${{ steps.build-link.outputs.run_url }} | |
let subweight = process.env.SUBWEIGHT; | |
let cmdOutput = process.env.CMD_OUTPUT; | |
let cmd = process.env.CMD; | |
console.log(cmdOutput); | |
let subweightCollapsed = subweight.trim() !== '' | |
? `<details>\n\n<summary>Subweight results:</summary>\n\n${subweight}\n\n</details>` | |
: ''; | |
let cmdOutputCollapsed = cmdOutput.trim() !== '' | |
? `<details>\n\n<summary>Command output:</summary>\n\n${cmdOutput}\n\n</details>` | |
: ''; | |
github.rest.issues.createComment({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: `Command "${cmd}" has finished β [See logs here](${runUrl})${subweightCollapsed}${cmdOutputCollapsed}` | |
}) | |
- name: Comment PR (Failure) | |
if: ${{ failure() && !contains(github.event.comment.body, '--quiet') }} | |
uses: actions/github-script@v7 | |
env: | |
CMD_OUTPUT: "${{ steps.cmd.outputs.cmd_output }}" | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
let jobUrl = ${{ steps.build-link.outputs.job_url }} | |
let cmdOutput = process.env.CMD_OUTPUT; | |
let cmd = process.env.CMD; | |
let cmdOutputCollapsed = cmdOutput.trim() !== '' | |
? `<details>\n\n<summary>Command output:</summary>\n\n${cmdOutput}\n\n</details>` | |
: ''; | |
github.rest.issues.createComment({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: `Command "${cmd}" has failed β! [See logs here](${jobUrl})${cmdOutputCollapsed}` | |
}) | |
- name: Add π reaction on failure | |
uses: actions/github-script@v7 | |
if: ${{ failure() }} | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
github.rest.reactions.createForIssueComment({ | |
comment_id: ${{ github.event.comment.id }}, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
content: 'confused' | |
}) | |
- name: Add π reaction on success | |
uses: actions/github-script@v7 | |
if: ${{ !failure() }} | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
github.rest.reactions.createForIssueComment({ | |
comment_id: ${{ github.event.comment.id }}, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
content: '+1' | |
}) |