-
Notifications
You must be signed in to change notification settings - Fork 6
/
action.yaml
175 lines (167 loc) · 6.88 KB
/
action.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
name: "Go Patch Coverage"
description: |
Display golang patch test coverage on your pull requests.
author: "Benjamin Boudreau"
branding:
icon: "code"
color: "orange"
inputs:
version:
description: |
The version of go-patch-cover to use.
required: true
default: "v0.2.0"
coverage_filename:
description: |
go coverage file for the code after change.
Can be generated with any cover mode.
Example generation:
go test -coverprofile=coverage.out -covermode=count ./...
required: true
default: "coverage.out"
diff_filename:
description: |
Unified diff file of the patch to compute coverage for.
Example generation:
git diff -U0 --no-color origin/main > patch.diff
required: false
default: "patch.diff"
previous_coverage_filename:
description: |
go coverage file for the code before changes.
When not provided, previous coverage information will not be displayed.
default: 'prev_coverage.out'
required: false
diff_enabled:
description: |
Controls whether go-patch-cover action should create the diff file or not.
default: 'true'
required: true
prev_coverage_mode:
description: |
The GitHub access token (e.g. secrets.GITHUB_TOKEN) used to create or update the comment. This defaults to {{ github.token }}.
Modes:
- git-notes: Use git notes: `refs/notes/coverage` to persist the coverage file on pushes.
- file: Expect the file pointed to by the previous_coverage_filename input to already exist.
default: 'git-notes'
required: false
github_token:
description: |
The GitHub access token (e.g. secrets.GITHUB_TOKEN) used to create or update the comment. This defaults to {{ github.token }}.
default: '${{ github.token }}'
required: false
runs:
using: "composite"
steps:
- if: github.event_name == 'pull_request' && inputs.diff_enabled == 'true'
run: |
git fetch --no-tags --prune --progress --no-recurse-submodules --depth=1 origin ${GITHUB_BASE_REF}
git diff -U0 --no-color origin/${GITHUB_BASE_REF} > patch.diff
shell: bash
- if: github.event_name == 'pull_request' && inputs.prev_coverage_mode == 'git-notes'
run: |
git fetch --no-tags --prune --progress --no-recurse-submodules --depth=1 origin ${GITHUB_BASE_REF} +refs/notes/coverage:refs/notes/coverage || true
git notes --ref coverage show origin/${GITHUB_BASE_REF} > ${previous_coverage_filename} || rm ${previous_coverage_filename}
shell: bash
env:
previous_coverage_filename : ${{ inputs.previous_coverage_filename }}
- if: github.event_name == 'pull_request'
run: |
patch_tmpl=$(cat <<EOF
<!-- go-patch-cover/report -->
<img src="https://badges.seriousben.com/badge?label=Patch%20Coverage&description={{printf "%.1f" .PatchCoverage}}%25" />
<table>
{{- if .HasPrevCoverage -}}
<tr>
<td>Previous Coverage
<td>{{printf "%.1f" .PrevCoverage}}% of statements
<td><img src="https://badges.seriousben.com/progress?percent={{.PrevCoverage}}">
{{ end -}}
<tr>
<td>New Coverage
<td>{{printf "%.1f" .Coverage}}% of statements
<td><img src="https://badges.seriousben.com/progress?percent={{.Coverage}}">
<tr>
<td>Patch Coverage
<td>{{printf "%.1f" .PatchCoverage}}% of changed statements ({{ .PatchCoverCount }}/{{ .PatchNumStmt }})
<td><img src="https://badges.seriousben.com/progress?percent={{.PatchCoverage}}">
</table>
---
<sup>Coverage provided by https://github.com/seriousben/go-patch-cover-action</sup>
EOF
)
go install "github.com/seriousben/go-patch-cover/cmd/go-patch-cover@${version}"
if [ -f "${previous_coverage_filename}" ]; then
out="$($(go env GOPATH)/bin/go-patch-cover -tmpl="${patch_tmpl}" ${coverage_filename} ${diff_filename} ${previous_coverage_filename})"
else
out="$($(go env GOPATH)/bin/go-patch-cover -tmpl="${patch_tmpl}" ${coverage_filename} ${diff_filename})"
fi
# Creating here doc to keep newline intact.
echo "GO_PATCH_COVER_OUT<<EOF" >> $GITHUB_ENV
echo "$out" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
shell: bash
env:
version: ${{ inputs.version }}
coverage_filename : ${{ inputs.coverage_filename }}
diff_filename : ${{ inputs.diff_filename }}
previous_coverage_filename : ${{ inputs.previous_coverage_filename }}
github_token: ${{ inputs.github_token }}
- if: github.event_name == 'pull_request'
run: |
comment_body=$(cat <<EOF
<!-- go-patch-cover/report -->
$GO_PATCH_COVER_OUT
EOF
)
comments="$(gh api graphql -F subjectId=$PULL_REQUEST_NODE_ID -f query='
query($subjectId: ID!) {
node(id: $subjectId) {
... on PullRequest {
comments(first: 100) {
nodes {
id
isMinimized
body
}
}
}
}
}
' --jq '.data.node.comments.nodes | map(select((.body | contains("<!-- go-patch-cover/report -->")) and .isMinimized == false)) | map(.id)[]')"
if [[ -n "$comments" ]]; then
for val in $comments; do
gh api graphql -X POST -F id=$val -F body="$comment_body" -f query='
mutation UpdateComment($id: ID!, $body: String!) {
updateIssueComment(input: {id: $id, body: $body}) {
clientMutationId
}
}
'
done
else
gh api graphql -X POST -F subjectId=$PULL_REQUEST_NODE_ID -F body="$comment_body" -f query='
mutation AddComment($subjectId: ID!, $body: String!) {
addComment(input: {subjectId: $subjectId, body: $body}) {
clientMutationId
}
}
'
fi
shell: bash
env:
GITHUB_TOKEN: ${{ inputs.github_token }}
OWNER: ${{ github.repository_owner }}
REPO: ${{ github.event.repository.name }}
PULL_REQUEST_NODE_ID: ${{ github.event.pull_request.node_id }}
- name: Save Coverage
if: github.event_name == 'push' && inputs.prev_coverage_mode == 'git-notes'
run: |
git fetch --no-tags --prune --progress --no-recurse-submodules --depth=1 origin ${GITHUB_BASE_REF} +refs/notes/coverage:refs/notes/coverage || true
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git notes --ref coverage add -f -F ${coverage_filename} origin/${GITHUB_REF_NAME}
git push origin refs/notes/coverage
shell: bash
env:
coverage_filename : ${{ inputs.coverage_filename }}