github.com/google/go-github/v57@v57.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  	IsAlphanumeric *bool   `json:"is_alphanumeric,omitempty"`
    18  }
    19  
    20  // Autolink represents autolinks to external resources like JIRA issues and Zendesk tickets.
    21  type Autolink struct {
    22  	ID             *int64  `json:"id,omitempty"`
    23  	KeyPrefix      *string `json:"key_prefix,omitempty"`
    24  	URLTemplate    *string `json:"url_template,omitempty"`
    25  	IsAlphanumeric *bool   `json:"is_alphanumeric,omitempty"`
    26  }
    27  
    28  // ListAutolinks returns a list of autolinks configured for the given repository.
    29  // Information about autolinks are only available to repository administrators.
    30  //
    31  // GitHub API docs: https://docs.github.com/rest/repos/autolinks#list-all-autolinks-of-a-repository
    32  //
    33  //meta:operation GET /repos/{owner}/{repo}/autolinks
    34  func (s *RepositoriesService) ListAutolinks(ctx context.Context, owner, repo string, opts *ListOptions) ([]*Autolink, *Response, error) {
    35  	u := fmt.Sprintf("repos/%v/%v/autolinks", owner, repo)
    36  	u, err := addOptions(u, opts)
    37  	if err != nil {
    38  		return nil, nil, err
    39  	}
    40  
    41  	req, err := s.client.NewRequest("GET", u, nil)
    42  	if err != nil {
    43  		return nil, nil, err
    44  	}
    45  
    46  	var autolinks []*Autolink
    47  	resp, err := s.client.Do(ctx, req, &autolinks)
    48  	if err != nil {
    49  		return nil, resp, err
    50  	}
    51  
    52  	return autolinks, resp, nil
    53  }
    54  
    55  // AddAutolink creates an autolink reference for a repository.
    56  // Users with admin access to the repository can create an autolink.
    57  //
    58  // GitHub API docs: https://docs.github.com/rest/repos/autolinks#create-an-autolink-reference-for-a-repository
    59  //
    60  //meta:operation POST /repos/{owner}/{repo}/autolinks
    61  func (s *RepositoriesService) AddAutolink(ctx context.Context, owner, repo string, opts *AutolinkOptions) (*Autolink, *Response, error) {
    62  	u := fmt.Sprintf("repos/%v/%v/autolinks", owner, repo)
    63  	req, err := s.client.NewRequest("POST", u, opts)
    64  	if err != nil {
    65  		return nil, nil, err
    66  	}
    67  
    68  	al := new(Autolink)
    69  	resp, err := s.client.Do(ctx, req, al)
    70  	if err != nil {
    71  		return nil, resp, err
    72  	}
    73  	return al, resp, nil
    74  }
    75  
    76  // GetAutolink returns a single autolink reference by ID that was configured for the given repository.
    77  // Information about autolinks are only available to repository administrators.
    78  //
    79  // GitHub API docs: https://docs.github.com/rest/repos/autolinks#get-an-autolink-reference-of-a-repository
    80  //
    81  //meta:operation GET /repos/{owner}/{repo}/autolinks/{autolink_id}
    82  func (s *RepositoriesService) GetAutolink(ctx context.Context, owner, repo string, id int64) (*Autolink, *Response, error) {
    83  	u := fmt.Sprintf("repos/%v/%v/autolinks/%v", owner, repo, id)
    84  
    85  	req, err := s.client.NewRequest("GET", u, nil)
    86  	if err != nil {
    87  		return nil, nil, err
    88  	}
    89  
    90  	var autolink *Autolink
    91  	resp, err := s.client.Do(ctx, req, &autolink)
    92  	if err != nil {
    93  		return nil, resp, err
    94  	}
    95  
    96  	return autolink, resp, nil
    97  }
    98  
    99  // DeleteAutolink deletes a single autolink reference by ID that was configured for the given repository.
   100  // Information about autolinks are only available to repository administrators.
   101  //
   102  // GitHub API docs: https://docs.github.com/rest/repos/autolinks#delete-an-autolink-reference-from-a-repository
   103  //
   104  //meta:operation DELETE /repos/{owner}/{repo}/autolinks/{autolink_id}
   105  func (s *RepositoriesService) DeleteAutolink(ctx context.Context, owner, repo string, id int64) (*Response, error) {
   106  	u := fmt.Sprintf("repos/%v/%v/autolinks/%v", owner, repo, id)
   107  	req, err := s.client.NewRequest("DELETE", u, nil)
   108  	if err != nil {
   109  		return nil, err
   110  	}
   111  	return s.client.Do(ctx, req, nil)
   112  }