Skip to content

Commit

Permalink
Fix for bidirectional cross-project links
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Hjelm committed Oct 2, 2024
1 parent c11fa8a commit b2a44bf
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/WorkItemMigrator/JiraExport/IJiraProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public interface IJiraProvider

string GetUserEmail(string usernameOrAccountId);

IssueLinkType GetLinkType(string linkTypeString, string targetItemKey);
IssueLinkType GetLinkType(string linkTypeString, string targetItemKey, out bool isInwardLink);

IEnumerable<Comment> GetCommentsByItemKey(string itemKey);

Expand Down
4 changes: 3 additions & 1 deletion src/WorkItemMigrator/JiraExport/JiraItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,8 @@ private static RevisionAction<JiraLink> TransformLinkChange(JiraChangeItem item,
Logger.Log(LogLevel.Error, $"Link change not handled!");
return null;
}
var linkType = jira.GetLinkType(linkTypeString, targetItemKey);
bool inward = false;
var linkType = jira.GetLinkType(linkTypeString, targetItemKey, out inward);
if (linkType == null)
{
Logger.Log(LogLevel.Debug, $"Link with description '{linkTypeString}' is either not found or this issue ({sourceItemKey}) is not inward issue.");
Expand All @@ -410,6 +411,7 @@ private static RevisionAction<JiraLink> TransformLinkChange(JiraChangeItem item,
SourceItem = sourceItemKey,
TargetItem = targetItemKey,
LinkType = linkType.Name,
IsInwardLink = inward
}
};
}
Expand Down
1 change: 1 addition & 0 deletions src/WorkItemMigrator/JiraExport/JiraLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public sealed class JiraLink : IEquatable<JiraLink>
public string SourceItem { get; set; }
public string TargetItem { get; set; }
public string LinkType { get; set; }
public bool IsInwardLink { get; set; }

public bool Equals(JiraLink other)
{
Expand Down
19 changes: 19 additions & 0 deletions src/WorkItemMigrator/JiraExport/JiraMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,11 @@ internal List<WiLink> MapLinks(JiraRevision r)

if (linkType != null)
{
// If link is inward link, reverse the direction of the mapped link
if (jiraLinkAction.Value.IsInwardLink)
{
linkType = GetReverseLinkTypeReferenceName(linkType);
}
link.Change = changeType;
link.SourceOriginId = jiraLinkAction.Value.SourceItem;
link.TargetOriginId = jiraLinkAction.Value.TargetItem;
Expand Down Expand Up @@ -479,5 +484,19 @@ internal object TruncateField(object value, string field)
}
return valueStr;
}

private string GetReverseLinkTypeReferenceName(string referenceName)
{
string Forward = "Forward";
string Reverse = "Reverse";
if (referenceName.Contains(Forward))
{
return referenceName.Replace(Forward, Reverse);
}
else
{
return referenceName.Replace(Reverse, Forward);
}
}
}
}
25 changes: 23 additions & 2 deletions src/WorkItemMigrator/JiraExport/JiraProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,30 @@ public JiraSettings GetSettings()
return Settings;
}

public IssueLinkType GetLinkType(string linkTypeString, string targetItemKey)
public IssueLinkType GetLinkType(string linkTypeString, string targetItemKey, out bool isInwardLink)
{
return LinkTypes.FirstOrDefault(lt => linkTypeString.EndsWith(lt.Outward + " " + targetItemKey));
var outward = LinkTypes.FirstOrDefault(lt => linkTypeString.EndsWith(lt.Outward + " " + targetItemKey));
var inward = LinkTypes.FirstOrDefault(lt => linkTypeString.EndsWith(lt.Inward + " " + targetItemKey));
if (outward != default)
{
isInwardLink = false;
return outward;
}
else if (inward != default)
{
isInwardLink = true;
return inward;
}
// Neither outward or inward link was found
else
{
Logger.Log(LogLevel.Error, "Found link did neither fit any available inward nor outward description. "
+ $"Link type = '{linkTypeString}', "
+ $"Target item key = '{targetItemKey}'"
);
isInwardLink = false;
return null;
}
}

public IEnumerable<Comment> GetCommentsByItemKey(string itemKey)
Expand Down

0 comments on commit b2a44bf

Please sign in to comment.