github.com/google/go-github/v42@v42.0.0/github/repos_autolinks.go (about) 1 // Copyright 2021 The go-github AUTHORS. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 package github 7 8 import ( 9 "context" 10 "fmt" 11 ) 12 13 // AutolinkOptions specifies parameters for RepositoriesService.AddAutolink method. 14 type AutolinkOptions struct { 15 KeyPrefix *string `json:"key_prefix,omitempty"` 16 URLTemplate *string `json:"url_template,omitempty"` 17 } 18 19 // Autolink represents autolinks to external resources like JIRA issues and Zendesk tickets. 20 type Autolink struct { 21 ID *int64 `json:"id,omitempty"` 22 KeyPrefix *string `json:"key_prefix,omitempty"` 23 URLTemplate *string `json:"url_template,omitempty"` 24 } 25 26 // ListAutolinks returns a list of autolinks configured for the given repository. 27 // Information about autolinks are only available to repository administrators. 28 // 29 // GitHub API docs: https://docs.github.com/en/rest/reference/repos#list-all-autolinks-of-a-repository 30 func (s *RepositoriesService) ListAutolinks(ctx context.Context, owner, repo string, opts *ListOptions) ([]*Autolink, *Response, error) { 31 u := fmt.Sprintf("repos/%v/%v/autolinks", owner, repo) 32 u, err := addOptions(u, opts) 33 if err != nil { 34 return nil, nil, err 35 } 36 37 req, err := s.client.NewRequest("GET", u, nil) 38 if err != nil { 39 return nil, nil, err 40 } 41 42 var autolinks []*Autolink 43 resp, err := s.client.Do(ctx, req, &autolinks) 44 if err != nil { 45 return nil, resp, err 46 } 47 48 return autolinks, resp, nil 49 } 50 51 // AddAutolink creates an autolink reference for a repository. 52 // Users with admin access to the repository can create an autolink. 53 // 54 // GitHub API docs: https://docs.github.com/en/rest/reference/repos#create-an-autolink-reference-for-a-repository 55 func (s *RepositoriesService) AddAutolink(ctx context.Context, owner, repo string, opts *AutolinkOptions) (*Autolink, *Response, error) { 56 u := fmt.Sprintf("repos/%v/%v/autolinks", owner, repo) 57 req, err := s.client.NewRequest("POST", u, opts) 58 if err != nil { 59 return nil, nil, err 60 } 61 62 al := new(Autolink) 63 resp, err := s.client.Do(ctx, req, al) 64 if err != nil { 65 return nil, resp, err 66 } 67 return al, resp, nil 68 } 69 70 // GetAutolink returns a single autolink reference by ID that was configured for the given repository. 71 // Information about autolinks are only available to repository administrators. 72 // 73 // GitHub API docs: https://docs.github.com/en/rest/reference/repos#get-an-autolink-reference-of-a-repository 74 func (s *RepositoriesService) GetAutolink(ctx context.Context, owner, repo string, id int64) (*Autolink, *Response, error) { 75 u := fmt.Sprintf("repos/%v/%v/autolinks/%v", owner, repo, id) 76 77 req, err := s.client.NewRequest("GET", u, nil) 78 if err != nil { 79 return nil, nil, err 80 } 81 82 var autolink *Autolink 83 resp, err := s.client.Do(ctx, req, &autolink) 84 if err != nil { 85 return nil, resp, err 86 } 87 88 return autolink, resp, nil 89 } 90 91 // DeleteAutolink deletes a single autolink reference by ID that was configured for the given repository. 92 // Information about autolinks are only available to repository administrators. 93 // 94 // GitHub API docs: https://docs.github.com/en/rest/reference/repos#delete-an-autolink-reference-from-a-repository 95 func (s *RepositoriesService) DeleteAutolink(ctx context.Context, owner, repo string, id int64) (*Response, error) { 96 u := fmt.Sprintf("repos/%v/%v/autolinks/%v", owner, repo, id) 97 req, err := s.client.NewRequest("DELETE", u, nil) 98 if err != nil { 99 return nil, err 100 } 101 return s.client.Do(ctx, req, nil) 102 }