Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 54 additions & 8 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ on:

permissions:
contents: read
security-events: read

jobs:
analyze:
analyze-head:
name: CodeQL compatibility analysis (${{ matrix.language }})
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: read
security-events: write
strategy:
fail-fast: false
matrix:
Expand All @@ -43,6 +42,7 @@ jobs:
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v6
with:
persist-credentials: false
ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}

- name: Initialize CodeQL
uses: github/codeql-action/init@8aad20d150bbac5944a9f9d289da16a4b0d87c1e # v4
Expand All @@ -54,8 +54,54 @@ jobs:
uses: github/codeql-action/analyze@8aad20d150bbac5944a9f9d289da16a4b0d87c1e # v4
with:
category: "/language:${{ matrix.language }}"
# This repository uses GitHub CodeQL default setup for code scanning
# uploads. Keep this workflow as CI/Scorecard SAST evidence without
# uploading duplicate CodeQL SARIF, which GitHub rejects while
# default setup is enabled.
upload: never
# Upload PR-head CodeQL SARIF so ruleset code_scanning (CodeQL) can
# evaluate mergeability. Default setup is not configured on this repo.
upload: always
ref: ${{ github.event_name == 'pull_request' && format('refs/pull/{0}/head', github.event.pull_request.number) || github.ref }}
sha: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}

analyze-merge:
name: CodeQL merge preview (${{ matrix.language }})
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write
strategy:
fail-fast: false
matrix:
include:
- language: actions
build-mode: none
- language: javascript-typescript
build-mode: none
- language: python
build-mode: none
steps:
- name: Harden the runner (Audit all outbound calls)
uses: step-security/harden-runner@9af89fc71515a100421586dfdb3dc9c984fbf411 # v2.19.4
with:
egress-policy: audit

- name: Checkout merge preview
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v6
with:
persist-credentials: false
ref: ${{ format('refs/pull/{0}/merge', github.event.pull_request.number) }}

- name: Initialize CodeQL
uses: github/codeql-action/init@8aad20d150bbac5944a9f9d289da16a4b0d87c1e # v4
with:
languages: ${{ matrix.language }}
build-mode: ${{ matrix.build-mode }}

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@8aad20d150bbac5944a9f9d289da16a4b0d87c1e # v4
with:
category: "/language:${{ matrix.language }}-merge"
upload: always
# Ruleset code_scanning evaluates merge_commit_sha, which can differ
# from the ephemeral SHA refs/pull/<n>/merge resolves to at upload time.
ref: ${{ format('refs/pull/{0}/merge', github.event.pull_request.number) }}
sha: ${{ github.event.pull_request.merge_commit_sha }}
29 changes: 24 additions & 5 deletions backend/tests/test_release_governance.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,16 +323,32 @@ def test_bandit_security_scan_does_not_continue_on_error() -> None:
assert "continue-on-error: true" not in workflow


def test_codeql_workflow_can_read_security_events_without_uploading_sarif() -> None:
def test_codeql_workflow_uploads_pr_head_sarif_for_ruleset_gate() -> None:
workflow = read_repo_text(".github/workflows/codeql.yml")

assert "permissions:\n contents: read\n security-events: read" in workflow
assert "permissions:\n contents: read\n\njobs:" in workflow
assert (
" permissions:\n actions: read\n contents: read\n security-events: read"
" permissions:\n actions: read\n contents: read\n security-events: write"
in workflow
)
assert "upload: always" in workflow
assert "upload: never" not in workflow
assert (
"ref: ${{ github.event_name == 'pull_request' && format('refs/pull/{0}/head', github.event.pull_request.number) || github.ref }}"
in workflow
)
assert (
"sha: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}"
in workflow
)
assert "analyze-merge:" in workflow
assert "CodeQL merge preview" in workflow
assert "refs/pull/{0}/merge" in workflow
assert "github.event.pull_request.merge_commit_sha" in workflow
assert (
"ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}"
in workflow
)
assert "upload: never" in workflow
assert "security-events: write" not in workflow


