55import java .util .List ;
66import java .util .Objects ;
77import java .util .Optional ;
8+ import java .util .regex .Pattern ;
89
910import org .hibernate .infra .replicate .jira .JiraConfig ;
1011import org .hibernate .infra .replicate .jira .service .jira .HandlerProjectContext ;
2021
2122abstract class JiraIssueAbstractEventHandler extends JiraEventHandler {
2223
24+ private static final Pattern FIX_VERSION_PATTERN = Pattern .compile ("Fix_version:.++" );
25+
2326 public JiraIssueAbstractEventHandler (ReportingConfig reportingConfig , HandlerProjectContext context , Long id ) {
2427 super (reportingConfig , context , id );
2528 }
@@ -30,7 +33,8 @@ protected void applyTransition(JiraIssue sourceIssue, String destinationKey) {
3033 }
3134
3235 protected void updateIssueBody (JiraIssue sourceIssue , String destinationKey ) {
33- JiraIssue issue = issueToCreate (sourceIssue );
36+ JiraIssue destIssue = context .destinationJiraClient ().getIssue (destinationKey );
37+ JiraIssue issue = issueToCreate (sourceIssue , destIssue );
3438
3539 updateIssue (destinationKey , issue , sourceIssue , context .notMappedAssignee ());
3640 }
@@ -83,7 +87,7 @@ protected JiraRemoteLink remoteSelfLink(JiraIssue sourceIssue) {
8387 return link ;
8488 }
8589
86- protected JiraIssue issueToCreate (JiraIssue sourceIssue ) {
90+ protected JiraIssue issueToCreate (JiraIssue sourceIssue , JiraIssue downstreamIssue ) {
8791 JiraIssue destinationIssue = new JiraIssue ();
8892 destinationIssue .fields = new JiraFields ();
8993
@@ -93,17 +97,7 @@ protected JiraIssue issueToCreate(JiraIssue sourceIssue) {
9397 Objects .toString (sourceIssue .fields .description , "" ));
9498 destinationIssue .fields .description = truncateContent (destinationIssue .fields .description );
9599
96- destinationIssue .fields .labels = sourceIssue .fields .labels ;
97- // let's also add fix versions to the labels
98- if (sourceIssue .fields .fixVersions != null ) {
99- if (destinationIssue .fields .labels == null ) {
100- destinationIssue .fields .labels = List .of ();
101- }
102- destinationIssue .fields .labels = new ArrayList <>(destinationIssue .fields .labels );
103- for (JiraSimpleObject fixVersion : sourceIssue .fields .fixVersions ) {
104- destinationIssue .fields .labels .add ("Fix version:%s" .formatted (fixVersion .name ).replace (' ' , '_' ));
105- }
106- }
100+ destinationIssue .fields .labels = prepareLabels (sourceIssue , downstreamIssue );
107101
108102 // if we can map the priority - great we'll do that, if no: we'll keep it blank
109103 // and let Jira use its default instead:
@@ -155,6 +149,38 @@ protected JiraIssue issueToCreate(JiraIssue sourceIssue) {
155149 return destinationIssue ;
156150 }
157151
152+ private List <String > prepareLabels (JiraIssue sourceIssue , JiraIssue downstreamIssue ) {
153+ List <String > labelsToSet = new ArrayList <>();
154+
155+ for (String label : sourceIssue .fields .labels ) {
156+ labelsToSet .add (asUpstreamLabel (label ));
157+ }
158+
159+ // let's also add fix versions to the labels
160+ if (sourceIssue .fields .fixVersions != null ) {
161+ for (JiraSimpleObject fixVersion : sourceIssue .fields .fixVersions ) {
162+ String fixVersionLabel = "Fix version:%s" .formatted (fixVersion .name ).replace (' ' , '_' );
163+ labelsToSet .add (fixVersionLabel );
164+ }
165+ }
166+
167+ for (String label : downstreamIssue .fields .labels ) {
168+ if (!(context .isSourceLabel (label ) || isFixVersion (label ))) {
169+ labelsToSet .add (label );
170+ }
171+ }
172+
173+ return labelsToSet ;
174+ }
175+
176+ private boolean isFixVersion (String label ) {
177+ return FIX_VERSION_PATTERN .matcher (label ).matches ();
178+ }
179+
180+ private String asUpstreamLabel (String label ) {
181+ return context .projectGroup ().formatting ().labelTemplate ().formatted (label );
182+ }
183+
158184 private JiraUser toUser (String value ) {
159185 return new JiraUser (context .projectGroup ().users ().mappedPropertyName (), value );
160186 }
0 commit comments