forked from palantir/godel
/
github_publish.go
154 lines (134 loc) · 4.95 KB
/
github_publish.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2016 Palantir Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package publish
import (
"context"
"fmt"
"io"
"mime"
"net/url"
"os"
"path"
"path/filepath"
"github.com/google/go-github/github"
"github.com/jtacoma/uritemplates"
"github.com/pkg/errors"
"golang.org/x/oauth2"
"gopkg.in/cheggaaa/pb.v1"
"github.com/palantir/godel/apps/distgo/params"
)
type GitHubConnectionInfo struct {
APIURL string
User string
Token string
Owner string
Repository string
}
func (g *GitHubConnectionInfo) Publish(buildSpec params.ProductBuildSpec, paths ProductPaths, stdout io.Writer) ([]string, error) {
client := github.NewClient(oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: g.Token},
)))
// set base URL (should be of the form "https://api.github.com/")
apiURL, err := url.Parse(g.APIURL)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse %s as URL for API calls", g.APIURL)
}
client.BaseURL = apiURL
if g.Owner == "" {
g.Owner = g.User
}
fmt.Fprintf(stdout, "Creating GitHub release %s for %s/%s...", buildSpec.ProductVersion, g.Owner, g.Repository)
releaseRes, _, err := client.Repositories.CreateRelease(context.Background(), g.Owner, g.Repository, &github.RepositoryRelease{
TagName: github.String(buildSpec.ProductVersion),
})
if err != nil {
// newline to complement "..." output
fmt.Fprintln(stdout)
if ghErr, ok := err.(*github.ErrorResponse); ok && len(ghErr.Errors) > 0 {
if ghErr.Errors[0].Code == "already_exists" {
return nil, errors.Errorf("GitHub release %s already exists for %s/%s", buildSpec.ProductVersion, g.Owner, g.Repository)
}
}
return nil, errors.Wrapf(err, "failed to create GitHub release %s for %s/%s...", buildSpec.ProductVersion, g.Owner, g.Repository)
}
fmt.Fprintln(stdout, "done")
var uploadURLs []string
for _, currPath := range paths.artifactPaths {
uploadURL, err := g.uploadFileAtPath(client, releaseRes, currPath, stdout)
if err != nil {
return nil, err
}
uploadURLs = append(uploadURLs, uploadURL)
}
return uploadURLs, nil
}
func (g *GitHubConnectionInfo) uploadFileAtPath(client *github.Client, release *github.RepositoryRelease, filePath string, stdout io.Writer) (string, error) {
uploadURI, err := uploadURIForProduct(release.GetUploadURL(), path.Base(filePath))
if err != nil {
return "", err
}
f, err := os.Open(filePath)
if err != nil {
return "", errors.Wrapf(err, "failed to open artifact %s for upload", filePath)
}
uploadRes, _, err := githubUploadReleaseAssetWithProgress(context.Background(), client, uploadURI, f, stdout)
if err != nil {
return "", errors.Wrapf(err, "failed to upload artifact %s", filePath)
}
return uploadRes.GetBrowserDownloadURL(), nil
}
// uploadURIForProduct returns an asset upload URI using the provided upload template from the release creation
// response. See https://developer.github.com/v3/repos/releases/#response for the specifics of the API.
func uploadURIForProduct(githubUploadURLTemplate, name string) (string, error) {
const nameTemplate = "name"
t, err := uritemplates.Parse(githubUploadURLTemplate)
if err != nil {
return "", errors.Wrapf(err, "failed to parse upload URI template %q", githubUploadURLTemplate)
}
uploadURI, err := t.Expand(map[string]interface{}{
nameTemplate: name,
})
if err != nil {
return "", errors.Wrapf(err, "failed to expand URI template %q with %q = %q", githubUploadURLTemplate, nameTemplate, name)
}
return uploadURI, nil
}
// Based on github.Repositories.UploadReleaseAsset. Adds support for progress reporting.
func githubUploadReleaseAssetWithProgress(ctx context.Context, client *github.Client, uploadURI string, file *os.File, stdout io.Writer) (*github.ReleaseAsset, *github.Response, error) {
stat, err := file.Stat()
if err != nil {
return nil, nil, err
}
if stat.IsDir() {
return nil, nil, errors.New("the asset to upload can't be a directory")
}
fmt.Fprintf(stdout, "Uploading %s to %s\n", file.Name(), uploadURI)
bar := pb.New(int(stat.Size())).SetUnits(pb.U_BYTES)
bar.Output = stdout
bar.SetMaxWidth(120)
bar.Start()
defer bar.Finish()
reader := bar.NewProxyReader(file)
mediaType := mime.TypeByExtension(filepath.Ext(file.Name()))
req, err := client.NewUploadRequest(uploadURI, reader, stat.Size(), mediaType)
if err != nil {
return nil, nil, err
}
asset := new(github.ReleaseAsset)
resp, err := client.Do(ctx, req, asset)
if err != nil {
return nil, resp, err
}
return asset, resp, nil
}