From d0ad2f659ab807d3bfad037a4b65139fb501b524 Mon Sep 17 00:00:00 2001 From: behnazh-w Date: Sun, 15 Nov 2026 13:32:01 +1000 Subject: [PATCH] chore: trigger OCA verification before rebasing main after release Signed-off-by: behnazh-w --- .github/workflows/_generate-rebase.yaml | 10 +++ .github/workflows/macaron-analysis.yaml | 2 +- README.md | 2 +- docs/source/pages/macaron_action.rst | 4 +- scripts/release_scripts/wait_for_oca.sh | 45 +++++++++++ tests/release_scripts/test_wait_for_oca.py | 93 ++++++++++++++++++++++ 6 files changed, 152 insertions(+), 4 deletions(-) create mode 100644 scripts/release_scripts/wait_for_oca.sh create mode 100644 tests/release_scripts/test_wait_for_oca.py diff --git a/.github/workflows/_generate-rebase.yaml b/.github/workflows/_generate-rebase.yaml index db1534b77..c157a3d0b 100644 --- a/.github/workflows/_generate-rebase.yaml +++ b/.github/workflows/_generate-rebase.yaml @@ -26,6 +26,7 @@ on: secrets: REPO_ACCESS_TOKEN: required: true + description: Token with contents and pull-request write access permissions: contents: read @@ -33,6 +34,7 @@ permissions: jobs: rebase: runs-on: ubuntu-latest + timeout-minutes: 35 steps: - name: Check out repository @@ -42,6 +44,14 @@ jobs: token: ${{ secrets.REPO_ACCESS_TOKEN }} ref: ${{ github.ref_name }} + - name: Trigger and wait for OCA if required + run: bash scripts/release_scripts/wait_for_oca.sh + env: + GH_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }} + GH_REPO: ${{ github.repository }} + TO_HEAD: ${{ inputs.to_head }} + FROM_BASE: ${{ inputs.from_base }} + - name: Do rebase run: | git config --global user.name "$USER_NAME" diff --git a/.github/workflows/macaron-analysis.yaml b/.github/workflows/macaron-analysis.yaml index 9533f617a..5cc03a877 100644 --- a/.github/workflows/macaron-analysis.yaml +++ b/.github/workflows/macaron-analysis.yaml @@ -35,7 +35,7 @@ jobs: # Note: adjust the policy_purl to refer to your repository URL. - name: Run Macaron action id: run_macaron - uses: oracle/macaron@794eec32782979f577efb5ff71d585f683a3550f # v0.25.0 + uses: oracle/macaron@5d8f162a5d33f91a7048e861f1ff946a9791fe64 # v0.26.0 with: repo_path: ./ policy_file: check-github-actions diff --git a/README.md b/README.md index bc6e7b85d..270b92b5a 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Use Macaron as a GitHub Action To use the Macaron GitHub Action, add the following step to your workflow (adjust the version as needed). In this example, we use an example policy. For detailed instructions and a comprehensive list of available options, please refer to the [Macaron GitHub Action documentation](https://oracle.github.io/macaron/pages/macaron_action.html). ```yaml -- uses: oracle/macaron@794eec32782979f577efb5ff71d585f683a3550f # v0.25.0 +- uses: oracle/macaron@5d8f162a5d33f91a7048e861f1ff946a9791fe64 # v0.26.0 with: repo_path: ./ policy_file: check-github-actions diff --git a/docs/source/pages/macaron_action.rst b/docs/source/pages/macaron_action.rst index 1b3f81f4f..d5a2a75cc 100644 --- a/docs/source/pages/macaron_action.rst +++ b/docs/source/pages/macaron_action.rst @@ -24,7 +24,7 @@ When you use this action, you can reference it directly in your workflow. For a steps: - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 - name: Run Macaron Security Analysis Action - uses: oracle/macaron@794eec32782979f577efb5ff71d585f683a3550f # v0.25.0 + uses: oracle/macaron@5d8f162a5d33f91a7048e861f1ff946a9791fe64 # v0.26.0 with: repo_path: ./ policy_file: check-github-actions @@ -45,7 +45,7 @@ verification reads the matching ``macaron.db``. .. code-block:: yaml - name: Verify policy - uses: oracle/macaron@794eec32782979f577efb5ff71d585f683a3550f # v0.25.0 + uses: oracle/macaron@5d8f162a5d33f91a7048e861f1ff946a9791fe64 # v0.26.0 with: policy_file: policy.dl diff --git a/scripts/release_scripts/wait_for_oca.sh b/scripts/release_scripts/wait_for_oca.sh new file mode 100644 index 000000000..96746983d --- /dev/null +++ b/scripts/release_scripts/wait_for_oca.sh @@ -0,0 +1,45 @@ +#!/usr/bin/env bash +# Copyright (c) 2026 - 2026, Oracle and/or its affiliates. All rights reserved. +# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. + +set -euo pipefail + +# Skip the PR workaround when the organization no longer requires OCA. +encoded_branch=$(jq -rn --arg branch "$TO_HEAD" '$branch | @uri') +rules=$(gh api "repos/$GH_REPO/rules/branches/$encoded_branch") +oca_required=$(jq -r 'any(.[] | select(.type == "required_status_checks") + | .parameters.required_status_checks[]; .context == "oca/oracle")' <<< "$rules") +if [[ "$oca_required" == "false" ]]; then + echo "OCA is not required; continuing with the rebase." + exit 0 +fi + +source_branch=${FROM_BASE#origin/} +pr_url=$(gh pr list --repo "$GH_REPO" --base "$TO_HEAD" --head "$source_branch" \ + --state open --json url --jq '.[0].url // empty') +if [[ -z "$pr_url" ]]; then + pr_url=$(gh pr create --repo "$GH_REPO" --base "$TO_HEAD" --head "$source_branch" \ + --title "chore: synchronize $TO_HEAD after release" \ + --body "Trigger OCA verification before the release workflow rebases $TO_HEAD onto $source_branch. No manual merge is needed.") +fi +head_sha=$(gh pr view "$pr_url" --repo "$GH_REPO" --json headRefOid --jq '.headRefOid') +echo "Waiting for OCA: $pr_url" +deadline=$((SECONDS + ${OCA_CHECK_TIMEOUT_SECONDS:-1800})) +while true; do + response=$(gh api "repos/$GH_REPO/commits/$head_sha/check-runs?check_name=oca%2Foracle&filter=latest&per_page=100") + state=$(jq -er ' + [.check_runs[] | select(.name == "oca/oracle" and .app.slug == "oracle-contributor-agreement")] + | max_by(.id) + | if . == null or .status != "completed" then "pending" else .conclusion end + ' <<< "$response") + case "$state" in + success) exit 0 ;; + pending) ;; + *) echo "OCA did not pass. Inspect $pr_url." >&2; exit 1 ;; + esac + if (( SECONDS >= deadline )); then + echo "Timed out waiting for OCA. Inspect $pr_url." >&2 + exit 1 + fi + sleep "${OCA_CHECK_INTERVAL_SECONDS:-15}" +done diff --git a/tests/release_scripts/test_wait_for_oca.py b/tests/release_scripts/test_wait_for_oca.py new file mode 100644 index 000000000..87671d75a --- /dev/null +++ b/tests/release_scripts/test_wait_for_oca.py @@ -0,0 +1,93 @@ +# Copyright (c) 2026 - 2026, Oracle and/or its affiliates. All rights reserved. +# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. + +"""Test the optional OCA gate without making GitHub requests.""" + +# The subprocess runs a repository script with a controlled GitHub CLI. +# ruff: noqa: S603, S607 + +import os +import subprocess +import sys +from pathlib import Path + +import pytest + + +@pytest.mark.parametrize( + "mode", + ["not_required", "success", "pending", "existing_pr", "failure", "cancelled", "missing", "api_error"], +) +def test_wait_for_oca(tmp_path: Path, mode: str) -> None: + """Skip an obsolete requirement, wait for a pending check, and stop on failure.""" + cli = tmp_path / "gh" + cli.write_text( + f"#!{sys.executable}\n" + + r""" +import json +import os +import sys +from pathlib import Path + +args = sys.argv[1:] +mode = os.environ["OCA_TEST_MODE"] +root = Path(os.environ["OCA_TEST_ROOT"]) +with (root / "requests").open("a") as log: + log.write(json.dumps(args) + "\n") +if mode == "api_error": + sys.exit("GitHub API unavailable") +if args[0] == "api" and "/rules/branches/" in args[1]: + print(json.dumps([] if mode == "not_required" else [{ + "type": "required_status_checks", + "parameters": {"required_status_checks": [{"context": "oca/oracle"}]}, + }])) +elif args[:2] == ["pr", "list"]: + print("https://github.com/example/project/pull/7" if mode == "existing_pr" else "") +elif args[:2] == ["pr", "create"]: + if mode == "existing_pr": + sys.exit("Existing PR must be reused") + print("https://github.com/example/project/pull/7") +elif args[:2] == ["pr", "view"]: + print("a" * 40) +elif args[0] == "api" and "/commits/" + "a" * 40 + "/check-runs?check_name=oca%2Foracle" in args[1]: + polled = root / "polled" + pending = mode == "pending" and not polled.exists() + polled.touch() + print(json.dumps({"check_runs": [] if mode == "missing" else [{ + "id": 1, + "name": "oca/oracle", + "app": {"slug": "oracle-contributor-agreement"}, + "status": "in_progress" if pending else "completed", + "conclusion": mode if mode in {"failure", "cancelled"} else "success", + }]})) +else: + sys.exit(f"Unexpected request: {args}") +""", + encoding="utf-8", + ) + cli.chmod(0o755) + script = Path(__file__).resolve().parents[2] / "scripts/release_scripts/wait_for_oca.sh" + result = subprocess.run( + ["bash", str(script)], + env={ + **os.environ, + "PATH": str(tmp_path) + os.pathsep + os.environ["PATH"], + "GH_REPO": "example/project", + "GH_TOKEN": "test-token", + "TO_HEAD": "main", + "FROM_BASE": "origin/release", + "OCA_TEST_MODE": mode, + "OCA_TEST_ROOT": str(tmp_path), + "OCA_CHECK_TIMEOUT_SECONDS": "1", + "OCA_CHECK_INTERVAL_SECONDS": "0", + }, + capture_output=True, + text=True, + check=False, + ) + assert (result.returncode == 0) is (mode in {"not_required", "success", "pending", "existing_pr"}), result.stderr + requests = (tmp_path / "requests").read_text(encoding="utf-8").splitlines() + if mode == "not_required": + assert len(requests) == 1, "No PR should be opened when OCA is no longer required" + if mode == "pending": + assert len([request for request in requests if "check-runs?" in request]) == 2