11name : Continuous Integration
22on :
33 pull_request :
4+ push :
5+ branches :
6+ - main
47
58env :
69 GO_VERSION : " 1.22.0"
6063 for package in packages:
6164 package_short = package.removeprefix('github.com/port-labs/terraform-provider-port-labs/')
6265 file = package_short.replace('/', '-').replace('.', '-') + ".test"
63- subprocess.run(("go", "test", package, "-c", "-o", file), capture_output=True, text=True, check=True)
66+ subprocess.run(("go", "test", package, "-c", "-cover", "- o", file), capture_output=True, text=True, check=True)
6467 if os.path.isfile(file):
6568 files.append(file)
6669 include.append(dict(test_file=file, package=package, package_short=package_short))
9093 test_file : ${{fromJSON(needs.acctest-build.outputs.test_files)}}
9194 include : ${{fromJSON(needs.acctest-build.outputs.test_files_include)}}
9295 steps :
96+ - name : Setup short name
97+ run : echo "SHORT_NAME=$(echo ${{ matrix.package_short }} | rev | cut -d'/' -f1 | rev)" >> $GITHUB_ENV
9398 - uses : hashicorp/setup-terraform@v3
9499 with :
95100 terraform_wrapper : false
@@ -113,7 +118,7 @@ jobs:
113118 chmod u+x "${{matrix.test_file}}"
114119 gotestsum --raw-command --rerun-fails --format testname --junitfile "./test-results/${{matrix.test_file}}.xml" \
115120 -- go tool test2json -t -p "${{matrix.package}}" "./${{matrix.test_file}}" \
116- -test.v=test2json -test.timeout 10m -test.parallel 1 -test.shuffle "${{ github.run_id }}"
121+ -test.v=test2json -test.timeout 10m -test.parallel 1 -test.shuffle "${{ github.run_id }}" -test.coverprofile=cover-${{ env.SHORT_NAME }}.out
117122
118123 - uses : actions/upload-artifact@v4
119124 if : always()
@@ -122,12 +127,20 @@ jobs:
122127 path : ' ./test-results/${{matrix.test_file}}.xml'
123128 if-no-files-found : error
124129
130+ - name : Upload coverage
131+ if : always()
132+ uses : actions/upload-artifact@v4
133+ with :
134+ path : ' ./cover-${{ env.SHORT_NAME }}.out'
135+ name : ' coverage-${{ env.SHORT_NAME }}'
136+
125137 publish_report :
126138 name : Publish Test Report
127139 if : ${{ always() }}
128140 needs : acctest
129141 runs-on : ubuntu-latest
130142 steps :
143+ - uses : actions/checkout@v4
131144 - uses : actions/download-artifact@v4
132145 with :
133146 pattern : ' test-results-*'
@@ -142,4 +155,110 @@ jobs:
142155 fail_on_failure : ' true'
143156 comment : ' true'
144157 check_retries : ' true'
145- flaky_summary : ' true'
158+ flaky_summary : ' true'
159+ - name : Download all coverage reports
160+ uses : actions/download-artifact@v4
161+ with :
162+ pattern : ' coverage-*'
163+ path : ' coverage-artifacts'
164+ - name : Merge coverage reports
165+ run : |
166+ mkdir -p coverage
167+ find coverage-artifacts -type f -name '*' -exec cp {} coverage/ \;
168+ echo "mode: set" > coverage/coverage.merged
169+ cat coverage/*.out | grep -v "mode: set" >> coverage/coverage.merged
170+ go tool cover -func=coverage/coverage.merged > coverage/coverage.txt
171+ go tool cover -html=coverage/coverage.merged -o coverage/coverage.html
172+ - name : Upload coverage report
173+ uses : actions/upload-artifact@v4
174+ id : upload-coverage
175+ with :
176+ path : ' coverage/coverage.html'
177+ name : ' coverage-html'
178+ - name : Get PR_NUMBER
179+ id : pr-number
180+ run : |
181+ if [ ! -z ${{ inputs.PR_NUMBER }} ]; then
182+ echo "PR_NUMBER=${{ inputs.PR_NUMBER }}" >> $GITHUB_OUTPUT
183+ elif [ ! -z ${{ github.event.pull_request.number }} ]; then
184+ echo "PR_NUMBER=${{ github.event.pull_request.number }}" >> $GITHUB_OUTPUT
185+ else
186+ echo "PR_NUMBER=0" >> $GITHUB_OUTPUT
187+ fi
188+ - name : Set repo code coverage percentage by the percentage of statements covered in the tests
189+ id : set-stmts-coverage
190+ run : |
191+ stmts=$(tail -n1 coverage/coverage.txt | awk '{print $3}' | sed 's/%//')
192+ if [ -z "$stmts" ]; then
193+ echo "STMTS_COVERAGE=0" >> $GITHUB_OUTPUT
194+ else
195+ echo "STMTS_COVERAGE=$stmts" >> $GITHUB_OUTPUT
196+ fi
197+ - name : Comment PR with code coverage summary
198+ uses : actions/github-script@v7
199+ env :
200+ CODE_COVERAGE_ARTIFACT_URL : ${{ steps.upload-coverage.outputs.artifact-url }}
201+ PR_NUMBER : ${{ steps.pr-number.outputs.PR_NUMBER }}
202+ with :
203+ github-token : ${{ secrets.GITHUB_TOKEN }}
204+ script : |
205+ const output = `#### Code Coverage Artifact 📈: ${{ env.CODE_COVERAGE_ARTIFACT_URL }}
206+ #### Code Coverage Total Percentage: \`${{ steps.set-stmts-coverage.outputs.STMTS_COVERAGE }}%\``;
207+
208+ github.rest.issues.createComment({
209+ issue_number: ${{ env.PR_NUMBER }},
210+ owner: context.repo.owner,
211+ repo: context.repo.repo,
212+ body: output
213+ })
214+ - name : Get current repo coverage percentage from Port
215+ uses : port-labs/port-github-action@v1
216+ id : get-current-coverage
217+ with :
218+ clientId : ${{ secrets.PORT_MAIN_CLIENT_ID }}
219+ clientSecret : ${{ secrets.PORT_MAIN_CLIENT_SECRET }}
220+ baseUrl : https://api.getport.io
221+ operation : GET
222+ identifier : terraform-provider-port-labs
223+ blueprint : repository
224+ - name : Set current code coverage
225+ id : set-current-coverage
226+ run : echo "CURRENT_COVERAGE=${{ fromJson(steps.get-current-coverage.outputs.entity).properties.coverage_percent }}" >> $GITHUB_OUTPUT
227+
228+ - name : Comment if Coverage Regression
229+ if : ${{ (fromJson(steps.set-stmts-coverage.outputs.STMTS_COVERAGE) < fromJson(steps.set-current-coverage.outputs.CURRENT_COVERAGE)) && (steps.pr-number.outputs.PR_NUMBER != 0) }}
230+ uses : actions/github-script@v7
231+ env :
232+ PR_NUMBER : ${{ steps.pr-number.outputs.PR_NUMBER }}
233+ CURRENT_COVERAGE : ${{ steps.set-current-coverage.outputs.CURRENT_COVERAGE }}
234+ NEW_COVERAGE : ${{ steps.set-stmts-coverage.outputs.STMTS_COVERAGE }}
235+ with :
236+ github-token : ${{ secrets.GITHUB_TOKEN }}
237+ script : |
238+ const output = `🚨 The new code coverage percentage is lower than the current one. Current coverage: \`${{ env.CURRENT_COVERAGE }}\`\n While the new one is: \`${{ env.NEW_COVERAGE }}\``;
239+
240+ github.rest.issues.createComment({
241+ issue_number: ${{ env.PR_NUMBER }},
242+ owner: context.repo.owner,
243+ repo: context.repo.repo,
244+ body: output
245+ })
246+
247+ - name : Fail PR if current code coverage percentage is higher than the new one
248+ if : ${{ (fromJson(steps.set-stmts-coverage.outputs.STMTS_COVERAGE) < fromJson(steps.set-current-coverage.outputs.CURRENT_COVERAGE)) && (vars.CODE_COVERAGE_ENFORCEMENT == 'true') }}
249+ run : exit 1
250+
251+ - name : Update service code coverage percentage in Port
252+ if : ${{ (github.event_name == 'push') }}
253+ uses : port-labs/port-github-action@v1
254+ with :
255+ clientId : ${{ secrets.PORT_MAIN_CLIENT_ID }}
256+ clientSecret : ${{ secrets.PORT_MAIN_CLIENT_SECRET }}
257+ baseUrl : https://api.getport.io
258+ operation : UPSERT
259+ identifier : terraform-provider-port-labs
260+ blueprint : repository
261+ properties : |-
262+ {
263+ "coverage_percent": "${{ steps.set-stmts-coverage.outputs.STMTS_COVERAGE }}"
264+ }
0 commit comments