Skip to content

Commit c74dbd5

Browse files
authored
ci: Add formatting pre-submit check (#3654)
# Summary Adds a formatting pre-submit check that runs `make format` and checks for file differences. The results of running `make format` are not included in this PR for readability. So the pre-submit will depend on #3655 and probably some more formatting before the check will pass. Added a `make shfmt` formatting check for bash scripts. `autogen` (for adding license headers) is now vendored in the `third_party` directory for convenience sake. `markdown-toc` and `autogen` are now run by `make format`. The formatting pre-submit now runs these as well so they don't have separate pre-submit jobs. We will need to adjust the required checks on branch protection to remove "markdown-toc" and "autogen" jobs, and also add the "formatting" job. `CONTRIBUTING.md` was updated to include some more information about coding style and conventions. ## Testing Process 1. Run `make format` 2. Verify the formatting results ## Checklist - [x] Review the contributing [guidelines](https://github.com/slsa-framework/slsa-github-generator/blob/main/CONTRIBUTING.md) - [x] Add a reference to related issues in the PR description. - [x] Update documentation if applicable. - [x] Add unit tests if applicable. - [x] Add changes to the [CHANGELOG](https://github.com/slsa-framework/slsa-github-generator/blob/main/CHANGELOG.md) if applicable. --------- Signed-off-by: Ian Lewis <[email protected]>
1 parent 80faa1f commit c74dbd5

39 files changed

+1909
-729
lines changed

.github/actions/generate-builder/builder-fetch.sh

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -29,46 +29,46 @@ PREFIX="refs/tags/"
2929

3030
# Extract version.
3131
if [[ "$BUILDER_REF" != "$PREFIX"* ]]; then
32-
echo "Invalid ref: $BUILDER_REF. Expected ref of the form refs/tags/vX.Y.Z"
33-
exit 2
32+
echo "Invalid ref: $BUILDER_REF. Expected ref of the form refs/tags/vX.Y.Z"
33+
exit 2
3434
fi
3535

3636
builder_tag="${BUILDER_REF#"$PREFIX"}"
3737

3838
if [[ "$builder_tag" == "$(echo -n "$builder_tag" | grep -P '^[a-f\d]{40}$')" ]]; then
39-
echo "Builder referenced by hash: $builder_tag"
40-
echo "Resolving..."
41-
42-
release_tag=""
43-
44-
# List the releases and find the corresponding hash.
45-
release_list=$(gh release -R "$BUILDER_REPOSITORY" -L 50 list)
46-
while read -r line; do
47-
tag=$(echo "$line" | cut -f1)
48-
branch=$(gh release -R "$BUILDER_REPOSITORY" view "$tag" --json targetCommitish --jq '.targetCommitish')
49-
if [[ "$branch" != "main" ]]; then
50-
continue
51-
fi
52-
commit=$(gh api /repos/"$BUILDER_REPOSITORY"/git/ref/tags/"$tag" | jq -r '.object.sha')
53-
if [[ "$commit" == "$builder_tag" ]]; then
54-
release_tag="$tag"
55-
echo "Found tag $builder_tag match at tag $tag and commit $commit"
56-
break
57-
fi
58-
done <<<"$release_list"
59-
60-
if [[ -z "$release_tag" ]]; then
61-
echo "Tag not found for $builder_tag"
62-
exit 3
39+
echo "Builder referenced by hash: $builder_tag"
40+
echo "Resolving..."
41+
42+
release_tag=""
43+
44+
# List the releases and find the corresponding hash.
45+
release_list=$(gh release -R "$BUILDER_REPOSITORY" -L 50 list)
46+
while read -r line; do
47+
tag=$(echo "$line" | cut -f1)
48+
branch=$(gh release -R "$BUILDER_REPOSITORY" view "$tag" --json targetCommitish --jq '.targetCommitish')
49+
if [[ "$branch" != "main" ]]; then
50+
continue
6351
fi
52+
commit=$(gh api /repos/"$BUILDER_REPOSITORY"/git/ref/tags/"$tag" | jq -r '.object.sha')
53+
if [[ "$commit" == "$builder_tag" ]]; then
54+
release_tag="$tag"
55+
echo "Found tag $builder_tag match at tag $tag and commit $commit"
56+
break
57+
fi
58+
done <<<"$release_list"
59+
60+
if [[ -z "$release_tag" ]]; then
61+
echo "Tag not found for $builder_tag"
62+
exit 3
63+
fi
6464

65-
builder_tag="$release_tag"
65+
builder_tag="$release_tag"
6666
fi
6767

6868
if [[ "$builder_tag" != "$(echo -n "$builder_tag" | grep -oe '^v[1-9]\+\.[0-9]\+\.[0-9]\+\(-rc\.[0-9]\+\)\?$')" ]]; then
69-
echo "Invalid builder version: $builder_tag. Expected version of the form vX.Y.Z(-rc.A)"
70-
echo "For details see https://github.com/slsa-framework/slsa-github-generator/blob/main/README.md#referencing-slsa-builders-and-generators"
71-
exit 7
69+
echo "Invalid builder version: $builder_tag. Expected version of the form vX.Y.Z(-rc.A)"
70+
echo "For details see https://github.com/slsa-framework/slsa-github-generator/blob/main/README.md#referencing-slsa-builders-and-generators"
71+
exit 7
7272
fi
7373

7474
echo "Builder version: $builder_tag"
@@ -88,23 +88,23 @@ echo "verifier hash verification has passed"
8888
# If this is a pre-release, set SLSA_VERIFIER_TESTING
8989
pre_release=$(echo "${builder_tag#"v"}" | cut -s -d '-' -f2)
9090
if [ "${pre_release}" != "" ]; then
91-
export SLSA_VERIFIER_TESTING="true"
91+
export SLSA_VERIFIER_TESTING="true"
9292
fi
9393

9494
# Verify the provenance of the builder.
9595
chmod a+x "$VERIFIER_RELEASE_BINARY"
9696
./"$VERIFIER_RELEASE_BINARY" verify-artifact \
97-
--source-branch "main" \
98-
--source-tag "$builder_tag" \
99-
--provenance-path "$BUILDER_RELEASE_BINARY.intoto.jsonl" \
100-
--source-uri "github.com/$BUILDER_REPOSITORY" \
101-
"$BUILDER_RELEASE_BINARY" || exit 6
97+
--source-branch "main" \
98+
--source-tag "$builder_tag" \
99+
--provenance-path "$BUILDER_RELEASE_BINARY.intoto.jsonl" \
100+
--source-uri "github.com/$BUILDER_REPOSITORY" \
101+
"$BUILDER_RELEASE_BINARY" || exit 6
102102

103103
builder_commit=$(gh api /repos/"$BUILDER_REPOSITORY"/git/ref/tags/"$builder_tag" | jq -r '.object.sha')
104104
provenance_commit=$(jq -r '.payload' <"$BUILDER_RELEASE_BINARY.intoto.jsonl" | base64 -d | jq -r '.predicate.materials[0].digest.sha1')
105105
if [[ "$builder_commit" != "$provenance_commit" ]]; then
106-
echo "Builder commit sha $builder_commit != provenance material $provenance_commit"
107-
exit 5
106+
echo "Builder commit sha $builder_commit != provenance material $provenance_commit"
107+
exit 5
108108
fi
109109

110110
#TODO: verify the command

.github/actions/generate-builder/generate-builder.sh

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,25 @@
1717
set -euo pipefail
1818

1919
if [[ "$COMPILE_BUILDER" == true ]]; then
20-
echo "Building the builder with ref: $BUILDER_REF"
20+
echo "Building the builder with ref: $BUILDER_REF"
2121

22-
cd "$BUILDER_DIR"
23-
git checkout "$BUILDER_REF"
22+
cd "$BUILDER_DIR"
23+
git checkout "$BUILDER_REF"
2424

25-
#TODO(reproducible)
26-
go mod vendor
25+
#TODO(reproducible)
26+
go mod vendor
2727

28-
# https://go.dev/ref/mod#build-commands.
29-
go build -mod=vendor -o "$BUILDER_RELEASE_BINARY"
28+
# https://go.dev/ref/mod#build-commands.
29+
go build -mod=vendor -o "$BUILDER_RELEASE_BINARY"
3030

31-
cd -
31+
cd -
3232

33-
mv "$BUILDER_DIR/$BUILDER_RELEASE_BINARY" .
33+
mv "$BUILDER_DIR/$BUILDER_RELEASE_BINARY" .
3434

3535
else
36-
echo "Fetching the builder with ref: $BUILDER_REF"
36+
echo "Fetching the builder with ref: $BUILDER_REF"
3737

38-
./__BUILDER_CHECKOUT_DIR__/.github/actions/generate-builder/builder-fetch.sh
38+
./__BUILDER_CHECKOUT_DIR__/.github/actions/generate-builder/builder-fetch.sh
3939
fi
4040

4141
chmod u+x "$BUILDER_RELEASE_BINARY"

.github/workflows/pre-submit.lint.yml

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,56 @@ permissions:
2828
contents: read
2929

3030
jobs:
31-
markdownlint:
31+
formatting:
3232
runs-on: ubuntu-latest
3333
steps:
34-
- uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
35-
- uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
34+
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
35+
- uses: actions/setup-go@cdcb36043654635271a94b9a6d1392de5bb323a7 # v5.0.1
36+
with:
37+
go-version: "1.22.3"
38+
- uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d # v3.8.1
3639
with:
3740
node-version: 20
38-
- run: make markdownlint
3941

40-
markdown-toc:
41-
name: markdown-toc
42+
- name: Install gofumpt
43+
run: go install mvdan.cc/[email protected]
44+
45+
- name: Install shfmt
46+
env:
47+
SHFMT_VERSION: "3.8.0"
48+
SHFMT_CHECKSUM: "27b3c6f9d9592fc5b4856c341d1ff2c88856709b9e76469313642a1d7b558fe0"
49+
run: |
50+
set -euo pipefail
51+
52+
#Install golangci-lint
53+
curl -sSLo shfmt "https://github.com/mvdan/sh/releases/download/v${SHFMT_VERSION}/shfmt_v${SHFMT_VERSION}_linux_amd64"
54+
echo "shfmt checksum is $(sha256sum shfmt | awk '{ print $1 }')"
55+
echo "expected checksum is ${SHFMT_CHECKSUM}"
56+
echo "${SHFMT_CHECKSUM} shfmt" | sha256sum --strict --check --status || exit 1
57+
chmod +x shfmt
58+
mv shfmt /usr/local/bin
59+
60+
- run: make format
61+
- name: Check for unformatted files
62+
id: diff
63+
run: |
64+
set -euo pipefail
65+
if [ "$(git diff --ignore-space-at-eol . | wc -l)" -gt "0" ]; then
66+
echo "Detected uncommitted changes after formatting."
67+
echo "Run 'make format' to format files in your PR."
68+
echo "See status below:"
69+
git diff
70+
exit 1
71+
fi
72+
73+
markdownlint:
4274
runs-on: ubuntu-latest
4375
steps:
76+
- uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
4477
- uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
4578
with:
4679
node-version: 20
47-
- uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
48-
- run: make markdown-toc
49-
- name: markdown-toc
50-
run: ./.github/workflows/scripts/pre-submit.markdown/markdown-toc.sh
80+
- run: make markdownlint
5181

5282
golangci-lint:
5383
runs-on: ubuntu-latest
@@ -143,24 +173,3 @@ jobs:
143173
with:
144174
node-version: 20
145175
- run: make renovate-config-validator
146-
147-
autogen:
148-
runs-on: ubuntu-latest
149-
steps:
150-
- uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
151-
- uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
152-
with:
153-
repository: mbrukman/autogen
154-
ref: 9026b78e17573b5dda4bff79033c352443551dc5
155-
path: autogen
156-
- run: |
157-
echo "${GITHUB_WORKSPACE}/autogen" >> "${GITHUB_PATH}"
158-
- run: make autogen
159-
- name: check diff
160-
run: |
161-
set -euo pipefail
162-
if [ "$(GIT_PAGER="cat" git diff --ignore-space-at-eol | wc -l)" -gt "0" ]; then
163-
echo "Detected license header changes. See status below:"
164-
GIT_PAGER="cat" git diff
165-
exit 1
166-
fi

0 commit comments

Comments
 (0)