Skip to content

Commit 57d8092

Browse files
PR以外のトリガーの場合でも特別なパラメータなしに動作するようにする (#466)
* PR以外のトリガーの場合でも特別な引数なしに動作するようにする * README修正 (#467) Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Add debug * Use github.ref_name * description修正 * Revert "Add debug" This reverts commit 014863e * HEAD_REFを設定する処理追加 * Add debug * Revert "Add debug" This reverts commit 481eaf2. Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent f2869e7 commit 57d8092

10 files changed

Lines changed: 51 additions & 84 deletions

File tree

.github/workflows/format-json-yml.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
- uses: actions/checkout@v3.1.0
2424
with:
2525
fetch-depth: 0
26-
ref: ${{ github.event.pull_request.head.sha }}
26+
ref: ${{ github.event.pull_request.head.sha || github.sha }}
2727
- uses: dev-hato/actions-format-json-yml@v0.0.24
2828
with:
2929
github-token: ${{secrets.GITHUB_TOKEN}}

.github/workflows/update-gitleaks.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
- uses: actions/checkout@v3.1.0
2424
with:
2525
fetch-depth: 0
26-
ref: ${{ github.event.pull_request.head.sha }}
26+
ref: ${{ github.event.pull_request.head.sha || github.sha }}
2727
- uses: actions/setup-node@v3.5.1
2828
with:
2929
cache: npm

.github/workflows/update-package.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
- uses: actions/checkout@v3.1.0
2424
with:
2525
fetch-depth: 0
26-
ref: ${{ github.event.pull_request.head.sha }}
26+
ref: ${{ github.event.pull_request.head.sha || github.sha }}
2727
- uses: actions/setup-node@v3.5.1
2828
with:
2929
cache: npm
@@ -34,7 +34,6 @@ jobs:
3434
branch-name-prefix: fix-package
3535
pr-title-prefix: package.json, package-lock.json修正
3636
pr-description-prefix: package.jsonやpackage-lock.jsonを修正しました。
37-
repo-name: ${{ github.event.pull_request.head.repo.full_name }}
3837

3938
concurrency:
4039
group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.ref }}

.github/workflows/update-readme.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
- uses: actions/checkout@v3.1.0
2323
with:
2424
fetch-depth: 0
25-
ref: ${{ github.event.pull_request.head.sha }}
25+
ref: ${{ github.event.pull_request.head.sha || github.sha }}
2626
- uses: actions/setup-node@v3.5.1
2727
with:
2828
cache: npm
@@ -31,7 +31,7 @@ jobs:
3131
id: get_inputs_markdown
3232
uses: actions/github-script@v6.3.3
3333
env:
34-
HEAD_REF: ${{github.event.pull_request.head.ref}}
34+
HEAD_REF: ${{github.event.pull_request.head.ref || github.head_ref}}
3535
with:
3636
github-token: ${{secrets.GITHUB_TOKEN}}
3737
result-encoding: string
@@ -47,7 +47,6 @@ jobs:
4747
branch-name-prefix: fix-readme
4848
pr-title-prefix: README修正
4949
pr-description-prefix: READMEを修正しました。
50-
repo-name: ${{ github.event.pull_request.head.repo.full_name }}
5150

5251
concurrency:
5352
group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.ref }}

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@ jobs:
2323
- uses: actions/checkout@v3
2424
with:
2525
fetch-depth: 0
26-
ref: ${{ github.event.pull_request.head.sha }}
26+
ref: ${{ github.event.pull_request.head.sha || github.sha }}
2727
- run: hoge fmt # FIXME フォーマッタを走らせる
2828
- uses: dev-hato/actions-diff-pr-management@v1
2929
with:
3030
github-token: ${{secrets.GITHUB_TOKEN}}
31-
repo-name: ${{ github.event.pull_request.head.repo.full_name }}
3231
```
3332
3433
## 例
@@ -41,11 +40,9 @@ jobs:
4140
| 引数名 | 説明 | 必須 |
4241
|:---:|:---:|:---:|
4342
| github-token | GitHubのトークン。 | O |
44-
| repo-name | リポジトリ名。 `pull_request` 以外のトリガーも設定している場合はリポジトリ名を決め打ちで入力。 | O |
4543
| branch-name-prefix | branch名の接頭語。 | |
4644
| pr-title-prefix | PRのタイトルの接頭語。 | |
4745
| pr-description-prefix | 本文の接頭語。 | |
48-
| base-branch | ベースブランチ。 `pull_request` 以外のトリガーの場合は決め打ちで入力。 | |
4946
5047
## 開発
5148

README.template.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@ jobs:
2323
- uses: actions/checkout@v3
2424
with:
2525
fetch-depth: 0
26-
ref: ${{ github.event.pull_request.head.sha }}
26+
ref: ${{ github.event.pull_request.head.sha || github.sha }}
2727
- run: hoge fmt # FIXME フォーマッタを走らせる
2828
- uses: dev-hato/actions-diff-pr-management@v1
2929
with:
3030
github-token: ${{secrets.GITHUB_TOKEN}}
31-
repo-name: ${{ github.event.pull_request.head.repo.full_name }}
3231
```
3332
3433
## 例

