@@ -517,6 +517,60 @@ def get_filters():
517517 def requires_github_api ():
518518 return True
519519
520+ @transformation (10000 )
521+ def _transform_clear_stale_merge_commit (self , issue_update ):
522+ """
523+ Transformation: If the "Pull Request ID" was changed after the "Merge
524+ Commit SHA" was set, this transformation clears the merge commit and
525+ related "Fixed In" field, as they are now considered stale.
526+ """
527+ issue_update .logger .debug ("Running _transform_clear_stale_merge_commit" )
528+ try :
529+ issue_with_journals = self .R .issue .get (issue_update .issue .id , include = ['journals' ])
530+ except redminelib .exceptions .ResourceNotFoundError :
531+ issue_update .logger .warning ("Could not fetch issue with journals. Skipping stale merge commit check." )
532+ return False
533+
534+ last_pr_id_change = None
535+ last_merge_commit_set = None
536+
537+ # Journals are ordered oldest to newest, so reverse to find the latest changes first.
538+ for journal in reversed (issue_with_journals .journals ):
539+ if last_pr_id_change and last_merge_commit_set :
540+ break
541+
542+ for detail in journal .details :
543+ if detail .get ('property' ) == 'cf' :
544+ try :
545+ field_id = int (detail .get ('name' ))
546+ if field_id == REDMINE_CUSTOM_FIELD_ID_PULL_REQUEST_ID and not last_pr_id_change :
547+ last_pr_id_change = journal .id
548+ issue_update .logger .debug (f"last_pr_id_change = { last_pr_id_change } " )
549+ elif field_id == REDMINE_CUSTOM_FIELD_ID_MERGE_COMMIT and not last_merge_commit_set :
550+ # We only care when the commit was set to a non-empty value.
551+ if detail .get ('new_value' ):
552+ last_merge_commit_set = journal .id
553+ issue_update .logger .debug (f"last_merge_commit_set = { last_merge_commit_set } " )
554+ except (ValueError , TypeError ):
555+ continue # Ignore if 'name' is not a valid integer field ID
556+
557+ if not last_pr_id_change or not last_merge_commit_set :
558+ issue_update .logger .debug ("Did not find journal entries for both PR ID and Merge Commit changes. No action taken." )
559+ return False
560+
561+ issue_update .logger .debug (f"Last PR ID change: { last_pr_id_change } , Last Merge Commit set: { last_merge_commit_set } " )
562+
563+ if last_pr_id_change > last_merge_commit_set :
564+ issue_update .logger .info ("The 'Pull Request ID' was changed after the 'Merge Commit SHA' was set. Clearing the stale merge commit." )
565+ # Clear the merge commit field and also the 'Fixed In' field which depends on it.
566+ changed = False
567+ changed |= issue_update .add_or_update_custom_field (REDMINE_CUSTOM_FIELD_ID_MERGE_COMMIT , "" )
568+ changed |= issue_update .add_or_update_custom_field (REDMINE_CUSTOM_FIELD_ID_FIXED_IN , "" )
569+ changed |= issue_update .add_or_update_custom_field (REDMINE_CUSTOM_FIELD_ID_RELEASED_IN , "" )
570+ return changed
571+
572+ return False
573+
520574 class FilterMerged (Filter ):
521575 """
522576 Filter issues that are closed but no merge commit is set.
0 commit comments