Skip to content

Commit 3ec7624

Browse files
authored
CI内のスクリプトを別ファイルに分離する (#381)
* 複数行のスクリプトを分離する * Revert name * envを各stepに移動 * Fix path * Fix style * スクリプトを別ファイルに分離する * Revert name * 1行のbashコマンドを元に戻す
1 parent bf1e1bf commit 3ec7624

File tree

11 files changed

+222
-163
lines changed

11 files changed

+222
-163
lines changed

.github/workflows/update-readme.yml

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,8 @@ jobs:
3737
github-token: ${{secrets.GITHUB_TOKEN}}
3838
result-encoding: string
3939
script: |
40-
const fs = require('fs')
41-
const yaml = require('js-yaml')
42-
43-
const yml_file = yaml.load(fs.readFileSync('action.yml', 'utf8'))
44-
const inputs = yml_file.inputs
45-
const rows = ['| 引数名 | 説明 | 必須 |', '|:---:|:---:|:---:|']
46-
47-
for (const input_name of Object.keys(inputs)) {
48-
let row = `| ${input_name} | ${inputs[input_name].description} | `
49-
50-
if (inputs[input_name].default === undefined && inputs[input_name].required) {
51-
row += 'O'
52-
}
53-
54-
row += ' |'
55-
rows.push(row)
56-
}
57-
58-
return rows.join("\n")
40+
const script = require('./scripts/update_readme/get_inputs_markdown.js')
41+
return script()
5942
- env:
6043
DESCRIPTION: ${{env.DESCRIPTION}}
6144
INPUTS: ${{steps.get_inputs_markdown.outputs.result}}

action.yml

Lines changed: 40 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -36,34 +36,19 @@ runs:
3636
id: diff
3737
shell: bash
3838
if: github.event_name != 'pull_request' || github.event.action != 'closed'
39-
run: |
40-
result=$(git diff --cached)
41-
result="${result//'%'/'%25'}"
42-
result="${result//$'\n'/'%0A'}"
43-
result="${result//$'\r'/'%0D'}"
44-
echo "result=$result" >> "${GITHUB_OUTPUT}"
39+
run: ${{ github.action_path }}/scripts/action/show_diff.sh
4540
# 差分があったときは、コミットを作りpushする
4641
- name: Push
4742
env:
4843
HEAD_REF: ${{github.event.pull_request.head.ref}}
44+
PR_TITLE_PREFIX: ${{inputs.pr-title-prefix}}
45+
AUTHOR: ${{github.actor}}
46+
GITHUB_TOKEN: ${{inputs.github-token}}
47+
REPOSITORY: ${{github.repository}}
48+
BRANCH_NAME_PREFIX: ${{inputs.branch-name-prefix}}
49+
BASE_BRANCH: ${{inputs.base-branch}}
4950
if: inputs.repo-name == github.repository && steps.diff.outputs.result != '' && ((github.event_name == 'pull_request' && github.event.action != 'closed') || github.event_name == 'schedule' || github.event_name == 'workflow_dispatch')
50-
run: |
51-
git config user.name "github-actions[bot]"
52-
EMAIL="41898282+github-actions[bot]@users.noreply.github.com"
53-
git config user.email "${EMAIL}"
54-
git commit -m "${{inputs.pr-title-prefix}}"
55-
REPO_URL="https://"
56-
REPO_URL+="${{github.actor}}:${{inputs.github-token}}@github.com/"
57-
REPO_URL+="${{github.repository}}.git"
58-
GITHUB_HEAD="HEAD:refs/heads/${{inputs.branch-name-prefix}}"
59-
60-
if [ "${HEAD_REF}" != "" ]; then
61-
GITHUB_HEAD+="-${HEAD_REF}"
62-
elif [ "${{inputs.base-branch}}" != "" ]; then
63-
GITHUB_HEAD+="-${{inputs.base-branch}}"
64-
fi
65-
66-
git push -f "${REPO_URL}" "${GITHUB_HEAD}"
51+
run: ${{ github.action_path }}/scripts/action/push.sh
6752
shell: bash
6853
- name: Set org name
6954
uses: actions/[email protected]
@@ -72,165 +57,76 @@ runs:
7257
with:
7358
github-token: ${{inputs.github-token}}
7459
result-encoding: string
75-
script: return process.env.GITHUB_REPOSITORY.split('/')[0]
60+
script: |
61+
const script = require('${{ github.action_path }}/scripts/action/set_org_name.js')
62+
return script()
7663
- name: Get HEAD_REF
7764
uses: actions/[email protected]
7865
env:
7966
HEAD_REF: ${{github.event.pull_request.head.ref}}
67+
BASE_BRANCH: ${{inputs.base-branch}}
8068
if: inputs.repo-name == github.repository
8169
id: get_head_ref
8270
with:
8371
github-token: ${{inputs.github-token}}
8472
result-encoding: string
8573
script: |
86-
let HEAD_REF = process.env["HEAD_REF"]
87-
88-
if (HEAD_REF === "" && "${{inputs.base-branch}}" !== "") {
89-
return "${{inputs.base-branch}}"
90-
}
91-
92-
return HEAD_REF
74+
const script = require('${{ github.action_path }}/scripts/action/get_head_ref.js')
75+
return script()
9376
- name: Get PullRequests
9477
uses: actions/[email protected]
9578
if: inputs.repo-name == github.repository && steps.diff.outputs.result != '' && ((github.event_name == 'pull_request' && github.event.action != 'closed') || github.event_name == 'schedule' || github.event_name == 'workflow_dispatch')
9679
id: get_pull_requests
80+
env:
81+
HEAD_REF: ${{steps.get_head_ref.outputs.result}}
82+
ORG_NAME: ${{steps.set_org_name.outputs.result}}
83+
BRANCH_NAME_PREFIX: ${{inputs.branch-name-prefix}}
9784
with:
9885
github-token: ${{inputs.github-token}}
9986
script: |
100-
const HEAD_REF = "${{steps.get_head_ref.outputs.result}}"
101-
let head = "${{steps.set_org_name.outputs.result}}:${{inputs.branch-name-prefix}}"
102-
103-
if (HEAD_REF != "") {
104-
head += "-" + HEAD_REF
105-
}
106-
107-
const pulls_list_params = {
108-
owner: context.repo.owner,
109-
repo: context.repo.repo,
110-
head,
111-
base: HEAD_REF,
112-
state: "open"
113-
}
114-
console.log("call pulls.list:", pulls_list_params)
115-
const pulls = await github.paginate(github.rest.pulls.list,
116-
pulls_list_params)
117-
return pulls.length
87+
const script = require('${{ github.action_path }}/scripts/action/get_pull_requests.js')
88+
return await script({github, context})
11889
# pushしたブランチでPRを作る
11990
- name: Create PullRequest
12091
uses: actions/[email protected]
12192
if: inputs.repo-name == github.repository && steps.diff.outputs.result != '' && steps.get_pull_requests.outputs.result == 0 && ((github.event_name == 'pull_request' && github.event.action != 'closed') || github.event_name == 'schedule' || github.event_name == 'workflow_dispatch')
12293
id: create_pull_request
94+
env:
95+
HEAD_REF: ${{steps.get_head_ref.outputs.result}}
96+
ORG_NAME: ${{steps.set_org_name.outputs.result}}
97+
PR_DESCRIPTION_PREFIX: ${{inputs.pr-description-prefix}}
98+
PR_NUMBER: ${{github.event.pull_request.number}}
99+
PR_TITLE_PREFIX: ${{inputs.pr-title-prefix}}
100+
BRANCH_NAME_PREFIX: ${{inputs.branch-name-prefix}}
123101
with:
124102
github-token: ${{inputs.github-token}}
125103
script: |
126-
const HEAD_REF = "${{steps.get_head_ref.outputs.result}}"
127-
let head = "${{inputs.branch-name-prefix}}"
128-
129-
if (HEAD_REF != "") {
130-
head += "-" + HEAD_REF
131-
}
132-
133-
const head_with_repo = `${{steps.set_org_name.outputs.result}}:${head}`
134-
let title = "${{inputs.pr-title-prefix}}"
135-
let body = "${{inputs.pr-description-prefix}}"
136-
137-
if ("${{github.event.pull_request.number}}" != "") {
138-
body += `本PR ( \`${head}\` ) をマージすると差分が次のPRに反映されます。\n`
139-
body += `* #${{github.event.pull_request.number}} ( \`${HEAD_REF}\` )\n`
140-
body += "```mermaid\n"
141-
body += `%%{init: {'gitGraph': {'mainBranchName': '${HEAD_REF}'}}}%%\n`
142-
body += "gitGraph\n"
143-
144-
for (let i = 0; i < 2; i++) {
145-
body += " commit\n"
146-
}
147-
148-
body += ` branch ${head}\n`
149-
body += ` checkout ${head}\n`
150-
let commit = "${{inputs.pr-title-prefix}}"
151-
152-
if (6 < commit.length) {
153-
commit = commit.substring(0, 6) + '......'
154-
}
155-
156-
body += ` commit id: "${commit}"\n`
157-
body += ` checkout ${HEAD_REF}\n`
158-
body += ` merge ${head}\n`
159-
body += "```"
160-
title += " #${{github.event.pull_request.number}}"
161-
}
162-
163-
const pulls_create_params = {
164-
owner: context.repo.owner,
165-
repo: context.repo.repo,
166-
head: head_with_repo,
167-
base: HEAD_REF,
168-
title,
169-
body
170-
}
171-
console.log("call pulls.create:", pulls_create_params)
172-
const create_pull_res = await github.rest.pulls.create(
173-
pulls_create_params
174-
)
175-
return create_pull_res.data.number
104+
const script = require('${{ github.action_path }}/scripts/action/create_pull_request.js')
105+
return await script({github, context})
176106
- name: Assign a user
177107
uses: actions/[email protected]
178108
if: inputs.repo-name == github.repository && steps.diff.outputs.result != '' && steps.get_pull_requests.outputs.result == 0 && github.event_name == 'pull_request' && github.event.action != 'closed' && github.event.pull_request.user.login != 'dependabot[bot]' && github.event.pull_request.user.login != 'renovate[bot]'
109+
env:
110+
PR_NUMBER: ${{steps.create_pull_request.outputs.result}}
111+
ASSIGN_USER: ${{github.event.pull_request.user.login}}
179112
with:
180113
github-token: ${{inputs.github-token}}
181114
script: |
182-
const issues_add_assignees_params = {
183-
owner: context.repo.owner,
184-
repo: context.repo.repo,
185-
issue_number: ${{steps.create_pull_request.outputs.result}},
186-
assignees: ["${{github.event.pull_request.user.login}}"]
187-
}
188-
console.log("call issues.addAssignees:")
189-
console.log(issues_add_assignees_params)
190-
await github.rest.issues.addAssignees(issues_add_assignees_params)
115+
const script = require('${{ github.action_path }}/scripts/action/assign_a_user.js')
116+
await script({github, context})
191117
# 既にformat修正のPRがある状態で、手動でformatを修正した場合、format修正のPRを閉じる
192118
- name: Close PullRequest
193119
uses: actions/[email protected]
194120
if: inputs.repo-name == github.repository && ((github.event_name == 'pull_request' && (github.event.action == 'closed' || steps.diff.outputs.result == '')) || ((github.event_name == 'schedule' || github.event_name == 'workflow_dispatch') && steps.diff.outputs.result == ''))
121+
env:
122+
HEAD_REF: ${{steps.get_head_ref.outputs.result}}
123+
ORG_NAME: ${{steps.set_org_name.outputs.result}}
124+
BRANCH_NAME_PREFIX: ${{inputs.branch-name-prefix}}
195125
with:
196126
github-token: ${{inputs.github-token}}
197127
script: |
198-
const HEAD_REF = "${{steps.get_head_ref.outputs.result}}"
199-
let head_name = "${{inputs.branch-name-prefix}}"
200-
201-
if (HEAD_REF != "") {
202-
head_name += "-" + HEAD_REF
203-
}
204-
205-
const common_params = {
206-
owner: context.repo.owner,
207-
repo: context.repo.repo
208-
}
209-
const pulls_list_params = {
210-
head: "${{steps.set_org_name.outputs.result}}:" + head_name,
211-
base: HEAD_REF,
212-
state: "open",
213-
...common_params
214-
}
215-
console.log("call pulls.list:", pulls_list_params)
216-
const pulls = await github.paginate(github.rest.pulls.list,
217-
pulls_list_params)
218-
219-
for (const pull of pulls) {
220-
const pulls_update_params = {
221-
pull_number: pull.number,
222-
state: "closed",
223-
...common_params
224-
}
225-
console.log("call pulls.update:", pulls_update_params)
226-
await github.rest.pulls.update(pulls_update_params)
227-
const git_deleteRef_params = {
228-
ref: "heads/" + head_name,
229-
...common_params
230-
}
231-
console.log("call git.deleteRef:", git_deleteRef_params)
232-
await github.rest.git.deleteRef(git_deleteRef_params)
233-
}
128+
const script = require('${{ github.action_path }}/scripts/action/close_pull_request.js')
129+
await script({github, context})
234130
- name: Exit
235131
if: (github.event_name != 'pull_request' || github.event.action != 'closed') && steps.diff.outputs.result != ''
236132
run: exit 1

scripts/action/assign_a_user.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = async ({ github, context }) => {
2+
const issuesAddAssigneesParams = {
3+
owner: context.repo.owner,
4+
repo: context.repo.repo,
5+
issue_number: process.env.PR_NUMBER,
6+
assignees: [process.env.ASSIGN_USER]
7+
}
8+
console.log('call issues.addAssignees:')
9+
console.log(issuesAddAssigneesParams)
10+
await github.rest.issues.addAssignees(issuesAddAssigneesParams)
11+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module.exports = async ({ github, context }) => {
2+
const HEAD_REF = process.env.HEAD_REF
3+
let headName = process.env.BRANCH_NAME_PREFIX
4+
5+
if (HEAD_REF !== '') {
6+
headName += '-' + HEAD_REF
7+
}
8+
9+
const commonParams = {
10+
owner: context.repo.owner,
11+
repo: context.repo.repo
12+
}
13+
const pullsListParams = {
14+
head: process.env.ORG_NAME + ':' + headName,
15+
base: HEAD_REF,
16+
state: 'open',
17+
...commonParams
18+
}
19+
console.log('call pulls.list:', pullsListParams)
20+
const pulls = await github.paginate(github.rest.pulls.list, pullsListParams)
21+
22+
for (const pull of pulls) {
23+
const pullsUpdateParams = {
24+
pull_number: pull.number,
25+
state: 'closed',
26+
...commonParams
27+
}
28+
console.log('call pulls.update:', pullsUpdateParams)
29+
await github.rest.pulls.update(pullsUpdateParams)
30+
const gitDeleteRefParams = {
31+
ref: 'heads/' + headName,
32+
...commonParams
33+
}
34+
console.log('call git.deleteRef:', gitDeleteRefParams)
35+
await github.rest.git.deleteRef(gitDeleteRefParams)
36+
}
37+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
module.exports = async ({ github, context }) => {
2+
const HEAD_REF = process.env.HEAD_REF
3+
const PR_NUMBER = process.env.PR_NUMBER
4+
const PR_TITLE_PREFIX = process.env.PR_TITLE_PREFIX
5+
let head = process.env.BRANCH_NAME_PREFIX
6+
7+
if (HEAD_REF !== '') {
8+
head += '-' + HEAD_REF
9+
}
10+
11+
const headWithRepo = process.env.ORG_NAME + ':' + head
12+
let title = PR_TITLE_PREFIX
13+
let body = process.env.PR_DESCRIPTION_PREFIX
14+
15+
if (PR_NUMBER !== '') {
16+
body += `本PR ( \`${head}\` ) をマージすると差分が次のPRに反映されます。\n`
17+
body += `* #${PR_NUMBER} ( \`${HEAD_REF}\` )\n`
18+
body += '```mermaid\n'
19+
body += `%%{init: {'gitGraph': {'mainBranchName': '${HEAD_REF}'}}}%%\n`
20+
body += 'gitGraph\n'
21+
22+
for (let i = 0; i < 2; i++) {
23+
body += ' commit\n'
24+
}
25+
26+
body += ` branch ${head}\n`
27+
body += ` checkout ${head}\n`
28+
let commit = PR_TITLE_PREFIX
29+
30+
if (commit.length > 6) {
31+
commit = commit.substring(0, 6) + '......'
32+
}
33+
34+
body += ` commit id: "${commit}"\n`
35+
body += ` checkout ${HEAD_REF}\n`
36+
body += ` merge ${head}\n`
37+
body += '```'
38+
title += ' #' + PR_NUMBER
39+
}
40+
41+
const pullsCreateParams = {
42+
owner: context.repo.owner,
43+
repo: context.repo.repo,
44+
head: headWithRepo,
45+
base: HEAD_REF,
46+
title,
47+
body
48+
}
49+
console.log('call pulls.create:', pullsCreateParams)
50+
const createPullRes = await github.rest.pulls.create(
51+
pullsCreateParams
52+
)
53+
return createPullRes.data.number
54+
}

scripts/action/get_head_ref.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = () => {
2+
const HEAD_REF = process.env.HEAD_REF
3+
const BASE_BRANCH = process.env.BASE_BRANCH
4+
5+
if (HEAD_REF === '' && BASE_BRANCH !== '') {
6+
return BASE_BRANCH
7+
}
8+
9+
return HEAD_REF
10+
}

0 commit comments

Comments
 (0)