Skip to content

Commit

Permalink
Fetch downstream issue to compare labels on sync
Browse files Browse the repository at this point in the history
  • Loading branch information
marko-bekhta committed Nov 4, 2024
1 parent 8b21473 commit c1d16d4
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 13 deletions.
18 changes: 18 additions & 0 deletions src/main/java/org/hibernate/infra/replicate/jira/JiraConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,24 @@ interface JiraProjectGroup {
* for the timeframe to end and will get process
*/
EventProcessing processing();

/**
* Allows customizing formatting options.
*/
Formatting formatting();
}

interface Formatting {

/**
* Specify how the label is formatted for a downstream issue.
* <p>
* Template receives a single String token that is an upstream label. {@code %s}
* <b>must</b> be used in the template as it will be replaced by a {@code .+}
* regex to find matches in the already synced labels.
*/
@WithDefault("upstream-%s")
String labelTemplate();
}

interface EventProcessing {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Optional;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.ReentrantLock;
import java.util.regex.Pattern;

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

private final Map<String, HandlerProjectContext> allProjectsContextMap;
private final Pattern sourceLabelPattern;

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

this.allProjectsContextMap = allProjectsContextMap;
this.sourceLabelPattern = Pattern
.compile(projectGroupContext.projectGroup().formatting().labelTemplate().formatted(".+"));
}

public JiraConfig.JiraProject project() {
Expand Down Expand Up @@ -217,4 +221,8 @@ public Optional<HandlerProjectContext> contextForProjectInSameGroup(String proje
}
return Optional.ofNullable(allProjectsContextMap.get(project));
}

public boolean isSourceLabel(String label) {
return sourceLabelPattern.matcher(label).matches();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Pattern;

import org.hibernate.infra.replicate.jira.JiraConfig;
import org.hibernate.infra.replicate.jira.service.jira.HandlerProjectContext;
Expand All @@ -20,6 +21,8 @@

abstract class JiraIssueAbstractEventHandler extends JiraEventHandler {

private static final Pattern FIX_VERSION_PATTERN = Pattern.compile("Fix_version:.++");

public JiraIssueAbstractEventHandler(ReportingConfig reportingConfig, HandlerProjectContext context, Long id) {
super(reportingConfig, context, id);
}
Expand All @@ -30,7 +33,8 @@ protected void applyTransition(JiraIssue sourceIssue, String destinationKey) {
}

protected void updateIssueBody(JiraIssue sourceIssue, String destinationKey) {
JiraIssue issue = issueToCreate(sourceIssue);
JiraIssue destIssue = context.destinationJiraClient().getIssue(destinationKey);
JiraIssue issue = issueToCreate(sourceIssue, destIssue);

updateIssue(destinationKey, issue, sourceIssue, context.notMappedAssignee());
}
Expand Down Expand Up @@ -83,7 +87,7 @@ protected JiraRemoteLink remoteSelfLink(JiraIssue sourceIssue) {
return link;
}

protected JiraIssue issueToCreate(JiraIssue sourceIssue) {
protected JiraIssue issueToCreate(JiraIssue sourceIssue, JiraIssue downstreamIssue) {
JiraIssue destinationIssue = new JiraIssue();
destinationIssue.fields = new JiraFields();

Expand All @@ -93,17 +97,7 @@ protected JiraIssue issueToCreate(JiraIssue sourceIssue) {
Objects.toString(sourceIssue.fields.description, ""));
destinationIssue.fields.description = truncateContent(destinationIssue.fields.description);

destinationIssue.fields.labels = sourceIssue.fields.labels;
// let's also add fix versions to the labels
if (sourceIssue.fields.fixVersions != null) {
if (destinationIssue.fields.labels == null) {
destinationIssue.fields.labels = List.of();
}
destinationIssue.fields.labels = new ArrayList<>(destinationIssue.fields.labels);
for (JiraSimpleObject fixVersion : sourceIssue.fields.fixVersions) {
destinationIssue.fields.labels.add("Fix version:%s".formatted(fixVersion.name).replace(' ', '_'));
}
}
destinationIssue.fields.labels = prepareLabels(sourceIssue, downstreamIssue);

// if we can map the priority - great we'll do that, if no: we'll keep it blank
// and let Jira use its default instead:
Expand Down Expand Up @@ -155,6 +149,38 @@ protected JiraIssue issueToCreate(JiraIssue sourceIssue) {
return destinationIssue;
}

private List<String> prepareLabels(JiraIssue sourceIssue, JiraIssue downstreamIssue) {
List<String> labelsToSet = new ArrayList<>();

for (String label : sourceIssue.fields.labels) {
labelsToSet.add(asUpstreamLabel(label));
}

// let's also add fix versions to the labels
if (sourceIssue.fields.fixVersions != null) {
for (JiraSimpleObject fixVersion : sourceIssue.fields.fixVersions) {
String fixVersionLabel = "Fix version:%s".formatted(fixVersion.name).replace(' ', '_');
labelsToSet.add(fixVersionLabel);
}
}

for (String label : downstreamIssue.fields.labels) {
if (!(context.isSourceLabel(label) || isFixVersion(label))) {
labelsToSet.add(label);
}
}

return labelsToSet;
}

private boolean isFixVersion(String label) {
return FIX_VERSION_PATTERN.matcher(label).matches();
}

private String asUpstreamLabel(String label) {
return context.projectGroup().formatting().labelTemplate().formatted(label);
}

private JiraUser toUser(String value) {
return new JiraUser(context.projectGroup().users().mappedPropertyName(), value);
}
Expand Down

0 comments on commit c1d16d4

Please sign in to comment.