chore: simplify code of Publish target #791
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: Commands on PR | |
on: | |
issue_comment: | |
types: [created] | |
permissions: | |
contents: write | |
issues: write | |
pull-requests: write | |
jobs: | |
run_command: | |
if: github.event.issue.pull_request != '' && contains(github.event.comment.body, '/run') | |
runs-on: ubuntu-20.04 | |
steps: | |
- name: Extract command to run | |
uses: actions/github-script@v3 | |
id: command-extractor | |
with: | |
result-encoding: string | |
script: | | |
if (context.eventName !== "issue_comment") throw "Error: This action only works on issue_comment events."; | |
// extract the command to run, allowed characters: a-z, A-Z, digits, hyphen, underscore | |
const regex = /^\/run ([a-zA-Z\d\-\_]+)/; | |
command = regex.exec(context.payload.comment.body); | |
if (command == null) throw "Error: No command found in the trigger phrase."; | |
return command[1]; | |
- name: Get github ref | |
uses: actions/github-script@v3 | |
id: get-pr | |
with: | |
script: | | |
const result = await github.pulls.get({ | |
pull_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
}); | |
return { "ref": result.data.head.ref, "repository": result.data.head.repo.full_name}; | |
- name: Checkout repo | |
uses: actions/checkout@v2 | |
with: | |
repository: ${{ fromJson(steps.get-pr.outputs.result).repository }} | |
ref: ${{ fromJson(steps.get-pr.outputs.result).ref }} | |
fetch-depth: 0 | |
- name: Install dotnet | |
uses: actions/setup-dotnet@v3 | |
- name: Install dotnet tools | |
run: dotnet tool restore | |
- name: Process fantomas command | |
if: steps.command-extractor.outputs.result == 'fantomas' | |
id: fantomas | |
run: dotnet fantomas . -r | |
- name: Commit and push changes | |
if: steps.fantomas.outcome == 'success' || steps.xlf.outcome == 'success' | |
run: | | |
git config --local user.name "github-actions[bot]" | |
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com" | |
git commit -a -m 'Automated command ran: ${{ steps.command-extractor.outputs.result }} | |
Co-authored-by: ${{ github.event.comment.user.login }} <${{ github.event.comment.user.id }}+${{ github.event.comment.user.login }}@users.noreply.github.com>' | |
git push | |
- name: Post command comment | |
if: steps.fantomas.outcome == 'success' | |
uses: actions/github-script@v3 | |
with: | |
script: | | |
// Probably, there's more universal way of getting outputs, but my gh-actions-fu is not that good. | |
var output = "" | |
if ("${{steps.command-extractor.outputs.result}}" == 'fantomas') { | |
output = "${{steps.fantomas.outputs.result}}" | |
} else if("${{steps.command-extractor.outputs.result}}" == 'xlf') { | |
output = "${{steps.xlf.outputs.result}}" | |
} | |
const body = `Ran ${{ steps.command-extractor.outputs.result }}: https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${process.env.GITHUB_RUN_ID}\n${output}`; | |
await github.issues.createComment({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: body | |
}); | |
- name: Post command failed comment | |
if: failure() | |
uses: actions/github-script@v3 | |
with: | |
script: | | |
const body = `Failed to run ${{ steps.command-extractor.outputs.result }}: https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${process.env.GITHUB_RUN_ID}`; | |
await github.issues.createComment({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: body | |
}); |