template-workflows-pr-command #68
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
# trigger by chatops '/template-workflows-pr owner/repo' | |
# First argument is a repository name | |
name: Template workflows PR command | |
on: | |
repository_dispatch: | |
types: [template-workflows-pr-command] | |
jobs: | |
publish: | |
runs-on: ubuntu-latest | |
env: | |
PR_COMMIT_TITLE: ":robot: Sync workflows with TemplateDataRepo" | |
TEMPLATE_REPO: BSData/TemplateDataRepo | |
steps: | |
- name: Link to this workflow in command comment | |
uses: peter-evans/create-or-update-comment@v2 | |
with: | |
token: ${{ secrets.BSDATA_BOT_TOKEN }} | |
repository: ${{ github.event.client_payload.github.payload.repository.full_name }} | |
comment-id: ${{ github.event.client_payload.github.payload.comment.id }} | |
body: | | |
<!-- Command output --> | |
--- | |
[Workflow run](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}) | |
- name: Create/get repo fork | |
uses: actions/github-script@v6 | |
id: fork_script | |
with: | |
github-token: ${{ secrets.BSDATA_BOT_TOKEN }} | |
script: | | |
let [owner, repo] = context.payload.client_payload.slash_command.args.unnamed.arg1.split('/'); | |
try { | |
let { data: response, status } = await github.rest.repos.createFork({ | |
owner: owner, | |
repo: repo | |
}); | |
if (status === 202) | |
{ | |
return response; | |
} | |
else { | |
throw `Unknown error (code ${status})`; | |
} | |
} catch (error) { | |
let msg = `Fork creation failed:\n${error}`; | |
core.setOutput('error', msg); | |
throw error; | |
} | |
- name: Checkout template repo | |
uses: actions/checkout@v3 | |
with: | |
repository: ${{ env.TEMPLATE_REPO }} | |
path: template | |
- name: Checkout target repo | |
uses: actions/checkout@v3 | |
with: | |
repository: ${{ fromJSON(steps.fork_script.outputs.result).parent.full_name }} | |
path: target | |
fetch-depth: 0 | |
- name: Copy, commit and push files | |
uses: Amadevus/pwsh-script@v2 | |
id: copy_files | |
with: | |
script: | | |
Set-Location target | |
$targetDir = New-Item ./.github/workflows -ItemType Directory -Force | |
Get-ChildItem ../template/.github/workflows *.yml | |
| Copy-Item -Destination $targetDir -Force -Verbose -PassThru | |
| Foreach-Object { git add --force -- $_ } | |
| Out-Host | |
- name: Create Pull Request | |
uses: peter-evans/create-pull-request@v4 | |
id: create_pr | |
with: | |
token: ${{ secrets.BSDATA_BOT_TOKEN }} | |
path: target | |
commit-message: ${{ env.PR_COMMIT_TITLE }} | |
branch: update/template-workflows | |
delete-branch: true | |
push-to-fork: ${{ fromJSON(steps.fork_script.outputs.result).full_name }} | |
title: ${{ env.PR_COMMIT_TITLE }} | |
body: | | |
Automated PR to sync GitHub Actions workflows with ${{ env.TEMPLATE_REPO }}. | |
For information, see https://github.com/BSData/chatops#template-workflows-pr-command | |
Triggered by ${{ github.event.client_payload.github.payload.comment.html_url }} | |
- name: Add reaction to command comment on success | |
uses: peter-evans/create-or-update-comment@v2 | |
with: | |
token: ${{ secrets.BSDATA_BOT_TOKEN }} | |
repository: ${{ github.event.client_payload.github.payload.repository.full_name }} | |
comment-id: ${{ github.event.client_payload.github.payload.comment.id }} | |
body: "PR created: ${{ steps.create_pr.outputs.pull-request-url }}" | |
reactions: hooray | |
- name: Add reaction to command comment on failure | |
uses: peter-evans/create-or-update-comment@v2 | |
if: failure() | |
with: | |
token: ${{ secrets.BSDATA_BOT_TOKEN }} | |
repository: ${{ github.event.client_payload.github.payload.repository.full_name }} | |
comment-id: ${{ github.event.client_payload.github.payload.comment.id }} | |
body: "**Error**: ${{ steps.copy_files.outputs.error }}" | |
reactions: "-1" |