...

Source file src/github.com/xanzy/go-gitlab/issue_links.go

Documentation: github.com/xanzy/go-gitlab

     1  //
     2  // Copyright 2021, 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  
    17  package gitlab
    18  
    19  import (
    20  	"fmt"
    21  	"net/http"
    22  	"time"
    23  )
    24  
    25  // IssueLinksService handles communication with the issue relations related methods
    26  // of the GitLab API.
    27  //
    28  // GitLab API docs: https://docs.gitlab.com/ee/api/issue_links.html
    29  type IssueLinksService struct {
    30  	client *Client
    31  }
    32  
    33  // IssueLink represents a two-way relation between two issues.
    34  //
    35  // GitLab API docs: https://docs.gitlab.com/ee/api/issue_links.html
    36  type IssueLink struct {
    37  	SourceIssue *Issue `json:"source_issue"`
    38  	TargetIssue *Issue `json:"target_issue"`
    39  	LinkType    string `json:"link_type"`
    40  }
    41  
    42  // IssueRelation gets a relation between two issues.
    43  //
    44  // GitLab API docs:
    45  // https://docs.gitlab.com/ee/api/issue_links.html#list-issue-relations
    46  type IssueRelation struct {
    47  	ID             int              `json:"id"`
    48  	IID            int              `json:"iid"`
    49  	State          string           `json:"state"`
    50  	Description    string           `json:"description"`
    51  	Confidential   bool             `json:"confidential"`
    52  	Author         *IssueAuthor     `json:"author"`
    53  	Milestone      *Milestone       `json:"milestone"`
    54  	ProjectID      int              `json:"project_id"`
    55  	Assignees      []*IssueAssignee `json:"assignees"`
    56  	Assignee       *IssueAssignee   `json:"assignee"`
    57  	UpdatedAt      *time.Time       `json:"updated_at"`
    58  	Title          string           `json:"title"`
    59  	CreatedAt      *time.Time       `json:"created_at"`
    60  	Labels         Labels           `json:"labels"`
    61  	DueDate        *ISOTime         `json:"due_date"`
    62  	WebURL         string           `json:"web_url"`
    63  	References     *IssueReferences `json:"references"`
    64  	Weight         int              `json:"weight"`
    65  	UserNotesCount int              `json:"user_notes_count"`
    66  	IssueLinkID    int              `json:"issue_link_id"`
    67  	LinkType       string           `json:"link_type"`
    68  	LinkCreatedAt  *time.Time       `json:"link_created_at"`
    69  	LinkUpdatedAt  *time.Time       `json:"link_updated_at"`
    70  }
    71  
    72  // ListIssueRelations gets a list of related issues of a given issue,
    73  // sorted by the relationship creation datetime (ascending).
    74  //
    75  // Issues will be filtered according to the user authorizations.
    76  //
    77  // GitLab API docs:
    78  // https://docs.gitlab.com/ee/api/issue_links.html#list-issue-relations
    79  func (s *IssueLinksService) ListIssueRelations(pid interface{}, issue int, options ...RequestOptionFunc) ([]*IssueRelation, *Response, error) {
    80  	project, err := parseID(pid)
    81  	if err != nil {
    82  		return nil, nil, err
    83  	}
    84  	u := fmt.Sprintf("projects/%s/issues/%d/links", PathEscape(project), issue)
    85  
    86  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
    87  	if err != nil {
    88  		return nil, nil, err
    89  	}
    90  
    91  	var is []*IssueRelation
    92  	resp, err := s.client.Do(req, &is)
    93  	if err != nil {
    94  		return nil, resp, err
    95  	}
    96  
    97  	return is, resp, nil
    98  }
    99  
   100  // GetIssueLink gets a specific issue link.
   101  //
   102  // GitLab API docs:
   103  // https://docs.gitlab.com/ee/api/issue_links.html#get-an-issue-link
   104  func (s *IssueLinksService) GetIssueLink(pid interface{}, issue, issueLink int, options ...RequestOptionFunc) (*IssueLink, *Response, error) {
   105  	project, err := parseID(pid)
   106  	if err != nil {
   107  		return nil, nil, err
   108  	}
   109  	u := fmt.Sprintf("projects/%s/issues/%d/links/%d", PathEscape(project), issue, issueLink)
   110  
   111  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
   112  	if err != nil {
   113  		return nil, nil, err
   114  	}
   115  
   116  	il := new(IssueLink)
   117  	resp, err := s.client.Do(req, il)
   118  	if err != nil {
   119  		return nil, resp, err
   120  	}
   121  
   122  	return il, resp, nil
   123  }
   124  
   125  // CreateIssueLinkOptions represents the available CreateIssueLink() options.
   126  //
   127  // GitLab API docs: https://docs.gitlab.com/ee/api/issue_links.html#create-an-issue-link
   128  type CreateIssueLinkOptions struct {
   129  	TargetProjectID *string `json:"target_project_id"`
   130  	TargetIssueIID  *string `json:"target_issue_iid"`
   131  	LinkType        *string `json:"link_type"`
   132  }
   133  
   134  // CreateIssueLink creates a two-way relation between two issues.
   135  // User must be allowed to update both issues in order to succeed.
   136  //
   137  // GitLab API docs:
   138  // https://docs.gitlab.com/ee/api/issue_links.html#create-an-issue-link
   139  func (s *IssueLinksService) CreateIssueLink(pid interface{}, issue int, opt *CreateIssueLinkOptions, options ...RequestOptionFunc) (*IssueLink, *Response, error) {
   140  	project, err := parseID(pid)
   141  	if err != nil {
   142  		return nil, nil, err
   143  	}
   144  	u := fmt.Sprintf("projects/%s/issues/%d/links", PathEscape(project), issue)
   145  
   146  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
   147  	if err != nil {
   148  		return nil, nil, err
   149  	}
   150  
   151  	i := new(IssueLink)
   152  	resp, err := s.client.Do(req, &i)
   153  	if err != nil {
   154  		return nil, resp, err
   155  	}
   156  
   157  	return i, resp, nil
   158  }
   159  
   160  // DeleteIssueLink deletes an issue link, thus removes the two-way relationship.
   161  //
   162  // GitLab API docs:
   163  // https://docs.gitlab.com/ee/api/issue_links.html#delete-an-issue-link
   164  func (s *IssueLinksService) DeleteIssueLink(pid interface{}, issue, issueLink int, options ...RequestOptionFunc) (*IssueLink, *Response, error) {
   165  	project, err := parseID(pid)
   166  	if err != nil {
   167  		return nil, nil, err
   168  	}
   169  	u := fmt.Sprintf("projects/%s/issues/%d/links/%d",
   170  		PathEscape(project),
   171  		issue,
   172  		issueLink)
   173  
   174  	req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
   175  	if err != nil {
   176  		return nil, nil, err
   177  	}
   178  
   179  	il := new(IssueLink)
   180  	resp, err := s.client.Do(req, &il)
   181  	if err != nil {
   182  		return nil, resp, err
   183  	}
   184  
   185  	return il, resp, nil
   186  }
   187  

View as plain text