Skip to content

Commit 44e8651

Browse files
Add a Makefile target and start running the API diff linter as part of CI.
1 parent b871e7e commit 44e8651

File tree

4 files changed

+100
-1
lines changed

4 files changed

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

.github/workflows/sanity.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ jobs:
1313
runs-on: ubuntu-latest
1414
steps:
1515
- uses: actions/checkout@v6
16+
with:
17+
fetch-depth: 0
1618

1719
- uses: actions/setup-go@v6
1820
with:
@@ -23,6 +25,8 @@ jobs:
2325
runs-on: ubuntu-latest
2426
steps:
2527
- uses: actions/checkout@v6
28+
with:
29+
fetch-depth: 0 # Fetch all history for all branches (needed for API diff)
2630

2731
- uses: actions/setup-go@v6
2832
with:

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+
bash 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: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,36 @@ check_linter_support() {
196196

197197
# Find golangci-lint binary
198198
find_golangci_lint() {
199-
# Check for custom build first
199+
# Check if Variables.mk exists and extract golangci-lint path
200+
if [[ -f ".bingo/Variables.mk" ]]; then
201+
# Extract version from GOLANGCI_LINT variable
202+
# Format: GOLANGCI_LINT := $(GOBIN)/golangci-lint-v2.7.2
203+
local version
204+
version=$(grep '^GOLANGCI_LINT' .bingo/Variables.mk | sed -E 's/.*golangci-lint-(v[0-9]+\.[0-9]+\.[0-9]+).*/\1/')
205+
206+
if [[ -n "${version}" ]]; then
207+
# Use go env to get the actual GOBIN/GOPATH
208+
local gobin
209+
gobin=$(go env GOBIN)
210+
211+
# If GOBIN is empty, use GOPATH/bin
212+
if [[ -z "${gobin}" ]]; then
213+
local gopath
214+
gopath=$(go env GOPATH)
215+
# Take first entry if GOPATH has multiple paths (colon-separated)
216+
gobin="${gopath%%:*}/bin"
217+
fi
218+
219+
# Check if the versioned binary exists
220+
local bingo_path="${gobin}/golangci-lint-${version}"
221+
if [[ -f "${bingo_path}" ]]; then
222+
echo "${bingo_path}"
223+
return 0
224+
fi
225+
fi
226+
fi
227+
228+
# Check for custom build
200229
if [[ -f ".bingo/golangci-lint" ]]; then
201230
echo ".bingo/golangci-lint"
202231
return 0
@@ -216,6 +245,7 @@ find_golangci_lint() {
216245

217246
echo -e "${RED}Error: golangci-lint not found.${NC}" >&2
218247
echo -e "${RED}Searched for:${NC}" >&2
248+
echo -e " - .bingo/Variables.mk (bingo-managed versioned binary)" >&2
219249
echo -e " - .bingo/golangci-lint" >&2
220250
echo -e " - bin/golangci-lint" >&2
221251
echo -e " - golangci-lint on your \$PATH" >&2
@@ -482,6 +512,23 @@ main() {
482512
# Create temporary config
483513
create_temp_config
484514

515+
# Ensure baseline branch is available (important for CI environments like GitHub Actions)
516+
if ! git rev-parse --verify "${BASELINE_BRANCH}" &> /dev/null; then
517+
echo -e "${YELLOW}Baseline branch '${BASELINE_BRANCH}' not found locally. Fetching from origin...${NC}" >&2
518+
519+
# Fetch the baseline branch from origin
520+
if ! git fetch origin "${BASELINE_BRANCH}:${BASELINE_BRANCH}" 2>&1; then
521+
# If direct fetch fails, try fetching with remote tracking
522+
if ! git fetch origin "${BASELINE_BRANCH}" 2>&1; then
523+
echo -e "${RED}Error: Failed to fetch baseline branch '${BASELINE_BRANCH}' from origin${NC}" >&2
524+
echo -e "${RED}Please ensure the branch exists in the remote repository.${NC}" >&2
525+
exit 1
526+
fi
527+
# Use the remote tracking branch
528+
BASELINE_BRANCH="origin/${BASELINE_BRANCH}"
529+
fi
530+
fi
531+
485532
# Get changed files
486533
get_changed_files > "${TEMP_DIR}/changed_files.txt"
487534

0 commit comments

Comments
 (0)