PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/Atlassian.Jira/Remote/IssueLinkService.cs

https://bitbucket.org/farmas/atlassian.net-sdk
C# | 107 lines | 89 code | 18 blank | 0 comment | 11 complexity | 9c56900bc8e98c7f84f3f192be6421c5 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using Newtonsoft.Json;
  7. using Newtonsoft.Json.Linq;
  8. using RestSharp;
  9. namespace Atlassian.Jira.Remote
  10. {
  11. internal class IssueLinkService : IIssueLinkService
  12. {
  13. private readonly Jira _jira;
  14. public IssueLinkService(Jira jira)
  15. {
  16. _jira = jira;
  17. }
  18. public Task CreateLinkAsync(string outwardIssueKey, string inwardIssueKey, string linkName, string comment, CancellationToken token = default(CancellationToken))
  19. {
  20. var bodyObject = new JObject();
  21. bodyObject.Add("type", new JObject(new JProperty("name", linkName)));
  22. bodyObject.Add("inwardIssue", new JObject(new JProperty("key", inwardIssueKey)));
  23. bodyObject.Add("outwardIssue", new JObject(new JProperty("key", outwardIssueKey)));
  24. if (!String.IsNullOrEmpty(comment))
  25. {
  26. bodyObject.Add("comment", new JObject(new JProperty("body", comment)));
  27. }
  28. return _jira.RestClient.ExecuteRequestAsync(Method.POST, "rest/api/2/issueLink", bodyObject, token);
  29. }
  30. public async Task<IEnumerable<IssueLink>> GetLinksForIssueAsync(string issueKey, CancellationToken token = default(CancellationToken))
  31. {
  32. var issue = await _jira.Issues.GetIssueAsync(issueKey, token);
  33. return await GetLinksForIssueAsync(issue, null, token);
  34. }
  35. public async Task<IEnumerable<IssueLink>> GetLinksForIssueAsync(Issue issue, IEnumerable<string> linkTypeNames = null, CancellationToken token = default(CancellationToken))
  36. {
  37. var serializerSettings = _jira.RestClient.Settings.JsonSerializerSettings;
  38. var resource = String.Format("rest/api/2/issue/{0}?fields=issuelinks,created", issue.Key.Value);
  39. var issueLinksResult = await _jira.RestClient.ExecuteRequestAsync(Method.GET, resource, null, token).ConfigureAwait(false);
  40. var issueLinksJson = issueLinksResult["fields"]["issuelinks"];
  41. if (issueLinksJson == null)
  42. {
  43. throw new InvalidOperationException("There is no 'issueLinks' field on the issue data, make sure issue linking is turned on in JIRA.");
  44. }
  45. var issueLinks = issueLinksJson.Cast<JObject>();
  46. var filteredIssueLinks = issueLinks;
  47. if (linkTypeNames != null)
  48. {
  49. filteredIssueLinks = issueLinks.Where(link => linkTypeNames.Contains(link["type"]["name"].ToString(), StringComparer.InvariantCultureIgnoreCase));
  50. }
  51. var issuesToGet = filteredIssueLinks.Select(issueLink =>
  52. {
  53. var issueJson = issueLink["outwardIssue"] ?? issueLink["inwardIssue"];
  54. return issueJson["key"].Value<string>();
  55. }).ToList();
  56. var issuesMap = await _jira.Issues.GetIssuesAsync(issuesToGet, token).ConfigureAwait(false);
  57. if(!issuesMap.Keys.Contains(issue.Key.ToString()))
  58. {
  59. issuesMap.Add(issue.Key.ToString(), issue);
  60. }
  61. return filteredIssueLinks.Select(issueLink =>
  62. {
  63. var linkType = JsonConvert.DeserializeObject<IssueLinkType>(issueLink["type"].ToString(), serializerSettings);
  64. var outwardIssue = issueLink["outwardIssue"];
  65. var inwardIssue = issueLink["inwardIssue"];
  66. var outwardIssueKey = outwardIssue != null ? (string)outwardIssue["key"] : null;
  67. var inwardIssueKey = inwardIssue != null ? (string)inwardIssue["key"] : null;
  68. return new IssueLink(
  69. linkType,
  70. outwardIssueKey == null ? issue : issuesMap[outwardIssueKey],
  71. inwardIssueKey == null ? issue : issuesMap[inwardIssueKey]);
  72. });
  73. }
  74. public async Task<IEnumerable<IssueLinkType>> GetLinkTypesAsync(CancellationToken token = default(CancellationToken))
  75. {
  76. var cache = _jira.Cache;
  77. var serializerSettings = _jira.RestClient.Settings.JsonSerializerSettings;
  78. if (!cache.LinkTypes.Any())
  79. {
  80. var results = await _jira.RestClient.ExecuteRequestAsync(Method.GET, "rest/api/2/issueLinkType", null, token).ConfigureAwait(false);
  81. var linkTypes = results["issueLinkTypes"]
  82. .Cast<JObject>()
  83. .Select(issueLinkJson => JsonConvert.DeserializeObject<IssueLinkType>(issueLinkJson.ToString(), serializerSettings));
  84. cache.LinkTypes.TryAdd(linkTypes);
  85. }
  86. return cache.LinkTypes.Values;
  87. }
  88. }
  89. }