Skip to content

Commit ac4f08d

Browse files
修正PRにラベルを付与できるようにする (#2084)
* 修正PRにラベルを付与できるようにする * format修正 (#2085) Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 039502c commit ac4f08d

File tree

4 files changed

+51
-17
lines changed

4 files changed

+51
-17
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ jobs:
5151
| branch-name-prefix | branch名の接頭語。 | | fix |
5252
| pr-title-prefix | PRのタイトルの接頭語。 | | fix |
5353
| pr-description-prefix | 本文の接頭語。 | | |
54+
| pr-labels | PRに付与するラベル。カンマ区切りで指定する。 | | |
5455
| exit-failure | 実行完了時にCIを失敗させるかどうか。 | | true |
5556
| working-directory | 実行対象のディレクトリ | | |
5657
| no-verify | `git commit`, `git push` 時のフックを無効化する | | false |

action.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ inputs:
1717
description: "本文の接頭語。"
1818
required: false
1919
default: ""
20+
pr-labels:
21+
description: "PRに付与するラベル。カンマ区切りで指定する。"
22+
required: false
23+
default: ""
2024
exit-failure:
2125
description: "実行完了時にCIを失敗させるかどうか。"
2226
required: false
@@ -52,6 +56,7 @@ runs:
5256
5357
echo "PR_NUMBER=${{github.event.pull_request.number}}" >> "$GITHUB_ENV"
5458
echo "PR_TITLE_PREFIX=${{inputs.pr-title-prefix}}" >> "$GITHUB_ENV"
59+
echo "PR_LABELS=${{inputs.pr-labels}}" >> "$GITHUB_ENV"
5560
echo "BRANCH_NAME_PREFIX=${{inputs.branch-name-prefix}}" >> "$GITHUB_ENV"
5661
# 差分があったときは、コミットを作りpushする
5762
- name: Push
@@ -92,7 +97,7 @@ runs:
9297
script: |
9398
const {script} = require('${{ github.action_path }}/dist/assign_a_user.js')
9499
await script(github, context)
95-
# 修正PRのタイトルやDescriptionを更新する
100+
# 修正PRのタイトルやDescription、ラベルを更新する
96101
- name: Update PullRequest
97102
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
98103
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')

dist/update_pull_request.js

Lines changed: 21 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/update_pull_request.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,35 @@ export async function script(
1111
const { title, body } = generateTitleDescription();
1212

1313
for (const pull of await getPullRequests(github, context)) {
14-
if (pull.title === title && pull.body === body) {
14+
// PRのタイトルやDescriptionを更新する
15+
if (pull.title !== title || pull.body !== body) {
16+
const pullsUpdateParams: RestEndpointMethodTypes["pulls"]["update"]["parameters"] =
17+
{
18+
owner: context.repo.owner,
19+
repo: context.repo.repo,
20+
pull_number: pull.number,
21+
title,
22+
body,
23+
};
24+
console.log("call pulls.update:", pullsUpdateParams);
25+
await github.rest.pulls.update(pullsUpdateParams);
26+
}
27+
28+
const labels: string[] | undefined = process.env.PR_LABELS?.split(",");
29+
30+
if (labels === undefined || labels.length === 0) {
1531
continue;
1632
}
1733

18-
// PRのタイトルやDescriptionを更新する
19-
const pullsUpdateParams: RestEndpointMethodTypes["pulls"]["update"]["parameters"] =
34+
// ラベルを付与する
35+
const issuesAddLabelsParams: RestEndpointMethodTypes["issues"]["addLabels"]["parameters"] =
2036
{
2137
owner: context.repo.owner,
2238
repo: context.repo.repo,
23-
pull_number: pull.number,
24-
title,
25-
body,
39+
issue_number: pull.number,
40+
labels,
2641
};
27-
console.log("call pulls.update:", pullsUpdateParams);
28-
await github.rest.pulls.update(pullsUpdateParams);
42+
console.log("call issues.addLabels:", issuesAddLabelsParams);
43+
await github.rest.issues.addLabels(issuesAddLabelsParams);
2944
}
3045
}

0 commit comments

Comments
 (0)