def test_required_code_scanning_workflows_upload_scorecard_and_trivy_sarif() -> None:
Expand Down Expand Up @@ -394,6 +410,7 @@ def test_required_code_scanning_workflows_upload_scorecard_and_trivy_sarif() ->
in scorecard_workflow
)


assert (
"aquasecurity/trivy-action@ed142fd0673e97e23eac54620cfb913e5ce36c25 # v0.36.0"
in trivy_workflow
Expand Down Expand Up @@ -883,6 +900,8 @@ def test_pr_governance_uses_metadata_only_events_without_checkout_or_admin_merge
assert "/issues/${PR_NUMBER}/comments" in gate_script
assert "COMMENT_MARKER" in gate_script
assert "Waiting for" in gate_script
assert "no required checks reported" in gate_script
assert "ruleset workflows and code-scanning gates" in gate_script
assert "reviewThreads" in gate_script
assert "CHANGES_REQUESTED" in gate_script
assert "gh pr merge" not in gate_script
Expand Down
7 changes: 6 additions & 1 deletion scripts/ci/pr_governance_gate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ if [ "$UNRESOLVED_THREADS" != "0" ]; then
fi

if ! REQUIRED_CHECKS="$(gh pr checks "$PR_NUMBER" --repo "$GITHUB_REPOSITORY" --required --json name,state,link 2>"$PR_CHECKS_ERROR_FILE")"; then
add_blocker "Required check metadata could not be read: $(<"$PR_CHECKS_ERROR_FILE")."
PR_CHECKS_ERROR="$(<"$PR_CHECKS_ERROR_FILE")"
if printf '%s' "$PR_CHECKS_ERROR" | grep -qi 'no required checks reported'; then
add_waiting "Ruleset-governed branch: no legacy required status contexts reported for ${HEAD_REF_OID}; relying on ruleset workflows and code-scanning gates."
else
add_blocker "Required check metadata could not be read: ${PR_CHECKS_ERROR}."
fi
else
while IFS= read -r item; do
[ -n "$item" ] && add_blocker "$item"
Expand Down
15 changes: 15 additions & 0 deletions scripts/ci/test_pr_governance_gate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ if [ "$1" = "api" ] && [ "$2" = "graphql" ]; then
fi

if [ "$1" = "pr" ] && [ "$2" = "checks" ]; then
if [ "${GH_SCENARIO:-pass}" = "no_required_checks" ]; then
printf 'no required checks reported on the test branch\n' >&2
exit 1
fi
case "${GH_SCENARIO:-pass}" in
pending)
printf '[{"name":"Application CI","state":"IN_PROGRESS","link":"https://checks/app-ci"}]'
Expand Down Expand Up @@ -330,6 +334,16 @@ assert_passing_gate_is_metadata_only_without_merge() {
assert_not_in_file 'continue-on-error' "$temp_dir/gh.log"
}

assert_no_required_checks_waits_without_hard_comment() {
local temp_dir
temp_dir="$(mktemp -d)"
run_gate no_required_checks "$temp_dir"

assert_in_file 'no legacy required status contexts reported' "$temp_dir/output.txt"
assert_not_in_file 'issues/42/comments -f body' "$temp_dir/gh.log"
assert_not_in_file '^pr merge' "$temp_dir/gh.log"
}

assert_no_comment_or_merge_for_pending_checks
assert_startup_failure_creates_marker_comment
assert_failed_checks_create_marker_comment
Expand All @@ -345,5 +359,6 @@ assert_coderabbit_current_review_comment_blocks
assert_coderabbit_stale_review_comment_does_not_block
assert_changes_requested_creates_marker_comment
assert_passing_gate_is_metadata_only_without_merge
assert_no_required_checks_waits_without_hard_comment

printf 'test_pr_governance_gate: PASS\n'
Loading