|
| 1 | +package org.hibernate.infra.replicate.jira.service.jira.handler; |
| 2 | + |
| 3 | +import java.net.URI; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Objects; |
| 7 | +import java.util.Optional; |
| 8 | + |
| 9 | +import org.hibernate.infra.replicate.jira.service.jira.HandlerProjectContext; |
| 10 | +import org.hibernate.infra.replicate.jira.service.jira.client.JiraRestException; |
| 11 | +import org.hibernate.infra.replicate.jira.service.jira.model.rest.JiraFields; |
| 12 | +import org.hibernate.infra.replicate.jira.service.jira.model.rest.JiraIssue; |
| 13 | +import org.hibernate.infra.replicate.jira.service.jira.model.rest.JiraIssueLink; |
| 14 | +import org.hibernate.infra.replicate.jira.service.jira.model.rest.JiraRemoteLink; |
| 15 | +import org.hibernate.infra.replicate.jira.service.jira.model.rest.JiraSimpleObject; |
| 16 | +import org.hibernate.infra.replicate.jira.service.jira.model.rest.JiraTextContent; |
| 17 | +import org.hibernate.infra.replicate.jira.service.jira.model.rest.JiraTransition; |
| 18 | +import org.hibernate.infra.replicate.jira.service.jira.model.rest.JiraUser; |
| 19 | +import org.hibernate.infra.replicate.jira.service.reporting.ReportingConfig; |
| 20 | + |
| 21 | +abstract class JiraIssueAbstractEventHandler extends JiraEventHandler { |
| 22 | + |
| 23 | + public JiraIssueAbstractEventHandler(ReportingConfig reportingConfig, HandlerProjectContext context, Long id) { |
| 24 | + super(reportingConfig, context, id); |
| 25 | + } |
| 26 | + |
| 27 | + protected void applyTransition(JiraIssue sourceIssue, String destinationKey) { |
| 28 | + prepareTransition(sourceIssue).ifPresent( |
| 29 | + jiraTransition -> context.destinationJiraClient().transition(destinationKey, jiraTransition)); |
| 30 | + } |
| 31 | + |
| 32 | + protected void updateIssueBody(JiraIssue sourceIssue, String destinationKey) { |
| 33 | + JiraIssue issue = issueToCreate(sourceIssue); |
| 34 | + |
| 35 | + updateIssue(destinationKey, issue, sourceIssue, context.notMappedAssignee()); |
| 36 | + } |
| 37 | + |
| 38 | + protected void updateIssue(String destinationKey, JiraIssue issue, JiraIssue sourceIssue, JiraUser assignee) { |
| 39 | + try { |
| 40 | + context.destinationJiraClient().update(destinationKey, issue); |
| 41 | + } catch (JiraRestException e) { |
| 42 | + if (issue.fields.assignee == null) { |
| 43 | + // if we failed with no assignee then there's no point in retrying ... |
| 44 | + throw e; |
| 45 | + } |
| 46 | + if (e.getMessage().contains("\"assignee\"")) { |
| 47 | + // let's try updating with the assignee passed to the method (it may be the |
| 48 | + // "notMappedAssignee") |
| 49 | + failureCollector.warning( |
| 50 | + "Unable to update issue %s with assignee %s, will try to update one more time with assignee %s." |
| 51 | + .formatted(sourceIssue.key, issue.fields.assignee, assignee), |
| 52 | + e); |
| 53 | + issue.fields.assignee = assignee; |
| 54 | + updateIssue(destinationKey, issue, sourceIssue, null); |
| 55 | + } else { |
| 56 | + throw e; |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + protected JiraRemoteLink remoteSelfLink(JiraIssue sourceIssue) { |
| 62 | + URI jiraLink = createJiraIssueUri(sourceIssue); |
| 63 | + |
| 64 | + JiraRemoteLink link = new JiraRemoteLink(); |
| 65 | + // >> Setting this field enables the remote issue link details to be updated or |
| 66 | + // deleted using remote system |
| 67 | + // >> and item details as the record identifier, rather than using the record's |
| 68 | + // Jira ID. |
| 69 | + // |
| 70 | + // Hence, we set this global id as a link to the issue, this way it should be |
| 71 | + // unique enough and easy to create: |
| 72 | + link.globalId = jiraLink.toString(); |
| 73 | + link.relationship = "Upstream issue"; |
| 74 | + link.object.title = sourceIssue.key; |
| 75 | + link.object.url = jiraLink; |
| 76 | + link.object.summary = "Link to an upstream JIRA issue, from which this one was cloned from."; |
| 77 | + |
| 78 | + return link; |
| 79 | + } |
| 80 | + |
| 81 | + protected JiraIssue issueToCreate(JiraIssue sourceIssue) { |
| 82 | + JiraIssue destinationIssue = new JiraIssue(); |
| 83 | + destinationIssue.fields = new JiraFields(); |
| 84 | + |
| 85 | + destinationIssue.fields.summary = sourceIssue.fields.summary; |
| 86 | + destinationIssue.fields.description = sourceIssue.fields.description; |
| 87 | + destinationIssue.fields.description = "%s%s".formatted(prepareDescriptionQuote(sourceIssue), |
| 88 | + Objects.toString(sourceIssue.fields.description, "")); |
| 89 | + destinationIssue.fields.description = truncateContent(destinationIssue.fields.description); |
| 90 | + |
| 91 | + destinationIssue.fields.labels = sourceIssue.fields.labels; |
| 92 | + // let's also add fix versions to the labels |
| 93 | + if (sourceIssue.fields.fixVersions != null) { |
| 94 | + if (destinationIssue.fields.labels == null) { |
| 95 | + destinationIssue.fields.labels = List.of(); |
| 96 | + } |
| 97 | + destinationIssue.fields.labels = new ArrayList<>(destinationIssue.fields.labels); |
| 98 | + for (JiraSimpleObject fixVersion : sourceIssue.fields.fixVersions) { |
| 99 | + destinationIssue.fields.labels.add("Fix version:%s".formatted(fixVersion.name).replace(' ', '_')); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // if we can map the priority - great we'll do that, if no: we'll keep it blank |
| 104 | + // and let Jira use its default instead: |
| 105 | + destinationIssue.fields.priority = sourceIssue.fields.priority != null |
| 106 | + ? priority(sourceIssue.fields.priority.id).map(JiraSimpleObject::new).orElse(null) |
| 107 | + : null; |
| 108 | + |
| 109 | + destinationIssue.fields.project.id = context.project().projectId(); |
| 110 | + |
| 111 | + destinationIssue.fields.issuetype = issueType(sourceIssue.fields.issuetype.id).map(JiraSimpleObject::new) |
| 112 | + .orElse(null); |
| 113 | + |
| 114 | + // now let's handle the users. we will consider only a mapped subset of users |
| 115 | + // and for other's the defaults will be used. |
| 116 | + // also the description is going to include a section mentioning who created and |
| 117 | + // who the issue is assigned to... |
| 118 | + if (context.projectGroup().canSetReporter()) { |
| 119 | + destinationIssue.fields.reporter = user(sourceIssue.fields.reporter).map(this::toUser) |
| 120 | + .orElseGet(context::notMappedAssignee); |
| 121 | + } |
| 122 | + |
| 123 | + destinationIssue.fields.assignee = user(sourceIssue.fields.assignee).map(this::toUser).orElse(null); |
| 124 | + |
| 125 | + return destinationIssue; |
| 126 | + } |
| 127 | + |
| 128 | + private JiraUser toUser(String value) { |
| 129 | + return new JiraUser(context.projectGroup().users().mappedPropertyName(), value); |
| 130 | + } |
| 131 | + |
| 132 | + private Optional<JiraTransition> prepareTransition(JiraIssue sourceIssue) { |
| 133 | + return statusToTransition(sourceIssue.fields.status.id).map( |
| 134 | + tr -> new JiraTransition(tr, "Upstream issue status updated to: " + sourceIssue.fields.status.name)); |
| 135 | + } |
| 136 | + |
| 137 | + protected Optional<JiraIssueLink> prepareParentLink(String destinationKey, JiraIssue sourceIssue) { |
| 138 | + if (sourceIssue.fields.parent != null) { |
| 139 | + String parent = toDestinationKey(sourceIssue.fields.parent.key); |
| 140 | + // we don't really need it, but as usual we are making sure that the issue is |
| 141 | + // available downstream: |
| 142 | + context.createNextPlaceholderBatch(parent); |
| 143 | + JiraIssueLink link = new JiraIssueLink(); |
| 144 | + link.type.id = context.projectGroup().issueLinkTypes().parentLinkType(); |
| 145 | + // "name": "Depend", |
| 146 | + // "inward": "is depended on by", |
| 147 | + // "outward": "depends on", |
| 148 | + // |
| 149 | + // TODO: Jira is sending a relates-to link created hook on its own |
| 150 | + // and it has the inward/outward sides opposite to what we do here |
| 151 | + // Let's double check what will happen with actual downstream jira |
| 152 | + // (there a depends on link should be created along side the one triggered |
| 153 | + // automatically by the source JIRA). |
| 154 | + link.inwardIssue.key = destinationKey; |
| 155 | + link.outwardIssue.key = parent; |
| 156 | + return Optional.of(link); |
| 157 | + } else { |
| 158 | + return Optional.empty(); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + private String prepareDescriptionQuote(JiraIssue issue) { |
| 163 | + URI issueUri = createJiraIssueUri(issue); |
| 164 | + URI reporterUri = createJiraUserUri(issue.self, issue.fields.reporter); |
| 165 | + URI assigneeUri = createJiraUserUri(issue.self, issue.fields.assignee); |
| 166 | + return """ |
| 167 | + {quote}This issue is created as a copy of [%s|%s]. |
| 168 | +
|
| 169 | + Assigned to: %s. |
| 170 | +
|
| 171 | + Reported by: %s.{quote} |
| 172 | +
|
| 173 | +
|
| 174 | + """.formatted(issue.key, issueUri, |
| 175 | + assigneeUri == null |
| 176 | + ? " Unassigned" |
| 177 | + : "[user %s|%s]".formatted(JiraTextContent.userIdPart(issue.fields.assignee), assigneeUri), |
| 178 | + reporterUri == null |
| 179 | + ? " Unknown" |
| 180 | + : "[user %s|%s]".formatted(JiraTextContent.userIdPart(issue.fields.reporter), reporterUri)); |
| 181 | + } |
| 182 | + |
| 183 | +} |
0 commit comments