|
| 1 | +name: 'diff-pr-management' |
| 2 | +description: '差分があればPRを作り、差分がなければPRを閉じます' |
| 3 | +inputs: |
| 4 | + who-to-greet: # id of input |
| 5 | + description: 'Who to greet' |
| 6 | + required: true |
| 7 | + default: 'World' |
| 8 | +outputs: |
| 9 | + random-number: |
| 10 | + description: "Random number" |
| 11 | + value: ${{ steps.random-number-generator.outputs.random-id }} |
| 12 | +runs: |
| 13 | + using: "composite" |
| 14 | + steps: |
| 15 | + # 差分があったときは差分を出力する |
| 16 | + - name: Show diff |
| 17 | + id: show_diff |
| 18 | + run: | |
| 19 | + echo "::set-output name=diff::$(git diff)" |
| 20 | + # 差分があったときは、コミットを作りpushする |
| 21 | + - name: Push |
| 22 | + if: steps.show_diff.outputs.diff != '' |
| 23 | + run: | |
| 24 | + git config user.name "hatohakaraage" |
| 25 | + git config user.email "hatohakaraage@example.com" |
| 26 | + git add -u |
| 27 | + git commit -m "鳩は唐揚げ!(自動で直してあげたよ!)" |
| 28 | + git push -f https://${{github.actor}}:${{secrets.GITHUB_TOKEN}}@github.com/${{github.repository}}.git HEAD:refs/heads/fix-text-${{github.event.pull_request.head.ref}} |
| 29 | + # pushしたブランチでPRを作る |
| 30 | + - name: Create PullRequest |
| 31 | + uses: actions/github-script@v3 |
| 32 | + if: steps.show_diff.outputs.diff != '' |
| 33 | + with: |
| 34 | + github-token: ${{secrets.GITHUB_TOKEN}} |
| 35 | + script: | |
| 36 | + const common_params = { |
| 37 | + owner: context.repo.owner, |
| 38 | + repo: context.repo.repo |
| 39 | + } |
| 40 | + const pull_params = { |
| 41 | + head: "dev-hato:fix-text-${{github.event.pull_request.head.ref}}", |
| 42 | + base: "${{github.event.pull_request.head.ref}}", |
| 43 | + ...common_params |
| 44 | + } |
| 45 | + const pulls_list_params = { |
| 46 | + state: "open", |
| 47 | + ...pull_params |
| 48 | + } |
| 49 | + console.log("call pulls.list:") |
| 50 | + console.log(pulls_list_params) |
| 51 | + github.pulls.list(pulls_list_params).then(list_res => { |
| 52 | + if (list_res.data.length === 0) { |
| 53 | + const pulls_create_params = { |
| 54 | + title: "日本語が間違ってたので直してあげたよ!PRをマージしてね! #${{github.event.pull_request.number}}", |
| 55 | + body: "鳩の唐揚げおいしい!😋😋😋 #${{github.event.pull_request.number}}", |
| 56 | + ...pull_params |
| 57 | + } |
| 58 | + console.log("call pulls.create:") |
| 59 | + console.log(pulls_create_params) |
| 60 | + github.pulls.create(pulls_create_params).then(create_res => { |
| 61 | + if("${{github.event.pull_request.user.login}}" !== "dependabot[bot]") { |
| 62 | + const issues_add_assignees_params = { |
| 63 | + issue_number: create_res.data.number, |
| 64 | + assignees: ["${{github.event.pull_request.user.login}}"], |
| 65 | + ...common_params |
| 66 | + } |
| 67 | + console.log("call issues.addAssignees:") |
| 68 | + console.log(issues_add_assignees_params) |
| 69 | + github.issues.addAssignees(issues_add_assignees_params) |
| 70 | + } |
| 71 | + }) |
| 72 | + } |
| 73 | + }) |
| 74 | + # 既にformat修正のPRがある状態で、手動でformatを修正した場合、format修正のPRを閉じる |
| 75 | + - name: Close PullRequest |
| 76 | + uses: actions/github-script@v3 |
| 77 | + if: steps.show_diff.outputs.diff == '' |
| 78 | + with: |
| 79 | + github-token: ${{secrets.GITHUB_TOKEN}} |
| 80 | + script: | |
| 81 | + const head_name = "fix-text-${{github.event.pull_request.head.ref}}" |
| 82 | + const common_params = { |
| 83 | + owner: context.repo.owner, |
| 84 | + repo: context.repo.repo |
| 85 | + } |
| 86 | + const pulls_list_params = { |
| 87 | + head: "dev-hato:" + head_name, |
| 88 | + base: "${{github.event.pull_request.head.ref}}", |
| 89 | + state: "open", |
| 90 | + ...common_params |
| 91 | + } |
| 92 | + console.log("call pulls.list:") |
| 93 | + console.log(pulls_list_params) |
| 94 | + github.pulls.list(pulls_list_params).then(res => { |
| 95 | + for(const data of res.data){ |
| 96 | + const pulls_update_params = { |
| 97 | + pull_number: data.number, |
| 98 | + state: "closed", |
| 99 | + ...common_params |
| 100 | + } |
| 101 | + console.log("call pulls.update:") |
| 102 | + console.log(pulls_update_params) |
| 103 | + github.pulls.update(pulls_update_params).then(res2 => { |
| 104 | + const git_deleteRef_params = { |
| 105 | + ref: "heads/" + head_name, |
| 106 | + ...common_params |
| 107 | + } |
| 108 | + console.log("call git.deleteRef:") |
| 109 | + console.log(git_deleteRef_params) |
| 110 | + github.git.deleteRef(git_deleteRef_params) |
| 111 | + }) |
| 112 | + } |
| 113 | + }) |
| 114 | + - name: Exit |
| 115 | + if: steps.show_diff.outputs.diff != '' |
| 116 | + run: exit 1 |
0 commit comments