diff --git a/contributing-docs/25_maintainer_pr_triage.md b/contributing-docs/25_maintainer_pr_triage.md index 333faefdd1fce..7ae286b62508a 100644 --- a/contributing-docs/25_maintainer_pr_triage.md +++ b/contributing-docs/25_maintainer_pr_triage.md @@ -257,12 +257,13 @@ for them, and the triage skill must treat those pull requests as out of scope fo latest commit is green, and undrafts only what it drafted. - **`ready for maintainer review`.** The reconciler adds it when the fork run is green and the pull request is not a draft the author made (it adds it once the author marks it ready), and - removes it when a new push starts a new run. + removes it when a new push starts a new run or the author converts the pull request to draft. - **`needs project ci`.** The reconciler adds it when the fork run is green but the pull request changes CI-defining files, and removes it once the state is anything else. These pull requests are not drafted and get no `ready for maintainer review` label. Their `Fork CI / Tests (AMD)` status stays pending without `CI passed`, and they keep `needs project ci` until a maintainer - reviews the CI change and adds `use project ci`. + reviews the CI change and adds `use project ci`. A draft the author made gets neither label + until the author marks it ready for review. - **Closing for changing the gate.** The reconciler closes a pull request that changes the fork CI gate (its code, configuration other than the author's own `[pilot]` entry, the `gate` job of `ci-amd.yml` or the monitor), and reports it on Slack. Add `use project ci` to accept such @@ -280,8 +281,8 @@ reopen the pull request (`gh pr close -R apache/airflow && gh pr reopen or ask the author to push. Pull requests that change CI-defining files are not drafted and get no `ready for maintainer review` label even when the fork run is green. Their `Fork CI / Tests (AMD)` status stays pending without `CI passed`, and they are labelled -`needs project ci` until a maintainer reviews the CI change and adds `use project ci`. For -every other pull request, when the fork run is green and trusted, the reconciler also posts the +`needs project ci` until a maintainer reviews the CI change and adds `use project ci`, unless +the author keeps the pull request a draft. For every other pull request, when the fork run is green and trusted, the reconciler also posts the `CI passed` status the merge ruleset requires, so it can be merged or auto-merged without project CI. See [Fork CI for external contributors](../dev/breeze/doc/ci/08_fork_ci_for_external_contributors.md). diff --git a/dev/breeze/doc/ci/08_fork_ci_for_external_contributors.md b/dev/breeze/doc/ci/08_fork_ci_for_external_contributors.md index f1007448d612e..72e690c6cf1c4 100644 --- a/dev/breeze/doc/ci/08_fork_ci_for_external_contributors.md +++ b/dev/breeze/doc/ci/08_fork_ci_for_external_contributors.md @@ -408,7 +408,8 @@ run's event, and fails when it cannot read them. Both rules use the same list as "Trusting a green fork run", so a gated pull request whose runs are approved automatically is also one whose green fork run is trusted, and one that changes CI is both -left waiting for approval and labelled `needs project ci` once its fork run is green. The gate +left waiting for approval and labelled `needs project ci` once its fork run is green, unless its +author keeps it a draft. The gate tampering check still applies to every run a maintainer approves by hand. The approval reads only API metadata and the git objects of the merge commit; it never checks out or @@ -532,28 +533,45 @@ pull requests without a fork run for their head commit, not by the total. ### State machine -| Observed fork state | Draft | `ready for maintainer review` | Commit status | Comment | -|---|---|---|---|---| -| **setup_required**: workflow state is `disabled_fork`, `disabled_manually` or `disabled_inactivity`, or the fork has no `ci-amd.yml` | draft | remove | `error` (red), link to the fork's Actions page for `ci-amd.yml` | create or update: how to enable, with the exact `gh repo sync --branch main` and `gh workflow enable ci-amd.yml -R ` lines, and `breeze ci audit`; on transition from any other state but `closed` (the workflow stopped running after it worked): also post a **new** comment saying why it stopped | -| **awaiting_push**: enabled, no run for the head SHA | draft | remove | `pending` (yellow), link to the fork's Actions page | on transition from setup_required: post a **new** comment asking to push again (an edit would not notify) | -| **running**: run in progress | unchanged (a rebase of an approved pull request does not draft it; a failed run does) | remove | `pending` (yellow), link to the run | update state and run link | -| **failed**: conclusion is failure, cancelled or timed out | draft | remove | `failure` (red), link to the run | update with the run link | -| **green**: conclusion success and the pull request does not change CI-defining files | undraft\* | add | `success` (green), link to the run | update: ready for review, run link | -| **green_unverified**: conclusion success, but the pull request changes CI-defining files (see "Trusting a green fork run") | undraft\* | remove | `pending` (yellow), link to the run | update: a maintainer must run project CI; labelled `needs project ci` | -| setup_required or awaiting_push for `close_after_days` | unchanged | unchanged | unchanged | close with a new comment; reopening restarts the clock | -| **ungated**: the base branch has no gate, for example after retargeting to an older release branch | undraft\* | unchanged | `success` (green), link to the pull request's checks | update: project CI is in use; `fork ci required` is removed | -| **gate_modified**: the pull request changes the fork CI gate (see "Gate protection") | draft | remove | `error` (red), link to the pull request's changed files | post a **new** comment explaining why, then close; checked after gate tampering, before the fork state | -| **gate_bypassed**: a recorded project CI run of the pull request's branch bypassed the gate (see "Gate tampering") | draft | remove | `error` (red), link to the project CI run that should not have happened | post a **new** comment explaining why, then close; takes precedence over every row above | - -\* only if the last convert-to-draft event on the pull request was by `github-actions[bot]`; -a pull request the author drafted stays a draft. +The draft state and the two review labels depend only on the state and on whether the pull +request is the author's draft: a draft whose last convert-to-draft event was not by +`github-actions[bot]`, or a pull request opened as a draft. `compute_projection` in +`fork_ci_reconcile.py` holds this table, and `test_projection_table` lists every row. + +| State | Draft | `ready for maintainer review` | `needs project ci` | +|---|---|---|---| +| setup_required, awaiting_push, failed | draft | remove | remove | +| running | unchanged | remove | remove | +| green | undraft | add | remove | +| green_unverified | undraft | remove | add | +| green or green_unverified, on the author's draft | unchanged | remove | remove | +| released, ungated | undraft, except the author's draft | unchanged | remove | +| closed, gate_modified, gate_bypassed | unchanged | remove | remove | + +The author's draft waits for its author, so neither review label lists it. The reconciler never +undrafts it, and closed pull requests are not drafted, since the draft-state job runs after the +close and GitHub refuses to draft a closed pull request. + +| Observed fork state | Commit status | Comment | +|---|---|---| +| **setup_required**: workflow state is `disabled_fork`, `disabled_manually` or `disabled_inactivity`, or the fork has no `ci-amd.yml` | `error` (red), link to the fork's Actions page for `ci-amd.yml` | create or update: how to enable, with the exact `gh repo sync --branch main` and `gh workflow enable ci-amd.yml -R ` lines, and `breeze ci audit`; on transition from any other state but `closed` (the workflow stopped running after it worked): also post a **new** comment saying why it stopped | +| **awaiting_push**: enabled, no run for the head SHA | `pending` (yellow), link to the fork's Actions page | on transition from setup_required: post a **new** comment asking to push again (an edit would not notify) | +| **running**: run in progress | `pending` (yellow), link to the run | update state and run link | +| **failed**: conclusion is failure, cancelled or timed out | `failure` (red), link to the run | update with the run link | +| **green**: conclusion success and the pull request does not change CI-defining files | `success` (green), link to the run | update: ready for review, run link; on the author's draft: mark it ready for review when it is | +| **green_unverified**: conclusion success, but the pull request changes CI-defining files (see "Trusting a green fork run") | `pending` (yellow), link to the run | update: a maintainer must run project CI; on the author's draft: mark it ready for review, then a maintainer runs project CI | +| **closed**: setup_required or awaiting_push for `close_after_days` | unchanged | close with a new comment; reopening restarts the clock | +| **released**: the pull request carries `use project ci` | `success` (green), link to the pull request's checks | update: project CI is in use; `fork ci required` is removed | +| **ungated**: the base branch has no gate, for example after retargeting to an older release branch | `success` (green), link to the pull request's checks | update: project CI is in use; `fork ci required` is removed | +| **gate_modified**: the pull request changes the fork CI gate (see "Gate protection") | `error` (red), link to the pull request's changed files | post a **new** comment explaining why, then close; checked after gate tampering, before the fork state | +| **gate_bypassed**: a recorded project CI run of the pull request's branch bypassed the gate (see "Gate tampering") | `error` (red), link to the project CI run that should not have happened | post a **new** comment explaining why, then close; takes precedence over every row above | Rules that sit under the table: - **Gate tampering wins.** The `gate_bypassed` check is evaluated first for every pull request whose eligibility reason is `external` or `pilot`; when it holds, the fork state is not - consulted at all. A pull request that was already `green` and undrafted is drafted again, - loses the review label and is closed on the same run. + consulted at all. A pull request that was already `green` loses the review label and is + closed on the same run. - **A new push resets the row.** The head SHA is recorded in the state comment. A push to a green pull request is observed as `running` or `awaiting_push` on the next run, which removes @@ -607,8 +625,8 @@ decided or recorded for the old commit, and the next run decides for the new one seen, the state is `green_unverified` instead of `green`. The pull request is not drafted (a draft the reconciler made earlier is undone, as for `green`), but it gets no review label, the status is pending, and it is labelled `needs project ci` so maintainers can list these pull -requests, review the change and add `use project ci`. Without `CI passed` it cannot be merged -until then. It is not posted to Slack: +requests, review the change and add `use project ci`. A draft the author made gets no label +until the author marks it ready for review. Without `CI passed` it cannot be merged until then. It is not posted to Slack: the label already lists these pull requests, and a post for every new green commit would bury the CI breakages that channel is for. The reconciler removes `needs project ci` as soon as the state is anything else. A renamed file @@ -710,8 +728,8 @@ contributor losing their collaborator association) with full runs in the last si (`TAMPER_WINDOW`) is closed. Signal 1 exists so that an external pull request that edits `ci-amd.yml` legitimately, and leaves the gate intact, is not closed for touching the file. -When both hold the reconciler records `gate_bypassed` in the state comment marker, drafts the -pull request, removes `ready for maintainer review`, posts an `error` status whose +When both hold the reconciler records `gate_bypassed` in the state comment marker, removes +`ready for maintainer review`, posts an `error` status whose `target_url` is the project CI run that should not have happened, posts a new comment and closes. The comment says which run was observed, that changes to `.github/workflows/` from a gated pull request cannot be verified in `apache/airflow` without a maintainer, and that a @@ -766,7 +784,7 @@ run checks the pull request again, with the gate configuration of its new base. retarget to a base without the gate in between could close a pull request that should be released to project CI. -The state is `gate_modified`: the pull request is drafted, loses `ready for maintainer review` +The state is `gate_modified`: the pull request loses `ready for maintainer review` and `needs project ci`, gets an `error` status linking its changed files, and the closure is reported to Slack as an attention item. Gate tampering is checked first and wins. As for gate tampering, only a reopen by someone other than the author counts: an open pull request whose diff --git a/dev/breeze/src/airflow_breeze/utils/fork_ci_messages.py b/dev/breeze/src/airflow_breeze/utils/fork_ci_messages.py index 10cca162083e8..8b927957b3247 100644 --- a/dev/breeze/src/airflow_breeze/utils/fork_ci_messages.py +++ b/dev/breeze/src/airflow_breeze/utils/fork_ci_messages.py @@ -144,6 +144,11 @@ def _setup_instructions(fork: str) -> str: ) +_CHANGES_CI_FILES = ( + "this pull request changes files that decide which CI jobs run and where (`.github/`, the breeze " + "sources, the breeze installation or `scripts/ci/prek/`), and the fork run used those changed files." +) + _STATE_TEXTS = { "setup_required": "**Fork CI: setup required.** CI for this pull request has not run in your fork yet, " "because the `Tests (AMD)` workflow is not enabled there.", @@ -155,10 +160,8 @@ def _setup_instructions(fork: str) -> str: "green": "**Fork CI: green.** CI for the latest commit passed in your fork. The pull request is " "ready for maintainer review.", "green_unverified": "**Fork CI: green, needs a maintainer.** CI for the latest commit passed in your " - "fork, but this pull request changes files that decide which CI jobs run and where (`.github/`, " - "the breeze sources, the breeze installation or `scripts/ci/prek/`), and the fork run used those " - "changed files. A maintainer will review the change and run project CI for it before it can be " - "merged.", + f"fork, but {_CHANGES_CI_FILES} A maintainer will review the change and run project CI for it before " + "it can be merged.", "closed": "**Fork CI: closed after inactivity.** CI did not run in your fork in time; see the " "comment below for how to continue.", "gate_bypassed": "**Fork CI: closed.** Project CI ran for this pull request although its CI runs in " @@ -166,6 +169,14 @@ def _setup_instructions(fork: str) -> str: "gate_modified": "**Fork CI: closed.** This pull request changes the fork CI gate; see the comment below.", } +_AUTHOR_DRAFT_TEXTS = { + "green": "**Fork CI: green.** CI for the latest commit passed in your fork. The pull request is a " + "draft, so maintainers do not review it yet. Mark it ready for review when it is.", + "green_unverified": "**Fork CI: green.** CI for the latest commit passed in your fork, but " + f"{_CHANGES_CI_FILES} The pull request is a draft. Once you mark it ready for review, a maintainer " + "will review the change and run project CI for it.", +} + def behind_warning(behind: int, *, base_ref: str) -> str: return ( @@ -175,8 +186,11 @@ def behind_warning(behind: int, *, base_ref: str) -> str: ) -def state_comment(marker: Marker, *, fork: str, run_url: str | None, base_ref: str = "main") -> str: - parts = [render_marker(marker), _STATE_TEXTS[marker.state], WHY_FORK_CI] +def state_comment( + marker: Marker, *, fork: str, run_url: str | None, base_ref: str = "main", author_draft: bool = False +) -> str: + text = _AUTHOR_DRAFT_TEXTS.get(marker.state) if author_draft else None + parts = [render_marker(marker), text or _STATE_TEXTS[marker.state], WHY_FORK_CI] if ( marker.state in ("green", "green_unverified") and marker.behind is not None diff --git a/dev/breeze/src/airflow_breeze/utils/fork_ci_reconcile.py b/dev/breeze/src/airflow_breeze/utils/fork_ci_reconcile.py index c9a6865cd965c..20f24070f55a6 100644 --- a/dev/breeze/src/airflow_breeze/utils/fork_ci_reconcile.py +++ b/dev/breeze/src/airflow_breeze/utils/fork_ci_reconcile.py @@ -344,6 +344,64 @@ def _set_label(plan: Plan, labels: set[str], label: str, *, present: bool) -> No plan.remove_labels.append(label) +@dataclass(frozen=True) +class Projection: + """ + What a gated pull request shows in a state; None leaves its draft state or label as it is. + + :param draft: whether the pull request should be a draft. + :param ready_for_review: whether it carries `ready for maintainer review`. + :param needs_project_ci: whether it carries `needs project ci`. + """ + + draft: bool | None + ready_for_review: bool | None + needs_project_ci: bool + + +def compute_projection(state: ForkState, *, author_draft: bool) -> Projection: + """ + Return what a pull request shows in a state. + + :param author_draft: the pull request is a draft that the bot did not make, including one opened + as a draft. It is never undrafted and waits for its author, not for a maintainer. + """ + if state in (ForkState.GREEN, ForkState.GREEN_UNVERIFIED): + if author_draft: + return Projection(draft=None, ready_for_review=False, needs_project_ci=False) + return Projection( + draft=False, + ready_for_review=state == ForkState.GREEN, + needs_project_ci=state == ForkState.GREEN_UNVERIFIED, + ) + if state in (ForkState.RELEASED, ForkState.UNGATED): + return Projection( + draft=None if author_draft else False, ready_for_review=None, needs_project_ci=False + ) + if state in (ForkState.SETUP_REQUIRED, ForkState.AWAITING_PUSH, ForkState.FAILED): + return Projection(draft=True, ready_for_review=False, needs_project_ci=False) + # A running pull request keeps its draft state, so rebasing an approved one does not draft it. A + # closed one is not drafted, since the draft-state job runs after the close and GitHub refuses + # to draft a closed pull request. + if state in (ForkState.RUNNING, ForkState.CLOSED, ForkState.GATE_BYPASSED, ForkState.GATE_MODIFIED): + return Projection(draft=None, ready_for_review=False, needs_project_ci=False) + raise ValueError(f"No projection for fork CI state {state.value!r}") + + +def _is_author_draft(pr: PullRequestSnapshot) -> bool: + return pr.is_draft and not _is_bot(pr.last_draft_actor) + + +def _enter_state(plan: Plan, pr: PullRequestSnapshot, labels: set[str], state: ForkState) -> None: + plan.state = state + projection = compute_projection(state, author_draft=_is_author_draft(pr)) + if projection.draft is not None and projection.draft != pr.is_draft: + plan.draft = projection.draft + if projection.ready_for_review is not None: + _set_label(plan, labels, LABEL_READY_FOR_REVIEW, present=projection.ready_for_review) + _set_label(plan, labels, LABEL_NEEDS_PROJECT_CI, present=projection.needs_project_ci) + + def _workflow_file(resource_path: str | None) -> str: return (resource_path or "").rsplit("/", 1)[-1] @@ -584,11 +642,8 @@ def classify( marker = None def release(state: ForkState, comment: Callable[[Marker], str]) -> Plan: - plan.state = state plan.remove_labels.append(LABEL_FORK_CI_REQUIRED) - _set_label(plan, labels, LABEL_NEEDS_PROJECT_CI, present=False) - if pr.is_draft and _is_bot(pr.last_draft_actor): - plan.draft = False + _enter_state(plan, pr, labels, state) since = marker.since if marker and marker.state == state.value else now _set_marker_comment(plan, pr, comment(Marker(state.value, pr.head_sha, since))) _set_status(plan, pr, state.value, f"https://github.com/{repo}/pull/{pr.number}/checks") @@ -610,12 +665,7 @@ def release(state: ForkState, comment: Callable[[Marker], str]) -> Plan: if reason in TAMPER_CHECKED_REASONS: bypass_run = find_bypass_run() if bypass_run is not None: - plan.state = ForkState.GATE_BYPASSED - if not pr.is_draft: - plan.draft = True - if LABEL_READY_FOR_REVIEW in labels: - plan.remove_labels.append(LABEL_READY_FOR_REVIEW) - _set_label(plan, labels, LABEL_NEEDS_PROJECT_CI, present=False) + _enter_state(plan, pr, labels, ForkState.GATE_BYPASSED) _set_status(plan, pr, ForkState.GATE_BYPASSED.value, bypass_run) _set_marker_comment( plan, @@ -642,8 +692,7 @@ def release(state: ForkState, comment: Callable[[Marker], str]) -> Plan: if marker and marker.state in reopened_since_closing: reopened = reopened_since_closing[marker.state] if reopened is None or reopened <= marker.since: - plan.state = ForkState(marker.state) - _set_label(plan, labels, LABEL_NEEDS_PROJECT_CI, present=False) + _enter_state(plan, pr, labels, ForkState(marker.state)) if plan.state == ForkState.CLOSED: comment = inactivity_close_comment(days=config.close_after_days, fork=pr.head_repo) elif plan.state == ForkState.GATE_MODIFIED: @@ -676,11 +725,7 @@ def release(state: ForkState, comment: Callable[[Marker], str]) -> Plan: if gate_checked and not already_checked: change = find_gate_change() if change is not None: - plan.state = ForkState.GATE_MODIFIED - if not pr.is_draft: - plan.draft = True - _set_label(plan, labels, LABEL_READY_FOR_REVIEW, present=False) - _set_label(plan, labels, LABEL_NEEDS_PROJECT_CI, present=False) + _enter_state(plan, pr, labels, ForkState.GATE_MODIFIED) _set_status( plan, pr, ForkState.GATE_MODIFIED.value, f"https://github.com/{repo}/pull/{pr.number}/files" ) @@ -753,8 +798,7 @@ def release(state: ForkState, comment: Callable[[Marker], str]) -> Plan: ) if state in CLOSABLE_STATES and now - since >= timedelta(days=config.close_after_days): - plan.state = ForkState.CLOSED - _set_label(plan, labels, LABEL_NEEDS_PROJECT_CI, present=False) + _enter_state(plan, pr, labels, ForkState.CLOSED) _set_marker_comment( plan, pr, @@ -768,26 +812,7 @@ def release(state: ForkState, comment: Callable[[Marker], str]) -> Plan: plan.close = True return plan - if state in (ForkState.GREEN, ForkState.GREEN_UNVERIFIED): - # A green run that changes CI files still needs a maintainer's review, which a draft would - # hide; the `needs project ci` label marks it instead of the review label. - if pr.is_draft and _is_bot(pr.last_draft_actor): - plan.draft = False - ready = not pr.is_draft or plan.draft is False - if state == ForkState.GREEN and ready and LABEL_READY_FOR_REVIEW not in labels: - plan.add_labels.append(LABEL_READY_FOR_REVIEW) - elif state == ForkState.GREEN_UNVERIFIED and LABEL_READY_FOR_REVIEW in labels: - plan.remove_labels.append(LABEL_READY_FOR_REVIEW) - else: - # A run in progress needs nothing from the author, and drafting on it would draft every - # approved pull request the moment it is rebased; it is drafted only if the run then fails. - if not pr.is_draft and state != ForkState.RUNNING: - plan.draft = True - if LABEL_READY_FOR_REVIEW in labels: - plan.remove_labels.append(LABEL_READY_FOR_REVIEW) - # Lets maintainers list the pull requests that only wait for them to run project CI, which - # otherwise look like any other pull request with a pending fork CI status. - _set_label(plan, labels, LABEL_NEEDS_PROJECT_CI, present=state == ForkState.GREEN_UNVERIFIED) + _enter_state(plan, pr, labels, state) target_url = fork.run_url or fork_actions_url(pr.head_repo) _set_status(plan, pr, state.value, target_url, force=refresh_status) @@ -809,6 +834,7 @@ def release(state: ForkState, comment: Callable[[Marker], str]) -> Plan: fork=pr.head_repo, run_url=fork.run_url, base_ref=pr.base_ref, + author_draft=_is_author_draft(pr), ), ) return plan diff --git a/dev/breeze/tests/test_fork_ci_messages.py b/dev/breeze/tests/test_fork_ci_messages.py index 6bf940c69df82..8d565cbecb435 100644 --- a/dev/breeze/tests/test_fork_ci_messages.py +++ b/dev/breeze/tests/test_fork_ci_messages.py @@ -90,6 +90,23 @@ def test_state_comment_warns_about_a_branch_far_behind(state, behind, warned): assert ("commits behind `v3-1-test`" in text) is warned +@pytest.mark.parametrize( + ("state", "author_draft", "asks_to_mark_ready"), + [ + pytest.param("green", True, True, id="green-author-draft"), + pytest.param("green_unverified", True, True, id="unverified-author-draft"), + pytest.param("green", False, False, id="green-ready"), + pytest.param("failed", True, False, id="failed-author-draft"), + ], +) +def test_state_comment_asks_the_author_to_mark_a_green_draft_ready(state, author_draft, asks_to_mark_ready): + text = state_comment( + Marker(state, "a", MARKER.since), fork="u/airflow", run_url=None, author_draft=author_draft + ) + + assert ("mark it ready for review" in text.lower()) is asks_to_mark_ready + + def test_parse_marker_absent(): assert parse_marker("just a comment") is None diff --git a/dev/breeze/tests/test_fork_ci_reconcile.py b/dev/breeze/tests/test_fork_ci_reconcile.py index 81962145a43a6..d632d500da7ae 100644 --- a/dev/breeze/tests/test_fork_ci_reconcile.py +++ b/dev/breeze/tests/test_fork_ci_reconcile.py @@ -52,6 +52,7 @@ GateChangeFinder, JobStatusReporter, Plan, + Projection, PullRequestChangedError, PullRequestSnapshot, apply_plan, @@ -59,6 +60,7 @@ build_slack_payload, changes_ci_definition, classify, + compute_projection, count_commits_behind, discover, drop_reported, @@ -250,6 +252,105 @@ def test_non_green_removes_review_label_and_drafts_only_when_the_author_must_act assert plan.remove_labels == ["ready for maintainer review"] +@pytest.mark.parametrize( + ("state", "author_draft", "expected"), + [ + pytest.param( + ForkState.SETUP_REQUIRED, + False, + Projection(draft=True, ready_for_review=False, needs_project_ci=False), + id="setup-required", + ), + pytest.param( + ForkState.AWAITING_PUSH, + False, + Projection(draft=True, ready_for_review=False, needs_project_ci=False), + id="awaiting-push", + ), + pytest.param( + ForkState.FAILED, + False, + Projection(draft=True, ready_for_review=False, needs_project_ci=False), + id="failed", + ), + pytest.param( + ForkState.FAILED, + True, + Projection(draft=True, ready_for_review=False, needs_project_ci=False), + id="failed-author-draft", + ), + pytest.param( + ForkState.RUNNING, + False, + Projection(draft=None, ready_for_review=False, needs_project_ci=False), + id="running", + ), + pytest.param( + ForkState.GREEN, + False, + Projection(draft=False, ready_for_review=True, needs_project_ci=False), + id="green", + ), + pytest.param( + ForkState.GREEN, + True, + Projection(draft=None, ready_for_review=False, needs_project_ci=False), + id="green-author-draft", + ), + pytest.param( + ForkState.GREEN_UNVERIFIED, + False, + Projection(draft=False, ready_for_review=False, needs_project_ci=True), + id="unverified", + ), + pytest.param( + ForkState.GREEN_UNVERIFIED, + True, + Projection(draft=None, ready_for_review=False, needs_project_ci=False), + id="unverified-author-draft", + ), + pytest.param( + ForkState.RELEASED, + False, + Projection(draft=False, ready_for_review=None, needs_project_ci=False), + id="released", + ), + pytest.param( + ForkState.RELEASED, + True, + Projection(draft=None, ready_for_review=None, needs_project_ci=False), + id="released-author-draft", + ), + pytest.param( + ForkState.UNGATED, + False, + Projection(draft=False, ready_for_review=None, needs_project_ci=False), + id="ungated", + ), + pytest.param( + ForkState.CLOSED, + False, + Projection(draft=None, ready_for_review=False, needs_project_ci=False), + id="closed", + ), + pytest.param( + ForkState.GATE_BYPASSED, + False, + Projection(draft=None, ready_for_review=False, needs_project_ci=False), + id="gate-bypassed", + ), + pytest.param( + ForkState.GATE_MODIFIED, + False, + Projection(draft=None, ready_for_review=False, needs_project_ci=False), + id="gate-modified", + ), + ], +) +def test_projection_table(state, author_draft, expected): + assert compute_projection(state, author_draft=author_draft) == expected + + def test_green_undrafts_what_the_bot_drafted(): plan, _, _ = _classify(_pr(last_draft_actor="github-actions"), GREEN) @@ -257,11 +358,22 @@ def test_green_undrafts_what_the_bot_drafted(): assert plan.add_labels == ["ready for maintainer review"] -def test_green_keeps_an_author_draft(): - plan, _, _ = _classify(_pr(last_draft_actor="someone"), GREEN) +@pytest.mark.parametrize( + "last_draft_actor", + [ + pytest.param("someone", id="drafted-by-author"), + pytest.param(None, id="opened-as-draft"), + ], +) +def test_green_keeps_an_author_draft_out_of_the_review_queue(last_draft_actor): + pr = _pr(last_draft_actor=last_draft_actor, labels=("fork ci required", "ready for maintainer review")) + + plan, _, _ = _classify(pr, GREEN) assert plan.draft is None assert plan.add_labels == [] + assert plan.remove_labels == ["ready for maintainer review"] + assert "Mark it ready for review when it is." in plan.marker_comment def test_green_labels_a_ready_pull_request(): @@ -291,18 +403,27 @@ def test_green_is_not_trusted_when_ci_files_change(): @pytest.mark.parametrize( - ("last_draft_actor", "expected_draft"), + ("last_draft_actor", "expected_draft", "expected_labels", "expected_text"), [ - pytest.param("github-actions[bot]", False, id="bot-draft-undrafted"), - pytest.param("someone", None, id="author-draft-kept"), + pytest.param( + "github-actions[bot]", + False, + ["needs project ci"], + "run project CI for it before it can be merged", + id="bot-draft-undrafted", + ), + pytest.param("someone", None, [], "Once you mark it ready for review", id="author-draft-kept"), ], ) -def test_green_unverified_does_not_keep_a_bot_draft(last_draft_actor, expected_draft): +def test_green_unverified_does_not_keep_a_bot_draft( + last_draft_actor, expected_draft, expected_labels, expected_text +): plan, _, _ = _classify(_pr(last_draft_actor=last_draft_actor), GREEN, changes_ci_files=True) assert plan.state == ForkState.GREEN_UNVERIFIED assert plan.draft is expected_draft - assert plan.add_labels == ["needs project ci"] + assert plan.add_labels == expected_labels + assert expected_text in plan.marker_comment LABELLED = ("fork ci required", "needs project ci") @@ -759,7 +880,7 @@ def test_gate_tampering_closes(pr_overrides, checked): assert plan.close is checked if checked: assert plan.state == ForkState.GATE_BYPASSED - assert plan.draft is True + assert plan.draft is None assert plan.status == ( "error", PROJECT_RUN_URL, @@ -2880,7 +3001,7 @@ def test_a_change_to_the_gate_closes(): assert plan.state == ForkState.GATE_MODIFIED assert plan.close is True - assert plan.draft is True + assert plan.draft is None assert plan.remove_labels == ["ready for maintainer review"] assert plan.status == ( "error",