-
Notifications
You must be signed in to change notification settings - Fork 21
121 lines (112 loc) · 4.73 KB
/
test.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
name: test
on:
pull_request:
push:
branches:
- main
env:
BUCKET: neon-github-public-dev
BUCKET_PATH: autoscaling/code-coverage
REPORT_PREFIX: https://neon-github-public-dev.s3.amazonaws.com/autoscaling/code-coverage
jobs:
tests:
runs-on: ubuntu-latest
timeout-minutes: 10
permissions:
# permissions are required for the fgrosse/go-coverage-report action
contents: read
actions: read
pull-requests: write
# aws-actions/configure-aws-credentials
id-token: write
strategy:
fail-fast: false
steps:
- uses: actions/checkout@v4
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: us-east-2
role-to-assume: arn:aws:iam::369495373322:role/gha-oidc-s3-rw
- uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'
- uses: tj-actions/changed-files@c65cd883420fd2eb864698a825fc4162dd94482c # v44.5.7
id: changed-files
with:
files: |
.github/workflows/test.yaml
**/**.go
"!test/"
go.mod
go.sum
# run tests on PR only if there are code changes and always on main
- name: Run Go Tests
if: ${{ steps.changed-files.outputs.any_changed == 'true' && github.event_name == 'pull_request' || github.ref_name == 'main' }}
run: |
make test
- name: Archive code coverage results
uses: actions/upload-artifact@v4
if: ${{ !cancelled() && (steps.changed-files.outputs.any_changed == 'true' && github.event_name == 'pull_request' || github.ref_name == 'main') }}
with:
name: code-coverage
path: cover.out
- name: Upload html coverage report
id: html-artifact
env:
COMMIT_SHA: ${{ github.event.pull_request.head.sha || github.sha }}
if: ${{ !cancelled() && (steps.changed-files.outputs.any_changed == 'true' && github.event_name == 'pull_request')}}
run: |
aws s3 cp cover.html "s3://${BUCKET}/${BUCKET_PATH}/${COMMIT_SHA}/index.html"
- name: "Code coverage report"
uses: fgrosse/[email protected]
if: ${{ !cancelled() && (steps.changed-files.outputs.any_changed == 'true' && github.event_name == 'pull_request')}}
with:
coverage-artifact-name: "code-coverage"
coverage-file-name: "cover.out"
- name: Create comment with html report
uses: actions/github-script@v7
if: ${{ !cancelled() && (steps.changed-files.outputs.any_changed == 'true' && github.event_name == 'pull_request')}}
env:
COMMIT_SHA: ${{ github.event.pull_request.head.sha || github.sha }}
with:
script: |
const commentIdentifier = "Coverage Δ"; // the same marker as in the fgrosse/go-coverage-report
const htmlCoverageTitle = "\n ### HTML Report"
// Get all comments on the pull request
const { data: comments } = await github.rest.issues.listComments({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
});
// Find the comment that contains the identifier
const existingComment = comments.find(comment => comment.body.includes(commentIdentifier));
// If there's no original comment, create a new one with hidden comment identifier
// to make it easier to find next time
let originalCommentBody = `<!-- ${commentIdentifier} -->No changes to the coverage.\n\n`
if (existingComment) {
const htmlCoverageTitleIdx = existingComment.body.indexOf(htmlCoverageTitle)
originalCommentBody = existingComment.body
if (htmlCoverageTitleIdx != -1) {
originalCommentBody = existingComment.body.slice(0, htmlCoverageTitleIdx)
}
}
const reportURL = "${{env.REPORT_PREFIX}}/${{env.COMMIT_SHA}}/index.html"
const newBody = `${originalCommentBody}${htmlCoverageTitle}\n<a href="${reportURL}">Click to open</a>`;
if (existingComment) {
// Update the existing comment
await github.rest.issues.updateComment({
comment_id: existingComment.id,
owner: context.repo.owner,
repo: context.repo.repo,
body: newBody,
});
} else {
// Create a new comment
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: newBody,
});
}