Skip to content

Commit b6a7e38

Browse files
authored
Merge pull request #3 from rogeruiz/rogeruiz/feat/add-support-for-checking-all-pr-changes
feat: Support for verifying all commits in PR
2 parents 5029183 + ab0b9db commit b6a7e38

File tree

5 files changed

+85
-33
lines changed

5 files changed

+85
-33
lines changed

.github/allowed_signers

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
[email protected],[email protected],[email protected] ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIflGAaoiK4/55TYIzFfCW5fgR18BZrPboXXTTQatfJu
2+
[email protected],[email protected],[email protected] ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINL+2VdQnANQ8Qpyu9D3j0tlT1ZUQlSELRnj0vEySjH4

.github/workflows/verify-commits.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
- name: Checkout the code
1010
uses: actions/checkout@v3
1111
- name: Run repasar on the latest SHA
12-
uses: rogeruiz/repasar@v1.0.0
12+
uses: rogeruiz/repasar@v1.1.0
1313
with:
1414
allowed-signers-file-path: ./.github/allowed_signers
1515
fail-on-unverified: true

README.md

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
# Repasar
44

5-
The Repasar GitHub Action (GHA) checks the current SHA that triggered the GHA
6-
and check to make sure that it is signed properly using subcommands supported by
7-
Git.
5+
The Repasar GitHub Action (GHA) checks the commit signatures for security. It
6+
now supports verifying **all commits in a pull request (PR)**, not just the
7+
latest commit. For push events, it continues to verify the latest commit as
8+
before.
9+
10+
- For PRs: All commits in the PR are checked for verified signatures.
11+
- For pushes: Only the latest commit is checked.
812

913
## Setup
1014

@@ -25,11 +29,12 @@ jobs:
2529
- name: Checkout the code
2630
uses: actions/checkout@v3
2731
- name: Run repasar on the latest SHA
28-
uses: rogeruiz/repasar@v1.0.0
32+
uses: rogeruiz/repasar@v1.1.0
2933
with:
3034
allowed-signers-file-path: ./.github/allowed_signers
3135
fail-on-unverified: true
3236
```
37+
3338
## Required inputs
3439

3540
The only required input is the `allowed-signers-file-path` which is recommended
@@ -40,19 +45,24 @@ keys in the following format per-line.
4045
<email>[,<email>...] <key type> <public key>
4146
```
4247

