Skip to content

Commit 0972694

Browse files
authored
Merge pull request #1 from dev-hato/composite_test
diffのPRを出すCIを共通化する
2 parents 45a869c + a436d19 commit 0972694

1 file changed

Lines changed: 186 additions & 98 deletions

File tree

action.yml

Lines changed: 186 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,198 @@
11
name: 'diff-pr-management'
22
description: '差分があればPRを作り、差分がなければPRを閉じます'
33
inputs:
4-
github-token: # id of input
4+
github-token: # id of input
55
description: 'GITHUB_TOKEN'
66
required: true
7-
outputs:
8-
random-number:
9-
description: "Random number"
10-
value: ${{ steps.random-number-generator.outputs.random-id }}
7+
branch-name-prefix:
8+
description: 'Prefix of PR branch name'
9+
required: true
10+
pr-title-prefix:
11+
description: 'Prefix of PR title'
12+
required: true
13+
repo_name:
14+
description: 'Repository name'
15+
required: true
1116
runs:
1217
using: "composite"
1318
steps:
14-
# 差分があったときは、コミットを作りpushする
15-
- name: Push
16-
run: |
17-
git config user.name "hatohakaraage"
18-
git config user.email "hatohakaraage@example.com"
19-
git add -u
20-
git commit -m "鳩は唐揚げ!(自動で直してあげたよ!)"
21-
git push -f https://${{github.actor}}:${{inputs.GITHUB_TOKEN}}@github.com/${{github.repository}}.git HEAD:refs/heads/fix-text-${{github.event.pull_request.head.ref}}
22-
shell: bash
23-
# pushしたブランチでPRを作る
24-
- name: Create PullRequest
25-
uses: actions/github-script@v3
26-
with:
27-
github-token: ${{inputs.GITHUB_TOKEN}}
28-
script: |
29-
const common_params = {
30-
owner: context.repo.owner,
31-
repo: context.repo.repo
32-
}
33-
const pull_params = {
34-
head: "dev-hato:fix-text-${{github.event.pull_request.head.ref}}",
35-
base: "${{github.event.pull_request.head.ref}}",
19+
# 差分があったときは差分を出力する
20+
- name: Show diff
21+
id: diff
22+
shell: bash
23+
run: |
24+
result=$(git diff)
25+
echo "::set-output name=result::$result"
26+
# 差分があったときは、コミットを作りpushする
27+
- name: Push
28+
env:
29+
HEAD_REF: ${{github.event.pull_request.head.ref}}
30+
if: inputs.repo_name == github.repository
31+
&& steps.diff.outputs.result != ''
32+
&& (github.event_name == 'pull_request' || github.event_name == 'schedule')
33+
run: |
34+
git config user.name "github-actions[bot]"
35+
EMAIL="41898282+github-actions[bot]@users.noreply.github.com"
36+
git config user.email "${EMAIL}"
37+
git add -u
38+
git commit -m "鳩は唐揚げ!(自動で直してあげたよ!)"
39+
REPO_URL="https://"
40+
REPO_URL+="${{github.actor}}:${{inputs.github-token}}@github.com/"
41+
REPO_URL+="${{github.repository}}.git"
42+
GITHUB_HEAD="HEAD:refs/heads/${{inputs.branch-name-prefix}}"
43+
44+
if [ "${HEAD_REF}" != "" ]; then
45+
GITHUB_HEAD+="-${HEAD_REF}"
46+
fi
47+
48+
git push -f "${REPO_URL}" "${GITHUB_HEAD}"
49+
shell: bash
50+
- name: Set org name
51+
uses: actions/github-script@v6.1.0
52+
if: inputs.repo_name == github.repository && (github.event_name == 'pull_request' || github.event_name == 'schedule')
53+
id: set_org_name
54+
with:
55+
github-token: ${{inputs.github-token}}
56+
result-encoding: string
57+
script: return process.env.GITHUB_REPOSITORY.split('/')[0]
58+
- name: Get PullRequests
59+
uses: actions/github-script@v6.1.0
60+
env:
61+
HEAD_REF: ${{github.event.pull_request.head.ref}}
62+
if: inputs.repo_name == github.repository
63+
&& steps.diff.outputs.result != ''
64+
&& (github.event_name == 'pull_request' || github.event_name == 'schedule')
65+
id: get_pull_requests
66+
with:
67+
github-token: ${{inputs.github-token}}
68+
script: |
69+
const HEAD_REF = process.env["HEAD_REF"]
70+
let head = "${{steps.set_org_name.outputs.result}}:${{inputs.branch-name-prefix}}"
71+
72+
if (HEAD_REF != "") {
73+
head += "-${HEAD_REF}"
74+
}
75+
76+
const pulls_list_params = {
77+
owner: context.repo.owner,
78+
repo: context.repo.repo,
79+
head,
80+
base: HEAD_REF,
81+
state: "open"
82+
}
83+
console.log("call pulls.list:", pulls_list_params)
84+
const pulls = await github.paginate(github.rest.pulls.list,
85+
pulls_list_params)
86+
return pulls.length
87+
# pushしたブランチでPRを作る
88+
- name: Create PullRequest
89+
uses: actions/github-script@v6.1.0
90+
env:
91+
HEAD_REF: ${{github.event.pull_request.head.ref}}
92+
if: inputs.repo_name == github.repository
93+
&& steps.diff.outputs.result != ''
94+
&& steps.get_pull_requests.outputs.result == 0
95+
&& (github.event_name == 'pull_request' || github.event_name == 'schedule')
96+
id: create_pull_request
97+
with:
98+
github-token: ${{inputs.github-token}}
99+
script: |
100+
const HEAD_REF = process.env["HEAD_REF"]
101+
let title = "${{inputs.pr-title-prefix}}PRをマージしてね!"
102+
let body = "鳩の唐揚げおいしい!😋😋😋"
103+
104+
if ("${{github.event.pull_request.number}}" != "") {
105+
body += " #${{github.event.pull_request.number}}"
106+
title += " #${{github.event.pull_request.number}}"
107+
}
108+
109+
let head = "${{steps.set_org_name.outputs.result}}:${{inputs.branch-name-prefix}}"
110+
111+
if (HEAD_REF != "") {
112+
head += "-${HEAD_REF}"
113+
}
114+
115+
const pulls_create_params = {
116+
owner: context.repo.owner,
117+
repo: context.repo.repo,
118+
head,
119+
base: HEAD_REF,
120+
title,
121+
body
122+
}
123+
console.log("call pulls.create:", pulls_create_params)
124+
const create_pull_res = await github.rest.pulls.create(
125+
pulls_create_params
126+
)
127+
return create_pull_res.data.number
128+
- name: Assign a user
129+
uses: actions/github-script@v6.1.0
130+
if: inputs.repo_name == github.repository
131+
&& steps.diff.outputs.result != ''
132+
&& steps.get_pull_requests.outputs.result == 0
133+
&& github.event_name == 'pull_request'
134+
&& github.event.pull_request.user.login != 'dependabot[bot]'
135+
&& github.event.pull_request.user.login != 'renovate[bot]'
136+
with:
137+
github-token: ${{inputs.github-token}}
138+
script: |
139+
const issues_add_assignees_params = {
140+
owner: context.repo.owner,
141+
repo: context.repo.repo,
142+
issue_number: ${{steps.create_pull_request.outputs.result}},
143+
assignees: ["${{github.event.pull_request.user.login}}"]
144+
}
145+
console.log("call issues.addAssignees:")
146+
console.log(issues_add_assignees_params)
147+
await github.rest.issues.addAssignees(issues_add_assignees_params)
148+
# 既にformat修正のPRがある状態で、手動でformatを修正した場合、format修正のPRを閉じる
149+
- name: Close PullRequest
150+
uses: actions/github-script@v6.1.0
151+
env:
152+
HEAD_REF: ${{github.event.pull_request.head.ref}}
153+
if: inputs.repo_name == github.repository
154+
&& steps.diff.outputs.result == ''
155+
&& (github.event_name == 'pull_request' || github.event_name == 'schedule')
156+
with:
157+
github-token: ${{inputs.github-token}}
158+
script: |
159+
const HEAD_REF = process.env["HEAD_REF"]
160+
let head_name = "${{inputs.branch-name-prefix}}"
161+
162+
if (HEAD_REF != "") {
163+
head_name += "-${HEAD_REF}"
164+
}
165+
166+
const common_params = {
167+
owner: context.repo.owner,
168+
repo: context.repo.repo
169+
}
170+
const pulls_list_params = {
171+
head: "${{steps.set_org_name.outputs.result}}:" + head_name,
172+
base: HEAD_REF,
173+
state: "open",
174+
...common_params
175+
}
176+
console.log("call pulls.list:", pulls_list_params)
177+
const pulls = await github.paginate(github.rest.pulls.list,
178+
pulls_list_params)
179+
180+
for (const pull of pulls) {
181+
const pulls_update_params = {
182+
pull_number: pull.number,
183+
state: "closed",
36184
...common_params
37185
}
38-
const pulls_list_params = {
39-
state: "open",
40-
...pull_params
41-
}
42-
console.log("call pulls.list:")
43-
console.log(pulls_list_params)
44-
github.pulls.list(pulls_list_params).then(list_res => {
45-
if (list_res.data.length === 0) {
46-
const pulls_create_params = {
47-
title: "日本語が間違ってたので直してあげたよ!PRをマージしてね! #${{github.event.pull_request.number}}",
48-
body: "鳩の唐揚げおいしい!😋😋😋 #${{github.event.pull_request.number}}",
49-
...pull_params
50-
}
51-
console.log("call pulls.create:")
52-
console.log(pulls_create_params)
53-
github.pulls.create(pulls_create_params).then(create_res => {
54-
if("${{github.event.pull_request.user.login}}" !== "dependabot[bot]") {
55-
const issues_add_assignees_params = {
56-
issue_number: create_res.data.number,
57-
assignees: ["${{github.event.pull_request.user.login}}"],
58-
...common_params
59-
}
60-
console.log("call issues.addAssignees:")
61-
console.log(issues_add_assignees_params)
62-
github.issues.addAssignees(issues_add_assignees_params)
63-
}
64-
})
65-
}
66-
})
67-
shell: bash
68-
# 既にformat修正のPRがある状態で、手動でformatを修正した場合、format修正のPRを閉じる
69-
- name: Close PullRequest
70-
uses: actions/github-script@v3
71-
with:
72-
github-token: ${{inputs.GITHUB_TOKEN}}
73-
script: |
74-
const head_name = "fix-text-${{github.event.pull_request.head.ref}}"
75-
const common_params = {
76-
owner: context.repo.owner,
77-
repo: context.repo.repo
78-
}
79-
const pulls_list_params = {
80-
head: "dev-hato:" + head_name,
81-
base: "${{github.event.pull_request.head.ref}}",
82-
state: "open",
186+
console.log("call pulls.update:", pulls_update_params)
187+
await github.rest.pulls.update(pulls_update_params)
188+
const git_deleteRef_params = {
189+
ref: "heads/" + head_name,
83190
...common_params
84191
}
85-
console.log("call pulls.list:")
86-
console.log(pulls_list_params)
87-
github.pulls.list(pulls_list_params).then(res => {
88-
for(const data of res.data){
89-
const pulls_update_params = {
90-
pull_number: data.number,
91-
state: "closed",
92-
...common_params
93-
}
94-
console.log("call pulls.update:")
95-
console.log(pulls_update_params)
96-
github.pulls.update(pulls_update_params).then(res2 => {
97-
const git_deleteRef_params = {
98-
ref: "heads/" + head_name,
99-
...common_params
100-
}
101-
console.log("call git.deleteRef:")
102-
console.log(git_deleteRef_params)
103-
github.git.deleteRef(git_deleteRef_params)
104-
})
105-
}
106-
})
107-
shell: bash
108-
- name: Exit
109-
run: exit 1
110-
shell: bash
192+
console.log("call git.deleteRef:", git_deleteRef_params)
193+
await github.rest.git.deleteRef(git_deleteRef_params)
194+
}
195+
- name: Exit
196+
if: steps.diff.outputs.result != ''
197+
run: exit 1
198+
shell: bash

0 commit comments

Comments
 (0)