-
Notifications
You must be signed in to change notification settings - Fork 749
111 lines (98 loc) · 4.24 KB
/
adapter-code-coverage.yml
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
name: Adapter Code Coverage
on:
pull_request_target:
paths: ["adapters/*/*.go"]
permissions:
pull-requests: write
contents: write
jobs:
run-coverage:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v5
with:
go-version: 1.22.3
- name: Checkout Code
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{github.event.pull_request.head.ref}}
repository: ${{github.event.pull_request.head.repo.full_name}}
- name: Discover Adapter Directories
id: get_directories
uses: actions/github-script@v7
with:
result-encoding: string
script: |
const utils = require('./.github/workflows/helpers/pull-request-utils.js')
function directoryExtractor(filepath, status) {
// extract directory name only if file is not removed and file is in adapters directory
if (status != "removed" && filepath.startsWith("adapters/") && filepath.split("/").length > 2) {
return filepath.split("/")[1]
}
return ""
}
const helper = utils.diffHelper({github, context})
const directories = await helper.getDirectories(directoryExtractor)
// run coverage for maximum of 2 directories
return (directories.length == 0 || directories.length > 2) ? "" : JSON.stringify(directories)
- name: Run Coverage Tests
id: run_coverage
if: steps.get_directories.outputs.result != ''
run: |
directories=$(echo '${{ steps.get_directories.outputs.result }}' | jq -r '.[]')
go mod download
# create a temporary directory to store the coverage output
temp_dir=$(mktemp -d)
touch ${temp_dir}/coverage_output.txt
# generate coverage for adapter
cd ./adapters
for directory in $directories; do
cd $directory
coverage_profile_path="${PWD}/${directory}.out"
go test -coverprofile="${coverage_profile_path}"
go tool cover -html="${coverage_profile_path}" -o "${temp_dir}/${directory}.html"
go tool cover -func="${coverage_profile_path}" -o "${temp_dir}/${directory}.txt"
cd ..
done
echo "coverage_dir=${temp_dir}" >> $GITHUB_OUTPUT
# remove pull request branch files
cd ..
rm -f -r ./*
- name: Checkout Coverage Preview Branch
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: coverage-preview
repository: prebid/prebid-server
- name: Upload Coverage Results
if: steps.run_coverage.outputs.coverage_dir != ''
id: commit_coverage
run: |
directory=.github/preview/${{ github.run_id }}_$(date +%s)
mkdir -p $directory
cp -r ${{ steps.run_coverage.outputs.coverage_dir }}/*.html ./$directory
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git add $directory/*
git commit -m 'Add coverage files'
git push origin coverage-preview
echo "remote_coverage_preview_dir=${directory}" >> $GITHUB_OUTPUT
- name: Checkout Master Branch
if: steps.get_directories.outputs.result != ''
run: git checkout master
- name: Add Coverage Summary To Pull Request
if: steps.run_coverage.outputs.coverage_dir != '' && steps.commit_coverage.outputs.remote_coverage_preview_dir != ''
uses: actions/github-script@v7
with:
script: |
const utils = require('./.github/workflows/helpers/pull-request-utils.js')
const helper = utils.coverageHelper({
github, context,
headSha: '${{ github.event.pull_request.head.sha }}',
tmpCoverageDir: '${{ steps.run_coverage.outputs.coverage_dir }}',
remoteCoverageDir: '${{ steps.commit_coverage.outputs.remote_coverage_preview_dir }}'
})
const adapterDirectories = JSON.parse('${{ steps.get_directories.outputs.result }}')
await helper.AddCoverageSummary(adapterDirectories)