Skip to content

Commit 2c05451

Browse files
committed
Add mage for CI checks
Signed-off-by: jose.vazquez <[email protected]>
1 parent 7c14938 commit 2c05451

File tree

6 files changed

+131
-7
lines changed

6 files changed

+131
-7
lines changed

.github/workflows/crd2go.yml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,9 @@ jobs:
5454

5555
- name: gci
5656
working-directory: ./tools/crd2go
57-
run: |
58-
echo "Did you run gofmt-gci?"
59-
devbox run -- 'gci write --skip-generated -s standard -s default -s localmodule . && git diff-index --name-only HEAD && git diff-index --quiet HEAD --'
57+
run: devbox run -- 'mage gci'
6058

61-
licenses:
59+
addlicense:
6260
runs-on: ubuntu-latest
6361
steps:
6462
- uses: actions/checkout@v5
@@ -73,5 +71,4 @@ jobs:
7371

7472
- name: Check license headers
7573
working-directory: ./tools/crd2go
76-
run: |
77-
devbox run -- 'addlicense -check -l apache -c "MongoDB Inc" -ignore "**/*.md" -ignore "**/*.yaml" -ignore "**/*.yml" -ignore "**/*.nix" -ignore ".devbox/**" .'
74+
run: devbox run -- 'mage addlicense'

tools/crd2go/devbox.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"gh@latest",
99
"gci@latest",
1010
"golangci-lint@latest",
11-
"addlicense@latest"
11+
"addlicense@latest",
12+
"mage@latest",
1213
],
1314
"shell": {
1415
"init_hook": [

tools/crd2go/devbox.lock

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,54 @@
365365
}
366366
}
367367
},
368+
"mage@latest": {
369+
"last_modified": "2025-07-28T17:09:23Z",
370+
"resolved": "github:NixOS/nixpkgs/648f70160c03151bc2121d179291337ad6bc564b#mage",
371+
"source": "devbox-search",
372+
"version": "1.15.0",
373+
"systems": {
374+
"aarch64-darwin": {
375+
"outputs": [
376+
{
377+
"name": "out",
378+
"path": "/nix/store/ckcxxf6r85zndgx4rh3x3g570sysczf2-mage-1.15.0",
379+
"default": true
380+
}
381+
],
382+
"store_path": "/nix/store/ckcxxf6r85zndgx4rh3x3g570sysczf2-mage-1.15.0"
383+
},
384+
"aarch64-linux": {
385+
"outputs": [
386+
{
387+
"name": "out",
388+
"path": "/nix/store/1xln1pdk907lxm25dpdvx1xmp1rh43pd-mage-1.15.0",
389+
"default": true
390+
}
391+
],
392+
"store_path": "/nix/store/1xln1pdk907lxm25dpdvx1xmp1rh43pd-mage-1.15.0"
393+
},
394+
"x86_64-darwin": {
395+
"outputs": [
396+
{
397+
"name": "out",
398+
"path": "/nix/store/gwwj202lbp3smjdmnff18xr4fm439sn4-mage-1.15.0",
399+
"default": true
400+
}
401+
],
402+
"store_path": "/nix/store/gwwj202lbp3smjdmnff18xr4fm439sn4-mage-1.15.0"
403+
},
404+
"x86_64-linux": {
405+
"outputs": [
406+
{
407+
"name": "out",
408+
"path": "/nix/store/8jyy6nrmi505iczgi7d19gddqk00qqc9-mage-1.15.0",
409+
"default": true
410+
}
411+
],
412+
"store_path": "/nix/store/8jyy6nrmi505iczgi7d19gddqk00qqc9-mage-1.15.0"
413+
}
414+
}
415+
},
368416
"vscodium@latest": {
369417
"last_modified": "2025-04-07T13:23:10Z",
370418
"resolved": "github:NixOS/nixpkgs/b0b4b5f8f621bfe213b8b21694bab52ecfcbf30b#vscodium",

tools/crd2go/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.24.3
44

55
require (
66
github.com/dave/jennifer v1.7.1
7+
github.com/magefile/mage v1.15.0
78
github.com/stretchr/testify v1.10.0
89
golang.org/x/text v0.24.0
910
gopkg.in/yaml.v3 v3.0.1

tools/crd2go/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
2929
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
3030
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
3131
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
32+
github.com/magefile/mage v1.15.0 h1:BvGheCMAsG3bWUDbZ8AyXXpCNwU9u5CB6sM+HNb9HYg=
33+
github.com/magefile/mage v1.15.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
3234
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
3335
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
3436
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=

tools/crd2go/magefile.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright 2025 MongoDB Inc
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
//go:build mage
17+
18+
package main
19+
20+
import (
21+
"fmt"
22+
23+
"github.com/magefile/mage/mg"
24+
"github.com/magefile/mage/sh"
25+
)
26+
27+
// CI runs all linting and validation checks.
28+
func CI() {
29+
mg.SerialDeps(Addlicense, GCI)
30+
}
31+
32+
// Addlicense runs the addlicense check to ensure source files have license headers.
33+
func Addlicense() error {
34+
fmt.Println("Running license header check...")
35+
36+
// sh.RunV runs the command verbosely (streaming output)
37+
// and returns an error if the command fails.
38+
return sh.RunV("addlicense",
39+
"-check",
40+
"-l", "apache",
41+
"-c", "MongoDB Inc",
42+
"-ignore", "**/*.md",
43+
"-ignore", "**/*.yaml",
44+
"-ignore", "**/*.yml",
45+
"-ignore", "**/*.nix",
46+
"-ignore", ".devbox/**",
47+
"-ignore", "magefile.go",
48+
".",
49+
)
50+
}
51+
52+
// GCI runs gci to check that Go import orders are correct.
53+
func GCI() error {
54+
fmt.Println("🧹 Formatting Go imports...")
55+
if err := sh.RunV(
56+
"gci", "write",
57+
"--skip-generated",
58+
"-s", "standard",
59+
"-s", "default",
60+
"-s", "localmodule",
61+
".",
62+
); err != nil {
63+
return fmt.Errorf("gci write command failed: %w", err)
64+
}
65+
66+
fmt.Println("🔍 Checking for changes...")
67+
if err := sh.Run("git", "diff-index", "--quiet", "HEAD", "--"); err != nil {
68+
fmt.Println("❗️ Go files were not correctly formatted. The following files have changes:")
69+
sh.RunV("git", "diff-index", "--name-only", "HEAD")
70+
return fmt.Errorf("please run 'mage gci' and commit the changes")
71+
}
72+
73+
fmt.Println("✅ Go imports are correctly formatted.")
74+
return nil
75+
}

0 commit comments

Comments
 (0)