-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathreleases.go
60 lines (48 loc) · 1.63 KB
/
releases.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package releasekit
import (
"context"
"net/http"
"os"
"path/filepath"
"github.com/google/go-github/github"
)
// GetReleaseByTag returns a repository release for the given tag if it exists.
func GetReleaseByTag(c *github.Client, owner, repo, tag string) (*github.RepositoryRelease, error) {
release, res, err := c.Repositories.GetReleaseByTag(context.Background(), owner, repo, tag)
if err != nil && res.StatusCode != http.StatusNotFound {
return nil, err
}
return release, nil
}
// CreateOrEditRelease creates a repository release if it doesn't exist, else it
// will edit an existing repository release.
func CreateOrEditRelease(c *github.Client, owner, repo string, release *github.RepositoryRelease) (*github.RepositoryRelease, error) {
var output *github.RepositoryRelease
var err error
if release.ID == nil {
output, _, err = c.Repositories.CreateRelease(context.Background(), owner, repo, release)
} else {
output, _, err = c.Repositories.EditRelease(context.Background(), owner, repo, *release.ID, release)
}
if err != nil {
return nil, err
}
return output, nil
}
// UploadReleaseAssets uploads the files to the release as assets.
func UploadReleaseAssets(c *github.Client, owner, repo string, id int, attachments []string) error {
for _, attachment := range attachments {
f, err := os.OpenFile(attachment, os.O_RDONLY, 0644)
if err != nil {
return err
}
defer f.Close()
name := filepath.Clean(filepath.Base(f.Name()))
opt := &github.UploadOptions{Name: name}
_, _, err = c.Repositories.UploadReleaseAsset(context.Background(), owner, repo, id, opt, f)
if err != nil {
return err
}
}
return nil
}