action.yml

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ inputs:
55
github-token: # id of input
66
description: 'GitHubのトークン。'
77
required: true
8-
repo-name:
9-
description: 'リポジトリ名。 `pull_request` 以外のトリガーも設定している場合はリポジトリ名を決め打ちで入力。'
10-
required: true
118
branch-name-prefix:
129
description: 'branch名の接頭語。'
1310
required: true
@@ -20,10 +17,6 @@ inputs:
2017
description: '本文の接頭語。'
2118
required: true
2219
default: ""
23-
base-branch:
24-
description: 'ベースブランチ。 `pull_request` 以外のトリガーの場合は決め打ちで入力。'
25-
required: false
26-
default: ""
2720
runs:
2821
using: "composite"
2922
steps:
@@ -36,45 +29,30 @@ runs:
3629
# 差分があったときは、コミットを作りpushする
3730
- name: Push
3831
env:
39-
HEAD_REF: ${{github.event.pull_request.head.ref}}
32+
HEAD_REF: ${{github.event.pull_request.head.ref || github.ref_name}}
4033
PR_TITLE_PREFIX: ${{inputs.pr-title-prefix}}
4134
AUTHOR: ${{github.actor}}
4235
GITHUB_TOKEN: ${{inputs.github-token}}
4336
REPOSITORY: ${{github.repository}}
4437
BRANCH_NAME_PREFIX: ${{inputs.branch-name-prefix}}
45-
BASE_BRANCH: ${{inputs.base-branch}}
46-
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')
38+
if: steps.diff.outputs.result != '' && ((github.event_name == 'pull_request' && github.event.action != 'closed') || github.event_name == 'schedule' || github.event_name == 'workflow_dispatch')
4739
run: ${{ github.action_path }}/scripts/action/push.sh
4840
shell: bash
4941
- name: Set org name
5042
uses: actions/github-script@v6.3.3
51-
if: inputs.repo-name == github.repository && (github.event_name == 'pull_request' || github.event_name == 'schedule' || github.event_name == 'workflow_dispatch')
43+
if: github.event_name == 'pull_request' || github.event_name == 'schedule' || github.event_name == 'workflow_dispatch'
5244
id: set_org_name
5345
with:
5446
github-token: ${{inputs.github-token}}
5547
result-encoding: string
5648
script: |
5749
const script = require('${{ github.action_path }}/scripts/action/set_org_name.js')
5850
return script()
59-
- name: Get HEAD_REF
60-
uses: actions/github-script@v6.3.3
61-
env:
62-
HEAD_REF: ${{github.event.pull_request.head.ref}}
63-
BASE_BRANCH: ${{inputs.base-branch}}
64-
if: inputs.repo-name == github.repository
65-
id: get_head_ref
66-
with:
67-
github-token: ${{inputs.github-token}}
68-
result-encoding: string
69-
script: |
70-
const script = require('${{ github.action_path }}/scripts/action/get_head_ref.js')
71-
return script()
7251
- name: Get PullRequests
7352
uses: actions/github-script@v6.3.3
74-
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')
53+
if: steps.diff.outputs.result != '' && ((github.event_name == 'pull_request' && github.event.action != 'closed') || github.event_name == 'schedule' || github.event_name == 'workflow_dispatch')
7554
id: get_pull_requests
7655
env:
77-
HEAD_REF: ${{steps.get_head_ref.outputs.result}}
7856
ORG_NAME: ${{steps.set_org_name.outputs.result}}
7957
BRANCH_NAME_PREFIX: ${{inputs.branch-name-prefix}}
8058
with:
@@ -85,10 +63,9 @@ runs:
8563
# pushしたブランチでPRを作る
8664
- name: Create PullRequest
8765
uses: actions/github-script@v6.3.3
88-
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')
66+
if: 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')
8967
id: create_pull_request
9068
env:
91-
HEAD_REF: ${{steps.get_head_ref.outputs.result}}
9269
ORG_NAME: ${{steps.set_org_name.outputs.result}}
9370
PR_DESCRIPTION_PREFIX: ${{inputs.pr-description-prefix}}
9471
PR_NUMBER: ${{github.event.pull_request.number}}
@@ -101,7 +78,7 @@ runs:
10178
return await script({github, context})
10279
- name: Assign a user
10380
uses: actions/github-script@v6.3.3
104-
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]'
81+
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]'
10582
env:
10683
PR_NUMBER: ${{steps.create_pull_request.outputs.result}}
10784
ASSIGN_USER: ${{github.event.pull_request.user.login}}
@@ -113,9 +90,8 @@ runs:
11390
# 既にformat修正のPRがある状態で、手動でformatを修正した場合、format修正のPRを閉じる
11491
- name: Close PullRequest
11592
uses: actions/github-script@v6.3.3
116-
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 == ''))
93+
if: (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 == '')
11794
env:
118-
HEAD_REF: ${{steps.get_head_ref.outputs.result}}
11995
ORG_NAME: ${{steps.set_org_name.outputs.result}}
12096
BRANCH_NAME_PREFIX: ${{inputs.branch-name-prefix}}
12197
with:

