issue_links.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // Copyright 2017, Arkbriar
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. package gitlab
  17. import (
  18. "fmt"
  19. "net/url"
  20. )
  21. // IssueLinksService handles communication with the issue relations related methods
  22. // of the GitLab API.
  23. //
  24. // GitLab API docs: https://docs.gitlab.com/ee/api/issue_links.html
  25. type IssueLinksService struct {
  26. client *Client
  27. }
  28. // IssueLink represents a two-way relation between two issues.
  29. //
  30. // GitLab API docs: https://docs.gitlab.com/ee/api/issue_links.html
  31. type IssueLink struct {
  32. SourceIssue *Issue `json:"source_issue"`
  33. TargetIssue *Issue `json:"target_issue"`
  34. }
  35. // ListIssueRelations gets a list of related issues of a given issue,
  36. // sorted by the relationship creation datetime (ascending).
  37. //
  38. // Issues will be filtered according to the user authorizations.
  39. //
  40. // GitLab API docs:
  41. // https://docs.gitlab.com/ee/api/issue_links.html#list-issue-relations
  42. func (s *IssueLinksService) ListIssueRelations(pid interface{}, issueIID int, options ...OptionFunc) ([]*Issue, *Response, error) {
  43. project, err := parseID(pid)
  44. if err != nil {
  45. return nil, nil, err
  46. }
  47. u := fmt.Sprintf("projects/%s/issues/%d/links", url.QueryEscape(project), issueIID)
  48. req, err := s.client.NewRequest("GET", u, nil, options)
  49. if err != nil {
  50. return nil, nil, err
  51. }
  52. var is []*Issue
  53. resp, err := s.client.Do(req, &is)
  54. if err != nil {
  55. return nil, resp, err
  56. }
  57. return is, resp, err
  58. }
  59. // CreateIssueLinkOptions represents the available CreateIssueLink() options.
  60. //
  61. // GitLab API docs: https://docs.gitlab.com/ee/api/issue_links.html
  62. type CreateIssueLinkOptions struct {
  63. TargetProjectID *string `json:"target_project_id"`
  64. TargetIssueIID *string `json:"target_issue_iid"`
  65. }
  66. // CreateIssueLink creates a two-way relation between two issues.
  67. // User must be allowed to update both issues in order to succeed.
  68. //
  69. // GitLab API docs:
  70. // https://docs.gitlab.com/ee/api/issue_links.html#create-an-issue-link
  71. func (s *IssueLinksService) CreateIssueLink(pid interface{}, issueIID int, opt *CreateIssueLinkOptions, options ...OptionFunc) (*IssueLink, *Response, error) {
  72. project, err := parseID(pid)
  73. if err != nil {
  74. return nil, nil, err
  75. }
  76. u := fmt.Sprintf("projects/%s/issues/%d/links", url.QueryEscape(project), issueIID)
  77. req, err := s.client.NewRequest("POST", u, opt, options)
  78. if err != nil {
  79. return nil, nil, err
  80. }
  81. i := new(IssueLink)
  82. resp, err := s.client.Do(req, &i)
  83. if err != nil {
  84. return nil, resp, err
  85. }
  86. return i, resp, err
  87. }
  88. // DeleteIssueLink deletes an issue link, thus removes the two-way relationship.
  89. //
  90. // GitLab API docs:
  91. // https://docs.gitlab.com/ee/api/issue_links.html#delete-an-issue-link
  92. func (s *IssueLinksService) DeleteIssueLink(pid interface{}, issueIID, issueLinkID int, options ...OptionFunc) (*IssueLink, *Response, error) {
  93. project, err := parseID(pid)
  94. if err != nil {
  95. return nil, nil, err
  96. }
  97. u := fmt.Sprintf("projects/%s/issues/%d/links/%d",
  98. url.QueryEscape(project),
  99. issueIID,
  100. issueLinkID)
  101. req, err := s.client.NewRequest("DELETE", u, nil, options)
  102. if err != nil {
  103. return nil, nil, err
  104. }
  105. i := new(IssueLink)
  106. resp, err := s.client.Do(req, &i)
  107. if err != nil {
  108. return nil, resp, err
  109. }
  110. return i, resp, err
  111. }