Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

ci: tell contributors when their commits aren't verified (#9359)

James Dam99f4ca28efa9
diff --git a/.github/workflows/signed-commits.yml b/.github/workflows/signed-commits.yml
new file mode 100644
index 000000000..b2556334a
--- /dev/null
+++ b/.github/workflows/signed-commits.yml
@@ -0,0 +1,124 @@
+# Comment on PRs that contain unsigned commits. The rulesets on main and
+# release/* require verified signatures, and GitHub only says so in the merge
+# box, so contributors otherwise find out late.
+#
+# pull_request_target so it can comment on PRs from forks. It must never check
+# out or run PR code: it only reads the PR's commit list through the API.
+name: Signed Commits
+on:
+ pull_request_target:
+ # edited: a PR retargeted to another base has a different commit list.
+ types: [opened, synchronize, reopened, edited]
+ branches:
+ - main
+ - release
+ - 'release/**'
+
+jobs:
+ check_signatures:
+ # Title and body edits also fire edited; only a base change matters.
+ if: github.event.action != 'edited' || github.event.changes.base
+ # Job-level so a skipped title edit can't cancel a real check.
+ concurrency:
+ group: signed-commits-${{ github.event.pull_request.number }}
+ cancel-in-progress: true
+ runs-on: ubuntu-latest
+ permissions:
+ contents: read
+ pull-requests: write
+ steps:
+ - name: Check commit signatures
+ uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
+ with:
+ github-token: ${{ secrets.GITHUB_TOKEN }}
+ script: |
+ const MARKER = '<!-- signed-commits-check -->';
+ const DOCS = 'https://github.com/janhq/jan/blob/main/CONTRIBUTING.md#signed-commits';
+ const { owner, repo } = context.repo;
+ const pr = context.payload.pull_request;
+
+ // The API returns at most 250 commits for a PR.
+ const commits = await github.paginate(github.rest.pulls.listCommits, {
+ owner, repo, pull_number: pr.number, per_page: 100,
+ });
+ const unsigned = commits.filter((c) => !c.commit.verification?.verified);
+
+ const comments = await github.paginate(github.rest.issues.listComments, {
+ owner, repo, issue_number: pr.number, per_page: 100,
+ });
+ const sticky = comments.find(
+ (c) => c.user?.login === 'github-actions[bot]' && c.body?.includes(MARKER),
+ );
+
+ // Past the cap the rest can't be seen, so a clean subset isn't a pass.
+ // A full page counts as capped too: a re-run carries the old payload,
+ // whose commit count may predate later pushes.
+ const truncated = commits.length >= 250 || pr.commits > commits.length;
+ const total = Math.max(pr.commits, commits.length);
+ if (unsigned.length === 0 && !truncated) {
+ core.info(`All ${commits.length} commits are verified.`);
+ if (sticky) {
+ await github.rest.issues.deleteComment({ owner, repo, comment_id: sticky.id });
+ }
+ return;
+ }
+
+ // Code spans keep commit messages from rendering markdown or @-mentions.
+ const code = (s) => '`' + s.replace(/`/g, "'") + '`';
+ const firstLine = (m) => {
+ const line = m.split('\n', 1)[0];
+ return line.length > 72 ? line.slice(0, 71) + '…' : line;
+ };
+ const hints = {
+ unsigned: 'no signature',
+ unknown_key: 'key not on GitHub as a signing key',
+ unverified_email: 'commit email not verified on GitHub',
+ bad_email: 'commit email isn\'t one of the GPG key\'s identities (gpg --list-keys)',
+ no_user: 'commit email not on any GitHub account',
+ gpgverify_unavailable: 'GitHub couldn\'t verify it just now; re-run this check',
+ gpgverify_error: 'GitHub couldn\'t verify it just now; re-run this check',
+ };
+ const reason = (v) => hints[v?.reason] ?? v?.reason ?? 'unknown';
+ const rows = unsigned.map((c) =>
+ `- ${c.sha.slice(0, 7)} ${code(firstLine(c.commit.message))}: ${reason(c.commit.verification)}`,
+ );
+ const unchecked = truncated
+ ? [``, `Only the first ${commits.length} commits could be checked; GitHub's API lists no more. Collapsing the branch into one commit (below) makes the rest checkable.`]
+ : [];
+ const headline = unsigned.length === 0
+ ? `Thanks for the PR! This PR has at least ${total} commits, more than this check can read, and \`${pr.base.ref}\` only accepts verified commits.`
+ : `Thanks for the PR! ${unsigned.length === 1 ? 'One commit doesn\'t' : `${unsigned.length} commits don't`} have a verified signature, and \`${pr.base.ref}\` only accepts verified commits, so this can't be merged yet:`;
+
+ const body = [
+ MARKER,
+ headline,
+ ``,
+ ...rows,
+ ...unchecked,
+ ``,
+ `If the reason is about a key or an email, fix that on GitHub first (add the key as a **signing key**, verify the email). Then replace the branch's commits with one signed commit with the same content and force-push (\`upstream\` is your remote for janhq/jan, \`origin\` your fork; PRs are squash-merged, so nothing is lost):`,
+ ``,
+ '```bash',
+ `git status --short # must print nothing: commit or stash your changes first`,
+ `git fetch upstream`,
+ `git branch -f unsigned-backup # replaces any older backup with this branch`,
+ `git reset --soft "$(git merge-base HEAD upstream/${pr.base.ref})"`,
+ `git commit -S -m "<your PR title>"`,
+ `git diff unsigned-backup HEAD # must print nothing`,
+ `git push --force-with-lease origin HEAD`,
+ '```',
+ ``,
+ `How to set up signing: [CONTRIBUTING.md#signed-commits](${DOCS}). This comment goes away once every commit is verified.`,
+ ].join('\n');
+
+ if (sticky) {
+ if (sticky.body !== body) {
+ await github.rest.issues.updateComment({ owner, repo, comment_id: sticky.id, body });
+ }
+ } else {
+ await github.rest.issues.createComment({ owner, repo, issue_number: pr.number, body });
+ }
+
+ core.setFailed(unsigned.length === 0
+ ? `Only the first ${commits.length} commits could be checked`
+ : `${unsigned.length} of ${commits.length} commits are not verified: ${unsigned.map((c) => c.sha.slice(0, 7)).join(', ')}`);
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 2e5439068..be413f0ed 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -225,34 +225,41 @@ docs: update installation instructions
### Signed Commits
-We ask that you **sign your commits** so we can verify that changes really come from you. Git supports signing with a GPG/SSH key or, on Linux, a PGP key via `git commit -S`.
+**Signed commits are required on `main`.** A PR with any commit that isn't verified can't be merged, and a bot will comment on the PR listing the commits that need fixing. A commit counts as signed when GitHub shows a green **Verified** badge, which needs two things:
+- the key that signed it is added to your GitHub account as a **signing key**, and
+- the commit email (`git config user.email`) is a verified email on that same account.
-**1. Create and publish your key** (if you don't have one):
+**SSH signing (quickest, needs Git 2.34+)** — reuse the SSH key you already push with (use whichever key that is, e.g. `id_rsa`):
```bash
-# Generate a GPG key (follow the prompts, add your email)
-gpg --full-generate-key
-
-# List your key IDs
-gpg --list-secret-keys --keyid-format=long
+git config --global gpg.format ssh
+git config --global user.signingkey ~/.ssh/id_ed25519 # the private key; use the .pub path only if the key is loaded in ssh-agent
+git config --global commit.gpgsign true
```
+Then go to GitHub → **Settings** → **SSH and GPG keys** → **New SSH key**, choose **Key type: Signing Key**, and paste the contents of `~/.ssh/id_ed25519.pub`. A key added only as an authentication key won't verify signatures, so add it again as a signing key even if it's already there.
-**2. Register the key on GitHub**
-- Export your public key: `gpg --armor --export YOUR_KEY_ID > public.gpg`
-- Go to GitHub → **Settings** → **SSH and GPG keys** → **New GPG key** and paste the contents
-- Add your key ID to Git:
+**GPG signing** — if you'd rather use GPG:
```bash
+gpg --full-generate-key # follow the prompts, use your GitHub email
+gpg --list-secret-keys --keyid-format=long # copy the key ID
+gpg --armor --export YOUR_KEY_ID # paste into GitHub → New GPG key
+git config --global gpg.format openpgp
git config --global user.signingkey YOUR_KEY_ID
git config --global commit.gpgsign true
```
-**3. Sign your commits**
+With `commit.gpgsign true`, every `git commit` is signed automatically. See [GitHub's commit signing docs](https://docs.github.com/en/authentication/managing-commit-signature-verification) for details.
+
+**Already opened a PR with unsigned commits?** Set up signing as above, then replace your branch's commits with one signed commit that has exactly the same content, and force-push:
```bash
-git commit -S -m 'feat: add support for Qwen models'
+git status --short # must print nothing: commit or stash your changes first
+git fetch upstream
+git branch -f unsigned-backup # replaces any older backup with this branch
+git reset --soft "$(git merge-base HEAD upstream/main)"
+git commit -S -m "<your PR title>"
+git diff unsigned-backup HEAD # must print nothing
+git push --force-with-lease origin HEAD
```
-
-The `-S` flag signs the commit; `commit.gpgsign true` (above) makes it the default. Signed commits show a green **Verified** badge on GitHub once your key is registered. See [Git's signing docs](https://git-scm.com/book/en/v2/Git-Tools-Signing-Your-Work) for details.
-
-> **SSH signing**: If you prefer SSH over GPG, follow [GitHub's SSH signing guide](https://docs.github.com/en/authentication/managing-commit-signature-verification/signing-commits-with-ssh) and commit with `git commit -S` as well.
+`upstream` is whatever your remote for `janhq/jan` is called and `origin` is your fork (check with `git remote -v`). PRs are squash-merged, so collapsing them into one commit loses nothing. Unlike a rebase, this keeps changes made in merge commits. If `git diff` prints anything, stop and restore with `git reset --hard unsigned-backup`. After the push, every commit on the PR's **Commits** tab shows a **Verified** badge and the bot comment disappears.
### Pull Request Requirements
- Include a screenshot or screen recording in your PR description showing the change in action