|
| 1 | +name: pr-check-pipfile |
| 2 | + |
| 3 | +# PipfileかPipfile-lock-*に差分があるPRに反応して起動する |
| 4 | +on: |
| 5 | + pull_request: |
| 6 | + paths: |
| 7 | + - "Pipfile*" |
| 8 | + |
| 9 | +env: |
| 10 | + WORKON_HOME: /tmp/.venv |
| 11 | + |
| 12 | +jobs: |
| 13 | + # PipfileかPipfile-lock-*に差分があれば、Pipfileからlockファイルを作り出す |
| 14 | + pr-check-pipfile: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + strategy: |
| 17 | + matrix: |
| 18 | + python-version: [3.8, 3.7] |
| 19 | + fail-fast: false |
| 20 | + |
| 21 | + steps: |
| 22 | + - uses: actions/checkout@v2 |
| 23 | + with: |
| 24 | + # ここでsubmodule持ってくるとdetached headにcommitして死ぬ |
| 25 | + # submodule: 'recursive' |
| 26 | + fetch-depth: 0 |
| 27 | + - name: Set up Python ${{ matrix.python-version }} |
| 28 | + uses: actions/setup-python@v2.1.4 |
| 29 | + with: |
| 30 | + python-version: ${{ matrix.python-version }} |
| 31 | + - name: Install pipenv |
| 32 | + run: curl https://raw.githubusercontent.com/pypa/pipenv/master/get-pipenv.py | python |
| 33 | + - name: pipenv lock |
| 34 | + run: pipenv lock |
| 35 | + - name: pipfile mv |
| 36 | + run: mv Pipfile.lock Pipfile.lock-${{ matrix.python-version }} |
| 37 | + # 差分があったときは差分を出力する |
| 38 | + - name: Show diff |
| 39 | + id: diff |
| 40 | + run: | |
| 41 | + result=`git diff` |
| 42 | + echo "::set-output name=result::$result" |
| 43 | + # 差分があったときは、コミットを作りpushする |
| 44 | + - name: Push |
| 45 | + if: github.event.pull_request.head.repo.full_name == github.repository && steps.diff.outputs.result != '' |
| 46 | + env: |
| 47 | + PYTHON_VERSION: ${{ matrix.python-version }} |
| 48 | + run: | |
| 49 | + git config user.name "hatohakaraage" |
| 50 | + git config user.email "hatohakaraage@example.com" |
| 51 | + git add -u |
| 52 | + git commit -m "鳩は唐揚げ!(自動で直してあげたよ!)" |
| 53 | + git push -f https://${{github.actor}}:${{secrets.GITHUB_TOKEN}}@github.com/${{github.repository}}.git HEAD:refs/heads/pipfile-${PYTHON_VERSION//\./_}-${{github.event.pull_request.head.ref}} |
| 54 | + # pushしたブランチでPRを作る |
| 55 | + - name: Create PullRequest |
| 56 | + uses: actions/github-script@v3 |
| 57 | + if: github.event.pull_request.head.repo.full_name == github.repository && steps.diff.outputs.result != '' |
| 58 | + env: |
| 59 | + PYTHON_VERSION: ${{ matrix.python-version }} |
| 60 | + with: |
| 61 | + github-token: ${{secrets.GITHUB_TOKEN}} |
| 62 | + script: | |
| 63 | + const common_params = { |
| 64 | + owner: context.repo.owner, |
| 65 | + repo: context.repo.repo |
| 66 | + } |
| 67 | + const pull_params = { |
| 68 | + head: "${{github.event.pull_request.head.repo.owner.login}}:pipfile-" + process.env.PYTHON_VERSION.replace('.', '_') + "-${{github.event.pull_request.head.ref}}", |
| 69 | + base: "${{github.event.pull_request.head.ref}}", |
| 70 | + ...common_params |
| 71 | + } |
| 72 | + const pulls_list_params = { |
| 73 | + state: "open", |
| 74 | + ...pull_params |
| 75 | + } |
| 76 | + console.log("call pulls.list:") |
| 77 | + console.log(pulls_list_params) |
| 78 | + github.pulls.list(pulls_list_params).then(list_res => { |
| 79 | + if (list_res.data.length === 0) { |
| 80 | + const pulls_create_params = { |
| 81 | + title: "pipfile (" + process.env.PYTHON_VERSION + ") が更新されたので直してあげたよ!PRをマージしてね! #${{github.event.pull_request.number}}", |
| 82 | + body: "鳩の唐揚げおいしい!😋😋😋 #${{github.event.pull_request.number}}", |
| 83 | + ...pull_params |
| 84 | + } |
| 85 | + console.log("call pulls.create:") |
| 86 | + console.log(pulls_create_params) |
| 87 | + github.pulls.create(pulls_create_params).then(create_res => { |
| 88 | + const issues_add_assignees_params = { |
| 89 | + issue_number: create_res.data.number, |
| 90 | + assignees: ["${{github.event.pull_request.user.login}}"], |
| 91 | + ...common_params |
| 92 | + } |
| 93 | + console.log("call issues.addAssignees:") |
| 94 | + console.log(issues_add_assignees_params) |
| 95 | + github.issues.addAssignees(issues_add_assignees_params) |
| 96 | + }) |
| 97 | + } |
| 98 | + }) |
| 99 | + # 既にPipfile修正のPRがある状態で、手動でPipfileを修正した場合、Pipfile修正のPRを閉じる |
| 100 | + - name: Close PullRequest |
| 101 | + uses: actions/github-script@v3 |
| 102 | + if: github.event.pull_request.head.repo.full_name == github.repository && steps.diff.outputs.result == '' |
| 103 | + env: |
| 104 | + PYTHON_VERSION: ${{ matrix.python-version }} |
| 105 | + with: |
| 106 | + github-token: ${{secrets.GITHUB_TOKEN}} |
| 107 | + script: | |
| 108 | + const head_name = "pipfile-" + process.env.PYTHON_VERSION.replace('.', '_') + "-${{github.event.pull_request.head.ref}}" |
| 109 | + const common_params = { |
| 110 | + owner: context.repo.owner, |
| 111 | + repo: context.repo.repo |
| 112 | + } |
| 113 | + const pulls_list_params = { |
| 114 | + head: "${{github.event.pull_request.head.repo.owner.login}}:" + head_name, |
| 115 | + base: "${{github.event.pull_request.head.ref}}", |
| 116 | + state: "open", |
| 117 | + ...common_params |
| 118 | + } |
| 119 | + console.log("call pulls.list:") |
| 120 | + console.log(pulls_list_params) |
| 121 | + github.pulls.list(pulls_list_params).then(res => { |
| 122 | + for(const data of res.data){ |
| 123 | + const pulls_update_params = { |
| 124 | + pull_number: data.number, |
| 125 | + state: "closed", |
| 126 | + ...common_params |
| 127 | + } |
| 128 | + console.log("call pulls.update:") |
| 129 | + console.log(pulls_update_params) |
| 130 | + github.pulls.update(pulls_update_params).then(res2 => { |
| 131 | + const git_deleteRef_params = { |
| 132 | + ref: "heads/" + head_name, |
| 133 | + ...common_params |
| 134 | + } |
| 135 | + console.log("call git.deleteRef:") |
| 136 | + console.log(git_deleteRef_params) |
| 137 | + github.git.deleteRef(git_deleteRef_params) |
| 138 | + }) |
| 139 | + } |
| 140 | + }) |
| 141 | + - name: Exit |
| 142 | + if: steps.diff.outputs.result != '' |
| 143 | + run: exit 1 |
0 commit comments