Skip to content

Commit 770dfe8

Browse files
committed
BUILD/MINOR: ci: add job to detect new go.mod dependencies in MRs
Add the check-dependencies job to the GitLab CI lint stage to automatically detect when new Go dependencies are introduced in a merge request. The job compares the go.mod file against the target branch and generates a JUnit report. If new dependencies are found, the report is marked as failed to alert reviewers. This ensures external package additions are highly visible during code review, aiding in security and maintenance.
1 parent 9e0bdb6 commit 770dfe8

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

.gitlab/lint.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,48 @@ commit-policy:
9797
- junit-report.xml
9898
reports:
9999
junit: junit-report.xml
100+
check-dependencies:
101+
stage: lint
102+
allow_failure: true
103+
needs: []
104+
rules:
105+
- if: $CI_PIPELINE_SOURCE == 'merge_request_event'
106+
image:
107+
name: $HAPROXY_REGISTRY_GO/golang:latest-alpine
108+
entrypoint: [""]
109+
tags:
110+
- go
111+
script:
112+
- |
113+
git fetch origin ${CI_MERGE_REQUEST_TARGET_BRANCH_NAME}
114+
# skip check if all go.mod changes are from Gopher Bot
115+
BOT_COMMITS=$(git log --author="noreply-gopher@haproxy.com" --format='%H' origin/${CI_MERGE_REQUEST_TARGET_BRANCH_NAME}..HEAD -- go.mod || true)
116+
ALL_COMMITS=$(git log --format='%H' origin/${CI_MERGE_REQUEST_TARGET_BRANCH_NAME}..HEAD -- go.mod || true)
117+
if [ -n "$ALL_COMMITS" ] && [ "$BOT_COMMITS" = "$ALL_COMMITS" ]; then
118+
echo "All go.mod changes are from Gopher Bot, skipping."
119+
junit-report add --status=ok --file="go.mod" --message="go.mod changes from Gopher Bot, skipped" --description="go.mod dependency check skipped"
120+
exit 0
121+
fi
122+
NEW_DEPS=$(git diff origin/${CI_MERGE_REQUEST_TARGET_BRANCH_NAME}...HEAD -- go.mod \
123+
| grep -E '^\+\s' \
124+
| grep -v '^\+\+\+' \
125+
| grep -v '^\+\s*\/\/' \
126+
|| true)
127+
if [ -n "$NEW_DEPS" ]; then
128+
echo "New dependencies detected in go.mod:"
129+
echo "$NEW_DEPS"
130+
junit-report add --status=failed --file="go.mod" --message="new dependencies added to go.mod" --description="$(printf "The following dependencies were added:\n\n%s" "$NEW_DEPS")"
131+
exit 1
132+
else
133+
echo "No new dependencies added to go.mod."
134+
junit-report add --status=ok --file="go.mod" --message="no new dependencies added to go.mod" --description="go.mod dependency check passed"
135+
fi
136+
artifacts:
137+
when: on_failure
138+
paths:
139+
- junit-report.xml
140+
reports:
141+
junit: junit-report.xml
100142
check-committer:
101143
stage: lint
102144
allow_failure: true

0 commit comments

Comments
 (0)