PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/Plugins/Stash/GetPullRequest.cs

https://github.com/vbjay/gitextensions
C# | 130 lines | 119 code | 10 blank | 1 comment | 5 complexity | 99279b802cd2b014e36cb51717aa396b MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0
  1. using System.Collections.Generic;
  2. using Newtonsoft.Json.Linq;
  3. using RestSharp;
  4. using System.Linq;
  5. using System;
  6. namespace Stash
  7. {
  8. class PullRequest
  9. {
  10. //public string Ref { get; set; }
  11. public static PullRequest Parse(JObject json)
  12. {
  13. var request = new PullRequest
  14. {
  15. Id = json["id"].ToString(),
  16. Version = json["version"].ToString(),
  17. State = json["state"].ToString(),
  18. Title = json["title"].ToString(),
  19. Description = json["description"] != null ? json["description"].ToString() : "",
  20. Author = json["author"]["user"]["displayName"].ToString(),
  21. SrcProjectName = json["fromRef"]["repository"]["project"]["name"].ToString(),
  22. SrcRepo = json["fromRef"]["repository"]["name"].ToString(),
  23. SrcBranch = json["fromRef"]["displayId"].ToString(),
  24. DestProjectName = json["toRef"]["repository"]["project"]["name"].ToString(),
  25. DestProjectKey = json["toRef"]["repository"]["project"]["key"].ToString(),
  26. DestRepo = json["toRef"]["repository"]["name"].ToString(),
  27. DestBranch = json["toRef"]["displayId"].ToString(),
  28. CreatedDate = Convert.ToDouble(json["createdDate"].ToString().Substring(0,10))
  29. };
  30. var reviewers = json["reviewers"];
  31. var participants = json["participants"];
  32. if (!reviewers.HasValues)
  33. request.Reviewers = "None";
  34. else
  35. {
  36. reviewers.ForEach(r => request.Reviewers += r["user"]["displayName"] + " (" + r["approved"] + ")" + System.Environment.NewLine);
  37. if (request.Reviewers.EndsWith(", "))
  38. request.Reviewers = request.Reviewers.Substring(0, request.Reviewers.Length - 2);
  39. }
  40. if (!participants.HasValues)
  41. request.Participants = "None";
  42. else
  43. {
  44. participants.ForEach(r => request.Reviewers += r["user"]["displayName"] + " (" + r["approved"] + ")" + System.Environment.NewLine);
  45. if (request.Reviewers.EndsWith(", "))
  46. request.Reviewers = request.Reviewers.Substring(0, request.Reviewers.Length - 2);
  47. }
  48. return request;
  49. }
  50. public string Id { get; set; }
  51. public string Version { get; set; }
  52. public string DestProjectKey { get; set; }
  53. public string State { get; set; }
  54. public string SrcProjectName { get; set; }
  55. public string DestProjectName { get; set; }
  56. public string Title { get; set; }
  57. public string Description { get; set; }
  58. public string Reviewers { get; set; }
  59. public string Participants { get; set; }
  60. public string Author { get; set; }
  61. public string SrcRepo { get; set; }
  62. public string SrcBranch { get; set; }
  63. public string DestRepo { get; set; }
  64. public string DestBranch { get; set; }
  65. public double CreatedDate { get; set; }
  66. public string SrcDisplayName
  67. {
  68. get { return string.Format("{0}/{1}", SrcProjectName, SrcRepo); }
  69. }
  70. public string DestDisplayName
  71. {
  72. get { return string.Format("{0}/{1}", DestProjectName, DestRepo); }
  73. }
  74. public string DisplayName
  75. {
  76. get { return string.Format("#{0}: {1}, {2}", Id, Title, (ConvertFromUnixTimestamp(CreatedDate)).ToString("yyyy-MM-dd")); }
  77. }
  78. public static DateTime ConvertFromUnixTimestamp(double timestamp)
  79. {
  80. DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
  81. return origin.AddSeconds(timestamp);
  82. }
  83. }
  84. class GetPullRequest : StashRequestBase<List<PullRequest>>
  85. {
  86. private readonly string _projectKey;
  87. private readonly string _repoName;
  88. public GetPullRequest(string projectKey, string repoName, Settings settings)
  89. : base(settings)
  90. {
  91. _projectKey = projectKey;
  92. _repoName = repoName;
  93. }
  94. protected override object RequestBody
  95. {
  96. get { return null; }
  97. }
  98. protected override Method RequestMethod
  99. {
  100. get { return Method.GET; }
  101. }
  102. protected override string ApiUrl
  103. {
  104. get
  105. {
  106. return string.Format("/rest/api/latest/projects/{0}/repos/{1}/pull-requests?directions=incoming&order=oldest",
  107. _projectKey, _repoName);
  108. }
  109. }
  110. protected override List<PullRequest> ParseResponse(JObject json)
  111. {
  112. var result = new List<PullRequest>();
  113. foreach (JObject val in json["values"])
  114. {
  115. result.Add(PullRequest.Parse(val));
  116. }
  117. return result;
  118. }
  119. }
  120. }