Skip to content

Commit

Permalink
feat: support go apps in repo subfolders
Browse files Browse the repository at this point in the history
  • Loading branch information
kilianc committed May 14, 2024
1 parent 598a6ee commit 4135396
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 18 deletions.
11 changes: 10 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@ jobs:
go-version: '1.22'

- name: Generate Coverage Files
run: make test
run: |
cd go-test-app-01
make test
- name: Go Beautiful HTML Coverage
uses: './'
with:
path: go-test-app-01

- name: Go Beautiful HTML Coverage
uses: './'
with:
path: go-test-app-02
8 changes: 0 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
.PHONY: test
test:
@go test -coverprofile=cover.out ./...

.PHONY: release
release:
git tag -d v1
git tag v1 HEAD
git push -f origin v1

.PHONY: clean
clean:
@rm -f cover.*
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ This GHA expects `cover.out` to be present in the root of your repo at runtime.
go test -coverprofile=cover.out ./...
```

For examples on how you might do that you can peak at the [`Makefile`](./Makefile), or some of my other go projects like [`pretender`](https://github.com/kilianc/pretender/blob/main/Makefile#L44-L57) and [`base-go-cli`](https://github.com/kilianc/base-golang-cli/blob/main/Makefile#L76-L92).
For examples on how you might do that you can peak at the `go-test-app` [`go-test-app/Makefile`](./Makefile), or some of my other go projects like [`pretender`](https://github.com/kilianc/pretender/blob/main/Makefile#L44-L57) and [`base-go-cli`](https://github.com/kilianc/base-golang-cli/blob/main/Makefile#L76-L92).

Once your test has ran and `cover.out` has been generated, the GHA does the following:

Expand Down Expand Up @@ -82,6 +82,10 @@ Once your test has ran and `cover.out` has been generated, the GHA does the foll
# The token to use for pushing to the repository.
# Default: ${{ github.token }}
token: ''
# The relative path of your go project. Useful for monorepos and custom folder structures.
# Default: ./
path: ''
```

## Examples
Expand Down Expand Up @@ -111,6 +115,17 @@ This is helpful if you don't want to clutter your project's repo, or if you want

Where `GHA_COVERAGE_TOKEN` is a repository secret with a personal token that has write access to `yourname/coverage`.

**You can customize the path to your go project in the repo.**

This is helpful if you have a monorepo with multiple apps, or simply you keep your go files in a subfolder.

```yaml
- name: Go Beautiful HTML Coverage
uses: 'gha-common/go-beautiful-html-coverage@v1'
with:
path: ./go-app
```

## License

MIT License, see [LICENSE](./LICENSE.md)
12 changes: 9 additions & 3 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ inputs:
token:
description: The token to use for pushing to the repository.
default: ${{ github.token }}
path:
description: The relative path of your go project. Useful for monorepos and custom folder structures.
default: './'
runs:
using: composite
steps:
Expand Down Expand Up @@ -42,10 +45,12 @@ runs:
shell: bash
run: |
export REVISION="${{ github.event.pull_request.head.sha || github.sha }}"
cd ${{ inputs.path }}
go tool cover -func=cover.out -o cover.txt
go tool cover -html=cover.out -o cover.html
cd go-cover
mv ../cover.html ${REVISION}.html
mv cover.html ${GITHUB_WORKSPACE}/go-cover/${REVISION}.html
mv cover.txt ${GITHUB_WORKSPACE}/go-cover/${REVISION}.txt
cd ${GITHUB_WORKSPACE}/go-cover
ex -sc '%s/<style>/<style>@import url("nord.css");/' -c 'x' ${REVISION}.html
ex -sc '%s/<\/script>/<\/script><script src="ln.js"><\/script>/' -c 'x' ${REVISION}.html
cp ${GITHUB_ACTION_PATH}/assets/* .
Expand All @@ -63,4 +68,5 @@ runs:
script: |
const script = require(`${process.env.GITHUB_ACTION_PATH}/src/update-comment.js`)
const revision = '${{ github.event.pull_request.head.sha || github.sha }}'
await script({ context, github }, revision)
const path = '${{ inputs.path }}'
await script({ context, github }, path, revision)
7 changes: 7 additions & 0 deletions go-test-app-01/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.PHONY: test
test:
@go test -coverprofile=cover.out ./...

.PHONY: clean
clean:
@rm -f cover.*
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions go-test-app-02
10 changes: 5 additions & 5 deletions src/update-comment.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const fs = require('fs')

const updateCodeCoverageComment = module.exports = async ({ context, github }, revision) => {
const updateCodeCoverageComment = module.exports = async ({ context, github }, path, revision) => {
const comments = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
Expand All @@ -9,15 +9,15 @@ const updateCodeCoverageComment = module.exports = async ({ context, github }, r
})

const coverageComment = comments.data.find((comment) => {
return comment.body.startsWith('<!-- coverage -->')
return comment.body.startsWith(`<!-- coverage (${path})-->`)
}) || {}

const coverageText = fs.readFileSync('cover.txt', 'utf8').split('\n').slice(0, -1)
const coverageText = fs.readFileSync(`go-cover/${revision}.txt`, 'utf8').split('\n').slice(0, -1)
const coverageTextSummary = coverageText[coverageText.length-1].split('\t').pop()

const commentBody = [
'<!-- coverage -->',
`### [Code Coverage Report 🔗](https://${context.repo.owner}.github.io/${context.repo.repo}/?hash=${revision}) for ${revision}`,
`<!-- coverage (${path})-->`,
`### [Code Coverage Report 🔗](https://${context.repo.owner}.github.io/${context.repo.repo}/?hash=${revision}) for \`${path}/\` at ${revision}`,
'```',
`Total: ${coverageTextSummary}`,
'```',
Expand Down

0 comments on commit 4135396

Please sign in to comment.