forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.go
136 lines (120 loc) · 3.36 KB
/
actions.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package github
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"strconv"
)
func CreateRelease(token string, repo string, tag string, name string) error {
params := ReleaseCreate{
TagName: tag,
Name: name,
}
payload, err := json.Marshal(params)
if err != nil {
return fmt.Errorf("can't encode release creation params, %v", err)
}
reader := bytes.NewReader(payload)
uri := fmt.Sprintf("/repos/keybase/%s/releases", repo)
resp, err := DoAuthRequest("POST", GithubAPIURL+uri, "application/json", token, nil, reader)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return fmt.Errorf("while submitting %v, %v", string(payload), err)
}
if resp.StatusCode != http.StatusCreated {
if resp.StatusCode == 422 {
return fmt.Errorf("github returned %v (this is probably because the release already exists)",
resp.Status)
}
return fmt.Errorf("github returned %v", resp.Status)
}
return nil
}
func Upload(token string, repo string, tag string, name string, file string) error {
release, err := ReleaseOfTag("keybase", repo, tag, token)
if err != nil {
return err
}
v := url.Values{}
v.Set("name", name)
url := release.CleanUploadURL() + "?" + v.Encode()
osfile, err := os.Open(file)
if err != nil {
return err
}
resp, err := DoAuthRequest("POST", url, "application/octet-stream", token, nil, osfile)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return err
}
if resp.StatusCode != http.StatusCreated {
if resp.StatusCode == 422 {
return fmt.Errorf("github returned %v (this is probably because the release already exists)",
resp.Status)
}
return fmt.Errorf("github returned %v", resp.Status)
}
return nil
}
func DownloadSource(token string, repo string, tag string) error {
url := GithubAPIURL + fmt.Sprintf("/repos/keybase/%s/tarball/%s", repo, tag)
name := fmt.Sprintf("%s-%s.tar.gz", repo, tag)
log.Printf("Url: %s", url)
return Download(token, url, name)
}
func DownloadAsset(token string, repo string, tag string, name string) error {
release, err := ReleaseOfTag("keybase", repo, tag, token)
if err != nil {
return err
}
assetID := 0
for _, asset := range release.Assets {
if asset.Name == name {
assetID = asset.ID
}
}
if assetID == 0 {
return fmt.Errorf("could not find asset named %s", name)
}
url := GithubAPIURL + fmt.Sprintf(AssetDownloadURI, "keybase", repo, assetID)
return Download(token, url, name)
}
func Download(token string, url string, name string) error {
resp, err := DoAuthRequest("GET", url, "", token, map[string]string{
"Accept": "application/octet-stream",
}, nil)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return fmt.Errorf("could not fetch releases, %v", err)
}
contentLength, err := strconv.ParseInt(resp.Header.Get("Content-Length"), 10, 64)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("github did not respond with 200 OK but with %v", resp.Status)
}
out, err := os.Create(name)
if err != nil {
return fmt.Errorf("could not create file %s", name)
}
defer out.Close()
n, err := io.Copy(out, resp.Body)
if n != contentLength {
return fmt.Errorf("downloaded data did not match content length %d != %d", contentLength, n)
}
return err
}