Skip to content

Commit 498fbcf

Browse files
authored
chore: Add make fix and make verify and check go fix in CI (#1645)
1 parent f6d1f2d commit 498fbcf

88 files changed

Lines changed: 479 additions & 572 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.githooks/pre-commit

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,9 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
STAGED_GO_FILES=$(git diff --cached --name-only | grep ".go$")
18-
19-
echo "==> Formatting changed go files... $STAGED_GO_FILES"
20-
for FILE in ${STAGED_GO_FILES}; do
21-
gofmt -w -s "${FILE}"
22-
goimports -w "${FILE}"
23-
git add "${FILE}"
24-
done
25-
26-
echo "==> Formatting changed sh files..."
27-
STAGED_SH_FILES=$(git diff --cached --name-only | grep ".sh$")
28-
for FILE in ${STAGED_SH_FILES}; do
29-
shfmt -l -w "${FILE}"
30-
git add "${FILE}"
31-
done
32-
33-
echo "==> Formatting changed JSON files..."
34-
STAGED_JSON_FILES=$(git diff --cached --name-only | grep ".json$")
35-
for FILE in ${STAGED_JSON_FILES}; do
36-
prettyFile=$(jq . "${FILE}")
37-
echo "${prettyFile}" > "${FILE}"
38-
git add "${FILE}"
39-
done
40-
41-
echo "==> Linting GitHub Actions..."
42-
STAGED_ACTION_FILES=$(git diff --name-only | grep -E "\.github/workflows/.*(\.yaml|\.yml)$")
43-
for FILE in ${STAGED_ACTION_FILES}; do
44-
actionlint "${FILE}" -color -verbose
45-
done
46-
47-
echo "==> Done..."
17+
set -euo pipefail
4818

19+
STAGED_GO_FILES=$(git diff --cached --name-only --diff-filter=ACMR | grep '\.go$' | tr '\n' ' ' || true)
20+
if [[ -n "${STAGED_GO_FILES}" ]]; then
21+
make verify files="${STAGED_GO_FILES}"
22+
fi

.github/pull_request_template.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ We're here to help! This is simply a reminder of what we are going to look for b
5151
- [ ] I have added tests that prove my fix is effective or that my feature works
5252
- [ ] I have checked that this change does not generate any credentials and that **they are NOT accidentally logged anywhere**.
5353
- [ ] I have added any necessary documentation (if appropriate)
54-
- [ ] I have run `make fmt` and formatted my code
54+
- [ ] I have run `make fix` and verified my code
5555
- [ ] For CFN Resources: I have released by changes in the private registry and proved by change
5656
works in Atlas
5757

.github/workflows/code-health.yaml

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,12 @@ jobs:
99
build:
1010
runs-on: ubuntu-latest
1111
steps:
12-
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
13-
- name: Set up Go
14-
uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c
15-
with:
16-
go-version-file: 'cfn-resources/go.mod'
17-
- name: Build
18-
run: |
19-
cd cfn-resources
20-
go build -v ./...
12+
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
13+
- uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c
14+
with:
15+
go-version-file: 'cfn-resources/go.mod'
16+
- name: Setup and verify
17+
run: make tools verify
2118
mock-generation:
2219
runs-on: ubuntu-latest
2320
steps:
@@ -33,32 +30,28 @@ jobs:
3330
run: |
3431
FILES=$(git ls-files -o -m --directory --exclude-standard --no-empty-directory)
3532
LINES=$(echo "$FILES" | awk 'NF' | wc -l)
36-
33+
3734
if [ "$LINES" -ne 0 ]; then
3835
echo "Detected files that need to be committed:"
3936
echo "$FILES" | while IFS= read -r line; do echo " $line"; done
4037
echo ""
4138
echo "Mock skeletons are not up-to-date, you may have forgotten to run mockery before committing your changes."
4239
exit 1
4340
fi
44-
lint:
41+
github-linters:
4542
runs-on: ubuntu-latest
4643
steps:
4744
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
4845
- uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c
4946
with:
5047
go-version-file: 'cfn-resources/go.mod'
51-
cache: false # see https://github.com/golangci/golangci-lint-action/issues/807
52-
- name: golangci-lint
53-
uses: golangci/golangci-lint-action@1e7e51e771db61008b38414a730f564565cf7c20
54-
with:
55-
version: v2.11.4 # Also update GOLANGCI_VERSION variable in Makefile when updating this version
56-
working-directory: cfn-resources
5748
- name: actionlint
58-
run: |
59-
make tools
60-
actionlint -verbose -color
49+
run: |
50+
make tools
51+
actionlint -verbose -color
6152
shell: bash
53+
- name: Run ShellCheck
54+
uses: bewuethr/shellcheck-action@80bac2daa9fcf95d648200a793d00060857e6dc4
6255
check-copyright:
6356
runs-on: ubuntu-latest
6457
steps:
@@ -79,13 +72,6 @@ jobs:
7972
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
8073
- name: 'Dependency Review'
8174
uses: actions/dependency-review-action@2031cfc080254a8a887f58cffee85186f0e49e48
82-
shellcheck:
83-
name: shellcheck
84-
runs-on: ubuntu-latest
85-
steps:
86-
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
87-
- name: Run ShellCheck
88-
uses: bewuethr/shellcheck-action@80bac2daa9fcf95d648200a793d00060857e6dc4
8975
cfn-lint:
9076
runs-on: ubuntu-latest
9177
steps:

CONTRIBUTING.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,20 @@ make update-atlas-sdk
9292
## Prerequisites
9393
- [AWS CloudFormation CLI](https://github.com/aws-cloudformation/cloudformation-cli)
9494
- (Optional - only need if building from source) [AWS CloudFormation CLI Go Plugin](https://github.com/aws-cloudformation/cloudformation-cli-go-plugin/) > v1.0
95-
- (Optional - only need if building from source) [Go](https://golang.org/doc/install) v1.23
95+
- (Optional - only need if building from source) [Go](https://golang.org/doc/install) (version pinned in [`cfn-resources/go.mod`](cfn-resources/go.mod))
96+
97+
## Development Setup
98+
99+
The Go module lives in `cfn-resources/`, but `make` targets are run from the repository root and `cd` into `cfn-resources/` as needed.
100+
101+
- Run `make tools` once to install the dev tools (`golangci-lint`, `goimports`, `shfmt`, `actionlint`, `mockery`, etc.). Tools install into `$(go env GOPATH)/bin`; the Makefile prepends it to `PATH` for subsequent targets.
102+
- Run `make link-git-hooks` to install the pre-commit hook. The hook calls `make verify files=$STAGED_GO_FILES` against staged Go files.
103+
- Run `make fix` to apply formatting (`gofmt`, `goimports`), `golangci-lint --fix`, `go mod tidy`, and `go fix ./...`. This is the default `make` target.
104+
- Run `make verify` to run the same checks read-only. CI runs this; missing `go fix` rewrites or formatter drift fail the build. Pass `files="path/one.go path/two.go"` to scope the checks.
105+
- Run `make unit-test` to run the Go unit tests (excludes the e2e packages).
106+
- Run `make generate-mocks` after changing a mocked interface (output lands in `cfn-resources/testutil/mocksvc`).
107+
108+
The `golangci-lint` version is pinned in the `Makefile` (`GOLANGCI_VERSION`) and is the single source of truth; the CI workflow picks it up via `make tools`.
96109

97110
## Testing the Provider
98111
Please see README for each resource for details on unit and integrated AWS testing.

Makefile

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
tags="logging callback metrics scheduler"
2-
cgo=0
2+
cgo=0
33
goos=linux
44
goarch=amd64
55

@@ -8,7 +8,12 @@ ldXflags=github.com/mongodb/mongodbatlas-cloudformation-resources/util.defaultLo
88
ldXflagsD=github.com/mongodb/mongodbatlas-cloudformation-resources/util.defaultLogLevel=debug
99

1010
MOCKERY_VERSION=v3.5.3
11-
GOLANGCI_VERSION=v2.11.4 # Also update golangci-lint GH action in code-health.yaml when updating this version
11+
GOLANGCI_VERSION=v2.11.4
12+
13+
export PATH := $(shell go env GOPATH)/bin:$(PATH)
14+
export SHELL := env PATH=$(PATH) /bin/bash
15+
16+
default: fix
1217

1318
.PHONY: submit
1419
submit:
@@ -18,9 +23,33 @@ submit:
1823
test:
1924
(cd cfn-resources && ./cfn-testing-helper.sh $(filter-out $@,$(MAKECMDGOALS)))
2025

21-
.PHONY: fmt
22-
fmt: ## Format changed go and sh
23-
@scripts/fmt.sh
26+
.PHONY: fix
27+
fix: ## Format, lint-fix, tidy, and apply go fix
28+
(cd cfn-resources && gofmt -s -w .)
29+
(cd cfn-resources && goimports -w .)
30+
(cd cfn-resources && golangci-lint run --fix --timeout 5m)
31+
(cd cfn-resources && go mod tidy)
32+
(cd cfn-resources && go fix ./...)
33+
34+
.PHONY: verify
35+
verify: ## Verify Go code without modifying files. Usage: make verify [files="file1.go file2.go"]
36+
ifdef files
37+
$(eval files_rel := $(patsubst cfn-resources/%,%,$(files)))
38+
@bad_fmt=$$(cd cfn-resources && gofmt -l -s $(files_rel)); \
39+
if [ -n "$$bad_fmt" ]; then echo "ERROR: gofmt issues:"; echo "$$bad_fmt"; exit 1; fi
40+
@bad_imports=$$(cd cfn-resources && goimports -l $(files_rel)); \
41+
if [ -n "$$bad_imports" ]; then echo "ERROR: goimports issues:"; echo "$$bad_imports"; exit 1; fi
42+
(cd cfn-resources && golangci-lint run --timeout 5m $(addsuffix ...,$(sort $(dir $(files_rel)))))
43+
(cd cfn-resources && go fix -diff $(addprefix ./,$(addsuffix ...,$(sort $(dir $(files_rel))))))
44+
else
45+
@bad_fmt=$$(cd cfn-resources && gofmt -l -s .); \
46+
if [ -n "$$bad_fmt" ]; then echo "ERROR: gofmt issues:"; echo "$$bad_fmt"; exit 1; fi
47+
@bad_imports=$$(cd cfn-resources && goimports -l .); \
48+
if [ -n "$$bad_imports" ]; then echo "ERROR: goimports issues:"; echo "$$bad_imports"; exit 1; fi
49+
(cd cfn-resources && golangci-lint run --timeout 5m)
50+
(cd cfn-resources && go mod tidy -diff)
51+
(cd cfn-resources && go fix -diff ./...)
52+
endif
2453

2554
.PHONY: tools
2655
tools: ## Install dev tools
@@ -41,10 +70,6 @@ link-git-hooks: ## Install git hooks
4170
find .git/hooks -type l -exec rm {} \;
4271
find .githooks -type f -exec ln -sf ../../{} .git/hooks/ \;
4372

44-
.PHONY: lint
45-
lint: ## Run linter
46-
@scripts/lint.sh
47-
4873
.PHONY: unit-test
4974
unit-test:
5075
(cd cfn-resources && go test $$(go list ./... | grep -v /e2e))
@@ -60,7 +85,7 @@ generate-mocks: # uses mockery to generate mocks in folder `cfn-resources/testut
6085
# resulting file placed in cfn-resources/resource-versions.md
6186
# aws regions must defined by using AWS_REGIONS env variable, example: `export AWS_REGIONS=af-south-1,ap-east-1`
6287
.PHONY: generate-resource-versions-markdown
63-
generate-resource-versions-markdown:
88+
generate-resource-versions-markdown:
6489
(cd cfn-resources && go run tool/markdown-generator/*.go)
6590

6691
.PHONY: gen-sbom-and-ssdlc-report

cfn-resources/access-list-api-key/cmd/resource/resource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ func List(req handler.Request, prevModel *Model, currentModel *Model) (handler.P
268268
return handleError(response, LIST, err)
269269
}
270270

271-
accessListModels := make([]interface{}, 0)
271+
accessListModels := make([]any, 0)
272272
apiResults := accessListResponse.GetResults()
273273
for i := range apiResults {
274274
l := apiResults[i]

cfn-resources/alert-configuration/cmd/resource/resource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ func expandAlertConfigurationNotification(notificationList []NotificationView) (
319319
DatadogRegion: notificationList[ind].DatadogRegion,
320320
EmailAddress: notificationList[ind].EmailAddress,
321321
EmailEnabled: notificationList[ind].EmailEnabled,
322-
IntervalMin: util.Pointer(int(*notificationList[ind].IntervalMin)),
322+
IntervalMin: new(int(*notificationList[ind].IntervalMin)),
323323
MicrosoftTeamsWebhookUrl: notificationList[ind].MicrosoftTeamsWebhookUrl,
324324
MobileNumber: notificationList[ind].MobileNumber,
325325
OpsGenieApiKey: notificationList[ind].OpsGenieApiKey,

cfn-resources/api-key/cmd/resource/resource.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ func List(req handler.Request, prevModel *Model, currentModel *Model) (handler.P
263263
}
264264

265265
apiKeyList := pagedAPIKeysList.GetResults()
266-
apiKeys := make([]interface{}, len(apiKeyList))
266+
apiKeys := make([]any, len(apiKeyList))
267267
for i := range apiKeyList {
268268
var model Model
269269
model.readAPIKeyDetails(apiKeyList[i])
@@ -334,7 +334,7 @@ func updateOrgKeyProjectRoles(projectAssignment ProjectAssignment, client *util.
334334
return assignAPIRequest.Execute()
335335
}
336336

337-
func unAssignProjectFromOrgKey(projectAssignment ProjectAssignment, client *util.MongoDBClient, orgKeyID *string) (map[string]interface{}, *http.Response, error) {
337+
func unAssignProjectFromOrgKey(projectAssignment ProjectAssignment, client *util.MongoDBClient, orgKeyID *string) (map[string]any, *http.Response, error) {
338338
unAssignAPIRequest := client.Atlas20231115014.ProgrammaticAPIKeysApi.RemoveProjectApiKey(
339339
context.Background(),
340340
*projectAssignment.ProjectId,
@@ -344,7 +344,7 @@ func unAssignProjectFromOrgKey(projectAssignment ProjectAssignment, client *util
344344
return unAssignAPIRequest.Execute()
345345
}
346346

347-
func updateProjectAssignments(atlasClient *util.MongoDBClient, currentModel *Model, existingModel *Model) (result interface{}, response *http.Response, err error) {
347+
func updateProjectAssignments(atlasClient *util.MongoDBClient, currentModel *Model, existingModel *Model) (result any, response *http.Response, err error) {
348348
// update projectAssignments
349349
newAssignments, updateAssignments, removeAssignments := getChangesInProjectAssignments(currentModel.ProjectAssignments, existingModel.ProjectAssignments)
350350

@@ -438,7 +438,7 @@ func areStringArraysEqualIgnoreOrder(arr1, arr2 []string) bool {
438438
copy(sortedArr2, arr2)
439439
sort.Strings(sortedArr2)
440440

441-
for i := 0; i < len(sortedArr1); i++ {
441+
for i := range sortedArr1 {
442442
if sortedArr1[i] != sortedArr2[i] {
443443
return false
444444
}

cfn-resources/autogen/main.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"flag"
2020
"fmt"
2121
"os"
22+
"strings"
2223
"unicode"
2324

2425
"github.com/dave/jennifer/jen"
@@ -288,11 +289,11 @@ func getLiteralsWithKeys(arr []string) []jen.Code {
288289
}
289290

290291
func prepareModel(arr []string) string {
291-
str := ""
292+
var str strings.Builder
292293
for _, p := range arr {
293-
str += "\t" + p + ":" + "currentModel." + p + ",\n"
294+
str.WriteString("\t" + p + ":" + "currentModel." + p + ",\n")
294295
}
295-
return str
296+
return str.String()
296297
}
297298
func capitalize(key string) string {
298299
r := []rune(key)

cfn-resources/autogen/schema-gen/main.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"errors"
2121
"fmt"
2222
"io"
23+
"maps"
2324
"net/http"
2425
"os"
2526
"path/filepath"
@@ -242,13 +243,13 @@ func main() {
242243
<-reqDone
243244
}
244245

245-
func sortProperties[V any](properties map[string]V) (props map[string]interface{}) {
246+
func sortProperties[V any](properties map[string]V) (props map[string]any) {
246247
var propertyNames []string
247248
for name := range properties {
248249
propertyNames = append(propertyNames, name)
249250
}
250251
sort.Strings(propertyNames)
251-
props = make(map[string]interface{}, len(properties))
252+
props = make(map[string]any, len(properties))
252253
for _, name := range propertyNames {
253254
props[name] = properties[name]
254255
}
@@ -705,9 +706,7 @@ func mergeDefinitionMaps(map1 map[string]Definitions, map2 map[string]Definition
705706
if map2 == nil {
706707
return map1
707708
}
708-
for k, v := range map2 {
709-
map1[k] = v
710-
}
709+
maps.Copy(map1, map2)
711710
return map1
712711
}
713712

0 commit comments

Comments
 (0)