Skip to content

Commit 2c55567

Browse files
eloymgguicauladazerok
authored
feat(run-capslock): add Run Capslock action (#1252)
* adding capslock-run action * put more robust separation symbol * removed formatted output and put it in pure go implementation * close the details * more format improvements * more compact title Co-authored-by: Guilherme Caulada <[email protected]> * caplock files verbose names * compare main refactor * pr comment example * added suggestions Co-authored-by: Horst Gutmann <[email protected]> * release please config * release please config --------- Co-authored-by: Guilherme Caulada <[email protected]> Co-authored-by: Horst Gutmann <[email protected]>
1 parent d005a71 commit 2c55567

File tree

9 files changed

+456
-1
lines changed

9 files changed

+456
-1
lines changed

.release-please-manifest.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@
2121
"actions/remove-checkout-credentials": "0.1.0",
2222
"actions/dependabot-auto-triage": "1.1.0",
2323
"actions/get-latest-workflow-artifact": "0.1.0",
24-
"actions/create-github-app-token": "v0.1.0"
24+
"actions/create-github-app-token": "v0.1.0",
25+
"actions/run-capslock": "v0.1.0"
2526
}

actions/run-capslock/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# run-capslock
2+
3+
Runs https://github.com/google/capslock action
4+
5+
<!-- x-release-please-start-version -->
6+
7+
```yaml
8+
name: Send And Respond to a Slack message using JSON payload
9+
jobs:
10+
run-capslock:
11+
name: Runs Capslock
12+
steps:
13+
- name: Run Capslock
14+
uses: grafana/shared-workflows/actions/run-capslock@run-capslock/v0.1.0
15+
id: run-capslock
16+
```
17+
18+
<!-- x-release-please-end-version -->
19+
20+
## Inputs
21+
22+
| Name | Description | Default |
23+
| ------------------ | ------------------------------------------------------ | ---------- |
24+
| `go-version` | Go version to use | 1.24.6 |
25+
| `capslock-version` | Capslock version to use | v0.2.8 |
26+
| `scope` | Path to run | |
27+
| `main-branch` | Main branch name | main |
28+
| `output-place` | Output place (options: pr-comment\*, summary and log ) | pr-comment |
29+
30+
\* For pr-comment the permission pull-request: write it's needed
31+
32+
## PR Comment example
33+
34+
![alt text](image.png)

actions/run-capslock/action.yaml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
name: Capslock compare
2+
description: Capslock compare
3+
4+
inputs:
5+
go-version:
6+
default: "1.24.6"
7+
required: true
8+
description: "Go version used"
9+
capslock-version:
10+
default: "v0.2.8"
11+
required: true
12+
description: "Capslock version used"
13+
scope:
14+
default: ""
15+
required: true
16+
description: "The scope of analysis"
17+
main-branch:
18+
default: "main"
19+
required: true
20+
description: "Name of the main branch"
21+
output-place:
22+
default: "pr-comment"
23+
required: true
24+
description: "Output place"
25+
runs:
26+
using: composite
27+
steps:
28+
- name: Checkout
29+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
30+
with:
31+
persist-credentials: false
32+
- uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
33+
with:
34+
go-version: "${{ inputs.go-version }}"
35+
- name: Cache capslock
36+
id: capslock-cache
37+
uses: actions/cache@0400d5f644dc74513175e3cd8d07132dd4860809 # v4.2.4
38+
with:
39+
path: ~/go
40+
key: ${{ runner.os }}-${{ runner.arch }}-capslock-${{ inputs.capslock-version }}
41+
- shell: bash
42+
if: steps.capslock-cache.outputs.cache-hit != 'true'
43+
id: capslock-install
44+
env:
45+
CAPSLOCK_VERSION: ${{ inputs.capslock-version }}
46+
run: |
47+
go install "github.com/google/capslock/cmd/capslock@${CAPSLOCK_VERSION}"
48+
- shell: bash
49+
id: capslock-run
50+
env:
51+
MAIN_BRANCH: ${{ inputs.main-branch }}
52+
SCOPE: ${{ inputs.scope }}
53+
CAPSLOCKTOOLSTMPDIR: ${{ runner.temp }}
54+
OUTPUT_PLACE: ${{ inputs.output-place }}
55+
run: |
56+
${GITHUB_ACTION_PATH}/capslock.sh
57+
- uses: marocchino/sticky-pull-request-comment@773744901bac0e8cbb5a0dc842800d45e9b2b405 # v2.9.4
58+
if: steps.capslock-run.outputs.output != ''
59+
with:
60+
hide_and_recreate: true
61+
hide_classify: "OUTDATED"
62+
message: |
63+
${{steps.capslock-run.outputs.output}}

