@@ -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@@ -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
77647865 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
94779578 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
1209112192 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
177107178108 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
193119194120 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
0 commit comments