...

Source file src/github.com/google/go-github/v55/github/repos_autolinks.go

Documentation: github.com/google/go-github/v55/github

     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/en/rest/repos/autolinks#list-all-autolinks-of-a-repository
    32  func (s *RepositoriesService) ListAutolinks(ctx context.Context, owner, repo string, opts *ListOptions) ([]*Autolink, *Response, error) {
    33  	u := fmt.Sprintf("repos/%v/%v/autolinks", owner, repo)
    34  	u, err := addOptions(u, opts)
    35  	if err != nil {
    36  		return nil, nil, err
    37  	}
    38  
    39  	req, err := s.client.NewRequest("GET", u, nil)
    40  	if err != nil {
    41  		return nil, nil, err
    42  	}
    43  
    44  	var autolinks []*Autolink
    45  	resp, err := s.client.Do(ctx, req, &autolinks)
    46  	if err != nil {
    47  		return nil, resp, err
    48  	}
    49  
    50  	return autolinks, resp, nil
    51  }
    52  
    53  // AddAutolink creates an autolink reference for a repository.
    54  // Users with admin access to the repository can create an autolink.
    55  //
    56  // GitHub API docs: https://docs.github.com/en/rest/repos/autolinks#create-an-autolink-reference-for-a-repository
    57  func (s *RepositoriesService) AddAutolink(ctx context.Context, owner, repo string, opts *AutolinkOptions) (*Autolink, *Response, error) {
    58  	u := fmt.Sprintf("repos/%v/%v/autolinks", owner, repo)
    59  	req, err := s.client.NewRequest("POST", u, opts)
    60  	if err != nil {
    61  		return nil, nil, err
    62  	}
    63  
    64  	al := new(Autolink)
    65  	resp, err := s.client.Do(ctx, req, al)
    66  	if err != nil {
    67  		return nil, resp, err
    68  	}
    69  	return al, resp, nil
    70  }
    71  
    72  // GetAutolink returns a single autolink reference by ID that was configured for the given repository.
    73  // Information about autolinks are only available to repository administrators.
    74  //
    75  // GitHub API docs: https://docs.github.com/en/rest/repos/autolinks#get-an-autolink-reference-of-a-repository
    76  func (s *RepositoriesService) GetAutolink(ctx context.Context, owner, repo string, id int64) (*Autolink, *Response, error) {
    77  	u := fmt.Sprintf("repos/%v/%v/autolinks/%v", owner, repo, id)
    78  
    79  	req, err := s.client.NewRequest("GET", u, nil)
    80  	if err != nil {
    81  		return nil, nil, err
    82  	}
    83  
    84  	var autolink *Autolink
    85  	resp, err := s.client.Do(ctx, req, &autolink)
    86  	if err != nil {
    87  		return nil, resp, err
    88  	}
    89  
    90  	return autolink, resp, nil
    91  }
    92  
    93  // DeleteAutolink deletes a single autolink reference by ID that was configured for the given repository.
    94  // Information about autolinks are only available to repository administrators.
    95  //
    96  // GitHub API docs: https://docs.github.com/en/rest/repos/autolinks#delete-an-autolink-reference-from-a-repository
    97  func (s *RepositoriesService) DeleteAutolink(ctx context.Context, owner, repo string, id int64) (*Response, error) {
    98  	u := fmt.Sprintf("repos/%v/%v/autolinks/%v", owner, repo, id)
    99  	req, err := s.client.NewRequest("DELETE", u, nil)
   100  	if err != nil {
   101  		return nil, err
   102  	}
   103  	return s.client.Do(ctx, req, nil)
   104  }
   105  

View as plain text