Skip to content

Commit 9e612b2

Browse files
dido18giulio93
andauthored
ci: setup ci and fix tests (#9)
* Refactor file reading to use os package instead of ioutil * Add Taskfile and .gitignore; improve error messages and test cleanup * Add GitHub Actions workflow for running Go tests * Update GitHub Actions workflow to trigger on all branches * Remove unnecessary dependency installation step and revert Go version to 1.24.0 * Update TestSearch to use "bash" for consistent test results * fix test using go-cmp * refactor test cleanup to simplify deferred removal of test files * added gitkeep --------- Co-authored-by: giulio93 <[email protected]> * Add GitHub Actions workflow for Go checks and update Taskfile with linting and formatting tasks * Update GitHub Actions workflow and add GolangCI configuration - Refactor Go test workflow to use environment variable for Go version - Add .golangci.yaml for linting and formatting configurations - Clean up commented debug statements in repos.go - Rename parameter in EditRepository function for clarity - Adjust file permissions in replaceFile function * Refactor Taskfile to streamline Go testing and remove deprecated linter setup * Add GitHub Actions workflow for Go testing and coverage reporting * Update CI configuration to specify repository for coverage reporting and enhance README with status badges * actions badge * Add GitHub Actions workflow for Go testing and coverage reporting --------- Co-authored-by: giulio93 <[email protected]>
1 parent 5613f84 commit 9e612b2

File tree

13 files changed

+516
-40
lines changed

13 files changed

+516
-40
lines changed

.github/workflows/check-go-task.yaml

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/check-go-task.md
2+
name: Check Go
3+
4+
env:
5+
# See: https://github.com/actions/setup-go/tree/main#supported-version-syntax
6+
GO_VERSION: "1.24"
7+
8+
# See: https://docs.github.com/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows
9+
on:
10+
create:
11+
push:
12+
paths:
13+
- ".github/workflows/check-go-task.ya?ml"
14+
- "Taskfile.ya?ml"
15+
- ".golangci.ya?ml"
16+
- "**/go.mod"
17+
- "**/go.sum"
18+
- "**.go"
19+
pull_request:
20+
paths:
21+
- ".github/workflows/check-go-task.ya?ml"
22+
- "Taskfile.ya?ml"
23+
- ".golangci.ya?ml"
24+
- "**/go.mod"
25+
- "**/go.sum"
26+
- "**.go"
27+
schedule:
28+
# Run periodically to catch breakage caused by external changes.
29+
- cron: "0 7 * * WED"
30+
workflow_dispatch:
31+
repository_dispatch:
32+
33+
jobs:
34+
run-determination:
35+
runs-on: ubuntu-latest
36+
permissions: {}
37+
outputs:
38+
result: ${{ steps.determination.outputs.result }}
39+
steps:
40+
- name: Determine if the rest of the workflow should run
41+
id: determination
42+
run: |
43+
RELEASE_BRANCH_REGEX="refs/heads/[0-9]+.[0-9]+.x"
44+
# The `create` event trigger doesn't support `branches` filters, so it's necessary to use Bash instead.
45+
if [[
46+
"${{ github.event_name }}" != "create" ||
47+
"${{ github.ref }}" =~ $RELEASE_BRANCH_REGEX
48+
]]; then
49+
# Run the other jobs.
50+
RESULT="true"
51+
else
52+
# There is no need to run the other jobs.
53+
RESULT="false"
54+
fi
55+
56+
echo "result=$RESULT" >> $GITHUB_OUTPUT
57+
58+
check-outdated:
59+
name: check-outdated (${{ matrix.module.path }})
60+
needs: run-determination
61+
if: needs.run-determination.outputs.result == 'true'
62+
runs-on: ubuntu-latest
63+
permissions:
64+
contents: read
65+
66+
strategy:
67+
fail-fast: false
68+
69+
matrix:
70+
module:
71+
# TODO: add paths of all Go modules here
72+
- path: ./
73+
74+
steps:
75+
- name: Checkout repository
76+
uses: actions/checkout@v4
77+
78+
- name: Install Go
79+
uses: actions/setup-go@v5
80+
with:
81+
go-version: ${{ env.GO_VERSION }}
82+
83+
- name: Install Task
84+
uses: arduino/setup-task@v2
85+
with:
86+
repo-token: ${{ secrets.GITHUB_TOKEN }}
87+
version: 3.x
88+
89+
- name: Modernize usages of outdated APIs
90+
env:
91+
GO_MODULE_PATH: ${{ matrix.module.path }}
92+
run: task go:fix
93+
94+
- name: Check if any fixes were needed
95+
run: git diff --color --exit-code
96+
97+
check-style:
98+
name: check-style (${{ matrix.module.path }})
99+
needs: run-determination
100+
if: needs.run-determination.outputs.result == 'true'
101+
runs-on: ubuntu-latest
102+
permissions:
103+
contents: read
104+
105+
strategy:
106+
fail-fast: false
107+
108+
matrix:
109+
module:
110+
# TODO: add paths of all Go modules here
111+
- path: ./
112+
113+
steps:
114+
- name: Checkout repository
115+
uses: actions/checkout@v4
116+
117+
- name: Install Go
118+
uses: actions/setup-go@v5
119+
with:
120+
go-version: ${{ env.GO_VERSION }}
121+
122+
- name: Install Task
123+
uses: arduino/setup-task@v2
124+
with:
125+
repo-token: ${{ secrets.GITHUB_TOKEN }}
126+
version: 3.x
127+
128+
- name: Install golangci-lint
129+
uses: golangci/golangci-lint-action@v3
130+
with:
131+
version: v1.54
132+
133+
- name: Check style
134+
env:
135+
GO_MODULE_PATH: ${{ matrix.module.path }}
136+
run: task --silent go:lint
137+
138+
check-formatting:
139+
name: check-formatting (${{ matrix.module.path }})
140+
needs: run-determination
141+
if: needs.run-determination.outputs.result == 'true'
142+
runs-on: ubuntu-latest
143+
permissions:
144+
contents: read
145+
146+
strategy:
147+
fail-fast: false
148+
149+
matrix:
150+
module:
151+
# TODO: add paths of all Go modules here
152+
- path: ./
153+
154+
steps:
155+
- name: Checkout repository
156+
uses: actions/checkout@v4
157+
158+
- name: Install Go
159+
uses: actions/setup-go@v5
160+
with:
161+
go-version: ${{ env.GO_VERSION }}
162+
163+
- name: Install Task
164+
uses: arduino/setup-task@v2
165+
with:
166+
repo-token: ${{ secrets.GITHUB_TOKEN }}
167+
version: 3.x
168+
169+
- name: Format code
170+
env:
171+
GO_MODULE_PATH: ${{ matrix.module.path }}
172+
run: task go:format
173+
174+
- name: Check formatting
175+
run: git diff --color --exit-code
176+
177+
check-config:
178+
name: check-config (${{ matrix.module.path }})
179+
needs: run-determination
180+
if: needs.run-determination.outputs.result == 'true'
181+
runs-on: ubuntu-latest
182+
permissions:
183+
contents: read
184+
185+
strategy:
186+
fail-fast: false
187+
188+
matrix:
189+
module:
190+
# TODO: add paths of all Go modules here
191+
- path: ./
192+
193+
steps:
194+
- name: Checkout repository
195+
uses: actions/checkout@v4
196+
197+
- name: Install Go
198+
uses: actions/setup-go@v5
199+
with:
200+
go-version: ${{ env.GO_VERSION }}
201+
202+
- name: Run go mod tidy
203+
working-directory: ${{ matrix.module.path }}
204+
run: go mod tidy
205+
206+
- name: Check whether any tidying was needed
207+
run: git diff --color --exit-code

.github/workflows/test-go-task.yml

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/test-go-task.md
2+
name: Test Go
3+
4+
env:
5+
# See: https://github.com/actions/setup-go/tree/main#supported-version-syntax
6+
GO_VERSION: "1.24"
7+
8+
on:
9+
create:
10+
push:
11+
paths:
12+
- ".github/workflows/test-go-task.ya?ml"
13+
- ".github/.?codecov.ya?ml"
14+
- "dev/.?codecov.ya?ml"
15+
- ".?codecov.ya?ml"
16+
- "**/go.mod"
17+
- "**/go.sum"
18+
- "Taskfile.ya?ml"
19+
- "**.go"
20+
- "**/testdata/**"
21+
pull_request:
22+
paths:
23+
paths:
24+
- ".github/workflows/test-go-task.ya?ml"
25+
- ".github/.?codecov.ya?ml"
26+
- "dev/.?codecov.ya?ml"
27+
- ".?codecov.ya?ml"
28+
- "**/go.mod"
29+
- "**/go.sum"
30+
- "Taskfile.ya?ml"
31+
- "**.go"
32+
- "**/testdata/**"
33+
schedule:
34+
# Run periodically to catch breakage caused by external changes.
35+
- cron: "0 11 * * WED"
36+
workflow_dispatch:
37+
repository_dispatch:
38+
39+
jobs:
40+
run-determination:
41+
runs-on: ubuntu-latest
42+
outputs:
43+
result: ${{ steps.determination.outputs.result }}
44+
permissions: {}
45+
steps:
46+
- name: Determine if the rest of the workflow should run
47+
id: determination
48+
run: |
49+
RELEASE_BRANCH_REGEX="refs/heads/[0-9]+.[0-9]+.x"
50+
# The `create` event trigger doesn't support `branches` filters, so it's necessary to use Bash instead.
51+
if [[
52+
"${{ github.event_name }}" != "create" ||
53+
"${{ github.ref }}" =~ $RELEASE_BRANCH_REGEX
54+
]]; then
55+
# Run the other jobs.
56+
RESULT="true"
57+
else
58+
# There is no need to run the other jobs.
59+
RESULT="false"
60+
fi
61+
62+
echo "result=$RESULT" >> $GITHUB_OUTPUT
63+
64+
test:
65+
name: test (${{ matrix.module.path }} - ${{ matrix.operating-system }})
66+
needs: run-determination
67+
if: needs.run-determination.outputs.result == 'true'
68+
permissions:
69+
contents: read
70+
71+
strategy:
72+
fail-fast: false
73+
74+
matrix:
75+
operating-system:
76+
- ubuntu-latest
77+
module:
78+
# TODO: add paths of all Go modules here
79+
- path: ./
80+
codecov-flags: unit
81+
82+
runs-on: ${{ matrix.operating-system }}
83+
84+
steps:
85+
- name: Checkout repository
86+
uses: actions/checkout@v4
87+
88+
- name: Install Go
89+
uses: actions/setup-go@v5
90+
with:
91+
go-version: ${{ env.GO_VERSION }}
92+
93+
- name: Install Task
94+
uses: arduino/setup-task@v2
95+
with:
96+
repo-token: ${{ secrets.GITHUB_TOKEN }}
97+
version: 3.x
98+
99+
- name: Run tests
100+
env:
101+
GO_MODULE_PATH: ${{ matrix.module.path }}
102+
# run: task go:test ## TODO: refactor the tests
103+
run: task go:test:docker
104+
105+
- name: Send unit tests coverage to Codecov
106+
if: runner.os == 'Linux'
107+
uses: codecov/codecov-action@v3
108+
with:
109+
file: ${{ matrix.module.path }}coverage_unit.txt
110+
flags: ${{ matrix.module.codecov-flags }}
111+
fail_ci_if_error: ${{ github.repository == 'arduino/go-apt-client' }}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.bin/
2+
coverage*.txt

.golangci.yaml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
version: "2"
2+
linters:
3+
enable:
4+
- gochecknoinits
5+
- goconst
6+
- gocritic
7+
- gosec
8+
- importas
9+
- misspell
10+
- revive
11+
- unconvert
12+
- unparam
13+
settings:
14+
misspell:
15+
locale: US
16+
exclusions:
17+
generated: lax
18+
presets:
19+
- comments
20+
- common-false-positives
21+
- legacy
22+
- std-error-handling
23+
paths:
24+
- third_party$
25+
- builtin$
26+
- examples$
27+
formatters:
28+
enable:
29+
- gofmt
30+
- goimports
31+
settings:
32+
goimports:
33+
local-prefixes:
34+
- github.com/bcmi-labs/provisoning-api
35+
exclusions:
36+
generated: lax
37+
paths:
38+
- third_party$
39+
- builtin$
40+
- examples$

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
This is a golang client library for Debian APT Package Manager (`dpkg` and `apt`).
44

5+
[![Check Go status](https://github.com/arduino/go-apt-client/actions/workflows/check-go-task.yml/badge.svg)](https://github.com/arduino/go-apt-client/actions/workflows/check-go-task.yml)
6+
[![Test Go status](https://github.com/arduino/go-apt-client/actions/workflows/test-go-task.yml/badge.svg)](https://github.com/arduino/go-apt-client/actions/workflows/test-go-task.yml)
7+
[![Codecov](https://codecov.io/gh/arduino/go-apt-client/branch/main/graph/badge.svg)](https://codecov.io/gh/arduino/go-apt-client)
8+
59
## License
610

711
Copyright (C) 2017 Arduino AG (http://www.arduino.cc/)
@@ -17,4 +21,3 @@ distributed under the License is distributed on an "AS IS" BASIS,
1721
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1822
See the License for the specific language governing permissions and
1923
limitations under the License.
20-

0 commit comments

Comments
 (0)