-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_artifact.go
316 lines (274 loc) · 8.55 KB
/
fetch_artifact.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Copyright 2020 Google Inc. All rights reserved.
//
// 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 main
import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"path"
"time"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
)
var (
target = flag.String("target", "", "the target to fetch from")
buildID = flag.String("build_id", "", "the build id to fetch from, can use '-branch' to get the latest passed build ID")
branch = flag.String("branch", "", "the branch to fetch from, used when '-build_id' is not provided,\nit would fetch the latest successful build")
artifact = flag.String("artifact", "", "the artifact to download")
output = flag.String("output", "", "the file name to save as")
clientID = flag.String("client_id", "", "[Optional] OAuth 2.0 Client ID. Must be used with '-secret'")
secret = flag.String("secret", "", "[Optional] OAuth 2.0 Client Secret. Must be used with '-client_id'")
port = flag.Int("port", 10502, "[Optional] the port number where the oauth callback server would listen on")
projectID = flag.String("project_id", "", "[Optional] the project id being used to access the fetch APIs.")
)
var writeToStdout = false
type BuildResponse struct {
Builds []Build `json:"builds"`
}
type Build struct {
BuildId string `json:"buildId"`
}
type Auth struct {
clientID string
secret string
}
func newAuth(clientID, secret string) *Auth {
return &Auth{clientID: clientID, secret: secret}
}
func newClient(ctx context.Context, auth *Auth) *http.Client {
if auth == nil {
return &http.Client{}
}
config := &oauth2.Config{
ClientID: auth.clientID,
ClientSecret: auth.secret,
Endpoint: google.Endpoint,
Scopes: []string{"https://www.googleapis.com/auth/androidbuild.internal"},
}
return newOAuthClient(ctx, config)
}
type FetchConfig struct {
client *http.Client
target string
buildID string
branch string
output string
artifact string
projectID string
args []string
}
func newFetchConfig(client *http.Client, target string, buildID string, branch string, output string, artifact string, projectID string, args []string) (*FetchConfig, error) {
config := &FetchConfig{
client: client,
target: target,
buildID: buildID,
branch: branch,
output: output,
artifact: artifact,
projectID: projectID,
args: args,
}
err := config.validate()
if err != nil {
return nil, err
}
return config, nil
}
func (c *FetchConfig) validate() error {
// We only support passing 1 argument `-` so if we have more than
// one argument this is an error state,
if len(c.args) > 1 {
return errors.New("too many arguments passed to fetch_artifact")
}
if len(c.args) > 0 {
writeToStdout = c.args[len(c.args)-1] == "-"
if !writeToStdout {
return fmt.Errorf(
"only supported final argument to fetch_artifact is `-` but got `%s`", c.args[len(c.args)-1])
}
if len(c.output) > 0 && writeToStdout {
return errors.New("both '-output' and '-' flags can not be used together")
}
}
// check user provided flags
if len(c.target) == 0 {
return errors.New("missing target")
}
if len(c.artifact) == 0 {
return errors.New("missing artifact")
}
if len(c.buildID) == 0 && len(c.branch) == 0 {
return errors.New("missing build_id or branch")
}
if len(c.buildID) != 0 && len(c.branch) != 0 {
return errors.New("too many arguments, you should only give build_id or branch")
}
if len(c.buildID) == 0 {
bid, err := getLatestGoodBuild(c)
if err != nil {
return err
}
c.buildID = bid
}
return nil
}
func errPrint(msg string) {
fmt.Fprintln(os.Stderr, msg)
os.Exit(1)
}
func main() {
flag.Parse()
args := flag.Args()
var auth *Auth
if len(*clientID) != 0 && len(*secret) != 0 {
auth = newAuth(*clientID, *secret)
} else if len(*clientID) != 0 || len(*secret) != 0 {
errPrint(fmt.Sprintf("missing client_id or client_secret."))
}
ctx := context.Background()
client := newClient(ctx, auth)
config, err := newFetchConfig(client, *target, *buildID, *branch, *output, *artifact, *projectID, args)
if err != nil {
errPrint(fmt.Sprintf("Config validation error: %s", err))
}
fetchArtifact(config)
}
func newOAuthClient(ctx context.Context, config *oauth2.Config) *http.Client {
token := tokenFromWeb(ctx, config)
return config.Client(ctx, token)
}
func tokenFromWeb(ctx context.Context, config *oauth2.Config) *oauth2.Token {
ch := make(chan string)
randState := fmt.Sprintf("st%d", time.Now().UnixNano())
ts := createServer(ch, randState)
go func() {
err := ts.ListenAndServe()
if err != http.ErrServerClosed {
errPrint(fmt.Sprintf("Listen and serve error: %v", err))
}
}()
defer ts.Close()
config.RedirectURL = "http://localhost" + ts.Addr
authURL := config.AuthCodeURL(randState)
log.Printf("Authorize this app at: %s", authURL)
code := <-ch
token, err := config.Exchange(ctx, code)
if err != nil {
errPrint(fmt.Sprintf("Token exchange error: %v", err))
}
return token
}
func createServer(ch chan string, state string) *http.Server {
return &http.Server{
Addr: fmt.Sprintf(":%d", *port),
Handler: http.HandlerFunc(handler(ch, state)),
}
}
func handler(ch chan string, randState string) func(http.ResponseWriter, *http.Request) {
return func(rw http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/favicon.ico" {
http.Error(rw, "error: visiting /favicon.ico", 404)
return
}
if req.FormValue("state") != randState {
log.Printf("state: %s doesn't match. (expected: %s)", req.FormValue("state"), randState)
http.Error(rw, "invalid state", 500)
return
}
if code := req.FormValue("code"); code != "" {
fmt.Fprintf(rw, "<h1>Success</h1>Authorized.")
rw.(http.Flusher).Flush()
ch <- code
return
}
ch <- ""
http.Error(rw, "invalid code", 500)
}
}
func sendRequest(client *http.Client, url string) (*http.Response, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json")
res, err := client.Do(req)
if err != nil {
return nil, err
}
return res, nil
}
func fetchArtifact(c *FetchConfig) error {
apiURL := fmt.Sprintf("https://androidbuildinternal.googleapis.com/android/internal/build/v3/builds/%s/%s/attempts/latest/artifacts/%s/url", url.QueryEscape(c.buildID), url.QueryEscape(c.target), url.QueryEscape(c.artifact))
if len(c.projectID) != 0 {
apiURL += fmt.Sprintf("?$userProject=%s", url.QueryEscape(c.projectID))
}
res, err := sendRequest(c.client, apiURL)
if err != nil {
return fmt.Errorf("error fetching artifact %w", err)
}
defer res.Body.Close()
if res.Status != "200 OK" {
body, _ := ioutil.ReadAll(res.Body)
errPrint(fmt.Sprintf("Unable to download artifact: %s\n %s.", res.Status, body))
}
if writeToStdout {
io.Copy(os.Stdout, res.Body)
return nil
}
fileName := c.artifact
if len(c.output) > 0 {
fileName = c.output
}
f, err := os.Create(path.Base(fileName))
if err != nil {
return fmt.Errorf("unable to create file %w", err)
}
defer f.Close()
io.Copy(f, res.Body)
fmt.Printf("File %s created.\n", f.Name())
return nil
}
func getLatestGoodBuild(c *FetchConfig) (string, error) {
apiURL := fmt.Sprintf("https://androidbuildinternal.googleapis.com/android/internal/build/v3/builds?branches=%s&buildAttemptStatus=complete&buildType=submitted&maxResults=1&successful=true&target=%s", url.QueryEscape(c.branch), url.QueryEscape(c.target))
if len(c.projectID) != 0 {
apiURL += fmt.Sprintf("&$userProject=%s", url.QueryEscape(c.projectID))
}
res, err := sendRequest(c.client, apiURL)
if err != nil {
return "", fmt.Errorf("send request error: %w", err)
}
defer res.Body.Close()
if res.Status != "200 OK" {
body, _ := ioutil.ReadAll(res.Body)
return "", fmt.Errorf("unable to get Build ID: %s\n %s", res.Status, body)
}
body, _ := io.ReadAll(res.Body)
var buildData BuildResponse
err = json.Unmarshal(body, &buildData)
if err != nil {
return "", fmt.Errorf("error parsing JSON: %w", err)
}
if len(buildData.Builds) == 0 {
return "", errors.New("error no build ID is found")
}
return buildData.Builds[0].BuildId, nil
}