-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtemplate-workflows-pr-command.yml
More file actions
110 lines (101 loc) · 4.17 KB
/
template-workflows-pr-command.yml
File metadata and controls
110 lines (101 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# 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"