|
| 1 | +--- |
| 2 | +name: pr-format |
| 3 | + |
| 4 | +# pull_requestで何かあった時に起動する |
| 5 | +on: |
| 6 | + pull_request: |
| 7 | + |
| 8 | +jobs: |
| 9 | + # PRが来たらformatをかけてみて、差分があればPRを作って、エラーで落ちるjob |
| 10 | + pr-format: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + |
| 13 | + steps: |
| 14 | + - uses: actions/checkout@v2 |
| 15 | + with: |
| 16 | + # ここでsubmodule持ってくるとdetached headにcommitして死ぬ |
| 17 | + # submodule: 'recursive' |
| 18 | + fetch-depth: 0 |
| 19 | + ref: ${{ github.event.pull_request.head.sha }} |
| 20 | + - run: echo "PYTHON_VERSION=$(cat .python-version)" >> "${GITHUB_ENV}" |
| 21 | + - name: Set up Python ${{ env.PYTHON_VERSION }} |
| 22 | + uses: actions/setup-python@v2.3.2 |
| 23 | + with: |
| 24 | + python-version: ${{env.PYTHON_VERSION }} |
| 25 | + cache: pipenv |
| 26 | + - name: Install pipenv |
| 27 | + id: install_pipenv |
| 28 | + continue-on-error: true |
| 29 | + run: | |
| 30 | + file_name=Dockerfile |
| 31 | + package_name=pipenv |
| 32 | +
|
| 33 | + if [ -f ${file_name} ] |
| 34 | + then |
| 35 | + PATTERN="${package_name}[^ ]+" |
| 36 | + package_name_with_version=$(grep -oE "${PATTERN}" ${file_name}) |
| 37 | + else |
| 38 | + package_name_with_version=${package_name} |
| 39 | + fi |
| 40 | +
|
| 41 | + pip install ${package_name_with_version} |
| 42 | +
|
| 43 | + if [ -f ${file_name} ] |
| 44 | + then |
| 45 | + new_version="$(pip list --outdated | grep pipenv || true)" |
| 46 | + new_version="$(echo -e "${new_version}" | awk '{print $3}')" |
| 47 | + if [ -n "${new_version}" ] |
| 48 | + then |
| 49 | + PATTERN_BEFORE="${package_name}[^ ]+" |
| 50 | + PATTERN_AFTER="${package_name}==${new_version}" |
| 51 | + sed -i -E "s/${PATTERN_BEFORE}/${PATTERN_AFTER}/g" ${file_name} |
| 52 | + pip install "${package_name}==${new_version}" |
| 53 | + exit 1 |
| 54 | + fi |
| 55 | + fi |
| 56 | + - name: pipenv version |
| 57 | + run: pipenv --version |
| 58 | + - name: Install dependencies |
| 59 | + run: | |
| 60 | + pipenv install --dev |
| 61 | + # autopep8でformatする |
| 62 | + # --exit-codeをつけることで、autopep8内でエラーが起きれば1、差分があれば2のエラーステータスコードが返ってくる。正常時は0が返る |
| 63 | + - name: Format files |
| 64 | + id: format |
| 65 | + run: | |
| 66 | + pipenv run autopep8 --exit-code --in-place --recursive . |
| 67 | + continue-on-error: true |
| 68 | + # 差分があったときは差分を出力する |
| 69 | + - name: Show diff |
| 70 | + if: ${{ steps.install_pipenv.outcome == 'failure' |
| 71 | + || steps.format.outcome == 'failure' }} |
| 72 | + run: | |
| 73 | + git diff |
| 74 | + - run: | |
| 75 | + REPO_NAME="${{ github.event.pull_request.head.repo.full_name }}" |
| 76 | + echo "REPO_NAME=${REPO_NAME}" >> "${GITHUB_ENV}" |
| 77 | + # 差分があったときは、コミットを作りpushする |
| 78 | + - name: Push |
| 79 | + env: |
| 80 | + HEAD_REF: ${{github.event.pull_request.head.ref}} |
| 81 | + if: ${{ env.REPO_NAME == github.repository |
| 82 | + && (steps.install_pipenv.outcome == 'failure' |
| 83 | + || steps.format.outcome == 'failure') }} |
| 84 | + run: | |
| 85 | + git config user.name "github-actions[bot]" |
| 86 | + EMAIL="41898282+github-actions[bot]@users.noreply.github.com" |
| 87 | + git config user.email "${EMAIL}" |
| 88 | + git add -u |
| 89 | + git commit -m "鳩は唐揚げ!(自動で直してあげたよ!)" |
| 90 | + REPO_URL="https://" |
| 91 | + REPO_URL+="${{github.actor}}:${{secrets.GITHUB_TOKEN}}@github.com/" |
| 92 | + REPO_URL+="${{github.repository}}.git" |
| 93 | + GITHUB_HEAD="HEAD:refs/heads/fix-format-${HEAD_REF}" |
| 94 | + git push -f "${REPO_URL}" "${GITHUB_HEAD}" |
| 95 | + - name: Get PullRequests |
| 96 | + uses: actions/github-script@v6 |
| 97 | + env: |
| 98 | + HEAD_REF: ${{github.event.pull_request.head.ref}} |
| 99 | + if: ${{ env.REPO_NAME == github.repository |
| 100 | + && (steps.install_pipenv.outcome == 'failure' |
| 101 | + || steps.format.outcome == 'failure') }} |
| 102 | + id: get_pull_requests |
| 103 | + with: |
| 104 | + github-token: ${{secrets.GITHUB_TOKEN}} |
| 105 | + script: | |
| 106 | + const HEAD_REF = process.env["HEAD_REF"] |
| 107 | + const pulls_list_params = { |
| 108 | + owner: context.repo.owner, |
| 109 | + repo: context.repo.repo, |
| 110 | + head: "dev-hato:fix-format-" + HEAD_REF, |
| 111 | + base: HEAD_REF, |
| 112 | + state: "open" |
| 113 | + } |
| 114 | + console.log("call pulls.list:", pulls_list_params) |
| 115 | + const pulls = await github.paginate(github.rest.pulls.list, |
| 116 | + pulls_list_params) |
| 117 | + return pulls.length |
| 118 | + # pushしたブランチでPRを作る |
| 119 | + - name: Create PullRequest |
| 120 | + uses: actions/github-script@v6 |
| 121 | + env: |
| 122 | + HEAD_REF: ${{github.event.pull_request.head.ref}} |
| 123 | + if: ${{ env.REPO_NAME == github.repository |
| 124 | + && (steps.install_pipenv.outcome == 'failure' |
| 125 | + || steps.format.outcome == 'failure') |
| 126 | + && steps.get_pull_requests.outputs.result == 0 }} |
| 127 | + id: create_pull_request |
| 128 | + with: |
| 129 | + github-token: ${{secrets.GITHUB_TOKEN}} |
| 130 | + script: | |
| 131 | + const HEAD_REF = process.env["HEAD_REF"] |
| 132 | + const number = "#${{github.event.pull_request.number}}" |
| 133 | + let title = "formatが間違ってたので直してあげたよ!PRをマージしてね! " |
| 134 | + title += number |
| 135 | + const pulls_create_params = { |
| 136 | + owner: context.repo.owner, |
| 137 | + repo: context.repo.repo, |
| 138 | + head: "dev-hato:fix-format-" + HEAD_REF, |
| 139 | + base: HEAD_REF, |
| 140 | + title, |
| 141 | + body: "鳩の唐揚げおいしい!😋😋😋 " + number |
| 142 | + } |
| 143 | + console.log("call pulls.create:", pulls_create_params) |
| 144 | + const create_pull_res = await github.rest.pulls.create( |
| 145 | + pulls_create_params |
| 146 | + ) |
| 147 | + return create_pull_res.data.number |
| 148 | + - name: Assign a user |
| 149 | + uses: actions/github-script@v6 |
| 150 | + if: ${{ env.REPO_NAME == github.repository |
| 151 | + && (steps.install_pipenv.outcome == 'failure' |
| 152 | + || steps.format.outcome == 'failure') |
| 153 | + && steps.get_pull_requests.outputs.result == 0 |
| 154 | + && github.event.pull_request.user.login != 'dependabot[bot]' }} |
| 155 | + with: |
| 156 | + github-token: ${{secrets.GITHUB_TOKEN}} |
| 157 | + script: | |
| 158 | + const issues_add_assignees_params = { |
| 159 | + owner: context.repo.owner, |
| 160 | + repo: context.repo.repo, |
| 161 | + issue_number: ${{steps.create_pull_request.outputs.result}}, |
| 162 | + assignees: ["${{github.event.pull_request.user.login}}"] |
| 163 | + } |
| 164 | + console.log("call issues.addAssignees:") |
| 165 | + console.log(issues_add_assignees_params) |
| 166 | + await github.rest.issues.addAssignees(issues_add_assignees_params) |
| 167 | + # 既にformat修正のPRがある状態で、手動でformatを修正した場合、format修正のPRを閉じる |
| 168 | + - name: Close PullRequest |
| 169 | + uses: actions/github-script@v6 |
| 170 | + env: |
| 171 | + HEAD_REF: ${{github.event.pull_request.head.ref}} |
| 172 | + if: ${{ env.REPO_NAME == github.repository |
| 173 | + && (steps.install_pipenv.outcome != 'failure' |
| 174 | + && steps.format.outcome != 'failure') }} |
| 175 | + with: |
| 176 | + github-token: ${{secrets.GITHUB_TOKEN}} |
| 177 | + script: | |
| 178 | + const HEAD_REF = process.env["HEAD_REF"] |
| 179 | + const head_name = "fix-format-" + HEAD_REF |
| 180 | + const common_params = { |
| 181 | + owner: context.repo.owner, |
| 182 | + repo: context.repo.repo |
| 183 | + } |
| 184 | + const pulls_list_params = { |
| 185 | + head: "dev-hato:" + head_name, |
| 186 | + base: HEAD_REF, |
| 187 | + state: "open", |
| 188 | + ...common_params |
| 189 | + } |
| 190 | + console.log("call pulls.list:", pulls_list_params) |
| 191 | + const pulls = await github.paginate(github.rest.pulls.list, |
| 192 | + pulls_list_params) |
| 193 | +
|
| 194 | + for(const data of pulls) { |
| 195 | + const pulls_update_params = { |
| 196 | + pull_number: data.number, |
| 197 | + state: "closed", |
| 198 | + ...common_params |
| 199 | + } |
| 200 | + console.log("call pulls.update:", pulls_update_params) |
| 201 | + await github.rest.pulls.update(pulls_update_params) |
| 202 | + const git_deleteRef_params = { |
| 203 | + ref: "heads/" + head_name, |
| 204 | + ...common_params |
| 205 | + } |
| 206 | + console.log("call git.deleteRef:", git_deleteRef_params) |
| 207 | + await github.rest.git.deleteRef(git_deleteRef_params) |
| 208 | + } |
| 209 | + - name: Exit |
| 210 | + if: ${{ steps.install_pipenv.outcome == 'failure' |
| 211 | + || steps.format.outcome == 'failure' }} |
| 212 | + run: exit 1 |
0 commit comments