|
1 | 1 | name: 'diff-pr-management' |
2 | 2 | description: '差分があればPRを作り、差分がなければPRを閉じます' |
3 | 3 | inputs: |
4 | | - github-token: # id of input |
| 4 | + github-token: # id of input |
5 | 5 | description: 'GITHUB_TOKEN' |
6 | 6 | required: true |
7 | 7 | runs: |
8 | 8 | using: "composite" |
9 | 9 | steps: |
10 | | - # 差分があったときは差分を出力する |
11 | | - - name: Show diff |
12 | | - id: diff |
13 | | - shell: bash |
14 | | - run: | |
15 | | - result=$(git diff) |
16 | | - echo "::set-output name=result::$result" |
17 | | - - shell: bash |
18 | | - run: | |
19 | | - REPO_NAME="${{ github.event.pull_request.head.repo.full_name }}" |
20 | | - echo "REPO_NAME=${REPO_NAME}" >> "${GITHUB_ENV}" |
21 | | - # 差分があったときは、コミットを作りpushする |
22 | | - - name: Push |
23 | | - env: |
24 | | - HEAD_REF: ${{github.event.pull_request.head.ref}} |
25 | | - if: env.REPO_NAME == github.repository && steps.diff.outputs.result != '' |
26 | | - run: | |
27 | | - git config user.name "github-actions[bot]" |
28 | | - EMAIL="41898282+github-actions[bot]@users.noreply.github.com" |
29 | | - git config user.email "${EMAIL}" |
30 | | - git add -u |
31 | | - git commit -m "鳩は唐揚げ!(自動で直してあげたよ!)" |
32 | | - REPO_URL="https://" |
33 | | - REPO_URL+="${{github.actor}}:${{inputs.github-token}}@github.com/" |
34 | | - REPO_URL+="${{github.repository}}.git" |
35 | | - GITHUB_HEAD="HEAD:refs/heads/fix-text-${HEAD_REF}" |
36 | | - git push -f "${REPO_URL}" "${GITHUB_HEAD}" |
37 | | - shell: bash |
38 | | - - name: Set org name |
39 | | - uses: actions/github-script@v6.1.0 |
40 | | - if: env.REPO_NAME == github.repository |
41 | | - id: set_org_name |
42 | | - with: |
43 | | - github-token: ${{inputs.github-token}} |
44 | | - result-encoding: string |
45 | | - script: return process.env.GITHUB_REPOSITORY.split('/')[0] |
46 | | - - name: Get PullRequests |
47 | | - uses: actions/github-script@v6.1.0 |
48 | | - env: |
49 | | - HEAD_REF: ${{github.event.pull_request.head.ref}} |
50 | | - if: env.REPO_NAME == github.repository && steps.diff.outputs.result != '' |
51 | | - id: get_pull_requests |
52 | | - with: |
53 | | - github-token: ${{inputs.github-token}} |
54 | | - script: | |
55 | | - const HEAD_REF = process.env["HEAD_REF"] |
56 | | - const pulls_list_params = { |
57 | | - owner: context.repo.owner, |
58 | | - repo: context.repo.repo, |
59 | | - head: "${{steps.set_org_name.outputs.result}}:fix-text-" + HEAD_REF, |
60 | | - base: HEAD_REF, |
61 | | - state: "open" |
62 | | - } |
63 | | - console.log("call pulls.list:", pulls_list_params) |
64 | | - const pulls = await github.paginate(github.rest.pulls.list, |
65 | | - pulls_list_params) |
66 | | - return pulls.length |
67 | | - # pushしたブランチでPRを作る |
68 | | - - name: Create PullRequest |
69 | | - uses: actions/github-script@v6.1.0 |
70 | | - env: |
71 | | - HEAD_REF: ${{github.event.pull_request.head.ref}} |
72 | | - if: env.REPO_NAME == github.repository |
73 | | - && steps.diff.outputs.result != '' |
74 | | - && steps.get_pull_requests.outputs.result == 0 |
75 | | - id: create_pull_request |
76 | | - with: |
77 | | - github-token: ${{inputs.github-token}} |
78 | | - script: | |
79 | | - const HEAD_REF = process.env["HEAD_REF"] |
80 | | - const number = "#${{github.event.pull_request.number}}" |
81 | | - let title = "日本語が間違ってたので直してあげたよ!PRをマージしてね! " |
82 | | - title += number |
83 | | - const pulls_create_params = { |
84 | | - owner: context.repo.owner, |
85 | | - repo: context.repo.repo, |
86 | | - head: "${{steps.set_org_name.outputs.result}}:fix-text-" + HEAD_REF, |
87 | | - base: HEAD_REF, |
88 | | - title, |
89 | | - body: "鳩の唐揚げおいしい!😋😋😋 " + number |
90 | | - } |
91 | | - console.log("call pulls.create:", pulls_create_params) |
92 | | - const create_pull_res = await github.rest.pulls.create( |
93 | | - pulls_create_params |
94 | | - ) |
95 | | - return create_pull_res.data.number |
96 | | - - name: Assign a user |
97 | | - uses: actions/github-script@v6.1.0 |
98 | | - if: env.REPO_NAME == github.repository |
99 | | - && steps.diff.outputs.result != '' |
100 | | - && steps.get_pull_requests.outputs.result == 0 |
101 | | - && github.event.pull_request.user.login != 'dependabot[bot]' |
102 | | - && github.event.pull_request.user.login != 'renovate[bot]' |
103 | | - with: |
104 | | - github-token: ${{inputs.github-token}} |
105 | | - script: | |
106 | | - const issues_add_assignees_params = { |
107 | | - owner: context.repo.owner, |
108 | | - repo: context.repo.repo, |
109 | | - issue_number: ${{steps.create_pull_request.outputs.result}}, |
110 | | - assignees: ["${{github.event.pull_request.user.login}}"] |
111 | | - } |
112 | | - console.log("call issues.addAssignees:") |
113 | | - console.log(issues_add_assignees_params) |
114 | | - await github.rest.issues.addAssignees(issues_add_assignees_params) |
115 | | - # 既にformat修正のPRがある状態で、手動でformatを修正した場合、format修正のPRを閉じる |
116 | | - - name: Close PullRequest |
117 | | - uses: actions/github-script@v6.1.0 |
118 | | - env: |
119 | | - HEAD_REF: ${{github.event.pull_request.head.ref}} |
120 | | - if: env.REPO_NAME == github.repository && steps.diff.outputs.result == '' |
121 | | - with: |
122 | | - github-token: ${{inputs.github-token}} |
123 | | - script: | |
124 | | - const HEAD_REF = process.env["HEAD_REF"] |
125 | | - const head_name = "fix-text-" + HEAD_REF |
126 | | - const common_params = { |
127 | | - owner: context.repo.owner, |
128 | | - repo: context.repo.repo |
129 | | - } |
130 | | - const pulls_list_params = { |
131 | | - head: "${{steps.set_org_name.outputs.result}}:" + head_name, |
132 | | - base: HEAD_REF, |
133 | | - state: "open", |
| 10 | + # 差分があったときは差分を出力する |
| 11 | + - name: Show diff |
| 12 | + id: diff |
| 13 | + shell: bash |
| 14 | + run: | |
| 15 | + result=$(git diff) |
| 16 | + echo "::set-output name=result::$result" |
| 17 | + - shell: bash |
| 18 | + run: | |
| 19 | + REPO_NAME="${{ github.event.pull_request.head.repo.full_name }}" |
| 20 | + echo "REPO_NAME=${REPO_NAME}" >> "${GITHUB_ENV}" |
| 21 | + # 差分があったときは、コミットを作りpushする |
| 22 | + - name: Push |
| 23 | + env: |
| 24 | + HEAD_REF: ${{github.event.pull_request.head.ref}} |
| 25 | + if: env.REPO_NAME == github.repository && steps.diff.outputs.result != '' |
| 26 | + run: | |
| 27 | + git config user.name "github-actions[bot]" |
| 28 | + EMAIL="41898282+github-actions[bot]@users.noreply.github.com" |
| 29 | + git config user.email "${EMAIL}" |
| 30 | + git add -u |
| 31 | + git commit -m "鳩は唐揚げ!(自動で直してあげたよ!)" |
| 32 | + REPO_URL="https://" |
| 33 | + REPO_URL+="${{github.actor}}:${{inputs.github-token}}@github.com/" |
| 34 | + REPO_URL+="${{github.repository}}.git" |
| 35 | + GITHUB_HEAD="HEAD:refs/heads/fix-text-${HEAD_REF}" |
| 36 | + git push -f "${REPO_URL}" "${GITHUB_HEAD}" |
| 37 | + shell: bash |
| 38 | + - name: Set org name |
| 39 | + uses: actions/github-script@v6.1.0 |
| 40 | + if: env.REPO_NAME == github.repository |
| 41 | + id: set_org_name |
| 42 | + with: |
| 43 | + github-token: ${{inputs.github-token}} |
| 44 | + result-encoding: string |
| 45 | + script: return process.env.GITHUB_REPOSITORY.split('/')[0] |
| 46 | + - name: Get PullRequests |
| 47 | + uses: actions/github-script@v6.1.0 |
| 48 | + env: |
| 49 | + HEAD_REF: ${{github.event.pull_request.head.ref}} |
| 50 | + if: env.REPO_NAME == github.repository && steps.diff.outputs.result != '' |
| 51 | + id: get_pull_requests |
| 52 | + with: |
| 53 | + github-token: ${{inputs.github-token}} |
| 54 | + script: | |
| 55 | + const HEAD_REF = process.env["HEAD_REF"] |
| 56 | + const pulls_list_params = { |
| 57 | + owner: context.repo.owner, |
| 58 | + repo: context.repo.repo, |
| 59 | + head: "${{steps.set_org_name.outputs.result}}:fix-text-" + HEAD_REF, |
| 60 | + base: HEAD_REF, |
| 61 | + state: "open" |
| 62 | + } |
| 63 | + console.log("call pulls.list:", pulls_list_params) |
| 64 | + const pulls = await github.paginate(github.rest.pulls.list, |
| 65 | + pulls_list_params) |
| 66 | + return pulls.length |
| 67 | + # pushしたブランチでPRを作る |
| 68 | + - name: Create PullRequest |
| 69 | + uses: actions/github-script@v6.1.0 |
| 70 | + env: |
| 71 | + HEAD_REF: ${{github.event.pull_request.head.ref}} |
| 72 | + if: env.REPO_NAME == github.repository |
| 73 | + && steps.diff.outputs.result != '' |
| 74 | + && steps.get_pull_requests.outputs.result == 0 |
| 75 | + id: create_pull_request |
| 76 | + with: |
| 77 | + github-token: ${{inputs.github-token}} |
| 78 | + script: | |
| 79 | + const HEAD_REF = process.env["HEAD_REF"] |
| 80 | + const number = "#${{github.event.pull_request.number}}" |
| 81 | + let title = "日本語が間違ってたので直してあげたよ!PRをマージしてね! " |
| 82 | + title += number |
| 83 | + const pulls_create_params = { |
| 84 | + owner: context.repo.owner, |
| 85 | + repo: context.repo.repo, |
| 86 | + head: "${{steps.set_org_name.outputs.result}}:fix-text-" + HEAD_REF, |
| 87 | + base: HEAD_REF, |
| 88 | + title, |
| 89 | + body: "鳩の唐揚げおいしい!😋😋😋 " + number |
| 90 | + } |
| 91 | + console.log("call pulls.create:", pulls_create_params) |
| 92 | + const create_pull_res = await github.rest.pulls.create( |
| 93 | + pulls_create_params |
| 94 | + ) |
| 95 | + return create_pull_res.data.number |
| 96 | + - name: Assign a user |
| 97 | + uses: actions/github-script@v6.1.0 |
| 98 | + if: env.REPO_NAME == github.repository |
| 99 | + && steps.diff.outputs.result != '' |
| 100 | + && steps.get_pull_requests.outputs.result == 0 |
| 101 | + && github.event.pull_request.user.login != 'dependabot[bot]' |
| 102 | + && github.event.pull_request.user.login != 'renovate[bot]' |
| 103 | + with: |
| 104 | + github-token: ${{inputs.github-token}} |
| 105 | + script: | |
| 106 | + const issues_add_assignees_params = { |
| 107 | + owner: context.repo.owner, |
| 108 | + repo: context.repo.repo, |
| 109 | + issue_number: ${{steps.create_pull_request.outputs.result}}, |
| 110 | + assignees: ["${{github.event.pull_request.user.login}}"] |
| 111 | + } |
| 112 | + console.log("call issues.addAssignees:") |
| 113 | + console.log(issues_add_assignees_params) |
| 114 | + await github.rest.issues.addAssignees(issues_add_assignees_params) |
| 115 | + # 既にformat修正のPRがある状態で、手動でformatを修正した場合、format修正のPRを閉じる |
| 116 | + - name: Close PullRequest |
| 117 | + uses: actions/github-script@v6.1.0 |
| 118 | + env: |
| 119 | + HEAD_REF: ${{github.event.pull_request.head.ref}} |
| 120 | + if: env.REPO_NAME == github.repository && steps.diff.outputs.result == '' |
| 121 | + with: |
| 122 | + github-token: ${{inputs.github-token}} |
| 123 | + script: | |
| 124 | + const HEAD_REF = process.env["HEAD_REF"] |
| 125 | + const head_name = "fix-text-" + HEAD_REF |
| 126 | + const common_params = { |
| 127 | + owner: context.repo.owner, |
| 128 | + repo: context.repo.repo |
| 129 | + } |
| 130 | + const pulls_list_params = { |
| 131 | + head: "${{steps.set_org_name.outputs.result}}:" + head_name, |
| 132 | + base: HEAD_REF, |
| 133 | + state: "open", |
| 134 | + ...common_params |
| 135 | + } |
| 136 | + console.log("call pulls.list:", pulls_list_params) |
| 137 | + const pulls = await github.paginate(github.rest.pulls.list, |
| 138 | + pulls_list_params) |
| 139 | +
|
| 140 | + for (const pull of pulls) { |
| 141 | + const pulls_update_params = { |
| 142 | + pull_number: pull.number, |
| 143 | + state: "closed", |
134 | 144 | ...common_params |
135 | 145 | } |
136 | | - console.log("call pulls.list:", pulls_list_params) |
137 | | - const pulls = await github.paginate(github.rest.pulls.list, |
138 | | - pulls_list_params) |
139 | | - |
140 | | - for (const pull of pulls) { |
141 | | - const pulls_update_params = { |
142 | | - pull_number: pull.number, |
143 | | - state: "closed", |
144 | | - ...common_params |
145 | | - } |
146 | | - console.log("call pulls.update:", pulls_update_params) |
147 | | - await github.rest.pulls.update(pulls_update_params) |
148 | | - const git_deleteRef_params = { |
149 | | - ref: "heads/" + head_name, |
150 | | - ...common_params |
151 | | - } |
152 | | - console.log("call git.deleteRef:", git_deleteRef_params) |
153 | | - await github.rest.git.deleteRef(git_deleteRef_params) |
| 146 | + console.log("call pulls.update:", pulls_update_params) |
| 147 | + await github.rest.pulls.update(pulls_update_params) |
| 148 | + const git_deleteRef_params = { |
| 149 | + ref: "heads/" + head_name, |
| 150 | + ...common_params |
154 | 151 | } |
155 | | - - name: Exit |
156 | | - if: steps.diff.outputs.result != '' |
157 | | - run: exit 1 |
158 | | - shell: bash |
| 152 | + console.log("call git.deleteRef:", git_deleteRef_params) |
| 153 | + await github.rest.git.deleteRef(git_deleteRef_params) |
| 154 | + } |
| 155 | + - name: Exit |
| 156 | + if: steps.diff.outputs.result != '' |
| 157 | + run: exit 1 |
| 158 | + shell: bash |
0 commit comments