-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathuploadtool.go
304 lines (259 loc) · 10.6 KB
/
uploadtool.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
// WORK IN PROGRESS
// Rewrite of uploadtool in Go
package main
import (
"fmt"
"log"
"net/http"
"os"
"path"
)
func main() {
fmt.Println("Hello World!")
if len(os.Args) < 2 {
log.Println("No artifacts provided, giving up.")
os.Exit(1)
}
// Exit immediately if one of the files given as arguments is not there
// because we don't want to delete the existing release if we don't have
// the new files that should be uploaded
for _, a := range os.Args[1:] {
if _, err := os.Stat(a); os.IsNotExist(err) {
log.Println(a, "is missing, giving up.")
os.Exit(1)
}
}
releaseName := "continuous"
releaseTitle := "Continuous build"
isPrerelease := true
// TODO: Port the following logic:
// The calling script (usually .travis.yml) can set a suffix to be used for
// the tag and release name. This way it is possible to have a release for
// the output of the CI/CD pipeline (marked as 'continuous') and also test
// builds for other branches.
// If this build was triggered by a tag, call the result a Release
// if [ ! -z "$UPLOADTOOL_SUFFIX" ] ; then
// if [ "$UPLOADTOOL_SUFFIX" = "$TRAVIS_TAG" ] ; then
// RELEASE_NAME="$TRAVIS_TAG"
// RELEASE_TITLE="Release build ($TRAVIS_TAG)"
// is_prerelease="false"
// else
// RELEASE_NAME="continuous-$UPLOADTOOL_SUFFIX"
// RELEASE_TITLE="Continuous build ($UPLOADTOOL_SUFFIX)"
// is_prerelease="true"
// fi
// else
// # ,, is a bash-ism to convert variable to lower case
// case $(tr '[:upper:]' '[:lower:]' <<< "$TRAVIS_TAG") in
// "")
// # Do not use "latest" as it is reserved by GitHub
// RELEASE_NAME="continuous"
// RELEASE_TITLE="Continuous build"
// is_prerelease="true"
// ;;
// *-alpha*|*-beta*|*-rc*)
// RELEASE_NAME="$TRAVIS_TAG"
// RELEASE_TITLE="Pre-release build ($TRAVIS_TAG)"
// is_prerelease="true"
// ;;
// *)
// RELEASE_NAME="$TRAVIS_TAG"
// RELEASE_TITLE="Release build ($TRAVIS_TAG)"
// is_prerelease="false"
// ;;
// esac
// fi
// TODO: Port the following logic:
// if [ "$ARTIFACTORY_BASE_URL" != "" ]; then
// echo "ARTIFACTORY_BASE_URL set, trying to upload to artifactory"
// if [ "$ARTIFACTORY_API_KEY" == "" ]; then
// echo "Please set ARTIFACTORY_API_KEY"
// exit 1
// fi
// files="$@"
// # artifactory doesn't support any kind of "artifact description", so we're uploading a text file containing the
// # relevant details along with the other artifacts
// tempdir=$(mktemp -d)
// info_file="$tempdir"/build-info.txt
// echo "Travis CI build log: ${TRAVIS_BUILD_WEB_URL}" > "$info_file"
// files+=("$info_file")
// set +x
// for file in ${files[@]}; do
// url="${ARTIFACTORY_BASE_URL}/travis-${TRAVIS_BUILD_NUMBER}/"$(basename "$file")
// md5sum=$(md5sum "$file" | cut -d' ' -f1)
// sha1sum=$(sha1sum "$file" | cut -d' ' -f1)
// sha256sum=$(sha256sum "$file" | cut -d' ' -f1)
// echo "Uploading $file to $url"
// hashsums=(-H "X-Checksum-Md5:$md5sum")
// hashsums+=(-H "X-Checksum-Sha1:$sha1sum")
// hashsums+=(-H "X-Checksum-Sha256:$sha256sum")
// if ! curl -H 'X-JFrog-Art-Api:'"$ARTIFACTORY_API_KEY" "${hashsums[@]}" -T "$file" "$url"; then
// echo "Failed to upload file, exiting"
// rm -r "$tempdir"
// exit 1
// fi
// echo
// echo "MD5 checksum: $md5sum"
// echo "SHA1 checksum: $sha1sum"
// echo "SHA256 checksum: $sha256sum"
// done
// rm -r "$tempdir"
// fi
// Do not upload non-master branch builds
// # if [ "$TRAVIS_TAG" != "$TRAVIS_BRANCH" ] && [ "$TRAVIS_BRANCH" != "master" ]; then export TRAVIS_EVENT_TYPE=pull_request; fi
if (os.Getenv("TRAVIS_TAG") != os.Getenv("TRAVIS_BRANCH")) && os.Getenv("TRAVIS_BRANCH") != "master" {
os.Setenv("TRAVIS_EVENT_TYPE", "pull_request")
}
// if [ "$TRAVIS_EVENT_TYPE" == "pull_request" ] ; then
// echo "Release uploading disabled for pull requests"
// if [ "$ARTIFACTORY_BASE_URL" != "" ]; then
// echo "Releases have already been uploaded to Artifactory, exiting"
// exit 0
// else
// echo "Release uploading disabled for pull requests, uploading to transfer.sh instead"
// rm -f ./uploaded-to
// for FILE in "$@" ; do
// BASENAME="$(basename "${FILE}")"
// curl --upload-file $FILE "https://transfer.sh/$BASENAME" > ./one-upload
// echo "$(cat ./one-upload)" # this way we get a newline
// echo -n "$(cat ./one-upload)\\n" >> ./uploaded-to # this way we get a \n but no newline
// done
// fi
if os.Getenv("TRAVIS_EVENT_TYPE") != "pull_request" {
log.Println("Release uploading disabled for pull requests")
if os.Getenv("ARTIFACTORY_BASE_URL") != "" {
log.Println("Releases have already been uploaded to Artifactory, exiting")
os.Exit(0)
} else {
log.Println("Release uploading disabled for pull requests, uploading to transfer.sh instead")
for _, a := range os.Args[1:] {
resp, err := http.Get("https://transfer.sh/" + path.Base(a))
if err != nil {
log.Println("Could not upload to transfer.sh:", err)
os.Exit(1)
}
defer resp.Body.Close()
}
}
}
if os.Getenv("TRAVIS_REPO_SLUG") != "" {
// We are running on Travis CI
log.Println("Running on Travis CI")
log.Println("TRAVIS_COMMIT:", os.Getenv("TRAVIS_COMMIT"))
if os.Getenv("TRAVIS_REPO_SLUG") == "" {
log.Println("$GITHUB_TOKEN missing, please set it in the Travis CI settings of this project")
log.Println("You can get one from https://github.com/settings/tokens")
os.Exit(1)
}
}
// TODO: Port the following logic:
// else
// # We are not running on Travis CI
// echo "Not running on Travis CI"
// if [ -z "$REPO_SLUG" ] ; then
// read -r -p "Repo Slug (GitHub and Travis CI username/reponame): " REPO_SLUG
// fi
// if [ -z "$GITHUB_TOKEN" ] ; then
// read -r -s -p "Token (https://github.com/settings/tokens): " GITHUB_TOKEN
// fi
// fi
// tag_url="https://api.github.com/repos/$REPO_SLUG/git/refs/tags/$RELEASE_NAME"
// tag_infos=$(curl -XGET --header "Authorization: token ${GITHUB_TOKEN}" "${tag_url}")
req, err := http.NewRequest("GET", os.ExpandEnv("https://api.github.com/repos/$REPO_SLUG/git/refs/tags/$RELEASE_NAME"), nil)
if err != nil {
log.Println("Could not construct request to https://api.github.com/repos/$REPO_SLUG/git/refs/tags/$RELEASE_NAME:", err)
os.Exit(1)
}
req.Header.Set("Authorization", os.ExpandEnv("token ${GITHUB_TOKEN}"))
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Println("Could not execute request to https://api.github.com/repos/$REPO_SLUG/git/refs/tags/$RELEASE_NAME:", err)
os.Exit(1)
}
defer resp.Body.Close()
log.Println("tag_infos:", resp.Body)
// tag_sha=$(echo "$tag_infos" | grep '"sha":' | head -n 1 | cut -d '"' -f 4 | cut -d '{' -f 1)
// echo "tag_sha: $tag_sha"
// release_url="https://api.github.com/repos/$REPO_SLUG/releases/tags/$RELEASE_NAME"
// echo "Getting the release ID..."
// echo "release_url: $release_url"
// release_infos=$(curl -XGET --header "Authorization: token ${GITHUB_TOKEN}" "${release_url}")
// echo "release_infos: $release_infos"
// release_id=$(echo "$release_infos" | grep "\"id\":" | head -n 1 | tr -s " " | cut -f 3 -d" " | cut -f 1 -d ",")
// echo "release ID: $release_id"
// upload_url=$(echo "$release_infos" | grep '"upload_url":' | head -n 1 | cut -d '"' -f 4 | cut -d '{' -f 1)
// echo "upload_url: $upload_url"
// release_url=$(echo "$release_infos" | grep '"url":' | head -n 1 | cut -d '"' -f 4 | cut -d '{' -f 1)
// echo "release_url: $release_url"
// target_commit_sha=$(echo "$release_infos" | grep '"target_commitish":' | head -n 1 | cut -d '"' -f 4 | cut -d '{' -f 1)
// echo "target_commit_sha: $target_commit_sha"
// if [ "$TRAVIS_COMMIT" != "$target_commit_sha" ] ; then
// echo "TRAVIS_COMMIT != target_commit_sha, hence deleting $RELEASE_NAME..."
// if [ ! -z "$release_id" ]; then
// delete_url="https://api.github.com/repos/$REPO_SLUG/releases/$release_id"
// echo "Delete the release..."
// echo "delete_url: $delete_url"
// curl -XDELETE \
// --header "Authorization: token ${GITHUB_TOKEN}" \
// "${delete_url}"
// fi
// # echo "Checking if release with the same name is still there..."
// # echo "release_url: $release_url"
// # curl -XGET --header "Authorization: token ${GITHUB_TOKEN}" \
// # "$release_url"
// if [ "$RELEASE_NAME" == "continuous" ] ; then
// # if this is a continuous build tag, then delete the old tag
// # in preparation for the new release
// echo "Delete the tag..."
// delete_url="https://api.github.com/repos/$REPO_SLUG/git/refs/tags/$RELEASE_NAME"
// echo "delete_url: $delete_url"
// curl -XDELETE \
// --header "Authorization: token ${GITHUB_TOKEN}" \
// "${delete_url}"
// fi
// echo "Create release..."
// if [ -z "$TRAVIS_BRANCH" ] ; then
// TRAVIS_BRANCH="master"
// fi
// if [ ! -z "$TRAVIS_JOB_ID" ] ; then
// if [ -z "${UPLOADTOOL_BODY+x}" ] ; then
// BODY="Travis CI build log: ${TRAVIS_BUILD_WEB_URL}"
// else
// BODY="$UPLOADTOOL_BODY"
// fi
// else
// BODY="$UPLOADTOOL_BODY"
// fi
// release_infos=$(curl -H "Authorization: token ${GITHUB_TOKEN}" \
// --data '{"tag_name": "'"$RELEASE_NAME"'","target_commitish": "'"$TRAVIS_COMMIT"'","name": "'"$RELEASE_TITLE"'","body": "'"$BODY"'","draft": false,"prerelease": '$is_prerelease'}' "https://api.github.com/repos/$REPO_SLUG/releases")
// echo "$release_infos"
// unset upload_url
// upload_url=$(echo "$release_infos" | grep '"upload_url":' | head -n 1 | cut -d '"' -f 4 | cut -d '{' -f 1)
// echo "upload_url: $upload_url"
// unset release_url
// release_url=$(echo "$release_infos" | grep '"url":' | head -n 1 | cut -d '"' -f 4 | cut -d '{' -f 1)
// echo "release_url: $release_url"
// fi # if [ "$TRAVIS_COMMIT" != "$tag_sha" ]
// if [ -z "$release_url" ] ; then
// echo "Cannot figure out the release URL for $RELEASE_NAME"
// exit 1
// fi
// echo "Upload binaries to the release..."
// for FILE in "$@" ; do
// FULLNAME="${FILE}"
// BASENAME="$(basename "${FILE}")"
// curl -H "Authorization: token ${GITHUB_TOKEN}" \
// -H "Accept: application/vnd.github.manifold-preview" \
// -H "Content-Type: application/octet-stream" \
// --data-binary @$FULLNAME \
// "$upload_url?name=$BASENAME"
// echo ""
// done
// $shatool "$@"
// if [ "$TRAVIS_COMMIT" != "$tag_sha" ] ; then
// echo "Publish the release..."
// release_infos=$(curl -H "Authorization: token ${GITHUB_TOKEN}" \
// --data '{"draft": false}' "$release_url")
// echo "$release_infos"
// fi # if [ "$TRAVIS_COMMIT" != "$tag_sha" ]
}