|
| 1 | +# This workflow is provided via the organization template repository |
| 2 | +# |
| 3 | +# https://github.com/nextcloud/.github |
| 4 | +# https://docs.github.com/en/actions/learn-github-actions/sharing-workflows-with-your-organization |
| 5 | +# |
| 6 | +# SPDX-FileCopyrightText: 2021-2024 Nextcloud GmbH and Nextcloud contributors |
| 7 | +# SPDX-License-Identifier: MIT |
| 8 | + |
| 9 | +name: Compile Command |
| 10 | +on: |
| 11 | + issue_comment: |
| 12 | + types: [created] |
| 13 | + |
| 14 | +permissions: |
| 15 | + contents: read |
| 16 | + |
| 17 | +jobs: |
| 18 | + init: |
| 19 | + runs-on: ubuntu-latest-low |
| 20 | + |
| 21 | + # On pull requests and if the comment starts with `/compile` |
| 22 | + if: github.event.issue.pull_request != '' && startsWith(github.event.comment.body, '/compile') |
| 23 | + |
| 24 | + outputs: |
| 25 | + git_path: ${{ steps.git-path.outputs.path }} |
| 26 | + arg1: ${{ steps.command.outputs.arg1 }} |
| 27 | + arg2: ${{ steps.command.outputs.arg2 }} |
| 28 | + head_ref: ${{ steps.comment-branch.outputs.head_ref }} |
| 29 | + base_ref: ${{ steps.comment-branch.outputs.base_ref }} |
| 30 | + |
| 31 | + steps: |
| 32 | + - name: Get repository from pull request comment |
| 33 | + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 34 | + id: get-repository |
| 35 | + with: |
| 36 | + github-token: ${{secrets.GITHUB_TOKEN}} |
| 37 | + script: | |
| 38 | + const pull = await github.rest.pulls.get({ |
| 39 | + owner: context.repo.owner, |
| 40 | + repo: context.repo.repo, |
| 41 | + pull_number: context.issue.number |
| 42 | + }); |
| 43 | +
|
| 44 | + const repositoryName = pull.data.head?.repo?.full_name |
| 45 | + console.log(repositoryName) |
| 46 | + return repositoryName |
| 47 | +
|
| 48 | + - name: Disabled on forks |
| 49 | + if: ${{ fromJSON(steps.get-repository.outputs.result) != github.repository }} |
| 50 | + run: | |
| 51 | + echo 'Can not execute /compile on forks' |
| 52 | + exit 1 |
| 53 | +
|
| 54 | + - name: Check actor permission |
| 55 | + uses: skjnldsv/check-actor-permission@69e92a3c4711150929bca9fcf34448c5bf5526e7 # v3.0 |
| 56 | + with: |
| 57 | + require: write |
| 58 | + |
| 59 | + - name: Add reaction on start |
| 60 | + uses: peter-evans/create-or-update-comment@e8674b075228eee787fea43ef493e45ece1004c9 # v5.0.0 |
| 61 | + with: |
| 62 | + token: ${{ secrets.COMMAND_BOT_PAT }} |
| 63 | + repository: ${{ github.event.repository.full_name }} |
| 64 | + comment-id: ${{ github.event.comment.id }} |
| 65 | + reactions: '+1' |
| 66 | + |
| 67 | + - name: Parse command |
| 68 | + uses: skjnldsv/parse-command-comment@5c955203c52424151e6d0e58fb9de8a9f6a605a1 # v3.1 |
| 69 | + id: command |
| 70 | + |
| 71 | + # Init path depending on which command is run |
| 72 | + - name: Init path |
| 73 | + id: git-path |
| 74 | + run: | |
| 75 | + if ${{ startsWith(steps.command.outputs.arg1, '/') }}; then |
| 76 | + echo "path=${{steps.command.outputs.arg1}}" >> $GITHUB_OUTPUT |
| 77 | + else |
| 78 | + echo "path=${{steps.command.outputs.arg2}}" >> $GITHUB_OUTPUT |
| 79 | + fi |
| 80 | +
|
| 81 | + - name: Init branch |
| 82 | + uses: xt0rted/pull-request-comment-branch@e8b8daa837e8ea7331c0003c9c316a64c6d8b0b1 # v3.0.0 |
| 83 | + id: comment-branch |
| 84 | + |
| 85 | + - name: Add reaction on failure |
| 86 | + uses: peter-evans/create-or-update-comment@e8674b075228eee787fea43ef493e45ece1004c9 # v5.0.0 |
| 87 | + if: failure() |
| 88 | + with: |
| 89 | + token: ${{ secrets.COMMAND_BOT_PAT }} |
| 90 | + repository: ${{ github.event.repository.full_name }} |
| 91 | + comment-id: ${{ github.event.comment.id }} |
| 92 | + reactions: '-1' |
| 93 | + |
| 94 | + process: |
| 95 | + runs-on: ubuntu-latest |
| 96 | + needs: init |
| 97 | + |
| 98 | + steps: |
| 99 | + - name: Restore cached git repository |
| 100 | + uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5 |
| 101 | + with: |
| 102 | + path: .git |
| 103 | + key: git-repo |
| 104 | + |
| 105 | + - name: Checkout ${{ needs.init.outputs.head_ref }} |
| 106 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 107 | + with: |
| 108 | + persist-credentials: false |
| 109 | + token: ${{ secrets.COMMAND_BOT_PAT }} |
| 110 | + fetch-depth: 0 |
| 111 | + ref: ${{ needs.init.outputs.head_ref }} |
| 112 | + |
| 113 | + - name: Setup git |
| 114 | + run: | |
| 115 | + git config --local user.email '[email protected]' |
| 116 | + git config --local user.name 'nextcloud-command' |
| 117 | +
|
| 118 | + - name: Read package.json node and npm engines version |
| 119 | + uses: skjnldsv/read-package-engines-version-actions@06d6baf7d8f41934ab630e97d9e6c0bc9c9ac5e4 # v3 |
| 120 | + id: package-engines-versions |
| 121 | + with: |
| 122 | + fallbackNode: '^24' |
| 123 | + fallbackNpm: '^11.3' |
| 124 | + |
| 125 | + - name: Set up node ${{ steps.package-engines-versions.outputs.nodeVersion }} |
| 126 | + uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0 |
| 127 | + with: |
| 128 | + node-version: ${{ steps.package-engines-versions.outputs.nodeVersion }} |
| 129 | + cache: npm |
| 130 | + |
| 131 | + - name: Set up npm ${{ steps.package-engines-versions.outputs.npmVersion }} |
| 132 | + run: npm i -g 'npm@${{ steps.package-engines-versions.outputs.npmVersion }}' |
| 133 | + |
| 134 | + - name: Rebase to ${{ needs.init.outputs.base_ref }} |
| 135 | + if: ${{ contains(needs.init.outputs.arg1, 'rebase') }} |
| 136 | + env: |
| 137 | + BASE_REF: ${{ needs.init.outputs.base_ref }} |
| 138 | + run: | |
| 139 | + git fetch origin "${BASE_REF}:${BASE_REF}" |
| 140 | +
|
| 141 | + # Start the rebase |
| 142 | + git rebase "origin/${BASE_REF}" || { |
| 143 | + # Handle rebase conflicts in a loop |
| 144 | + while [ -d .git/rebase-merge ] || [ -d .git/rebase-apply ]; do |
| 145 | + echo "Handling rebase conflict..." |
| 146 | +
|
| 147 | + # Remove and checkout /dist and /js folders from the base branch |
| 148 | + if [ -d "dist" ]; then |
| 149 | + rm -rf dist |
| 150 | + git checkout "origin/${BASE_REF}" -- dist/ 2>/dev/null || echo "No dist folder in base branch" |
| 151 | + fi |
| 152 | + if [ -d "js" ]; then |
| 153 | + rm -rf js |
| 154 | + git checkout "origin/${BASE_REF}" -- js/ 2>/dev/null || echo "No js folder in base branch" |
| 155 | + fi |
| 156 | +
|
| 157 | + # Stage all changes |
| 158 | + git add . |
| 159 | +
|
| 160 | + # Check if there are any changes after resolving conflicts |
| 161 | + if git diff --cached --quiet; then |
| 162 | + echo "No changes after conflict resolution, skipping commit" |
| 163 | + git rebase --skip |
| 164 | + else |
| 165 | + echo "Changes found, continuing rebase without editing commit message" |
| 166 | + git -c core.editor=true rebase --continue |
| 167 | + fi |
| 168 | +
|
| 169 | + # Break if rebase is complete |
| 170 | + if [ ! -d .git/rebase-merge ] && [ ! -d .git/rebase-apply ]; then |
| 171 | + break |
| 172 | + fi |
| 173 | + done |
| 174 | + } |
| 175 | +
|
| 176 | + - name: Install dependencies & build |
| 177 | + env: |
| 178 | + CYPRESS_INSTALL_BINARY: 0 |
| 179 | + PUPPETEER_SKIP_DOWNLOAD: true |
| 180 | + run: | |
| 181 | + npm ci |
| 182 | + npm run build --if-present |
| 183 | +
|
| 184 | + - name: Commit default |
| 185 | + if: ${{ !contains(needs.init.outputs.arg1, 'fixup') && !contains(needs.init.outputs.arg1, 'amend') }} |
| 186 | + env: |
| 187 | + GIT_PATH: ${{ needs.init.outputs.git_path }} |
| 188 | + run: | |
| 189 | + git add "${GITHUB_WORKSPACE}${GIT_PATH}" |
| 190 | + git commit --signoff -m 'chore(assets): Recompile assets' |
| 191 | +
|
| 192 | + - name: Commit fixup |
| 193 | + if: ${{ contains(needs.init.outputs.arg1, 'fixup') }} |
| 194 | + env: |
| 195 | + GIT_PATH: ${{ needs.init.outputs.git_path }} |
| 196 | + run: | |
| 197 | + git add "${GITHUB_WORKSPACE}${GIT_PATH}" |
| 198 | + git commit --fixup=HEAD --signoff |
| 199 | +
|
| 200 | + - name: Commit amend |
| 201 | + if: ${{ contains(needs.init.outputs.arg1, 'amend') }} |
| 202 | + env: |
| 203 | + GIT_PATH: ${{ needs.init.outputs.git_path }} |
| 204 | + run: | |
| 205 | + git add "${GITHUB_WORKSPACE}${GIT_PATH}" |
| 206 | + git commit --amend --no-edit --signoff |
| 207 | + # Remove any [skip ci] from the amended commit |
| 208 | + git commit --amend -m "$(git log -1 --format='%B' | sed '/\[skip ci\]/d')" |
| 209 | +
|
| 210 | + - name: Push normally |
| 211 | + if: ${{ !contains(needs.init.outputs.arg1, 'rebase') && !contains(needs.init.outputs.arg1, 'amend') }} |
| 212 | + env: |
| 213 | + HEAD_REF: ${{ needs.init.outputs.head_ref }} |
| 214 | + BOT_TOKEN: ${{ secrets.COMMAND_BOT_PAT }} # zizmor: ignore[secrets-outside-env] |
| 215 | + run: | |
| 216 | + git remote set-url origin "https://x-access-token:${BOT_TOKEN}@github.com/${{ github.repository }}.git" |
| 217 | + git push origin "$HEAD_REF" |
| 218 | +
|
| 219 | + - name: Force push |
| 220 | + if: ${{ contains(needs.init.outputs.arg1, 'rebase') || contains(needs.init.outputs.arg1, 'amend') }} |
| 221 | + env: |
| 222 | + HEAD_REF: ${{ needs.init.outputs.head_ref }} |
| 223 | + BOT_TOKEN: ${{ secrets.COMMAND_BOT_PAT }} # zizmor: ignore[secrets-outside-env] |
| 224 | + run: | |
| 225 | + git remote set-url origin "https://x-access-token:${BOT_TOKEN}@github.com/${{ github.repository }}.git" |
| 226 | + git push --force-with-lease origin "$HEAD_REF" |
| 227 | +
|
| 228 | + - name: Add reaction on failure |
| 229 | + uses: peter-evans/create-or-update-comment@e8674b075228eee787fea43ef493e45ece1004c9 # v5.0.0 |
| 230 | + if: failure() |
| 231 | + with: |
| 232 | + token: ${{ secrets.COMMAND_BOT_PAT }} |
| 233 | + repository: ${{ github.event.repository.full_name }} |
| 234 | + comment-id: ${{ github.event.comment.id }} |
| 235 | + reactions: '-1' |
0 commit comments