This is an automated email from the ASF dual-hosted git repository. potiuk pushed a commit to branch fork-ci-label-commands in repository https://gitbox.apache.org/repos/asf/airflow.git
commit 6263b549fab76f193b58f7bb283d6fc85e187ef4 Author: Jarek Potiuk <[email protected]> AuthorDate: Mon Nov 16 04:30:49 2026 +0900 Add breeze commands to move a pull request between project and fork CI Adding `use project ci` by hand left the pull request mergeable on its fork result until the next monitor run, and did not start project CI. The pull_request_target revoke that closed the gap was dropped because that trigger is being disabled, so the maintainer's own command now withholds the result before adding the label, then restarts the pull request. Generated-by: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01NyQq9U7i5HhjDgrQ9qd3gq --- dev/breeze/doc/08_ci_tasks.rst | 37 +++++ dev/breeze/doc/images/output_ci.svg | 28 +++- dev/breeze/doc/images/output_ci.txt | 2 +- dev/breeze/doc/images/output_ci_use-fork-ci.svg | 112 +++++++++++++ dev/breeze/doc/images/output_ci_use-fork-ci.txt | 1 + dev/breeze/doc/images/output_ci_use-project-ci.svg | 124 +++++++++++++++ dev/breeze/doc/images/output_ci_use-project-ci.txt | 1 + .../src/airflow_breeze/commands/ci_commands.py | 51 ++++++ .../airflow_breeze/commands/ci_commands_config.py | 19 +++ .../src/airflow_breeze/utils/fork_ci_github.py | 7 + .../src/airflow_breeze/utils/fork_ci_pr_move.py | 118 ++++++++++++++ dev/breeze/tests/test_fork_ci_github.py | 18 +++ dev/breeze/tests/test_fork_ci_pr_move.py | 177 +++++++++++++++++++++ 13 files changed, 686 insertions(+), 9 deletions(-) diff --git a/dev/breeze/doc/08_ci_tasks.rst b/dev/breeze/doc/08_ci_tasks.rst index c4001b658c0..571237b0454 100644 --- a/dev/breeze/doc/08_ci_tasks.rst +++ b/dev/breeze/doc/08_ci_tasks.rst @@ -226,6 +226,43 @@ These are all available flags of ``approve-workflow-runs`` command: :width: 100% :alt: Breeze ci approve-workflow-runs +Moving a pull request between project CI and fork CI +---------------------------------------------------- + +A maintainer moves a single pull request to project CI with the ``use project ci`` label, and back +to fork CI with ``use fork ci`` (AIP-120). The fork CI monitor reacts to the labels only on its next +run, so a fork ``CI passed`` success posted before ``use project ci`` was added still lets the pull +request merge until then, and the label alone does not start project CI. ``use-project-ci`` does both +in order, with your own ``gh`` credentials: it sets a fork ``CI passed`` success on the head commit +to ``pending`` first, then adds ``use project ci`` (removing ``use fork ci``), and closes and reopens +the pull request to start project CI (``--no-restart`` leaves that to the author's next push). A +``CI passed`` success that project CI earned is kept. + +.. code-block:: bash + + breeze ci use-project-ci 12345 + +These are all available flags of ``use-project-ci`` command: + +.. image:: ./images/output_ci_use-project-ci.svg + :target: https://raw.githubusercontent.com/apache/airflow/main/dev/breeze/images/output_ci_use-project-ci.svg + :width: 100% + :alt: Breeze ci use-project-ci + +``use-fork-ci`` labels the pull request ``use fork ci`` instead of ``use project ci``. The monitor +gates it on its next run, and fork CI runs on the author's next push to their fork. + +.. code-block:: bash + + breeze ci use-fork-ci 12345 + +These are all available flags of ``use-fork-ci`` command: + +.. image:: ./images/output_ci_use-fork-ci.svg + :target: https://raw.githubusercontent.com/apache/airflow/main/dev/breeze/images/output_ci_use-fork-ci.svg + :width: 100% + :alt: Breeze ci use-fork-ci + ----- Next step: Follow the `Release management tasks <09_release_management_tasks.rst>`_ guide to learn how diff --git a/dev/breeze/doc/images/output_ci.svg b/dev/breeze/doc/images/output_ci.svg index f0dfe263de9..700d0402d10 100644 --- a/dev/breeze/doc/images/output_ci.svg +++ b/dev/breeze/doc/images/output_ci.svg @@ -1,4 +1,4 @@ -<svg class="rich-terminal" viewBox="0 0 1482 635.5999999999999" xmlns="http://www.w3.org/2000/svg"> +<svg class="rich-terminal" viewBox="0 0 1482 708.8" xmlns="http://www.w3.org/2000/svg"> <!-- Generated with Rich https://www.textualize.io --> <style> @@ -42,7 +42,7 @@ <defs> <clipPath id="breeze-ci-clip-terminal"> - <rect x="0" y="0" width="1463.0" height="584.5999999999999" /> + <rect x="0" y="0" width="1463.0" height="657.8" /> </clipPath> <clipPath id="breeze-ci-line-0"> <rect x="0" y="1.5" width="1464" height="24.65"/> @@ -113,9 +113,18 @@ <clipPath id="breeze-ci-line-22"> <rect x="0" y="538.3" width="1464" height="24.65"/> </clipPath> +<clipPath id="breeze-ci-line-23"> + <rect x="0" y="562.7" width="1464" height="24.65"/> + </clipPath> +<clipPath id="breeze-ci-line-24"> + <rect x="0" y="587.1" width="1464" height="24.65"/> + </clipPath> +<clipPath id="breeze-ci-line-25"> + <rect x="0" y="611.5" width="1464" height="24.65"/> + </clipPath> </defs> - <rect fill="#292929" stroke="rgba(255,255,255,0.35)" stroke-width="1" x="1" y="1" width="1480" height="633.6" rx="8"/><text class="breeze-ci-title" fill="#c5c8c6" text-anchor="middle" x="740" y="27">Command: ci</text> + <rect fill="#292929" stroke="rgba(255,255,255,0.35)" stroke-width="1" x="1" y="1" width="1480" height="706.8" rx="8"/><text class="breeze-ci-title" fill="#c5c8c6" text-anchor="middle" x="740" y="27">Command: ci</text> <g transform="translate(26,22)"> <circle cx="0" cy="0" r="7" fill="#ff5f57"/> <circle cx="22" cy="0" r="7" fill="#febc2e"/> @@ -144,11 +153,14 @@ </text><text class="breeze-ci-r5" x="0" y="410.4" textLength="12.2" clip-path="url(#breeze-ci-line-16)">│</text><text class="breeze-ci-r4" x="24.4" y="410.4" textLength="256.2" clip-path="url(#breeze-ci-line-16)">set-milestone        </text><text class="breeze-ci-r1" x="305" y="410.4" textLength="1134.6" clip-path="url(#breeze-ci-line-16)">Set milestone on a merged PR if it doesn't have one. [...] </text><text class="breeze-ci-r5" x="0" y="434.8" textLength="12.2" clip-path="url(#breeze-ci-line-17)">│</text><text class="breeze-ci-r1" x="305" y="434.8" textLength="1134.6" clip-path="url(#breeze-ci-line-17)">workflow.                                              [...] </text><text class="breeze-ci-r5" x="0" y="459.2" textLength="12.2" clip-path="url(#breeze-ci-line-18)">│</text><text class="breeze-ci-r4" x="24.4" y="459.2" textLength="256.2" clip-path="url(#breeze-ci-line-18)">fork-ci-reconcile    </text><text class="breeze-ci-r1" x="305" y="459.2" textLength="1134.6" clip-path="url(#breeze-ci-line-18)">Reconcile pull requests whose CI runs in contributor forks (AIP-120). Run o [...] -</text><text class="breeze-ci-r5" x="0" y="483.6" textLength="12.2" clip-path="url(#breeze-ci-line-19)">│</text><text class="breeze-ci-r4" x="24.4" y="483.6" textLength="256.2" clip-path="url(#breeze-ci-line-19)">audit                </text><text class="breeze-ci-r1" x="305" y="483.6" textLength="1134.6" clip-path="url(#breeze-ci-line-19)">Check that your checkout and fork are [...] -</text><text class="breeze-ci-r5" x="0" y="508" textLength="12.2" clip-path="url(#breeze-ci-line-20)">│</text><text class="breeze-ci-r4" x="24.4" y="508" textLength="256.2" clip-path="url(#breeze-ci-line-20)">approve-workflow-runs</text><text class="breeze-ci-r1" x="305" y="508" textLength="1134.6" clip-path="url(#breeze-ci-line-20)">Approve the waiting workflow runs of external contributors' PRs whose changes are safe  [...] -</text><text class="breeze-ci-r5" x="0" y="532.4" textLength="12.2" clip-path="url(#breeze-ci-line-21)">│</text><text class="breeze-ci-r1" x="305" y="532.4" textLength="1134.6" clip-path="url(#breeze-ci-line-21)">without a maintainer's look, and label the others 'need workflow approval' with a 'Workflow  </text><text class="breeze-ci-r5" x="1451.8" y="532.4" textLength="12.2" clip-path="url(#br [...] -</text><text class="breeze-ci-r5" x="0" y="556.8" textLength="12.2" clip-path="url(#breeze-ci-line-22)">│</text><text class="breeze-ci-r1" x="305" y="556.8" textLength="1134.6" clip-path="url(#breeze-ci-line-22)">approval' commit status saying why. Used by the auto-approve-workflow-runs workflow.         </text><text class="breeze-ci-r5" x="1451.8" y="556.8" textLength="12.2" clip-path="url(#br [...] -</text><text class="breeze-ci-r5" x="0" y="581.2" textLength="1464" clip-path="url(#breeze-ci-line-23)">╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯</text><text class="breeze-ci-r1" x="1464" y="581.2" textLength="12.2" clip-path="url(#breeze-ci-line-23)"> +</text><text class="breeze-ci-r5" x="0" y="483.6" textLength="12.2" clip-path="url(#breeze-ci-line-19)">│</text><text class="breeze-ci-r4" x="24.4" y="483.6" textLength="256.2" clip-path="url(#breeze-ci-line-19)">use-project-ci       </text><text class="breeze-ci-r1" x="305" y="483.6" textLength="1134.6" clip-path="url(#breeze-ci-line-19)">Move a pull request to project CI (AIP-120): withhold its fork [...] +</text><text class="breeze-ci-r5" x="0" y="508" textLength="12.2" clip-path="url(#breeze-ci-line-20)">│</text><text class="breeze-ci-r1" x="305" y="508" textLength="1134.6" clip-path="url(#breeze-ci-line-20)">project ci` and close and reopen it to start project CI.                              [...] +</text><text class="breeze-ci-r5" x="0" y="532.4" textLength="12.2" clip-path="url(#breeze-ci-line-21)">│</text><text class="breeze-ci-r4" x="24.4" y="532.4" textLength="256.2" clip-path="url(#breeze-ci-line-21)">use-fork-ci          </text><text class="breeze-ci-r1" x="305" y="532.4" textLength="1134.6" clip-path="url(#breeze-ci-line-21)">Move a pull request to fork CI (AIP-120): label it  [...] +</text><text class="breeze-ci-r5" x="0" y="556.8" textLength="12.2" clip-path="url(#breeze-ci-line-22)">│</text><text class="breeze-ci-r4" x="24.4" y="556.8" textLength="256.2" clip-path="url(#breeze-ci-line-22)">audit                </text><text class="breeze-ci-r1" x="305" y="556.8" textLength="1134.6" clip-path="url(#breeze-ci-line-22)">Check that your checkout and fork are [...] +</text><text class="breeze-ci-r5" x="0" y="581.2" textLength="12.2" clip-path="url(#breeze-ci-line-23)">│</text><text class="breeze-ci-r4" x="24.4" y="581.2" textLength="256.2" clip-path="url(#breeze-ci-line-23)">approve-workflow-runs</text><text class="breeze-ci-r1" x="305" y="581.2" textLength="1134.6" clip-path="url(#breeze-ci-line-23)">Approve the waiting workflow runs of external contributors' PRs whose changes are saf [...] +</text><text class="breeze-ci-r5" x="0" y="605.6" textLength="12.2" clip-path="url(#breeze-ci-line-24)">│</text><text class="breeze-ci-r1" x="305" y="605.6" textLength="1134.6" clip-path="url(#breeze-ci-line-24)">without a maintainer's look, and label the others 'need workflow approval' with a 'Workflow  </text><text class="breeze-ci-r5" x="1451.8" y="605.6" textLength="12.2" clip-path="url(#br [...] +</text><text class="breeze-ci-r5" x="0" y="630" textLength="12.2" clip-path="url(#breeze-ci-line-25)">│</text><text class="breeze-ci-r1" x="305" y="630" textLength="1134.6" clip-path="url(#breeze-ci-line-25)">approval' commit status saying why. Used by the auto-approve-workflow-runs workflow.         </text><text class="breeze-ci-r5" x="1451.8" y="630" textLength="12.2" clip-path="url(#breeze-c [...] +</text><text class="breeze-ci-r5" x="0" y="654.4" textLength="1464" clip-path="url(#breeze-ci-line-26)">╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯</text><text class="breeze-ci-r1" x="1464" y="654.4" textLength="12.2" clip-path="url(#breeze-ci-line-26)"> </text> </g> </g> diff --git a/dev/breeze/doc/images/output_ci.txt b/dev/breeze/doc/images/output_ci.txt index f2021370546..e9027ac44d6 100644 --- a/dev/breeze/doc/images/output_ci.txt +++ b/dev/breeze/doc/images/output_ci.txt @@ -1 +1 @@ -bc527ecfffabbbd21bdcf1fbbfe25bc5 +920b300247600a80acaf9e57075149bb diff --git a/dev/breeze/doc/images/output_ci_use-fork-ci.svg b/dev/breeze/doc/images/output_ci_use-fork-ci.svg new file mode 100644 index 00000000000..171aaccfdcc --- /dev/null +++ b/dev/breeze/doc/images/output_ci_use-fork-ci.svg @@ -0,0 +1,112 @@ +<svg class="rich-terminal" viewBox="0 0 1482 367.2" xmlns="http://www.w3.org/2000/svg"> + <!-- Generated with Rich https://www.textualize.io --> + <style> + + @font-face { + font-family: "Fira Code"; + src: local("FiraCode-Regular"), + url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Regular.woff2") format("woff2"), + url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Regular.woff") format("woff"); + font-style: normal; + font-weight: 400; + } + @font-face { + font-family: "Fira Code"; + src: local("FiraCode-Bold"), + url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Bold.woff2") format("woff2"), + url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Bold.woff") format("woff"); + font-style: bold; + font-weight: 700; + } + + .breeze-ci-use-fork-ci-matrix { + font-family: Fira Code, monospace; + font-size: 20px; + line-height: 24.4px; + font-variant-east-asian: full-width; + } + + .breeze-ci-use-fork-ci-title { + font-size: 18px; + font-weight: bold; + font-family: arial; + } + + .breeze-ci-use-fork-ci-r1 { fill: #c5c8c6 } +.breeze-ci-use-fork-ci-r2 { fill: #d0b344 } +.breeze-ci-use-fork-ci-r3 { fill: #c5c8c6;font-weight: bold } +.breeze-ci-use-fork-ci-r4 { fill: #68a0b3;font-weight: bold } +.breeze-ci-use-fork-ci-r5 { fill: #868887 } +.breeze-ci-use-fork-ci-r6 { fill: #98a84b;font-weight: bold } +.breeze-ci-use-fork-ci-r7 { fill: #8d7b39 } + </style> + + <defs> + <clipPath id="breeze-ci-use-fork-ci-clip-terminal"> + <rect x="0" y="0" width="1463.0" height="316.2" /> + </clipPath> + <clipPath id="breeze-ci-use-fork-ci-line-0"> + <rect x="0" y="1.5" width="1464" height="24.65"/> + </clipPath> +<clipPath id="breeze-ci-use-fork-ci-line-1"> + <rect x="0" y="25.9" width="1464" height="24.65"/> + </clipPath> +<clipPath id="breeze-ci-use-fork-ci-line-2"> + <rect x="0" y="50.3" width="1464" height="24.65"/> + </clipPath> +<clipPath id="breeze-ci-use-fork-ci-line-3"> + <rect x="0" y="74.7" width="1464" height="24.65"/> + </clipPath> +<clipPath id="breeze-ci-use-fork-ci-line-4"> + <rect x="0" y="99.1" width="1464" height="24.65"/> + </clipPath> +<clipPath id="breeze-ci-use-fork-ci-line-5"> + <rect x="0" y="123.5" width="1464" height="24.65"/> + </clipPath> +<clipPath id="breeze-ci-use-fork-ci-line-6"> + <rect x="0" y="147.9" width="1464" height="24.65"/> + </clipPath> +<clipPath id="breeze-ci-use-fork-ci-line-7"> + <rect x="0" y="172.3" width="1464" height="24.65"/> + </clipPath> +<clipPath id="breeze-ci-use-fork-ci-line-8"> + <rect x="0" y="196.7" width="1464" height="24.65"/> + </clipPath> +<clipPath id="breeze-ci-use-fork-ci-line-9"> + <rect x="0" y="221.1" width="1464" height="24.65"/> + </clipPath> +<clipPath id="breeze-ci-use-fork-ci-line-10"> + <rect x="0" y="245.5" width="1464" height="24.65"/> + </clipPath> +<clipPath id="breeze-ci-use-fork-ci-line-11"> + <rect x="0" y="269.9" width="1464" height="24.65"/> + </clipPath> + </defs> + + <rect fill="#292929" stroke="rgba(255,255,255,0.35)" stroke-width="1" x="1" y="1" width="1480" height="365.2" rx="8"/><text class="breeze-ci-use-fork-ci-title" fill="#c5c8c6" text-anchor="middle" x="740" y="27">Command: ci use-fork-ci</text> + <g transform="translate(26,22)"> + <circle cx="0" cy="0" r="7" fill="#ff5f57"/> + <circle cx="22" cy="0" r="7" fill="#febc2e"/> + <circle cx="44" cy="0" r="7" fill="#28c840"/> + </g> + + <g transform="translate(9, 41)" clip-path="url(#breeze-ci-use-fork-ci-clip-terminal)"> + + <g class="breeze-ci-use-fork-ci-matrix"> + <text class="breeze-ci-use-fork-ci-r1" x="1464" y="20" textLength="12.2" clip-path="url(#breeze-ci-use-fork-ci-line-0)"> +</text><text class="breeze-ci-use-fork-ci-r2" x="12.2" y="44.4" textLength="73.2" clip-path="url(#breeze-ci-use-fork-ci-line-1)">Usage:</text><text class="breeze-ci-use-fork-ci-r3" x="97.6" y="44.4" textLength="256.2" clip-path="url(#breeze-ci-use-fork-ci-line-1)">breeze ci use-fork-ci</text><text class="breeze-ci-use-fork-ci-r1" x="366" y="44.4" textLength="12.2" clip-path="url(#breeze-ci-use-fork-ci-line-1)">[</text><text class="breeze-ci-use-fork-ci-r4" x="378.2" y="44.4" te [...] +</text><text class="breeze-ci-use-fork-ci-r1" x="1464" y="68.8" textLength="12.2" clip-path="url(#breeze-ci-use-fork-ci-line-2)"> +</text><text class="breeze-ci-use-fork-ci-r1" x="12.2" y="93.2" textLength="1134.6" clip-path="url(#breeze-ci-use-fork-ci-line-3)">Move a pull request to fork CI (AIP-120): label it `use fork ci` instead of `use project ci`.</text><text class="breeze-ci-use-fork-ci-r1" x="1464" y="93.2" textLength="12.2" clip-path="url(#breeze-ci-use-fork-ci-line-3)"> +</text><text class="breeze-ci-use-fork-ci-r1" x="1464" y="117.6" textLength="12.2" clip-path="url(#breeze-ci-use-fork-ci-line-4)"> +</text><text class="breeze-ci-use-fork-ci-r5" x="0" y="142" textLength="24.4" clip-path="url(#breeze-ci-use-fork-ci-line-5)">╭─</text><text class="breeze-ci-use-fork-ci-r5" x="24.4" y="142" textLength="231.8" clip-path="url(#breeze-ci-use-fork-ci-line-5)"> Use fork CI flags </text><text class="breeze-ci-use-fork-ci-r5" x="256.2" y="142" textLength="1183.4" clip-path="url(#breeze-ci-use-fork-ci-line-5)">───────────────────────────────────────────────────────────── [...] +</text><text class="breeze-ci-use-fork-ci-r5" x="0" y="166.4" textLength="12.2" clip-path="url(#breeze-ci-use-fork-ci-line-6)">│</text><text class="breeze-ci-use-fork-ci-r4" x="24.4" y="166.4" textLength="231.8" clip-path="url(#breeze-ci-use-fork-ci-line-6)">--github-repository</text><text class="breeze-ci-use-fork-ci-r6" x="280.6" y="166.4" textLength="24.4" clip-path="url(#breeze-ci-use-fork-ci-line-6)">-g</text><text class="breeze-ci-use-fork-ci-r1" x="329.4" y="166.4" textLength="597 [...] +</text><text class="breeze-ci-use-fork-ci-r5" x="0" y="190.8" textLength="1464" clip-path="url(#breeze-ci-use-fork-ci-line-7)">╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯</text><text class="breeze-ci-use-fork-ci-r1" x="1464" y="190.8" textLength="12.2" clip-path="url(#breeze-ci-use-fork-ci-line-7)"> +</text><text class="breeze-ci-use-fork-ci-r5" x="0" y="215.2" textLength="24.4" clip-path="url(#breeze-ci-use-fork-ci-line-8)">╭─</text><text class="breeze-ci-use-fork-ci-r5" x="24.4" y="215.2" textLength="195.2" clip-path="url(#breeze-ci-use-fork-ci-line-8)"> Common options </text><text class="breeze-ci-use-fork-ci-r5" x="219.6" y="215.2" textLength="1220" clip-path="url(#breeze-ci-use-fork-ci-line-8)">────────────────────────────────────────────────────────────────────── [...] +</text><text class="breeze-ci-use-fork-ci-r5" x="0" y="239.6" textLength="12.2" clip-path="url(#breeze-ci-use-fork-ci-line-9)">│</text><text class="breeze-ci-use-fork-ci-r4" x="24.4" y="239.6" textLength="109.8" clip-path="url(#breeze-ci-use-fork-ci-line-9)">--verbose</text><text class="breeze-ci-use-fork-ci-r6" x="158.6" y="239.6" textLength="24.4" clip-path="url(#breeze-ci-use-fork-ci-line-9)">-v</text><text class="breeze-ci-use-fork-ci-r1" x="207.4" y="239.6" textLength="585.6" clip-p [...] +</text><text class="breeze-ci-use-fork-ci-r5" x="0" y="264" textLength="12.2" clip-path="url(#breeze-ci-use-fork-ci-line-10)">│</text><text class="breeze-ci-use-fork-ci-r4" x="24.4" y="264" textLength="109.8" clip-path="url(#breeze-ci-use-fork-ci-line-10)">--dry-run</text><text class="breeze-ci-use-fork-ci-r6" x="158.6" y="264" textLength="24.4" clip-path="url(#breeze-ci-use-fork-ci-line-10)">-D</text><text class="breeze-ci-use-fork-ci-r1" x="207.4" y="264" textLength="719.8" clip-path=" [...] +</text><text class="breeze-ci-use-fork-ci-r5" x="0" y="288.4" textLength="12.2" clip-path="url(#breeze-ci-use-fork-ci-line-11)">│</text><text class="breeze-ci-use-fork-ci-r4" x="24.4" y="288.4" textLength="109.8" clip-path="url(#breeze-ci-use-fork-ci-line-11)">--help   </text><text class="breeze-ci-use-fork-ci-r6" x="158.6" y="288.4" textLength="24.4" clip-path="url(#breeze-ci-use-fork-ci-line-11)">-h</text><text class="breeze-ci-use-fork-ci-r1" x="207.4" y="288.4" textLen [...] +</text><text class="breeze-ci-use-fork-ci-r5" x="0" y="312.8" textLength="1464" clip-path="url(#breeze-ci-use-fork-ci-line-12)">╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯</text><text class="breeze-ci-use-fork-ci-r1" x="1464" y="312.8" textLength="12.2" clip-path="url(#breeze-ci-use-fork-ci-line-12)"> +</text> + </g> + </g> +</svg> diff --git a/dev/breeze/doc/images/output_ci_use-fork-ci.txt b/dev/breeze/doc/images/output_ci_use-fork-ci.txt new file mode 100644 index 00000000000..f03800ed603 --- /dev/null +++ b/dev/breeze/doc/images/output_ci_use-fork-ci.txt @@ -0,0 +1 @@ +cb4f4dbcb648025d3c5843ebcb26e70e diff --git a/dev/breeze/doc/images/output_ci_use-project-ci.svg b/dev/breeze/doc/images/output_ci_use-project-ci.svg new file mode 100644 index 00000000000..356f0e93cd4 --- /dev/null +++ b/dev/breeze/doc/images/output_ci_use-project-ci.svg @@ -0,0 +1,124 @@ +<svg class="rich-terminal" viewBox="0 0 1482 440.4" xmlns="http://www.w3.org/2000/svg"> + <!-- Generated with Rich https://www.textualize.io --> + <style> + + @font-face { + font-family: "Fira Code"; + src: local("FiraCode-Regular"), + url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Regular.woff2") format("woff2"), + url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Regular.woff") format("woff"); + font-style: normal; + font-weight: 400; + } + @font-face { + font-family: "Fira Code"; + src: local("FiraCode-Bold"), + url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Bold.woff2") format("woff2"), + url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Bold.woff") format("woff"); + font-style: bold; + font-weight: 700; + } + + .breeze-ci-use-project-ci-matrix { + font-family: Fira Code, monospace; + font-size: 20px; + line-height: 24.4px; + font-variant-east-asian: full-width; + } + + .breeze-ci-use-project-ci-title { + font-size: 18px; + font-weight: bold; + font-family: arial; + } + + .breeze-ci-use-project-ci-r1 { fill: #c5c8c6 } +.breeze-ci-use-project-ci-r2 { fill: #d0b344 } +.breeze-ci-use-project-ci-r3 { fill: #c5c8c6;font-weight: bold } +.breeze-ci-use-project-ci-r4 { fill: #68a0b3;font-weight: bold } +.breeze-ci-use-project-ci-r5 { fill: #868887 } +.breeze-ci-use-project-ci-r6 { fill: #98a84b;font-weight: bold } +.breeze-ci-use-project-ci-r7 { fill: #8d7b39 } + </style> + + <defs> + <clipPath id="breeze-ci-use-project-ci-clip-terminal"> + <rect x="0" y="0" width="1463.0" height="389.4" /> + </clipPath> + <clipPath id="breeze-ci-use-project-ci-line-0"> + <rect x="0" y="1.5" width="1464" height="24.65"/> + </clipPath> +<clipPath id="breeze-ci-use-project-ci-line-1"> + <rect x="0" y="25.9" width="1464" height="24.65"/> + </clipPath> +<clipPath id="breeze-ci-use-project-ci-line-2"> + <rect x="0" y="50.3" width="1464" height="24.65"/> + </clipPath> +<clipPath id="breeze-ci-use-project-ci-line-3"> + <rect x="0" y="74.7" width="1464" height="24.65"/> + </clipPath> +<clipPath id="breeze-ci-use-project-ci-line-4"> + <rect x="0" y="99.1" width="1464" height="24.65"/> + </clipPath> +<clipPath id="breeze-ci-use-project-ci-line-5"> + <rect x="0" y="123.5" width="1464" height="24.65"/> + </clipPath> +<clipPath id="breeze-ci-use-project-ci-line-6"> + <rect x="0" y="147.9" width="1464" height="24.65"/> + </clipPath> +<clipPath id="breeze-ci-use-project-ci-line-7"> + <rect x="0" y="172.3" width="1464" height="24.65"/> + </clipPath> +<clipPath id="breeze-ci-use-project-ci-line-8"> + <rect x="0" y="196.7" width="1464" height="24.65"/> + </clipPath> +<clipPath id="breeze-ci-use-project-ci-line-9"> + <rect x="0" y="221.1" width="1464" height="24.65"/> + </clipPath> +<clipPath id="breeze-ci-use-project-ci-line-10"> + <rect x="0" y="245.5" width="1464" height="24.65"/> + </clipPath> +<clipPath id="breeze-ci-use-project-ci-line-11"> + <rect x="0" y="269.9" width="1464" height="24.65"/> + </clipPath> +<clipPath id="breeze-ci-use-project-ci-line-12"> + <rect x="0" y="294.3" width="1464" height="24.65"/> + </clipPath> +<clipPath id="breeze-ci-use-project-ci-line-13"> + <rect x="0" y="318.7" width="1464" height="24.65"/> + </clipPath> +<clipPath id="breeze-ci-use-project-ci-line-14"> + <rect x="0" y="343.1" width="1464" height="24.65"/> + </clipPath> + </defs> + + <rect fill="#292929" stroke="rgba(255,255,255,0.35)" stroke-width="1" x="1" y="1" width="1480" height="438.4" rx="8"/><text class="breeze-ci-use-project-ci-title" fill="#c5c8c6" text-anchor="middle" x="740" y="27">Command: ci use-project-ci</text> + <g transform="translate(26,22)"> + <circle cx="0" cy="0" r="7" fill="#ff5f57"/> + <circle cx="22" cy="0" r="7" fill="#febc2e"/> + <circle cx="44" cy="0" r="7" fill="#28c840"/> + </g> + + <g transform="translate(9, 41)" clip-path="url(#breeze-ci-use-project-ci-clip-terminal)"> + + <g class="breeze-ci-use-project-ci-matrix"> + <text class="breeze-ci-use-project-ci-r1" x="1464" y="20" textLength="12.2" clip-path="url(#breeze-ci-use-project-ci-line-0)"> +</text><text class="breeze-ci-use-project-ci-r2" x="12.2" y="44.4" textLength="73.2" clip-path="url(#breeze-ci-use-project-ci-line-1)">Usage:</text><text class="breeze-ci-use-project-ci-r3" x="97.6" y="44.4" textLength="292.8" clip-path="url(#breeze-ci-use-project-ci-line-1)">breeze ci use-project-ci</text><text class="breeze-ci-use-project-ci-r1" x="402.6" y="44.4" textLength="12.2" clip-path="url(#breeze-ci-use-project-ci-line-1)">[</text><text class="breeze-ci-use-project-ci [...] +</text><text class="breeze-ci-use-project-ci-r1" x="1464" y="68.8" textLength="12.2" clip-path="url(#breeze-ci-use-project-ci-line-2)"> +</text><text class="breeze-ci-use-project-ci-r1" x="12.2" y="93.2" textLength="1439.6" clip-path="url(#breeze-ci-use-project-ci-line-3)">Move a pull request to project CI (AIP-120): withhold its fork CI `CI passed`, label it `use project ci` and close and</text><text class="breeze-ci-use-project-ci-r1" x="1464" y="93.2" textLength="12.2" clip-path="url(#breeze-ci-use-project-ci-line-3)"> +</text><text class="breeze-ci-use-project-ci-r1" x="12.2" y="117.6" textLength="366" clip-path="url(#breeze-ci-use-project-ci-line-4)">reopen it to start project CI.</text><text class="breeze-ci-use-project-ci-r1" x="1464" y="117.6" textLength="12.2" clip-path="url(#breeze-ci-use-project-ci-line-4)"> +</text><text class="breeze-ci-use-project-ci-r1" x="1464" y="142" textLength="12.2" clip-path="url(#breeze-ci-use-project-ci-line-5)"> +</text><text class="breeze-ci-use-project-ci-r5" x="0" y="166.4" textLength="24.4" clip-path="url(#breeze-ci-use-project-ci-line-6)">╭─</text><text class="breeze-ci-use-project-ci-r5" x="24.4" y="166.4" textLength="268.4" clip-path="url(#breeze-ci-use-project-ci-line-6)"> Use project CI flags </text><text class="breeze-ci-use-project-ci-r5" x="292.8" y="166.4" textLength="1146.8" clip-path="url(#breeze-ci-use-project-ci-line-6)">────────────────────────────────── [...] +</text><text class="breeze-ci-use-project-ci-r5" x="0" y="190.8" textLength="12.2" clip-path="url(#breeze-ci-use-project-ci-line-7)">│</text><text class="breeze-ci-use-project-ci-r4" x="24.4" y="190.8" textLength="109.8" clip-path="url(#breeze-ci-use-project-ci-line-7)">--restart</text><text class="breeze-ci-use-project-ci-r1" x="134.2" y="190.8" textLength="12.2" clip-path="url(#breeze-ci-use-project-ci-line-7)">/</text><text class="breeze-ci-use-project-ci-r4" x="146.4" y="190.8" textL [...] +</text><text class="breeze-ci-use-project-ci-r5" x="0" y="215.2" textLength="12.2" clip-path="url(#breeze-ci-use-project-ci-line-8)">│</text><text class="breeze-ci-use-project-ci-r1" x="366" y="215.2" textLength="573.4" clip-path="url(#breeze-ci-use-project-ci-line-8)">it, project CI runs on the author's next push. </text><text class="breeze-ci-use-project-ci-r5" x="939.4" y="215.2" textLength="219.6" clip-path="url(#breeze-ci-use-project [...] +</text><text class="breeze-ci-use-project-ci-r5" x="0" y="239.6" textLength="12.2" clip-path="url(#breeze-ci-use-project-ci-line-9)">│</text><text class="breeze-ci-use-project-ci-r4" x="24.4" y="239.6" textLength="268.4" clip-path="url(#breeze-ci-use-project-ci-line-9)">--github-repository   </text><text class="breeze-ci-use-project-ci-r6" x="317.2" y="239.6" textLength="24.4" clip-path="url(#breeze-ci-use-project-ci-line-9)">-g</text><text class="breeze-ci-use-project-ci- [...] +</text><text class="breeze-ci-use-project-ci-r5" x="0" y="264" textLength="1464" clip-path="url(#breeze-ci-use-project-ci-line-10)">╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯</text><text class="breeze-ci-use-project-ci-r1" x="1464" y="264" textLength="12.2" clip-path="url(#breeze-ci-use-project-ci-line-10)"> +</text><text class="breeze-ci-use-project-ci-r5" x="0" y="288.4" textLength="24.4" clip-path="url(#breeze-ci-use-project-ci-line-11)">╭─</text><text class="breeze-ci-use-project-ci-r5" x="24.4" y="288.4" textLength="195.2" clip-path="url(#breeze-ci-use-project-ci-line-11)"> Common options </text><text class="breeze-ci-use-project-ci-r5" x="219.6" y="288.4" textLength="1220" clip-path="url(#breeze-ci-use-project-ci-line-11)">───────────────────────────────────────────────── [...] +</text><text class="breeze-ci-use-project-ci-r5" x="0" y="312.8" textLength="12.2" clip-path="url(#breeze-ci-use-project-ci-line-12)">│</text><text class="breeze-ci-use-project-ci-r4" x="24.4" y="312.8" textLength="109.8" clip-path="url(#breeze-ci-use-project-ci-line-12)">--verbose</text><text class="breeze-ci-use-project-ci-r6" x="158.6" y="312.8" textLength="24.4" clip-path="url(#breeze-ci-use-project-ci-line-12)">-v</text><text class="breeze-ci-use-project-ci-r1" x="207.4" y="312.8" t [...] +</text><text class="breeze-ci-use-project-ci-r5" x="0" y="337.2" textLength="12.2" clip-path="url(#breeze-ci-use-project-ci-line-13)">│</text><text class="breeze-ci-use-project-ci-r4" x="24.4" y="337.2" textLength="109.8" clip-path="url(#breeze-ci-use-project-ci-line-13)">--dry-run</text><text class="breeze-ci-use-project-ci-r6" x="158.6" y="337.2" textLength="24.4" clip-path="url(#breeze-ci-use-project-ci-line-13)">-D</text><text class="breeze-ci-use-project-ci-r1" x="207.4" y="337.2" t [...] +</text><text class="breeze-ci-use-project-ci-r5" x="0" y="361.6" textLength="12.2" clip-path="url(#breeze-ci-use-project-ci-line-14)">│</text><text class="breeze-ci-use-project-ci-r4" x="24.4" y="361.6" textLength="109.8" clip-path="url(#breeze-ci-use-project-ci-line-14)">--help   </text><text class="breeze-ci-use-project-ci-r6" x="158.6" y="361.6" textLength="24.4" clip-path="url(#breeze-ci-use-project-ci-line-14)">-h</text><text class="breeze-ci-use-project-ci-r1" x="207 [...] +</text><text class="breeze-ci-use-project-ci-r5" x="0" y="386" textLength="1464" clip-path="url(#breeze-ci-use-project-ci-line-15)">╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯</text><text class="breeze-ci-use-project-ci-r1" x="1464" y="386" textLength="12.2" clip-path="url(#breeze-ci-use-project-ci-line-15)"> +</text> + </g> + </g> +</svg> diff --git a/dev/breeze/doc/images/output_ci_use-project-ci.txt b/dev/breeze/doc/images/output_ci_use-project-ci.txt new file mode 100644 index 00000000000..373536a474a --- /dev/null +++ b/dev/breeze/doc/images/output_ci_use-project-ci.txt @@ -0,0 +1 @@ +24303163fc12e3cbf972c20949b16d03 diff --git a/dev/breeze/src/airflow_breeze/commands/ci_commands.py b/dev/breeze/src/airflow_breeze/commands/ci_commands.py index 81ea81a5228..d79dffe6502 100644 --- a/dev/breeze/src/airflow_breeze/commands/ci_commands.py +++ b/dev/breeze/src/airflow_breeze/commands/ci_commands.py @@ -1796,6 +1796,57 @@ def fork_ci_reconcile( f.write(get_ga_output("has-alerts", has_alerts) + "\n") +def _move_pull_request(move, pr_number: int, github_repository: str) -> None: + from airflow_breeze.utils.fork_ci_github import GitHubClient, GitHubError + from airflow_breeze.utils.shared_options import get_dry_run + + try: + done = move(GitHubClient(github_repository, dry_run=get_dry_run()), pr_number) + except (GitHubError, ValueError) as e: + console_print(f"[error]#{pr_number}: {e}[/]") + sys.exit(1) + for action in done or ["nothing to change"]: + console_print(f"[info]#{pr_number}: {action}[/]") + + +@ci_group.command( + name="use-project-ci", + help="Move a pull request to project CI (AIP-120): withhold its fork CI `CI passed`, label it " + "`use project ci` and close and reopen it to start project CI.", +) [email protected]("pr_number", type=int) [email protected]( + "--restart/--no-restart", + default=True, + show_default=True, + help="Close and reopen the pull request to start project CI, which the label does not. Without " + "it, project CI runs on the author's next push.", +) +@option_github_repository +@option_verbose +@option_dry_run +def use_project_ci(pr_number: int, restart: bool, github_repository: str): + import functools + + from airflow_breeze.utils.fork_ci_pr_move import move_to_project_ci + + _move_pull_request(functools.partial(move_to_project_ci, restart=restart), pr_number, github_repository) + + +@ci_group.command( + name="use-fork-ci", + help="Move a pull request to fork CI (AIP-120): label it `use fork ci` instead of `use project ci`.", +) [email protected]("pr_number", type=int) +@option_github_repository +@option_verbose +@option_dry_run +def use_fork_ci(pr_number: int, github_repository: str): + from airflow_breeze.utils.fork_ci_pr_move import move_to_fork_ci + + _move_pull_request(move_to_fork_ci, pr_number, github_repository) + + @ci_group.command( name="audit", help="Check that your checkout and fork are set up for fork CI (AIP-120), and offer the fixes.", diff --git a/dev/breeze/src/airflow_breeze/commands/ci_commands_config.py b/dev/breeze/src/airflow_breeze/commands/ci_commands_config.py index 8e28de10af7..b1fe72c9d53 100644 --- a/dev/breeze/src/airflow_breeze/commands/ci_commands_config.py +++ b/dev/breeze/src/airflow_breeze/commands/ci_commands_config.py @@ -27,6 +27,8 @@ CI_COMMANDS: dict[str, str | list[str]] = { "upgrade", "set-milestone", "fork-ci-reconcile", + "use-project-ci", + "use-fork-ci", "audit", "approve-workflow-runs", ], @@ -135,6 +137,23 @@ CI_PARAMETERS: dict[str, list[dict[str, str | list[str]]]] = { ], }, ], + "breeze ci use-project-ci": [ + { + "name": "Use project CI flags", + "options": [ + "--restart", + "--github-repository", + ], + }, + ], + "breeze ci use-fork-ci": [ + { + "name": "Use fork CI flags", + "options": [ + "--github-repository", + ], + }, + ], "breeze ci audit": [ { "name": "Audit flags", diff --git a/dev/breeze/src/airflow_breeze/utils/fork_ci_github.py b/dev/breeze/src/airflow_breeze/utils/fork_ci_github.py index 9d84bc89f1c..0d1574e3840 100644 --- a/dev/breeze/src/airflow_breeze/utils/fork_ci_github.py +++ b/dev/breeze/src/airflow_breeze/utils/fork_ci_github.py @@ -256,6 +256,13 @@ class GitHubClient: {"state": "closed"}, ) + def reopen(self, pr: int) -> None: + self._mutate( + f"#{pr}: reopen", + ["api", "-X", "PATCH", f"repos/{self.repo}/pulls/{pr}"], + {"state": "open"}, + ) + def delete_run(self, pr: int, run_id: int) -> None: self._mutate( f"#{pr}: delete workflow run {run_id}", diff --git a/dev/breeze/src/airflow_breeze/utils/fork_ci_pr_move.py b/dev/breeze/src/airflow_breeze/utils/fork_ci_pr_move.py new file mode 100644 index 00000000000..b4c69f08b91 --- /dev/null +++ b/dev/breeze/src/airflow_breeze/utils/fork_ci_pr_move.py @@ -0,0 +1,118 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +""" +Move a single pull request between project CI and fork CI (AIP-120), for a maintainer. + +The fork CI monitor reacts to the `use project ci` and `use fork ci` labels only on its next run, so +a fork `CI passed` success posted before `use project ci` was added still lets the pull request +merge until then. These run with the maintainer's own `gh` credentials and do in order what the +label alone leaves to that run. +""" + +from __future__ import annotations + +from typing import TYPE_CHECKING, Any + +from airflow_breeze.utils.fork_ci_gate import LABEL_USE_FORK_CI, LABEL_USE_PROJECT_CI +from airflow_breeze.utils.fork_ci_messages import ( + CI_PASSED_CONTEXT, + CI_PASSED_WAITING_PROJECT_CI_DESCRIPTION, + is_fork_ci_passed_description, +) + +if TYPE_CHECKING: + from airflow_breeze.utils.fork_ci_github import GitHubClient + + +def read_open_pull_request(client: GitHubClient, pr_number: int) -> dict[str, Any]: + pr = client.rest_get(f"repos/{client.repo}/pulls/{pr_number}") + if pr.get("state") != "open": + raise ValueError(f"pull request #{pr_number} is not open") + return pr + + +def find_ci_passed_status(client: GitHubClient, sha: str) -> dict[str, Any] | None: + # Paginated: the fork CI monitor posts a status per failed job, so a commit can have many contexts. + statuses = client.rest_get( + f"repos/{client.repo}/commits/{sha}/status?per_page=100", paginate=True, items_key="statuses" + ) + return next((status for status in statuses if status["context"] == CI_PASSED_CONTEXT), None) + + +def move_to_project_ci(client: GitHubClient, pr_number: int, *, restart: bool = True) -> list[str]: + """ + Withhold a fork `CI passed` success, label the pull request `use project ci` and restart it. + + The success is withheld before the label is added, so there is no moment at which the pull + request carries the label and can still merge on the fork result. The status and its description + are those the monitor writes when it releases the pull request, so its next run changes nothing. + A success that project CI earned is kept. Closing and reopening starts project CI, which the label + does not, and the monitor deleted the gated runs there would be to re-run. + + :return: what was done, for the maintainer. + """ + pr = read_open_pull_request(client, pr_number) + head_sha = pr["head"]["sha"] + labels = {label["name"] for label in pr.get("labels") or []} + done = [] + status = find_ci_passed_status(client, head_sha) + if ( + status is not None + and status["state"] == "success" + and is_fork_ci_passed_description(status.get("description")) + ): + client.set_status( + pr_number, + head_sha, + "pending", + f"https://github.com/{client.repo}/pull/{pr_number}/checks", + CI_PASSED_WAITING_PROJECT_CI_DESCRIPTION, + context=CI_PASSED_CONTEXT, + ) + done.append(f"withheld the fork CI `{CI_PASSED_CONTEXT}` success on {head_sha[:10]}") + if LABEL_USE_FORK_CI in labels: + client.remove_label(pr_number, LABEL_USE_FORK_CI) + done.append(f"removed `{LABEL_USE_FORK_CI}`") + if LABEL_USE_PROJECT_CI not in labels: + client.add_labels(pr_number, [LABEL_USE_PROJECT_CI]) + done.append(f"added `{LABEL_USE_PROJECT_CI}`") + if restart: + client.close(pr_number) + client.reopen(pr_number) + done.append("closed and reopened it to start project CI") + return done + + +def move_to_fork_ci(client: GitHubClient, pr_number: int) -> list[str]: + """ + Label the pull request `use fork ci` instead of `use project ci`. + + A `CI passed` success that project CI earned stays, as it still holds for the commit. The monitor + gates the pull request on its next run, and fork CI runs on the author's next push to their fork. + + :return: what was done, for the maintainer. + """ + pr = read_open_pull_request(client, pr_number) + labels = {label["name"] for label in pr.get("labels") or []} + done = [] + if LABEL_USE_PROJECT_CI in labels: + client.remove_label(pr_number, LABEL_USE_PROJECT_CI) + done.append(f"removed `{LABEL_USE_PROJECT_CI}`") + if LABEL_USE_FORK_CI not in labels: + client.add_labels(pr_number, [LABEL_USE_FORK_CI]) + done.append(f"added `{LABEL_USE_FORK_CI}`") + return done diff --git a/dev/breeze/tests/test_fork_ci_github.py b/dev/breeze/tests/test_fork_ci_github.py index ab497dd3b34..35f7f9c45e0 100644 --- a/dev/breeze/tests/test_fork_ci_github.py +++ b/dev/breeze/tests/test_fork_ci_github.py @@ -307,3 +307,21 @@ def test_does_not_shrink_run_job_pages_for_errors_other_than_server_errors(mock_ with pytest.raises(GitHubError, match="HTTP 404"): GitHubClient().get_run_jobs("someone/airflow", 7) mock_run.assert_called_once() + + [email protected]("airflow_breeze.utils.fork_ci_github.subprocess.run", autospec=True) +def test_reopen(mock_run): + mock_run.return_value = _completed() + + GitHubClient().reopen(1) + + assert mock_run.call_args.args[0] == [ + "gh", + "api", + "-X", + "PATCH", + "repos/apache/airflow/pulls/1", + "--input", + "-", + ] + assert json.loads(mock_run.call_args.kwargs["input"]) == {"state": "open"} diff --git a/dev/breeze/tests/test_fork_ci_pr_move.py b/dev/breeze/tests/test_fork_ci_pr_move.py new file mode 100644 index 00000000000..d53d0b4c8d7 --- /dev/null +++ b/dev/breeze/tests/test_fork_ci_pr_move.py @@ -0,0 +1,177 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +from unittest import mock + +import pytest +from click.testing import CliRunner + +from airflow_breeze.commands.ci_commands import use_fork_ci, use_project_ci +from airflow_breeze.utils.fork_ci_github import GitHubClient, GitHubError +from airflow_breeze.utils.fork_ci_messages import ( + CI_PASSED_PROJECT_DESCRIPTION, + CI_PASSED_WAITING_PROJECT_CI_DESCRIPTION, +) +from airflow_breeze.utils.fork_ci_pr_move import move_to_fork_ci, move_to_project_ci + +HEAD = "a" * 40 +FORK_SUCCESS = { + "context": "CI passed", + "state": "success", + "description": "Fork CI passed; base main@bbbbbbbbbb was 3 commits behind, no overlap", +} + + +def _client(labels=(), statuses=(), state="open"): + client = mock.create_autospec(GitHubClient, instance=True) + client.repo = "apache/airflow" + pr = {"state": state, "head": {"sha": HEAD}, "labels": [{"name": label} for label in labels]} + + def rest_get(path, paginate=False, items_key=None): + if path == "repos/apache/airflow/pulls/42": + return pr + if path == f"repos/apache/airflow/commits/{HEAD}/status?per_page=100": + return [{"context": "Fork CI / Tests (AMD)", "state": "success"}, *statuses] + raise AssertionError(path) + + client.rest_get.side_effect = rest_get + return client + + +def test_move_to_project_ci_withholds_the_fork_success_before_labelling(): + client = _client(labels=("fork ci required",), statuses=[FORK_SUCCESS]) + + done = move_to_project_ci(client, 42) + + assert client.mock_calls[2:] == [ + mock.call.set_status( + 42, + HEAD, + "pending", + "https://github.com/apache/airflow/pull/42/checks", + CI_PASSED_WAITING_PROJECT_CI_DESCRIPTION, + context="CI passed", + ), + mock.call.add_labels(42, ["use project ci"]), + mock.call.close(42), + mock.call.reopen(42), + ] + assert done == [ + "withheld the fork CI `CI passed` success on aaaaaaaaaa", + "added `use project ci`", + "closed and reopened it to start project CI", + ] + + [email protected]( + "statuses", + [ + pytest.param([], id="no-ci-passed"), + pytest.param( + [{"context": "CI passed", "state": "success", "description": CI_PASSED_PROJECT_DESCRIPTION}], + id="project-ci-success", + ), + pytest.param([{**FORK_SUCCESS, "state": "pending"}], id="already-pending"), + ], +) +def test_move_to_project_ci_keeps_a_status_it_must_not_withhold(statuses): + client = _client(statuses=statuses) + + move_to_project_ci(client, 42, restart=False) + + client.set_status.assert_not_called() + client.add_labels.assert_called_once_with(42, ["use project ci"]) + client.close.assert_not_called() + + +def test_move_to_project_ci_replaces_use_fork_ci(): + client = _client(labels=("use fork ci",)) + + done = move_to_project_ci(client, 42, restart=False) + + client.remove_label.assert_called_once_with(42, "use fork ci") + assert done == ["removed `use fork ci`", "added `use project ci`"] + + +def test_move_to_project_ci_on_a_labelled_pull_request_still_restarts_it(): + client = _client(labels=("use project ci",)) + + assert move_to_project_ci(client, 42) == ["closed and reopened it to start project CI"] + client.add_labels.assert_not_called() + + [email protected]( + ("labels", "removed", "added", "done"), + [ + pytest.param( + ("use project ci",), + ["use project ci"], + [["use fork ci"]], + ["removed `use project ci`", "added `use fork ci`"], + id="from-project-ci", + ), + pytest.param((), [], [["use fork ci"]], ["added `use fork ci`"], id="unlabelled"), + pytest.param(("use fork ci",), [], [], [], id="already-on-fork-ci"), + ], +) +def test_move_to_fork_ci(labels, removed, added, done): + client = _client(labels=labels) + + assert move_to_fork_ci(client, 42) == done + assert [c.args[1] for c in client.remove_label.call_args_list] == removed + assert [c.args[1] for c in client.add_labels.call_args_list] == added + client.set_status.assert_not_called() + + [email protected]("move", [move_to_project_ci, move_to_fork_ci]) +def test_moving_a_closed_pull_request_fails(move): + client = _client(state="closed") + + with pytest.raises(ValueError, match="not open"): + move(client, 42) + + client.add_labels.assert_not_called() + + [email protected]( + ("command", "args", "target", "kwargs"), + [ + pytest.param(use_project_ci, ["42"], "move_to_project_ci", {"restart": True}, id="use-project-ci"), + pytest.param( + use_project_ci, ["42", "--no-restart"], "move_to_project_ci", {"restart": False}, id="no-restart" + ), + pytest.param(use_fork_ci, ["42"], "move_to_fork_ci", {}, id="use-fork-ci"), + ], +) +def test_commands_move_the_pull_request(command, args, target, kwargs): + with mock.patch(f"airflow_breeze.utils.fork_ci_pr_move.{target}", autospec=True) as move: + move.return_value = ["added a label"] + result = CliRunner().invoke(command, args) + + assert result.exit_code == 0, result.output + assert move.call_args.args[1] == 42 + assert move.call_args.kwargs == kwargs + + [email protected]("error", [GitHubError("gh api failed: HTTP 404"), ValueError("not open")]) +def test_commands_fail_on_errors(error): + with mock.patch("airflow_breeze.utils.fork_ci_pr_move.move_to_fork_ci", autospec=True) as move: + move.side_effect = error + result = CliRunner().invoke(use_fork_ci, ["42"]) + + assert result.exit_code == 1
