github2gerrit 2.2.0__py3-none-any.whl → 2.2.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
github2gerrit/cli.py CHANGED
@@ -120,6 +120,46 @@ def _exit_for_pr_state_error(pr_number: int, pr_state: str) -> None:
120
120
  exit_for_pr_state_error(pr_number, pr_state)
121
121
 
122
122
 
123
+ def _stop_for_pr_state(
124
+ gh: GitHubContext, pr_state: str, progress_tracker: Any = None
125
+ ) -> NoReturn:
126
+ """Stop because the pull request is not open.
127
+
128
+ On a pull request event this is not an error. GitHub delivers
129
+ several events around a close — Dependabot edits the body before
130
+ closing, for instance — and any of them may land after the
131
+ ``closed`` event whose handler already ran. Such a run has nothing
132
+ to do, and a red check on the contributor's pull request would
133
+ misreport that. The report behind #441 cited exactly this run and
134
+ took it for the close handler failing.
135
+
136
+ A dispatch or direct invocation naming a closed pull request is
137
+ different: somebody asked for that one specifically, and being told
138
+ it cannot be processed is the useful answer.
139
+ """
140
+ if gh.event_name in ("pull_request", "pull_request_target"):
141
+ log.info(
142
+ "Pull request #%s is %s; nothing to do for a '%s' event. Cleanup "
143
+ "for a closed pull request runs on the 'closed' event itself.",
144
+ gh.pr_number,
145
+ pr_state,
146
+ gh.event_action or gh.event_name,
147
+ )
148
+ safe_console_print(
149
+ f"⏩ Pull request #{gh.pr_number} is {pr_state}; nothing to do",
150
+ style="yellow",
151
+ progress_tracker=progress_tracker,
152
+ )
153
+ # The caller's own stop() is downstream of this raise, and
154
+ # safe_console_print only suspends and resumes the display, so
155
+ # an in-process caller catching the exit would otherwise be
156
+ # left with an active Rich context.
157
+ if progress_tracker is not None:
158
+ progress_tracker.stop()
159
+ raise typer.Exit(int(ExitCode.SUCCESS))
160
+ exit_for_pr_state_error(gh.pr_number or 0, pr_state)
161
+
162
+
123
163
  def _exit_for_pr_not_found(pr_number: int, repository: str) -> None:
124
164
  """Exit with error message for PR not found."""
125
165
  exit_for_pr_not_found(pr_number, repository)