scripts/action/create_pull_request.js

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,42 @@ module.exports = async ({ github, context }) => {
1212
let title = PR_TITLE_PREFIX
1313
let body = process.env.PR_DESCRIPTION_PREFIX
1414

15+
body += `本PR ( \`${head}\` ) をマージすると差分が次のPRに反映されます。\n`
16+
body += '* '
17+
18+
if (PR_NUMBER !== '') {
19+
body += `#${PR_NUMBER} ( `
20+
}
21+
22+
body += `\`${HEAD_REF}\``
23+
24+
if (PR_NUMBER !== '') {
25+
body += ' )'
26+
}
27+
28+
body += '\n'
29+
body += '```mermaid\n'
30+
body += `%%{init: {'gitGraph': {'mainBranchName': '${HEAD_REF}'}}}%%\n`
31+
body += 'gitGraph\n'
32+
33+
for (let i = 0; i < 2; i++) {
34+
body += ' commit\n'
35+
}
36+
37+
body += ` branch ${head}\n`
38+
body += ` checkout ${head}\n`
39+
let commit = PR_TITLE_PREFIX
40+
41+
if (commit.length > 6) {
42+
commit = commit.substring(0, 6) + '......'
43+
}
44+
45+
body += ` commit id: "${commit}"\n`
46+
body += ` checkout ${HEAD_REF}\n`
47+
body += ` merge ${head}\n`
48+
body += '```'
49+
1550
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 += '```'
3851
title += ' #' + PR_NUMBER
3952
}
4053

scripts/action/get_head_ref.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

scripts/action/push.sh

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
#!/usr/bin/env bash
22

3+
echo "HEAD_REF=${HEAD_REF}" >>"${GITHUB_ENV}"
34
git config user.name "github-actions[bot]"
45
EMAIL="41898282+github-actions[bot]@users.noreply.github.com"
56
git config user.email "${EMAIL}"
67
git commit -m "${PR_TITLE_PREFIX}"
78
REPO_URL="https://"
89
REPO_URL+="${AUTHOR}:${GITHUB_TOKEN}@github.com/"
910
REPO_URL+="${REPOSITORY}.git"
10-
GITHUB_HEAD="HEAD:refs/heads/${BRANCH_NAME_PREFIX}"
11-
12-
if [ "${HEAD_REF}" != "" ]; then
13-
GITHUB_HEAD+="-${HEAD_REF}"
14-
elif [ "${BASE_BRANCH}" != "" ]; then
15-
GITHUB_HEAD+="-${BASE_BRANCH}"
16-
fi
17-
11+
GITHUB_HEAD="HEAD:refs/heads/${BRANCH_NAME_PREFIX}-${HEAD_REF}"
1812
git push -f "${REPO_URL}" "${GITHUB_HEAD}"

0 commit comments

Comments
 (0)