Skip to content

Commit 1fa4169

Browse files
camilamacedo86tmshortCopilotpedjak
authored
🌱 Add a Makefile target and start running the API diff linter as part of CI (#2411)
* Add a Makefile target and start running the API diff linter as part of CI. * Update hack/api-lint-diff/run.sh Co-authored-by: Todd Short <tmshort@users.noreply.github.com> * Run for pull requests only * Update .github/workflows/api-diff-lint.yaml Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update .github/workflows/api-diff-lint.yaml Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update hack/api-lint-diff/run.sh Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update Makefile Co-authored-by: Predrag Knezevic <pedjak@gmail.com> * Apply suggestion from to improve branch check * Apply suggestion from @camilamacedo86 By Copilot: When sourcing .bingo/variables.env, the file contains GOBIN=${GOBIN:=$(go env GOBIN)} which requires the go command to be available. While this works in CI after the setup-go step, the script might fail if run locally without Go in PATH. Consider adding a check that Go is available before sourcing, or handle the source operation with error checking using source .bingo/variables.env 2>/dev/null || true and validate the variable afterward. * Update .github/workflows/api-diff-lint.yaml Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Todd Short <tmshort@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Predrag Knezevic <pedjak@gmail.com>
1 parent 9d8fda0 commit 1fa4169

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: api-diff-lint
2+
3+
on:
4+
pull_request:
5+
6+
jobs:
7+
lint-api-diff:
8+
runs-on: ubuntu-latest
9+
permissions:
10+
pull-requests: read
11+
steps:
12+
- uses: actions/checkout@v6
13+
with:
14+
fetch-depth: 0 # Fetch all history for all branches (needed for API diff)
15+
16+
- uses: actions/setup-go@v6
17+
with:
18+
go-version-file: go.mod
19+
20+
- name: Run API diff linting checks
21+
id: lint-api-diff
22+
continue-on-error: true
23+
run: make lint-api-diff
24+
25+
- name: Check for override label
26+
if: ${{ steps.lint-api-diff.outcome == 'failure' }}
27+
run: |
28+
if gh api repos/$OWNER/$REPO/pulls/$PR --jq '.labels.[].name' | grep -q "${OVERRIDE_LABEL}"; then
29+
echo "Found ${OVERRIDE_LABEL} label, overriding failed results."
30+
exit 0
31+
else
32+
echo "No ${OVERRIDE_LABEL} label found, failing the job."
33+
exit 1
34+
fi
35+
env:
36+
GH_TOKEN: ${{ github.token }}
37+
OWNER: ${{ github.repository_owner }}
38+
REPO: ${{ github.event.repository.name }}
39+
PR: ${{ github.event.pull_request.number }}
40+
OVERRIDE_LABEL: "api-diff-lint-override"
41+

‎Makefile‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ custom-linter-build: #EXHELP Build custom linter
149149
lint-custom: custom-linter-build #EXHELP Call custom linter for the project
150150
go vet -tags=$(GO_BUILD_TAGS) -vettool=./bin/custom-linter ./...
151151

152+
.PHONY: lint-api-diff
153+
lint-api-diff: $(GOLANGCI_LINT) #HELP Validate API changes using kube-api-linter with diff-aware analysis
154+
hack/api-lint-diff/run.sh
155+
152156
.PHONY: k8s-pin
153157
k8s-pin: #EXHELP Pin k8s staging modules based on k8s.io/kubernetes version (in go.mod or from K8S_IO_K8S_VERSION env var) and run go mod tidy.
154158
K8S_IO_K8S_VERSION='$(K8S_IO_K8S_VERSION)' go run hack/tools/k8smaintainer/main.go

‎hack/api-lint-diff/run.sh‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,16 @@ check_linter_support() {
196196

197197
# Find golangci-lint binary
198198
find_golangci_lint() {
199-
# Check for custom build first
199+
# Check if variables.env exists and extract golangci-lint path
200+
if [[ -f ".bingo/variables.env" ]]; then
201+
source .bingo/variables.env 2>/dev/null || true
202+
if [[ -n "${GOLANGCI_LINT}" && -f "${GOLANGCI_LINT}" ]]; then
203+
echo "${GOLANGCI_LINT}"
204+
return 0
205+
fi
206+
fi
207+
208+
# Check for custom build
200209
if [[ -f ".bingo/golangci-lint" ]]; then
201210
echo ".bingo/golangci-lint"
202211
return 0
@@ -216,6 +225,7 @@ find_golangci_lint() {
216225

217226
echo -e "${RED}Error: golangci-lint not found.${NC}" >&2
218227
echo -e "${RED}Searched for:${NC}" >&2
228+
echo -e " - .bingo/variables.env (bingo-managed variables for GOLANGCI_LINT)" >&2
219229
echo -e " - .bingo/golangci-lint" >&2
220230
echo -e " - bin/golangci-lint" >&2
221231
echo -e " - golangci-lint on your \$PATH" >&2
@@ -482,6 +492,25 @@ main() {
482492
# Create temporary config
483493
create_temp_config
484494

495+
# Ensure baseline branch is available (important for CI environments like GitHub Actions)
496+
if ! git rev-parse --verify "${BASELINE_BRANCH}" &> /dev/null; then
497+
echo -e "${YELLOW}Baseline branch '${BASELINE_BRANCH}' not found locally. Fetching from origin...${NC}" >&2
498+
499+
# Fetch the baseline branch from origin
500+
if ! git fetch origin "${BASELINE_BRANCH}:${BASELINE_BRANCH}" 2>&1; then
501+
# If direct fetch fails, try fetching with remote tracking
502+
if ! git fetch origin "${BASELINE_BRANCH}" 2>&1; then
503+
echo -e "${RED}Error: Failed to fetch baseline branch '${BASELINE_BRANCH}' from origin${NC}" >&2
504+
echo -e "${RED}Please ensure the branch exists in the remote repository.${NC}" >&2
505+
exit 1
506+
fi
507+
# Use the remote tracking branch, adding 'origin/' only if not already present
508+
if [[ "${BASELINE_BRANCH}" != origin/* ]]; then
509+
BASELINE_BRANCH="origin/${BASELINE_BRANCH}"
510+
fi
511+
fi
512+
fi
513+
485514
# Get changed files
486515
get_changed_files > "${TEMP_DIR}/changed_files.txt"
487516

0 commit comments

Comments
 (0)