Skip to content

Commit 9455cef

Browse files
authored
修正PRのタイトルやDescriptionの更新に対応する (#1704)
* 修正PRのタイトルやDescriptionの更新に対応する * get_number_of_pull_requestsの条件削除
1 parent 38c1276 commit 9455cef

7 files changed

Lines changed: 126 additions & 77 deletions

action.yml

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,38 +42,37 @@ runs:
4242
- name: Set HEAD_REF
4343
shell: bash
4444
run: echo "HEAD_REF=${{github.event.pull_request.head.ref || github.ref_name}}" >>"${GITHUB_ENV}"
45+
- name: Set env
46+
id: set-env
47+
shell: bash
48+
run: |
49+
echo "PR_DESCRIPTION_PREFIX=${{inputs.pr-description-prefix}}" >> "$GITHUB_ENV"
50+
echo "PR_NUMBER=${{github.event.pull_request.number}}" >> "$GITHUB_ENV"
51+
echo "PR_TITLE_PREFIX=${{inputs.pr-title-prefix}}" >> "$GITHUB_ENV"
52+
echo "BRANCH_NAME_PREFIX=${{inputs.branch-name-prefix}}" >> "$GITHUB_ENV"
4553
# 差分があったときは、コミットを作りpushする
4654
- name: Push
4755
env:
48-
PR_TITLE_PREFIX: ${{inputs.pr-title-prefix}}
4956
TOKEN: ${{inputs.github-token}}
50-
BRANCH_NAME_PREFIX: ${{inputs.branch-name-prefix}}
5157
NO_VERIFY: ${{inputs.no-verify}}
5258
if: steps.diff.outputs.result != '' && ((github.event_name == 'pull_request' && github.event.action != 'closed') || github.event_name == 'push' || github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' || github.event_name == 'repository_dispatch')
5359
working-directory: ${{inputs.working-directory}}
5460
run: ${{ github.action_path }}/scripts/action/push.sh
5561
shell: bash
56-
- name: Get PullRequests
62+
- name: Get a number of PullRequests
5763
uses: actions/github-script@v7.0.1
5864
if: steps.diff.outputs.result != '' && ((github.event_name == 'pull_request' && github.event.action != 'closed') || github.event_name == 'push' || github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' || github.event_name == 'repository_dispatch')
59-
id: get_pull_requests
60-
env:
61-
BRANCH_NAME_PREFIX: ${{inputs.branch-name-prefix}}
65+
id: get_number_of_pull_requests
6266
with:
6367
github-token: ${{inputs.github-token}}
6468
script: |
65-
const script = require('${{ github.action_path }}/scripts/action/get_pull_requests.js')
69+
const script = require('${{ github.action_path }}/scripts/action/get_number_of_pull_requests.js')
6670
return await script({github, context})
6771
# pushしたブランチで修正PRを作る
6872
- name: Create PullRequest
6973
uses: actions/github-script@v7.0.1
70-
if: steps.diff.outputs.result != '' && steps.get_pull_requests.outputs.result == 0 && ((github.event_name == 'pull_request' && github.event.action != 'closed') || github.event_name == 'push' || github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' || github.event_name == 'repository_dispatch')
74+
if: steps.diff.outputs.result != '' && steps.get_number_of_pull_requests.outputs.result == 0 && ((github.event_name == 'pull_request' && github.event.action != 'closed') || github.event_name == 'push' || github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' || github.event_name == 'repository_dispatch')
7175
id: create_pull_request
72-
env:
73-
PR_DESCRIPTION_PREFIX: ${{inputs.pr-description-prefix}}
74-
PR_NUMBER: ${{github.event.pull_request.number}}
75-
PR_TITLE_PREFIX: ${{inputs.pr-title-prefix}}
76-
BRANCH_NAME_PREFIX: ${{inputs.branch-name-prefix}}
7776
with:
7877
github-token: ${{inputs.github-token}}
7978
script: |
@@ -82,20 +81,28 @@ runs:
8281
# 元のPRを出したユーザーを修正PRにアサインする
8382
- name: Assign a user
8483
uses: actions/github-script@v7.0.1
85-
if: 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]'
84+
if: steps.diff.outputs.result != '' && steps.get_number_of_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]'
8685
env:
8786
PR_NUMBER: ${{steps.create_pull_request.outputs.result}}
8887
with:
8988
github-token: ${{inputs.github-token}}
9089
script: |
9190
const script = require('${{ github.action_path }}/scripts/action/assign_a_user.js')
9291
await script({github, context})
92+
# 修正PRのタイトルやDescriptionを更新する
93+
- name: Update PullRequest
94+
uses: actions/github-script@v7.0.1
95+
if: steps.diff.outputs.result != '' && ((github.event_name == 'pull_request' && github.event.action != 'closed') || github.event_name == 'push' || github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' || github.event_name == 'repository_dispatch')
96+
id: update_pull_request
97+
with:
98+
github-token: ${{inputs.github-token}}
99+
script: |
100+
const script = require('${{ github.action_path }}/scripts/action/update_pull_request.js')
101+
return await script({github, context})
93102
# 既に修正PRがある状態で、手動でformatを修正した場合、修正PRを閉じる
94103
- name: Close PullRequest
95104
uses: actions/github-script@v7.0.1
96105
if: (github.event_name == 'pull_request' && (github.event.action == 'closed' || steps.diff.outputs.result == '')) || ((github.event_name == 'push' || github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' || github.event_name == 'repository_dispatch') && steps.diff.outputs.result == '')
97-
env:
98-
BRANCH_NAME_PREFIX: ${{inputs.branch-name-prefix}}
99106
with:
100107
github-token: ${{inputs.github-token}}
101108
script: |

scripts/action/close_pull_request.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const getPullRequests = require("./get_pull_requests.js");
2+
13
module.exports = async ({ github, context }) => {
24
const HEAD_REF = process.env.HEAD_REF;
35
let headName = process.env.BRANCH_NAME_PREFIX;
@@ -11,16 +13,7 @@ module.exports = async ({ github, context }) => {
1113
repo: context.repo.repo,
1214
};
1315

14-
// 修正PRの情報を取得する
15-
const pullsListParams = {
16-
head: context.repo.owner + ":" + headName,
17-
state: "open",
18-
...commonParams,
19-
};
20-
console.log("call pulls.list:", pullsListParams);
21-
const pulls = await github.paginate(github.rest.pulls.list, pullsListParams);
22-
23-
for (const pull of pulls) {
16+
for (const pull of await getPullRequests({ github, context })) {
2417
// 修正PRをcloseする (修正PRのstateをclosedに更新する)
2518
const pullsUpdateParams = {
2619
pull_number: pull.number,

scripts/action/create_pull_request.js

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,15 @@
1+
const generateTitleDescription = require("./generate_title_description.js");
2+
13
module.exports = async ({ github, context }) => {
24
const HEAD_REF = process.env.HEAD_REF;
3-
const escapedHeadRef = HEAD_REF.replaceAll("#", "");
4-
const PR_NUMBER = process.env.PR_NUMBER;
5-
const PR_TITLE_PREFIX = process.env.PR_TITLE_PREFIX;
65
let head = process.env.BRANCH_NAME_PREFIX;
76

87
if (HEAD_REF !== "") {
98
head += "-" + HEAD_REF;
109
}
1110

12-
const escapedHead = head.replaceAll("#", "");
1311
const headWithRepo = context.repo.owner + ":" + head;
14-
let title = PR_TITLE_PREFIX;
15-
let body = process.env.PR_DESCRIPTION_PREFIX;
16-
17-
body += `本PR ( \`${escapedHead}\` ) をマージすると差分が次のPRに反映されます。\n`;
18-
body += "* ";
19-
20-
if (PR_NUMBER !== "") {
21-
body += `#${PR_NUMBER} ( `;
22-
}
23-
24-
body += `\`${escapedHeadRef}\``;
25-
26-
if (PR_NUMBER !== "") {
27-
body += " )";
28-
}
29-
30-
body += "\n\n";
31-
body += `CIが再度実行されると本PR ( \`${escapedHead}\` ) にforce pushされます。\n`;
32-
body += "```mermaid\n";
33-
body += `%%{init: {'gitGraph': {'mainBranchName': '${escapedHeadRef}'}}}%%\n`;
34-
body += "gitGraph\n";
35-
36-
for (let i = 0; i < 2; i++) {
37-
body += " commit\n";
38-
}
39-
40-
body += ` branch ${escapedHead}\n`;
41-
body += ` checkout ${escapedHead}\n`;
42-
let commit = PR_TITLE_PREFIX;
43-
44-
if (commit.length > 6) {
45-
commit = commit.substring(0, 6) + "......";
46-
}
47-
48-
body += ` commit id: "${commit}"\n`;
49-
body += ` checkout ${escapedHeadRef}\n`;
50-
body += ` merge ${escapedHead}\n`;
51-
body += "```";
52-
53-
if (PR_NUMBER !== "") {
54-
title += " #" + PR_NUMBER;
55-
}
56-
12+
const { title, body } = generateTitleDescription();
5713
const pullsCreateParams = {
5814
owner: context.repo.owner,
5915
repo: context.repo.repo,
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
module.exports = () => {
2+
const HEAD_REF = process.env.HEAD_REF;
3+
const escapedHeadRef = HEAD_REF.replaceAll("#", "");
4+
const PR_NUMBER = process.env.PR_NUMBER;
5+
const PR_TITLE_PREFIX = process.env.PR_TITLE_PREFIX;
6+
let head = process.env.BRANCH_NAME_PREFIX;
7+
8+
if (HEAD_REF !== "") {
9+
head += "-" + HEAD_REF;
10+
}
11+
12+
const escapedHead = head.replaceAll("#", "");
13+
let title = PR_TITLE_PREFIX;
14+
let body = process.env.PR_DESCRIPTION_PREFIX;
15+
16+
body += `本PR ( \`${escapedHead}\` ) をマージすると差分が次のPRに反映されます。\n`;
17+
body += "* ";
18+
19+
if (PR_NUMBER !== "") {
20+
body += `#${PR_NUMBER} ( `;
21+
}
22+
23+
body += `\`${escapedHeadRef}\``;
24+
25+
if (PR_NUMBER !== "") {
26+
body += " )";
27+
}
28+
29+
body += "\n\n";
30+
body += `CIが再度実行されると本PR ( \`${escapedHead}\` ) にforce pushされます。\n`;
31+
body += "```mermaid\n";
32+
body += `%%{init: {'gitGraph': {'mainBranchName': '${escapedHeadRef}'}}}%%\n`;
33+
body += "gitGraph\n";
34+
35+
for (let i = 0; i < 2; i++) {
36+
body += " commit\n";
37+
}
38+
39+
body += ` branch ${escapedHead}\n`;
40+
body += ` checkout ${escapedHead}\n`;
41+
let commit = PR_TITLE_PREFIX;
42+
43+
if (commit.length > 6) {
44+
commit = commit.substring(0, 6) + "......";
45+
}
46+
47+
body += ` commit id: "${commit}"\n`;
48+
body += ` checkout ${escapedHeadRef}\n`;
49+
body += ` merge ${escapedHead}\n`;
50+
body += "```";
51+
52+
if (PR_NUMBER !== "") {
53+
title += " #" + PR_NUMBER;
54+
}
55+
56+
return {
57+
title,
58+
body,
59+
};
60+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const getPullRequests = require("./get_pull_requests.js");
2+
3+
module.exports = async ({ github, context }) => {
4+
const HEAD_REF = process.env.HEAD_REF;
5+
const pulls = await getPullRequests({
6+
github,
7+
context,
8+
additionalParams: { base: HEAD_REF },
9+
});
10+
return pulls.length;
11+
};
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = async ({ github, context }) => {
1+
module.exports = async ({ github, context, additionalParams }) => {
22
const HEAD_REF = process.env.HEAD_REF;
33
let head = context.repo.owner + ":" + process.env.BRANCH_NAME_PREFIX;
44

@@ -10,10 +10,9 @@ module.exports = async ({ github, context }) => {
1010
owner: context.repo.owner,
1111
repo: context.repo.repo,
1212
head,
13-
base: HEAD_REF,
1413
state: "open",
14+
...additionalParams,
1515
};
1616
console.log("call pulls.list:", pullsListParams);
17-
const pulls = await github.paginate(github.rest.pulls.list, pullsListParams);
18-
return pulls.length;
17+
return await github.paginate(github.rest.pulls.list, pullsListParams);
1918
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const generateTitleDescription = require("./generate_title_description.js");
2+
const getPullRequests = require("./get_pull_requests.js");
3+
4+
module.exports = async ({ github, context }) => {
5+
const { title, body } = generateTitleDescription();
6+
7+
for (const pull of await getPullRequests({ github, context })) {
8+
if (pull.title === title && pull.body === body) {
9+
continue;
10+
}
11+
12+
// PRのタイトルやDescriptionを更新する
13+
const pullsUpdateParams = {
14+
owner: context.repo.owner,
15+
repo: context.repo.repo,
16+
pull_number: pull.number,
17+
title,
18+
body,
19+
};
20+
console.log("call pulls.update:", pullsUpdateParams);
21+
await github.rest.pulls.update(pullsUpdateParams);
22+
}
23+
};

0 commit comments

Comments
 (0)