@@ -756,10 +796,10 @@ def _extract_and_display_pr_info(
756
796
  repo = get_repo_from_env(client)
757
797
  pr_obj = get_pull(repo, int(gh.pr_number))
758
798
 
759
- # Check PR state and exit if not processable
799
+ # Check PR state and stop if not processable
760
800
  pr_state = getattr(pr_obj, "state", "unknown")
761
801
  if pr_state != "open":
762
- _exit_for_pr_state_error(gh.pr_number, pr_state)
802
+ _stop_for_pr_state(gh, str(pr_state), progress_tracker)
763
803
 
764
804
  # Check if automation_only is enabled and reject non-automation PRs
765
805
  _check_automation_only(pr_obj, gh, progress_tracker)
@@ -792,6 +832,12 @@ def _extract_and_display_pr_info(
792
832
  except GitHub2GerritError:
793
833
  # Let our structured errors propagate
794
834
  raise
835
+ except typer.Exit:
836
+ # display_and_exit and the clean stop above both raise this.
837
+ # It derives from Exception through click, so without this
838
+ # clause the handler below would swallow a deliberate exit 8
839
+ # and report "Failed to fetch PR details: 8" with exit 1.
840
+ raise
795
841
  except Exception as exc:
796
842
  log.debug("Failed to display PR info: %s", exc)
797
843
  if "404" in str(exc) or "Not Found" in str(exc):
@@ -1468,13 +1514,13 @@ def main(
1468
1514
  "",
1469
1515
  "--gerrit-project",
1470
1516
  envvar="GERRIT_PROJECT",
1471
- help="Gerrit project (optional; .gitreview preferred).",
1517
+ help="Gerrit project (optional; overrides .gitreview when set).",
1472
1518
  ),
1473
1519
  gerrit_server: str = typer.Option(
1474
1520
  "",
1475
1521
  "--gerrit-server",
1476
1522
  envvar="GERRIT_SERVER",
1477
- help="Gerrit server hostname (optional; .gitreview preferred).",
1523
+ help="Gerrit server hostname (optional; overrides .gitreview).",
1478
1524
  ),
1479
1525
  gerrit_server_port: int = typer.Option(
1480
1526
  29418,
@@ -3036,14 +3082,23 @@ def _log_abandoned_change_url(
3036
3082
 
3037
3083
 
3038
3084
  def _abandon_change_for_closed_pr(data: Inputs, gh: GitHubContext) -> None:
3039
- """Abandon the Gerrit change associated with a closed pull request."""
3085
+ """Abandon the Gerrit change associated with a closed pull request.
3086
+
3087
+ Three outcomes, reported distinctly: a change was abandoned, no
3088
+ open change carries this pull request's trailer, or the lookup or
3089
+ abandon itself failed. The last two used to share one message, and
3090
+ a failed query read as a clean "nothing to do". A failure does not
3091
+ say which side of the abandon it happened on -- Gerrit may have
3092
+ applied it and the response been lost -- so the report says the
3093
+ state is unconfirmed rather than claiming the change is open.
3094
+ """
3040
3095
  if gh.pr_number is None:
3041
3096
  return
3097
+ log.debug(
3098
+ "Checking for Gerrit change to abandon for PR #%s",
3099
+ gh.pr_number,
3100
+ )
3042
3101
  try:
3043
- log.debug(
3044
- "Checking for Gerrit change to abandon for PR #%s",
3045
- gh.pr_number,
3046
- )
3047
3102
  change_number = abandon_gerrit_change_for_closed_pr(
3048
3103
  pr_number=gh.pr_number,
3049
3104
  gerrit_server=data.gerrit_server,
@@ -3052,21 +3107,46 @@ def _abandon_change_for_closed_pr(data: Inputs, gh: GitHubContext) -> None:
3052
3107
  dry_run=data.dry_run,
3053
3108
  progress_tracker=None,
3054
3109
  )
3055
- if change_number:
3056
- _log_abandoned_change_url(data, gh, change_number)
3057
- # Console output already done by
3058
- # abandon_gerrit_change_for_closed_pr
3059
- else:
3060
- log.debug(
3061
- "No open Gerrit change found for pull request #%s",
3062
- gh.pr_number,
3063
- )
3064
3110
  except Exception as exc:
3065
3111
  log.warning(
3066
- "Failed to abandon Gerrit change for PR #%s: %s",
3112
+ "Failed to look up or abandon the Gerrit change for pull "
3113
+ "request #%s in project %r on %s: %s. Its state could not be "
3114
+ "confirmed; check Gerrit.",
3067
3115
  gh.pr_number,
3116
+ data.gerrit_project,
3117
+ data.gerrit_server,
3068
3118
  exc,
3069
3119
  )
3120
+ log.debug("Cleanup failure detail", exc_info=True)
3121
+ safe_console_print(
3122
+ f"⚠️ Could not confirm the Gerrit change state for PR "
3123
+ f"#{gh.pr_number}: {exc}",
3124
+ style="yellow",
3125
+ )
3126
+ return
3127
+
3128
+ if change_number:
3129
+ _log_abandoned_change_url(data, gh, change_number)
3130
+ # Console output already done by
3131
+ # abandon_gerrit_change_for_closed_pr
3132
+ return
3133
+
3134
+ # Audible on purpose. This is the outcome when the project name is
3135
+ # wrong, and at debug level it hid 239 stranded changes on one
3136
+ # server for six months (#441). Naming the project queried is what
3137
+ # lets a reader spot the mismatch.
3138
+ log.info(
3139
+ "No open Gerrit change found for pull request #%s in "
3140
+ "project %r on %s; nothing to abandon",
3141
+ gh.pr_number,
3142
+ data.gerrit_project,
3143
+ data.gerrit_server,
3144
+ )
3145
+ safe_console_print(
3146
+ f"⏩ No open Gerrit change for PR #{gh.pr_number} in "
3147
+ f"project '{data.gerrit_project}'",
3148
+ style="dim",
3149
+ )
3070
3150
 
3071
3151
 
3072
3152
  def _handle_pr_closed(
@@ -3104,13 +3184,28 @@ def _handle_pr_closed(
3104
3184
 
3105
3185
  # First, abandon the specific Gerrit change for this closed PR
3106
3186
  # Skip in G2G_NO_GERRIT: no Gerrit server to query
3107
- if (
3108
- not no_gerrit
3109
- and gh.pr_number
3187
+ if no_gerrit:
3188
+ pass
3189
+ elif not (
3190
+ gh.pr_number
3191
+ and gh.repository
3110
3192
  and data.gerrit_server
3111
3193
  and data.gerrit_project
3112
- and gh.repository
3113
3194
  ):
3195
+ # Say so. Silently skipping here is indistinguishable from a
3196
+ # successful cleanup that found nothing, and the change stays
3197
+ # open with no trace of why.
3198
+ log.warning(
3199
+ "Cannot look for a Gerrit change to abandon for PR #%s: "
3200
+ "repository=%r server=%r project=%r. Set GERRIT_SERVER and "
3201
+ "GERRIT_PROJECT, or add a .gitreview so they can be derived; "
3202
+ "the repository comes from GITHUB_REPOSITORY.",
3203
+ gh.pr_number,
3204
+ gh.repository,
3205
+ data.gerrit_server,
3206
+ data.gerrit_project,
3207
+ )
3208
+ else:
3114
3209
  _abandon_change_for_closed_pr(data, gh)
3115
3210
 
3116
3211
  _run_gerrit_cleanup_tasks(data, gh, no_gerrit=no_gerrit)