actions/run-capslock/capslock.sh

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
# Fail with a clear message if required env vars are missing
5+
for var in GITHUB_ACTION_PATH MAIN_BRANCH GITHUB_HEAD_REF OUTPUT_PLACE GITHUB_SHA; do
6+
if [[ -z "${!var:-}" ]]; then
7+
echo "ERROR: Environment variable ${var} is required but not set"
8+
exit 1
9+
fi
10+
done
11+
12+
BASE_DIR="${PWD}"
13+
cd "${SCOPE:-.}" || exit 1
14+
15+
if [[ ! -f "go.mod" ]]; then
16+
echo "No go module found in ${PWD}"
17+
exit 0
18+
fi
19+
20+
# Helper: run capslock on all packages and save result to file
21+
run_capslock() {
22+
local output_file=$1
23+
local packages
24+
packages=$(go list ./... | paste -sd "," -)
25+
if [[ -z "${packages}" ]]; then
26+
echo "No packages found"
27+
exit 0
28+
fi
29+
capslock -granularity intermediate -packages "${packages}" -output json >"${output_file}"
30+
}
31+
32+
# Work with MAIN_BRANCH state
33+
git fetch origin "${MAIN_BRANCH}" --depth 1
34+
git checkout "${MAIN_BRANCH}"
35+
if [[ ! -f "go.mod" ]]; then
36+
echo "No go module found on branch ${MAIN_BRANCH}"
37+
exit 0
38+
fi
39+
run_capslock "${BASE_DIR}/capslock-main.json"
40+
41+
# Switch back to feature branch and rerun
42+
git stash --quiet
43+
git checkout "${GITHUB_SHA}"
44+
run_capslock "${BASE_DIR}/capslock-branch.json"
45+
git stash pop --quiet || true
46+
47+
# Move results back to action path
48+
mv "${BASE_DIR}/capslock-main.json" "${GITHUB_ACTION_PATH}/capslock-main.json"
49+
mv "${BASE_DIR}/capslock-branch.json" "${GITHUB_ACTION_PATH}/capslock-branch.json"
50+
51+
# Run comparison
52+
cd "${GITHUB_ACTION_PATH}"
53+
go mod tidy
54+
OUTPUT=$(go run compare.go capslock-main.json capslock-branch.json || true)
55+
56+
if [[ "${OUTPUT}" =~ "Between those commits, there were no uses of capabilities via a new package" ]]; then
57+
echo "No new Capabilities"
58+
exit 0
59+
fi
60+
61+
# Direct log output
62+
if [[ "${OUTPUT_PLACE}" == "log" ]]; then
63+
printf "%s\n" "${OUTPUT}"
64+
exit 0
65+
fi
66+
67+
68+
# Send to GitHub Actions outputs
69+
case "${OUTPUT_PLACE}" in
70+
pr-comment)
71+
{
72+
echo 'output<<EOF'
73+
printf "%s\nEOF" "${OUTPUT}"
74+
} >>"${GITHUB_OUTPUT}"
75+
;;
76+
summary)
77+
{
78+
echo 'output<<EOF'
79+
printf "%s\nEOF" "${OUTPUT}"
80+
} >>"${GITHUB_STEP_SUMMARY}"
81+
;;
82+
*)
83+
echo "ERROR: Only 'log', 'pr-comment' or 'summary' are allowed values for output_place input"
84+
;;
85+
esac

0 commit comments

Comments
 (0)