Skip to content

Commit 69e574e

Browse files
authored
Cargo vet integration (#21610)
Run the shared cargo-vet workflow in CI to ensure we don't build packages that the dependency review automation has not audited. This only runs when a relevant `Cargo.lock` changes and runs on all current and future workspaces in the repository.
1 parent acee8d9 commit 69e574e

4 files changed

Lines changed: 129 additions & 4 deletions

File tree

‎.github/renovate.json5‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,12 @@
115115
},
116116
],
117117
customManagers: [
118-
// uv release used by the reusable security review workflow.
118+
// uv release used by reusable workflows.
119119
{
120120
customType: "regex",
121121
managerFilePatterns: ["/^\\.github/workflows/ci\\.yml$/"],
122122
matchStrings: [
123-
'# renovate: datasource=github-release-attachments depName=astral-sh/uv\\s+uv-version: "(?<currentValue>[^"\\r\\n]+)"\\s+uv-checksum: "(?<currentDigest>[a-f0-9]{64})"',
123+
'# renovate: datasource=github-release-attachments depName=astral-sh/uv\\s+uv-version: "(?<currentValue>[^"\\r\\n]+)"(?:\\s+uv-checksum: "(?<currentDigest>[a-f0-9]{64})")?',
124124
],
125125
depNameTemplate: "astral-sh/uv",
126126
datasourceTemplate: "github-release-attachments",

‎.github/workflows/ci.yml‎

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,48 @@ env:
1717
UV_LOCKED: 1
1818

1919
jobs:
20-
plan:
20+
detect-vet-workspaces:
2121
# CI on main is redundant in the security repository.
2222
if: ${{ github.repository != 'astral-sh/uv-security' || github.ref != 'refs/heads/main' }}
23+
runs-on: ${{ github.repository_owner == 'astral-sh' && 'github-ubuntu-24.04-x86_64-2' || 'ubuntu-latest' }}
24+
outputs:
25+
workspaces: ${{ steps.detect.outputs.workspaces }}
26+
steps:
27+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
28+
with:
29+
fetch-depth: ${{ github.event_name == 'pull_request' && 2 || 0 }}
30+
persist-credentials: false
31+
- name: "Install uv"
32+
uses: astral-sh/setup-uv@1c37ad07a6a961277cf70c0d37d6f313000f5884 # v10.3.0
33+
with:
34+
# renovate: datasource=github-release-attachments depName=astral-sh/uv
35+
version: "0.13.4"
36+
checksum: "2b428396f12572d4429cf210ca234dd599a33b3af0958a4d5ed569d63e3f344f"
37+
- name: "Detect Cargo Vet workspaces"
38+
id: detect
39+
env:
40+
HEAD_REPOSITORY: ${{ github.event.pull_request.head.repo.full_name }}
41+
BASE_SHA: ${{ github.event.pull_request.base.sha }}
42+
PUSH_BEFORE_SHA: ${{ github.event.before }}
43+
UV_PYTHON_DOWNLOADS_JSON_URL: ${{ github.workspace }}/crates/uv-python-managed/download-metadata.json
44+
run: uv run --script scripts/cargo-vet-workspaces.py >> "$GITHUB_OUTPUT"
45+
46+
check-vet:
47+
name: "Cargo Vet"
48+
needs: detect-vet-workspaces
49+
uses: astral-sh/github-actions/.github/workflows/cargo-vet.yml@c064e17eb42bf9ac71f66d7f5eda694f326d16cd
50+
with:
51+
runner: ${{ github.repository_owner == 'astral-sh' && 'github-ubuntu-24.04-x86_64-4' || 'ubuntu-latest' }}
52+
workspaces: ${{ needs.detect-vet-workspaces.outputs.workspaces }}
53+
# renovate: datasource=github-release-attachments depName=astral-sh/uv
54+
uv-version: "0.13.4"
55+
secrets: inherit
56+
permissions:
57+
contents: read
58+
id-token: write
59+
60+
plan:
61+
needs: check-vet
2362
uses: $/.github/workflows/plan.yml
2463

2564
check-fmt:
@@ -365,6 +404,7 @@ jobs:
365404
needs:
366405
- plan
367406
- check-cargo-lock-changes
407+
- check-vet
368408
- check-fmt
369409
- check-lint
370410
- check-docs
@@ -386,7 +426,7 @@ jobs:
386426
echo "$NEEDS_JSON" | jq -r --argjson conditional_jobs "$CONDITIONAL_JOBS" '
387427
to_entries[]
388428
| select($conditional_jobs[.key] != false)
389-
| select(.value.result != "success" and .value.result != "skipped")
429+
| select(.value.result != "success" and (.value.result != "skipped" or .key == "check-vet"))
390430
| "\(.key): \(.value.result)"
391431
'
392432
)

‎scripts/cargo-vet-workspaces.py‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# /// script
2+
# requires-python = ">=3.11"
3+
# dependencies = []
4+
#
5+
# [tool.uv]
6+
# no-build = true
7+
# exclude-newer = "P7D"
8+
# ///
9+
"""Select Cargo workspaces to vet in CI."""
10+
11+
import json
12+
import os
13+
import subprocess
14+
import sys
15+
from pathlib import Path
16+
17+
LOCKFILE_PATHS = ("Cargo.lock", ":(glob)crates/**/Cargo.lock")
18+
19+
20+
def git(*arguments: str) -> bytes:
21+
return subprocess.check_output(["git", *arguments])
22+
23+
24+
def lockfiles(*arguments: str) -> list[Path]:
25+
return [
26+
Path(os.fsdecode(path))
27+
for path in git(*arguments, "--", *LOCKFILE_PATHS).split(b"\0")
28+
if path
29+
]
30+
31+
32+
def main() -> int:
33+
event = os.environ["GITHUB_EVENT_NAME"]
34+
reference = os.environ["GITHUB_REF"]
35+
if os.environ["GITHUB_REPOSITORY_OWNER"] != "astral-sh" or (
36+
event == "pull_request"
37+
and os.environ["HEAD_REPOSITORY"] != os.environ["GITHUB_REPOSITORY"]
38+
):
39+
print("workspaces=[]")
40+
return 0
41+
42+
if event == "push":
43+
comparison_base = os.environ["PUSH_BEFORE_SHA"]
44+
elif reference.startswith("refs/pull/") and reference.endswith("/merge"):
45+
comparison_base = "HEAD^1"
46+
else:
47+
base_sha = os.environ["BASE_SHA"] or "origin/main"
48+
comparison_base = git("merge-base", base_sha, "HEAD").decode().strip()
49+
50+
changed = lockfiles(
51+
"diff",
52+
"--no-ext-diff",
53+
"--no-textconv",
54+
"--no-renames",
55+
"--name-only",
56+
"-z",
57+
comparison_base,
58+
"HEAD",
59+
)
60+
if not changed:
61+
print("workspaces=[]")
62+
return 0
63+
64+
for lockfile in changed:
65+
if not lockfile.is_file() and lockfile.with_name("Cargo.toml").is_file():
66+
print(
67+
f"::error::Cannot vet a Cargo workspace without its lockfile: {lockfile}",
68+
file=sys.stderr,
69+
)
70+
return 1
71+
72+
workspaces = [str(lockfile.parent) for lockfile in lockfiles("ls-files", "-z")]
73+
print(f"workspaces={json.dumps(workspaces)}")
74+
return 0
75+
76+
77+
if __name__ == "__main__":
78+
sys.exit(main())
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
version = 1
2+
revision = 5
3+
requires-python = ">=3.11"
4+
5+
[options]
6+
exclude-newer = "0001-01-01T00:00:00Z" # This has no effect and is included for backwards compatibility when using relative exclude-newer values.
7+
exclude-newer-span = "P7D"

0 commit comments

Comments
 (0)