github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/release/github/releases.go (about)

     1  // Modified from https://github.com/aktau/github-release/blob/master/releases.go
     2  
     3  package github
     4  
     5  import (
     6  	"fmt"
     7  	"strings"
     8  	"time"
     9  )
    10  
    11  const (
    12  	releaseListPath   = "/repos/%s/%s/releases"
    13  	releaseLatestPath = "/repos/%s/%s/releases/latest"
    14  )
    15  
    16  // Release is a Github API Release type
    17  type Release struct {
    18  	URL         string     `json:"url"`
    19  	PageURL     string     `json:"html_url"`
    20  	UploadURL   string     `json:"upload_url"`
    21  	ID          int        `json:"id"`
    22  	Name        string     `json:"name"`
    23  	Description string     `json:"body"`
    24  	TagName     string     `json:"tag_name"`
    25  	Draft       bool       `json:"draft"`
    26  	Prerelease  bool       `json:"prerelease"`
    27  	Created     *time.Time `json:"created_at"`
    28  	Published   *time.Time `json:"published_at"`
    29  	Assets      []Asset    `json:"assets"`
    30  }
    31  
    32  // CleanUploadURL is URL for uploading a release
    33  func (r *Release) CleanUploadURL() string {
    34  	bracket := strings.Index(r.UploadURL, "{")
    35  
    36  	if bracket == -1 {
    37  		return r.UploadURL
    38  	}
    39  
    40  	return r.UploadURL[0:bracket]
    41  }
    42  
    43  // ReleaseCreate is a Github API ReleaseCreate type
    44  type ReleaseCreate struct {
    45  	TagName         string `json:"tag_name"`
    46  	TargetCommitish string `json:"target_commitish,omitempty"`
    47  	Name            string `json:"name"`
    48  	Body            string `json:"body"`
    49  	Draft           bool   `json:"draft"`
    50  	Prerelease      bool   `json:"prerelease"`
    51  }
    52  
    53  // Releases returns releases for a repo
    54  func Releases(user, repo, token string) (releases []Release, err error) {
    55  	u, err := githubURL(githubAPIURL)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  	u.Path = fmt.Sprintf(releaseListPath, user, repo)
    60  	err = Get(token, u.String(), &releases)
    61  	if err != nil {
    62  		return
    63  	}
    64  	return
    65  }
    66  
    67  // LatestRelease returns latest release for repo
    68  func LatestRelease(user, repo, token string) (release *Release, err error) {
    69  	u, err := githubURL(githubAPIURL)
    70  	if err != nil {
    71  		return
    72  	}
    73  	u.Path = fmt.Sprintf(releaseLatestPath, user, repo)
    74  	err = Get(token, u.String(), &release)
    75  	return
    76  }
    77  
    78  // ReleaseOfTag returns release for tag
    79  func ReleaseOfTag(user, repo, tag, token string) (*Release, error) {
    80  	releases, err := Releases(user, repo, token)
    81  	if err != nil {
    82  		return nil, err
    83  	}
    84  
    85  	for _, release := range releases {
    86  		if release.TagName == tag {
    87  			return &release, nil
    88  		}
    89  	}
    90  
    91  	return nil, &ErrNotFound{Name: "release", Key: "tag", Value: tag}
    92  }