github.com/prebid/prebid-server@v0.275.0/.github/workflows/adapter-code-coverage.yml (about) 1 name: Adapter code coverage 2 on: 3 pull_request_target: 4 paths: ["adapters/*/*.go"] 5 permissions: 6 pull-requests: write 7 contents: write 8 jobs: 9 run-coverage: 10 runs-on: ubuntu-latest 11 steps: 12 - name: Install Go 13 uses: actions/setup-go@v4 14 with: 15 go-version: 1.20.5 16 17 - name: Checkout pull request branch 18 uses: actions/checkout@v3 19 with: 20 fetch-depth: 0 21 ref: ${{github.event.pull_request.head.ref}} 22 repository: ${{github.event.pull_request.head.repo.full_name}} 23 24 - name: Get adapter directories 25 id: get_directories 26 uses: actions/github-script@v6 27 with: 28 result-encoding: string 29 script: | 30 const utils = require('./.github/workflows/helpers/pull-request-utils.js') 31 function directoryExtractor(filepath) { 32 // extract directory name from filepath of the form adapters/<adapter-name>/*.go 33 if (filepath.startsWith("adapters/") && filepath.split("/").length > 2) { 34 return filepath.split("/")[1] 35 } 36 return "" 37 } 38 const helper = utils.diffHelper({github, context}) 39 const files = await helper.getDirectories(directoryExtractor) 40 return files.length == 0 ? "" : JSON.stringify(files); 41 42 - name: Run coverage tests 43 id: run_coverage 44 if: ${{ steps.get_directories.outputs.result }} != "" 45 run: | 46 directories=$(echo '${{ steps.get_directories.outputs.result }}' | jq -r '.[]') 47 go mod download 48 49 # create a temporary directory to store the coverage output 50 temp_dir=$(mktemp -d) 51 touch ${temp_dir}/coverage_output.txt 52 53 # generate coverage for adapter 54 cd ./adapters 55 for directory in $directories; do 56 cd $directory 57 coverage_profile_path="${PWD}/${directory}.out" 58 go test -coverprofile="${coverage_profile_path}" 59 go tool cover -html="${coverage_profile_path}" -o "${temp_dir}/${directory}.html" 60 go tool cover -func="${coverage_profile_path}" -o "${temp_dir}/${directory}.txt" 61 cd .. 62 done 63 echo "coverage_dir=${temp_dir}" >> $GITHUB_OUTPUT 64 65 # remove pull request branch files 66 cd .. 67 rm -f -r ./* 68 69 - name: Checkout coverage-preview branch 70 uses: actions/checkout@v3 71 with: 72 fetch-depth: 0 73 ref: coverage-preview 74 repository: prebid/prebid-server 75 76 - name: Commit coverage files to coverage-preview branch 77 if: ${{ steps.run_coverage.outputs.coverage_dir }} != "" 78 id: commit_coverage 79 run: | 80 directory=.github/preview/${{ github.run_id }}_$(date +%s) 81 mkdir -p $directory 82 cp -r ${{ steps.run_coverage.outputs.coverage_dir }}/*.html ./$directory 83 git config --global user.name "github-actions[bot]" 84 git config --global user.email "github-actions[bot]@users.noreply.github.com" 85 git add $directory/* 86 git commit -m 'Add coverage files' 87 git push origin coverage-preview 88 echo "remote_coverage_preview_dir=${directory}" >> $GITHUB_OUTPUT 89 90 - name: Checkout master branch 91 if: ${{ steps.get_directories.outputs.result }} != "" 92 run: git checkout master 93 94 - name: Add coverage summary to pull request 95 if: ${{ steps.run_coverage.outputs.coverage_dir }} != "" && ${{ steps.commit_coverage.outputs.remote_coverage_preview_dir }} != "" 96 uses: actions/github-script@v6 97 with: 98 script: | 99 const utils = require('./.github/workflows/helpers/pull-request-utils.js') 100 const helper = utils.coverageHelper({ 101 github, context, 102 headSha: '${{ github.event.pull_request.head.sha }}', 103 tmpCoverageDir: '${{ steps.run_coverage.outputs.coverage_dir }}', 104 remoteCoverageDir: '${{ steps.commit_coverage.outputs.remote_coverage_preview_dir }}' 105 }) 106 const adapterDirectories = JSON.parse('${{ steps.get_directories.outputs.result }}') 107 await helper.AddCoverageSummary(adapterDirectories)