43-
These public keys are safe to check into your repository. To generate this file
44-
based on the public keys available on GitHub for committers to the repository,
45-
you can generate them using the `allowedSignersFile` project by @frankywahl.
46-
This file can also be maintained manually and checked in by each individual
47-
committer for your projects.
48-
49-
[=> frankywahl/allowedSignersFile project](https://github.com/frankywahl/allowedSignersFile)
48+
> [!IMPORTANT]
49+
> This file can be created manually by taking the public key file you have
50+
> locally and rearranging the comment email at the end to the beginning.
51+
> Remember to add only the emails you'd like to allow for verification purposes.
5052
5153
## Optional inputs
5254

5355
By default, this Action does not fail the run if the verification of the commit
54-
is unsuccessful. If you would like to have the Action fail, then set the `fail-on-unverified` to `true` in the `workflows/` YAML file.
56+
is unsuccessful. If you would like to have the Action fail, then set the
57+
`fail-on-unverified` to `true` in the `workflows/` Yaml file.
5558

5659
## Environment variables the action uses
5760

58-
This action uses the `${GITHUB_SHA}` variable to pull the current commit.
61+
- `${GITHUB_SHA}`: Used for single commit verification (push events).
62+
- `${GITHUB_EVENT_NAME}` and `${GITHUB_EVENT_PATH}`: Used to detect PR context
63+
and extract PR number.
64+
- `${GITHUB_TOKEN}`: **Required for PR verification** to fetch all commits in
65+
the PR using the GitHub API.
66+
67+
**Note:** For PRs, ensure the workflow has access to `GITHUB_TOKEN` (default in
68+
GitHub Actions) and that the token has `repo` scope for private repositories.

action.yml

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
name: 'Repasar Action'
2-
author: 'Roger Steve Ruiz'
3-
description: 'Run git-verify-commit command on the latest SHA'
1+
name: "Repasar Action"
2+
author: "Roger Steve Ruiz"
3+
description: "Run git-verify-commit command on all commits in a PR (or the latest SHA for push events)"
44
inputs:
55
allowed-signers-file-path:
6-
description: 'A path to a file in the repository to use as a list of SSH public keys IDs.'
6+
description: "A path to a file in the repository to use as a list of SSH public keys IDs."
77
required: true
8+
# Note: For PR verification, GITHUB_TOKEN must be available in the environment to fetch commits via the GitHub API.'
89
fail-on-unverified:
9-
description: 'Set the variable to true to fail the Action on when the git-verify-commit fails to be verified.'
10+
description: "Set the variable to true to fail the Action on when the git-verify-commit fails to be verified."
1011
required: false
1112
outputs:
1213
verified:
13-
description: 'The verification status'
14+
description: "The verification status"
1415
runs:
15-
using: 'docker'
16-
image: 'Dockerfile'
16+
using: "docker"
17+
image: "Dockerfile"
1718
args:
1819
- ${{ inputs.allowed-signers-file-path }}
1920
- ${{ inputs.fail-on-unverified }}
2021
branding:
21-
icon: 'check-circle'
22-
color: 'green'
22+
icon: "check-circle"
23+
color: "green"

entrypoint.sh

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,56 @@ git config --global tag.gpgsign true
77
git config --global gpg.format ssh
88
git config --global gpg.ssh.allowedSignersFile "${1}"
99

10-
if git verify-commit "${GITHUB_SHA}" &>/dev/null
11-
then
12-
echo "Commit ${GITHUB_SHA} is verified successfully!"
13-
git verify-commit "${GITHUB_SHA}"
14-
echo "verified=true" >> "${GITHUB_OUTPUT}"
10+
# Detect PR context
11+
if [[ "$GITHUB_EVENT_NAME" == "pull_request" || "$GITHUB_EVENT_NAME" == "pull_request_target" ]]; then
12+
echo "Detected PR context. Verifying all commits in the PR."
13+
# Extract PR number from event payload
14+
if command -v jq &>/dev/null; then
15+
PR_NUMBER=$(jq '.number' "$GITHUB_EVENT_PATH")
16+
else
17+
PR_NUMBER=$(grep '"number":' "$GITHUB_EVENT_PATH" | head -1 | sed 's/[^0-9]*//g')
18+
fi
19+
echo "PR number: $PR_NUMBER"
20+
# Fetch all commit SHAs in the PR
21+
COMMITS_JSON=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" "https://api.github.com/repos/${GITHUB_REPOSITORY}/pulls/${PR_NUMBER}/commits")
22+
if command -v jq &>/dev/null; then
23+
SHAS=($(echo "$COMMITS_JSON" | jq -r '.[].sha'))
24+
else
25+
SHAS=($(echo "$COMMITS_JSON" | grep '"sha":' | awk -F '"' '{print $4}'))
26+
fi
27+
FAILED_SHAS=()
28+
for sha in "${SHAS[@]}"; do
29+
if git verify-commit "$sha" &>/dev/null; then
30+
echo "Commit $sha is verified successfully!"
31+
git verify-commit "$sha"
32+
else
33+
echo "Failed to verify the commit $sha."
34+
FAILED_SHAS+=("$sha")
35+
fi
36+
done
37+
if [[ ${#FAILED_SHAS[@]} -eq 0 ]]; then
38+
echo "All commits in PR are verified."
39+
echo "verified=true" >> "${GITHUB_OUTPUT}"
40+
else
41+
echo "The following commits failed verification: ${FAILED_SHAS[*]}"
42+
echo "verified=false" >> "${GITHUB_OUTPUT}"
43+
if [[ ${2} == 'true' ]]; then
44+
exit 1
45+
fi
46+
fi
1547
else
16-
echo "Failed to verify the commit ${GITHUB_SHA}."
17-
echo "verified=false" >> "${GITHUB_OUTPUT}"
18-
if [[ ${2} == 'true' ]]
48+
# Fallback: verify latest commit as before
49+
if git verify-commit "${GITHUB_SHA}" &>/dev/null
1950
then
20-
exit 1
51+
echo "Commit ${GITHUB_SHA} is verified successfully!"
52+
git verify-commit "${GITHUB_SHA}"
53+
echo "verified=true" >> "${GITHUB_OUTPUT}"
54+
else
55+
echo "Failed to verify the commit ${GITHUB_SHA}."
56+
echo "verified=false" >> "${GITHUB_OUTPUT}"
57+
if [[ ${2} == 'true' ]]
58+
then
59+
exit 1
60+
fi
2161
fi
2262
fi

0 commit comments

Comments
 (0)