Skip to content

Commit b653046

Browse files
committed
Updated posting of coverage comments to replace existing comments.
1 parent 6b65fe0 commit b653046

51 files changed

Lines changed: 1513 additions & 240 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.circleci/config.yml

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -410,18 +410,7 @@ jobs:
410410
command: |
411411
[ "${CIRCLE_NODE_TOTAL:-1}" -gt 1 ] && [ "${CIRCLE_NODE_INDEX:-0}" -ne 0 ] && exit 0
412412
[ "${VORTEX_CI_CODE_COVERAGE_PR_COMMENT_SKIP:-0}" = "1" ] && exit 0
413-
[ -z "${CIRCLE_PULL_REQUEST}" ] && exit 0
414-
[ -z "${GITHUB_TOKEN}" ] && exit 0
415-
COVERAGE_CONTENT=$(sed '/./,$!d' /tmp/artifacts/coverage/phpunit/coverage.txt)
416-
PR_NUMBER=$(echo "${CIRCLE_PULL_REQUEST}" | cut -d'/' -f 7)
417-
REPO_SLUG="${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}"
418-
curl -s -X POST \
419-
-H "Authorization: token ${GITHUB_TOKEN}" \
420-
-H "Accept: application/vnd.github.v3+json" \
421-
"https://api.github.com/repos/${REPO_SLUG}/issues/${PR_NUMBER}/comments" \
422-
-d "$(jq -n --arg body "\`\`\`
423-
${COVERAGE_CONTENT}
424-
\`\`\`" '{body: $body}')"
413+
.circleci/post-coverage-comment.sh /tmp/artifacts/coverage/phpunit/coverage.txt
425414
426415
- run:
427416
name: Upload code coverage reports to Codecov

.circleci/post-coverage-comment.sh

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env bash
2+
##
3+
## Post code coverage summary as a PR comment on GitHub.
4+
##
5+
## Minimizes previous coverage comments before posting a new one.
6+
##
7+
## Environment variables:
8+
## CIRCLE_PULL_REQUEST - CircleCI PR URL.
9+
## GITHUB_TOKEN - GitHub token for API access.
10+
## CIRCLE_PROJECT_USERNAME - GitHub org/user.
11+
## CIRCLE_PROJECT_REPONAME - GitHub repo name.
12+
##
13+
## Usage:
14+
## .circleci/post-coverage-comment.sh /path/to/coverage.txt
15+
16+
set -euo pipefail
17+
18+
COVERAGE_FILE="${1:-}"
19+
20+
if [ -z "${COVERAGE_FILE}" ] || [ ! -f "${COVERAGE_FILE}" ]; then
21+
echo "ERROR: Coverage file not found: ${COVERAGE_FILE}" >&2
22+
exit 1
23+
fi
24+
25+
if [ -z "${CIRCLE_PULL_REQUEST:-}" ]; then
26+
echo "Not a pull request. Skipping."
27+
exit 0
28+
fi
29+
30+
if [ -z "${GITHUB_TOKEN:-}" ]; then
31+
echo "GITHUB_TOKEN is not set. Skipping."
32+
exit 0
33+
fi
34+
35+
COVERAGE_CONTENT=$(sed '/./,$!d' "${COVERAGE_FILE}")
36+
PR_NUMBER=$(echo "${CIRCLE_PULL_REQUEST}" | cut -d'/' -f 7)
37+
REPO_SLUG="${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}"
38+
39+
MARKER="<!-- coverage-circleci -->"
40+
41+
BODY="$(jq -n --arg body "**Code coverage (CircleCI)**
42+
\`\`\`
43+
${COVERAGE_CONTENT}
44+
\`\`\`
45+
${MARKER}" '{body: $body}')"
46+
47+
# Minimize previous coverage comments.
48+
EXISTING_IDS=$(curl -s \
49+
-H "Authorization: token ${GITHUB_TOKEN}" \
50+
-H "Accept: application/vnd.github.v3+json" \
51+
"https://api.github.com/repos/${REPO_SLUG}/issues/${PR_NUMBER}/comments?per_page=100" |
52+
jq -r '.[] | select(.body | contains("<!-- coverage-circleci -->")) | .node_id')
53+
54+
for NODE_ID in ${EXISTING_IDS}; do
55+
curl -s -X POST \
56+
-H "Authorization: bearer ${GITHUB_TOKEN}" \
57+
-H "Content-Type: application/json" \
58+
"https://api.github.com/graphql" \
59+
-d "$(jq -n --arg id "${NODE_ID}" '{query: "mutation($id:ID!){minimizeComment(input:{subjectId:$id,classifier:OUTDATED}){minimizedComment{isMinimized}}}", variables: {id: $id}}')"
60+
done
61+
62+
# Post new coverage comment.
63+
curl -s -X POST \
64+
-H "Authorization: token ${GITHUB_TOKEN}" \
65+
-H "Accept: application/vnd.github.v3+json" \
66+
"https://api.github.com/repos/${REPO_SLUG}/issues/${PR_NUMBER}/comments" \
67+
-d "${BODY}"

.circleci/vortex-test-common.yml

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -276,18 +276,7 @@ jobs:
276276
command: |
277277
[ "${CIRCLE_NODE_TOTAL:-1}" -gt 1 ] && [ "${CIRCLE_NODE_INDEX:-0}" -ne 0 ] && exit 0
278278
[ "${VORTEX_CI_CODE_COVERAGE_PR_COMMENT_SKIP:-0}" = "1" ] && exit 0
279-
[ -z "${CIRCLE_PULL_REQUEST}" ] && exit 0
280-
[ -z "${GITHUB_TOKEN}" ] && exit 0
281-
COVERAGE_CONTENT=$(sed '/./,$!d' /tmp/artifacts/coverage/phpunit/coverage.txt)
282-
PR_NUMBER=$(echo "${CIRCLE_PULL_REQUEST}" | cut -d'/' -f 7)
283-
REPO_SLUG="${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}"
284-
curl -s -X POST \
285-
-H "Authorization: token ${GITHUB_TOKEN}" \
286-
-H "Accept: application/vnd.github.v3+json" \
287-
"https://api.github.com/repos/${REPO_SLUG}/issues/${PR_NUMBER}/comments" \
288-
-d "$(jq -n --arg body "\`\`\`
289-
${COVERAGE_CONTENT}
290-
\`\`\`" '{body: $body}')"
279+
.circleci/post-coverage-comment.sh /tmp/artifacts/coverage/phpunit/coverage.txt
291280
292281
- run:
293282
name: Upload code coverage reports to Codecov

.github/workflows/build-test-deploy.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,9 @@ jobs:
442442
if: ${{ github.event_name == 'pull_request' && (matrix.instance == 0 || strategy.job-total == 1) && vars.VORTEX_CI_CODE_COVERAGE_PR_COMMENT_SKIP != '1' }}
443443
uses: marocchino/sticky-pull-request-comment@773744901bac0e8cbb5a0dc842800d45e9b2b405 # v2
444444
with:
445+
header: coverage-gha
445446
message: |
447+
**Code coverage (GitHub Actions)**
446448
```
447449
${{ env.COVERAGE_CONTENT }}
448450
```

.vortex/docs/content/continuous-integration/circleci.mdx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,5 +224,6 @@ environment variable in **Project Settings → Environment Variables**. Default
224224
is `90` (percent).
225225

226226
Coverage reports can be posted as PR comments. This requires a `GITHUB_TOKEN`
227-
environment variable with permission to post comments. To disable PR comments,
228-
set `VORTEX_CI_CODE_COVERAGE_PR_COMMENT_SKIP` to `1`.
227+
environment variable with permission to post comments. Each new report replaces
228+
the previous one — older comments are minimized to keep the PR timeline clean.
229+
To disable PR comments, set `VORTEX_CI_CODE_COVERAGE_PR_COMMENT_SKIP` to `1`.

.vortex/docs/content/continuous-integration/github-actions.mdx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,5 +192,7 @@ Configure the threshold by setting the `VORTEX_CI_CODE_COVERAGE_THRESHOLD`
192192
variable in **Settings → Secrets and variables → Actions → Variables**. Default
193193
is `90` (percent).
194194

195-
Coverage reports are automatically posted as PR comments. To disable this, set
196-
`VORTEX_CI_CODE_COVERAGE_PR_COMMENT_SKIP` to `1`.
195+
Coverage reports are automatically posted as PR comments. Each new report
196+
replaces the previous one — older comments are minimized to keep the PR
197+
timeline clean. To disable this, set `VORTEX_CI_CODE_COVERAGE_PR_COMMENT_SKIP`
198+
to `1`.

.vortex/docs/content/tools/phpunit.mdx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,12 @@ minimum percentage (default: `90`).
153153

154154
### PR comments
155155

156-
Coverage reports are posted as PR comments automatically. Set
157-
`VORTEX_CI_CODE_COVERAGE_PR_COMMENT_SKIP` to `1` to disable.
156+
Coverage reports are posted as PR comments automatically. Each new report
157+
replaces the previous one — older comments are minimized to keep the PR
158+
timeline clean. The comment includes a header indicating the CI source
159+
(GitHub Actions or CircleCI).
160+
161+
Set `VORTEX_CI_CODE_COVERAGE_PR_COMMENT_SKIP` to `1` to disable.
158162

159163
### Ignoring lines from coverage
160164

.vortex/installer/tests/Fixtures/handler_process/_baseline/.github/workflows/build-test-deploy.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,9 @@ jobs:
394394
if: ${{ github.event_name == 'pull_request' && (matrix.instance == 0 || strategy.job-total == 1) && vars.VORTEX_CI_CODE_COVERAGE_PR_COMMENT_SKIP != '1' }}
395395
uses: marocchino/sticky-pull-request-comment@__HASH__ # __VERSION__
396396
with:
397+
header: coverage-gha
397398
message: |
399+
**Code coverage (GitHub Actions)**
398400
```
399401
${{ env.COVERAGE_CONTENT }}
400402
```

.vortex/installer/tests/Fixtures/handler_process/ciprovider_circleci/.circleci/config.yml

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -365,18 +365,7 @@ jobs:
365365
command: |
366366
[ "${CIRCLE_NODE_TOTAL:-1}" -gt 1 ] && [ "${CIRCLE_NODE_INDEX:-0}" -ne 0 ] && exit 0
367367
[ "${VORTEX_CI_CODE_COVERAGE_PR_COMMENT_SKIP:-0}" = "1" ] && exit 0
368-
[ -z "${CIRCLE_PULL_REQUEST}" ] && exit 0
369-
[ -z "${GITHUB_TOKEN}" ] && exit 0
370-
COVERAGE_CONTENT=$(sed '/./,$!d' /tmp/artifacts/coverage/phpunit/coverage.txt)
371-
PR_NUMBER=$(echo "${CIRCLE_PULL_REQUEST}" | cut -d'/' -f 7)
372-
REPO_SLUG="${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}"
373-
curl -s -X POST \
374-
-H "Authorization: token ${GITHUB_TOKEN}" \
375-
-H "Accept: application/vnd.github.v3+json" \
376-
"https://api.github.com/repos/${REPO_SLUG}/issues/${PR_NUMBER}/comments" \
377-
-d "$(jq -n --arg body "\`\`\`
378-
${COVERAGE_CONTENT}
379-
\`\`\`" '{body: $body}')"
368+
.circleci/post-coverage-comment.sh /tmp/artifacts/coverage/phpunit/coverage.txt
380369
381370
- run:
382371
name: Upload code coverage reports to Codecov
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env bash
2+
##
3+
## Post code coverage summary as a PR comment on GitHub.
4+
##
5+
## Minimizes previous coverage comments before posting a new one.
6+
##
7+
## Environment variables:
8+
## CIRCLE_PULL_REQUEST - CircleCI PR URL.
9+
## GITHUB_TOKEN - GitHub token for API access.
10+
## CIRCLE_PROJECT_USERNAME - GitHub org/user.
11+
## CIRCLE_PROJECT_REPONAME - GitHub repo name.
12+
##
13+
## Usage:
14+
## .circleci/post-coverage-comment.sh /path/to/coverage.txt
15+
16+
set -euo pipefail
17+
18+
COVERAGE_FILE="${1:-}"
19+
20+
if [ -z "${COVERAGE_FILE}" ] || [ ! -f "${COVERAGE_FILE}" ]; then
21+
echo "ERROR: Coverage file not found: ${COVERAGE_FILE}" >&2
22+
exit 1
23+
fi
24+
25+
if [ -z "${CIRCLE_PULL_REQUEST:-}" ]; then
26+
echo "Not a pull request. Skipping."
27+
exit 0
28+
fi
29+
30+
if [ -z "${GITHUB_TOKEN:-}" ]; then
31+
echo "GITHUB_TOKEN is not set. Skipping."
32+
exit 0
33+
fi
34+
35+
COVERAGE_CONTENT=$(sed '/./,$!d' "${COVERAGE_FILE}")
36+
PR_NUMBER=$(echo "${CIRCLE_PULL_REQUEST}" | cut -d'/' -f 7)
37+
REPO_SLUG="${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}"
38+
39+
MARKER="<!-- coverage-circleci -->"
40+
41+
BODY="$(jq -n --arg body "**Code coverage (CircleCI)**
42+
\`\`\`
43+
${COVERAGE_CONTENT}
44+
\`\`\`
45+
${MARKER}" '{body: $body}')"
46+
47+
# Minimize previous coverage comments.
48+
EXISTING_IDS=$(curl -s \
49+
-H "Authorization: token ${GITHUB_TOKEN}" \
50+
-H "Accept: application/vnd.github.v3+json" \
51+
"https://api.github.com/repos/${REPO_SLUG}/issues/${PR_NUMBER}/comments?per_page=100" |
52+
jq -r '.[] | select(.body | contains("<!-- coverage-circleci -->")) | .node_id')
53+
54+
for NODE_ID in ${EXISTING_IDS}; do
55+
curl -s -X POST \
56+
-H "Authorization: bearer ${GITHUB_TOKEN}" \
57+
-H "Content-Type: application/json" \
58+
"https://api.github.com/graphql" \
59+
-d "$(jq -n --arg id "${NODE_ID}" '{query: "mutation($id:ID!){minimizeComment(input:{subjectId:$id,classifier:OUTDATED}){minimizedComment{isMinimized}}}", variables: {id: $id}}')"
60+
done
61+
62+
# Post new coverage comment.
63+
curl -s -X POST \
64+
-H "Authorization: token ${GITHUB_TOKEN}" \
65+
-H "Accept: application/vnd.github.v3+json" \
66+
"https://api.github.com/repos/${REPO_SLUG}/issues/${PR_NUMBER}/comments" \
67+
-d "${BODY}"

0 commit comments

Comments
 (0)