|
| 1 | +name: e2e on a real PR |
| 2 | + |
| 3 | +# Tests a cpp-linter-action branch, tag or commit on a real pull request: |
| 4 | +# |
| 5 | +# 1. "Run workflow" (workflow_dispatch) creates a branch `e2e/run-<id>` with known lint problems |
| 6 | +# (.github/e2e/demo.cpp.txt) and opens a PR for it. |
| 7 | +# 2. The PR runs the action under test on 3 operating systems. |
| 8 | +# 3. The `verify` job checks what the action left on the PR (thread comment, review, annotations, |
| 9 | +# outputs), posts the result as a PR comment, and closes the PR if everything passed. |
| 10 | +# |
| 11 | +# A PR opened with the default GITHUB_TOKEN does not start workflows. Either add a repository |
| 12 | +# secret named E2E_TOKEN (a token that may push branches and open PRs here), or close and reopen |
| 13 | +# the PR that step 1 created. |
| 14 | + |
| 15 | +on: |
| 16 | + workflow_dispatch: |
| 17 | + inputs: |
| 18 | + action-ref: |
| 19 | + description: Branch, tag or commit of cpp-linter-action to test |
| 20 | + default: main |
| 21 | + required: true |
| 22 | + clang-version: |
| 23 | + description: Version of the clang tools to use |
| 24 | + default: '21' |
| 25 | + required: true |
| 26 | + pull_request: |
| 27 | + branches: [master] |
| 28 | + paths: [.github/e2e/target.env] |
| 29 | + |
| 30 | +permissions: {} |
| 31 | + |
| 32 | +concurrency: |
| 33 | + group: e2e-pr-${{ github.event.pull_request.number || github.run_id }} |
| 34 | + cancel-in-progress: true |
| 35 | + |
| 36 | +jobs: |
| 37 | + open-pr: |
| 38 | + if: github.event_name == 'workflow_dispatch' |
| 39 | + runs-on: ubuntu-latest |
| 40 | + permissions: |
| 41 | + contents: write |
| 42 | + pull-requests: write |
| 43 | + steps: |
| 44 | + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 |
| 45 | + with: |
| 46 | + # the credentials are kept because the next step pushes a branch |
| 47 | + token: ${{ secrets.E2E_TOKEN || github.token }} |
| 48 | + |
| 49 | + - name: Create the test branch and open a PR |
| 50 | + env: |
| 51 | + GH_TOKEN: ${{ secrets.E2E_TOKEN || github.token }} |
| 52 | + HAS_E2E_TOKEN: ${{ secrets.E2E_TOKEN != '' }} |
| 53 | + ACTION_REF: ${{ inputs.action-ref }} |
| 54 | + CLANG_VERSION: ${{ inputs.clang-version }} |
| 55 | + run: | |
| 56 | + for value in "$ACTION_REF" "$CLANG_VERSION"; do |
| 57 | + if [[ ! "$value" =~ ^[A-Za-z0-9._/-]+$ ]]; then |
| 58 | + echo "::error::Unexpected characters in input: $value" |
| 59 | + exit 1 |
| 60 | + fi |
| 61 | + done |
| 62 | + branch="e2e/run-${GITHUB_RUN_ID}" |
| 63 | + git switch -c "$branch" |
| 64 | + cp .github/e2e/demo.cpp.txt src/demo.cpp |
| 65 | + printf 'action-ref=%s\nclang-version=%s\n' "$ACTION_REF" "$CLANG_VERSION" > .github/e2e/target.env |
| 66 | + git config user.name "github-actions[bot]" |
| 67 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 68 | + git add src/demo.cpp .github/e2e/target.env |
| 69 | + git commit -m "test: e2e fixture for cpp-linter-action@${ACTION_REF}" |
| 70 | + git push origin "$branch" |
| 71 | + url=$(gh pr create --base master --head "$branch" \ |
| 72 | + --title "test: e2e of cpp-linter-action@${ACTION_REF} (clang ${CLANG_VERSION})" \ |
| 73 | + --body "Opened by the \`e2e on a real PR\` workflow. Do not merge. It is closed automatically when all checks pass.") |
| 74 | + echo "Opened $url" >> "$GITHUB_STEP_SUMMARY" |
| 75 | + if [[ "$HAS_E2E_TOKEN" != "true" ]]; then |
| 76 | + { |
| 77 | + echo |
| 78 | + echo "There is no \`E2E_TOKEN\` secret, so this PR was opened with the default token" |
| 79 | + echo "and GitHub will not start the test for it. Close and reopen the PR to start it." |
| 80 | + } >> "$GITHUB_STEP_SUMMARY" |
| 81 | + echo "::warning::Close and reopen $url to start the test (no E2E_TOKEN secret)." |
| 82 | + fi |
| 83 | +
|
| 84 | + lint: |
| 85 | + # The `paths` filter above limits this to PRs that change `.github/e2e/target.env`. |
| 86 | + if: >- |
| 87 | + github.event_name == 'pull_request' && |
| 88 | + github.event.pull_request.head.repo.full_name == github.repository |
| 89 | + strategy: |
| 90 | + fail-fast: false |
| 91 | + matrix: |
| 92 | + os: [ubuntu-latest, macos-latest, windows-latest] |
| 93 | + runs-on: ${{ matrix.os }} |
| 94 | + permissions: |
| 95 | + contents: read |
| 96 | + pull-requests: write |
| 97 | + outputs: |
| 98 | + action-ref: ${{ steps.target.outputs.action-ref }} |
| 99 | + action-sha: ${{ steps.action-sha.outputs.sha }} |
| 100 | + clang-version: ${{ steps.target.outputs.clang-version }} |
| 101 | + ubuntu-latest: ${{ steps.result.outputs.ubuntu-latest }} |
| 102 | + macos-latest: ${{ steps.result.outputs.macos-latest }} |
| 103 | + windows-latest: ${{ steps.result.outputs.windows-latest }} |
| 104 | + steps: |
| 105 | + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 |
| 106 | + with: |
| 107 | + persist-credentials: false |
| 108 | + |
| 109 | + - name: Read which action version to test |
| 110 | + id: target |
| 111 | + shell: bash |
| 112 | + run: | |
| 113 | + ref=$(sed -n 's/^action-ref=//p' .github/e2e/target.env | head -n 1) |
| 114 | + clang=$(sed -n 's/^clang-version=//p' .github/e2e/target.env | head -n 1) |
| 115 | + for value in "$ref" "$clang"; do |
| 116 | + if [[ ! "$value" =~ ^[A-Za-z0-9._/-]+$ ]]; then |
| 117 | + echo "::error::Unexpected value in .github/e2e/target.env: $value" |
| 118 | + exit 1 |
| 119 | + fi |
| 120 | + done |
| 121 | + echo "action-ref=$ref" >> "$GITHUB_OUTPUT" |
| 122 | + echo "clang-version=$clang" >> "$GITHUB_OUTPUT" |
| 123 | +
|
| 124 | + - name: Get the action under test |
| 125 | + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 |
| 126 | + with: |
| 127 | + repository: cpp-linter/cpp-linter-action |
| 128 | + ref: ${{ steps.target.outputs.action-ref }} |
| 129 | + path: .e2e-action |
| 130 | + persist-credentials: false |
| 131 | + |
| 132 | + - name: Record the action's commit |
| 133 | + id: action-sha |
| 134 | + shell: bash |
| 135 | + run: echo "sha=$(git -C .e2e-action rev-parse HEAD)" >> "$GITHUB_OUTPUT" |
| 136 | + |
| 137 | + - name: Generate compilation database |
| 138 | + shell: bash |
| 139 | + run: mkdir build && cmake -Bbuild src |
| 140 | + |
| 141 | + # Only one job posts to the PR, so that the jobs do not overwrite each other's feedback. |
| 142 | + - name: Run the action under test |
| 143 | + id: linter |
| 144 | + uses: ./.e2e-action |
| 145 | + env: |
| 146 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 147 | + with: |
| 148 | + style: file |
| 149 | + lines-changed-only: true |
| 150 | + ignore: build|.github|.e2e-action |
| 151 | + database: build |
| 152 | + verbosity: debug |
| 153 | + version: ${{ steps.target.outputs.clang-version }} |
| 154 | + extra-args: -std=c++14 -Wall |
| 155 | + step-summary: true |
| 156 | + thread-comments: ${{ matrix.os == 'ubuntu-latest' && 'update' || 'false' }} |
| 157 | + file-annotations: ${{ matrix.os == 'ubuntu-latest' }} |
| 158 | + tidy-review: ${{ matrix.os == 'ubuntu-latest' }} |
| 159 | + format-review: ${{ matrix.os == 'ubuntu-latest' }} |
| 160 | + |
| 161 | + - name: Record the outputs |
| 162 | + id: result |
| 163 | + if: always() |
| 164 | + shell: bash |
| 165 | + env: |
| 166 | + OS: ${{ matrix.os }} |
| 167 | + TOTAL: ${{ steps.linter.outputs.checks-failed }} |
| 168 | + TIDY: ${{ steps.linter.outputs.clang-tidy-checks-failed }} |
| 169 | + FORMAT: ${{ steps.linter.outputs.clang-format-checks-failed }} |
| 170 | + run: echo "${OS}=${TOTAL:-none}/${TIDY:-none}/${FORMAT:-none}" >> "$GITHUB_OUTPUT" |
| 171 | + |
| 172 | + verify: |
| 173 | + needs: lint |
| 174 | + if: always() && needs.lint.result != 'skipped' |
| 175 | + runs-on: ubuntu-latest |
| 176 | + permissions: |
| 177 | + contents: write # to delete the test branch when the test passed |
| 178 | + pull-requests: write |
| 179 | + checks: read |
| 180 | + steps: |
| 181 | + - name: Check what the action left on the PR |
| 182 | + env: |
| 183 | + GH_TOKEN: ${{ github.token }} |
| 184 | + PR: ${{ github.event.pull_request.number }} |
| 185 | + HEAD_SHA: ${{ github.event.pull_request.head.sha }} |
| 186 | + LINT_RESULT: ${{ needs.lint.result }} |
| 187 | + ACTION_REF: ${{ needs.lint.outputs.action-ref }} |
| 188 | + ACTION_SHA: ${{ needs.lint.outputs.action-sha }} |
| 189 | + CLANG_VERSION: ${{ needs.lint.outputs.clang-version }} |
| 190 | + RESULT_UBUNTU: ${{ needs.lint.outputs.ubuntu-latest }} |
| 191 | + RESULT_MACOS: ${{ needs.lint.outputs.macos-latest }} |
| 192 | + RESULT_WINDOWS: ${{ needs.lint.outputs.windows-latest }} |
| 193 | + run: | |
| 194 | + marker='<!-- cpp linter action -->' |
| 195 | + repo="$GITHUB_REPOSITORY" |
| 196 | + failed=0 |
| 197 | + rows="" |
| 198 | + check() { # check <name> <actual> <expected> <ok: true|false> |
| 199 | + local mark=":white_check_mark:" |
| 200 | + if [[ "$4" != "true" ]]; then mark=":x:"; failed=1; fi |
| 201 | + rows+="| $1 | $2 | $3 | $mark |"$'\n' |
| 202 | + } |
| 203 | +
|
| 204 | + check "lint jobs" "$LINT_RESULT" "success" "$([[ "$LINT_RESULT" == success ]] && echo true || echo false)" |
| 205 | +
|
| 206 | + for pair in "ubuntu-latest=$RESULT_UBUNTU" "macos-latest=$RESULT_MACOS" "windows-latest=$RESULT_WINDOWS"; do |
| 207 | + os=${pair%%=*} |
| 208 | + IFS=/ read -r total tidy format <<< "${pair#*=}" |
| 209 | + ok=false |
| 210 | + if [[ "$total" =~ ^[0-9]+$ && "$tidy" =~ ^[0-9]+$ && "$format" =~ ^[0-9]+$ ]] && |
| 211 | + (( tidy > 0 && format > 0 && total == tidy + format )); then |
| 212 | + ok=true |
| 213 | + fi |
| 214 | + check "outputs on $os (total/tidy/format)" "${total:-none}/${tidy:-none}/${format:-none}" "tidy > 0, format > 0, total = tidy + format" "$ok" |
| 215 | + done |
| 216 | +
|
| 217 | + comments=$(gh api "repos/$repo/issues/$PR/comments" --paginate | |
| 218 | + jq -s --arg m "$marker" '[.[][] | select(.body | contains($m))] | length') |
| 219 | + check "thread comments" "$comments" "1" "$([[ "$comments" == 1 ]] && echo true || echo false)" |
| 220 | +
|
| 221 | + reviews=$(gh api "repos/$repo/pulls/$PR/reviews" --paginate | |
| 222 | + jq -s --arg m "$marker" '[.[][] | select(.body | contains($m))]') |
| 223 | + review_count=$(jq 'length' <<< "$reviews") |
| 224 | + review_state=$(jq -r '.[-1].state // "none"' <<< "$reviews") |
| 225 | + check "PR reviews" "$review_count (latest: $review_state)" "1 or more (latest: CHANGES_REQUESTED)" \ |
| 226 | + "$([[ "$review_count" -ge 1 && "$review_state" == CHANGES_REQUESTED ]] && echo true || echo false)" |
| 227 | +
|
| 228 | + suggestions=$(gh api "repos/$repo/pulls/$PR/comments" --paginate | |
| 229 | + jq -s --arg m "$marker" '[.[][] | select(.body | contains($m))] | length') |
| 230 | + check "review comments" "$suggestions" "1 or more" "$([[ "$suggestions" -ge 1 ]] && echo true || echo false)" |
| 231 | +
|
| 232 | + run_id=$(gh api "repos/$repo/commits/$HEAD_SHA/check-runs" --paginate | |
| 233 | + jq -s -r '[.[].check_runs[] | select(.name == "lint (ubuntu-latest)")] | sort_by(.started_at) | .[-1].id // empty') |
| 234 | + annotations=0 |
| 235 | + if [[ -n "$run_id" ]]; then |
| 236 | + annotations=$(gh api "repos/$repo/check-runs/$run_id/annotations" --paginate | |
| 237 | + jq -s '[.[][] | select(.path | startswith("src/"))] | length') |
| 238 | + fi |
| 239 | + check "file annotations on src/" "$annotations" "2 or more" "$([[ "$annotations" -ge 2 ]] && echo true || echo false)" |
| 240 | +
|
| 241 | + { |
| 242 | + echo "## e2e result: $([[ $failed == 0 ]] && echo passed || echo failed)" |
| 243 | + echo |
| 244 | + echo "cpp-linter-action \`$ACTION_REF\` (\`${ACTION_SHA:0:7}\`), clang $CLANG_VERSION," |
| 245 | + echo "[workflow run]($GITHUB_SERVER_URL/$repo/actions/runs/$GITHUB_RUN_ID)" |
| 246 | + echo |
| 247 | + echo "| Check | Actual | Expected | |" |
| 248 | + echo "|---|---|---|---|" |
| 249 | + printf '%s' "$rows" |
| 250 | + } > result.md |
| 251 | + cat result.md >> "$GITHUB_STEP_SUMMARY" |
| 252 | + gh pr comment "$PR" --repo "$repo" --body-file result.md |
| 253 | + echo "failed=$failed" >> "$GITHUB_ENV" |
| 254 | +
|
| 255 | + - name: Close the PR when everything passed |
| 256 | + if: env.failed == '0' |
| 257 | + env: |
| 258 | + GH_TOKEN: ${{ github.token }} |
| 259 | + PR: ${{ github.event.pull_request.number }} |
| 260 | + run: gh pr close "$PR" --repo "$GITHUB_REPOSITORY" --delete-branch |
| 261 | + |
| 262 | + - name: Fail when a check did not pass |
| 263 | + if: env.failed != '0' |
| 264 | + run: | |
| 265 | + echo "::error::The e2e test failed. The PR stays open so that it can be inspected." |
| 266 | + exit 1 |
0 commit comments