Skip to content

Commit c1d16d4

Browse files
committed
Fetch downstream issue to compare labels on sync
1 parent 8b21473 commit c1d16d4

File tree

3 files changed

+65
-13
lines changed

3 files changed

+65
-13
lines changed

src/main/java/org/hibernate/infra/replicate/jira/JiraConfig.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,24 @@ interface JiraProjectGroup {
9292
* for the timeframe to end and will get process
9393
*/
9494
EventProcessing processing();
95+
96+
/**
97+
* Allows customizing formatting options.
98+
*/
99+
Formatting formatting();
100+
}
101+
102+
interface Formatting {
103+
104+
/**
105+
* Specify how the label is formatted for a downstream issue.
106+
* <p>
107+
* Template receives a single String token that is an upstream label. {@code %s}
108+
* <b>must</b> be used in the template as it will be replaced by a {@code .+}
109+
* regex to find matches in the already synced labels.
110+
*/
111+
@WithDefault("upstream-%s")
112+
String labelTemplate();
95113
}
96114

97115
interface EventProcessing {

src/main/java/org/hibernate/infra/replicate/jira/service/jira/HandlerProjectContext.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.Optional;
55
import java.util.concurrent.atomic.AtomicLong;
66
import java.util.concurrent.locks.ReentrantLock;
7+
import java.util.regex.Pattern;
78

89
import org.hibernate.infra.replicate.jira.JiraConfig;
910
import org.hibernate.infra.replicate.jira.service.jira.client.JiraRestClient;
@@ -37,6 +38,7 @@ public final class HandlerProjectContext implements AutoCloseable {
3738
private final JiraUser notMappedAssignee;
3839

3940
private final Map<String, HandlerProjectContext> allProjectsContextMap;
41+
private final Pattern sourceLabelPattern;
4042

4143
public HandlerProjectContext(String projectName, String projectGroupName, JiraRestClient sourceJiraClient,
4244
JiraRestClient destinationJiraClient, HandlerProjectGroupContext projectGroupContext,
@@ -55,6 +57,8 @@ public HandlerProjectContext(String projectName, String projectGroupName, JiraRe
5557
.map(v -> new JiraUser(projectGroup().users().mappedPropertyName(), v)).orElse(null);
5658

5759
this.allProjectsContextMap = allProjectsContextMap;
60+
this.sourceLabelPattern = Pattern
61+
.compile(projectGroupContext.projectGroup().formatting().labelTemplate().formatted(".+"));
5862
}
5963

6064
public JiraConfig.JiraProject project() {
@@ -217,4 +221,8 @@ public Optional<HandlerProjectContext> contextForProjectInSameGroup(String proje
217221
}
218222
return Optional.ofNullable(allProjectsContextMap.get(project));
219223
}
224+
225+
public boolean isSourceLabel(String label) {
226+
return sourceLabelPattern.matcher(label).matches();
227+
}
220228
}

src/main/java/org/hibernate/infra/replicate/jira/service/jira/handler/JiraIssueAbstractEventHandler.java

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.List;
66
import java.util.Objects;
77
import java.util.Optional;
8+
import java.util.regex.Pattern;
89

910
import org.hibernate.infra.replicate.jira.JiraConfig;
1011
import org.hibernate.infra.replicate.jira.service.jira.HandlerProjectContext;
@@ -20,6 +21,8 @@
2021

2122
abstract 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

Comments
 (0)