forked from BirmacherAkos/bitrise-step-xctest-html-report
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgithubrelease.go
60 lines (48 loc) · 1.84 KB
/
githubrelease.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 main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/bitrise-io/go-steputils/stepconf"
"github.com/bitrise-io/go-utils/log"
)
// GithubRelease returned from the Github API
type GithubRelease struct {
TagName string `json:"tag_name"`
}
// https://api.github.com/repos/TitouanVanBelle/XCTestHTMLReport/releases/latest
// Returns the information of the latest release from the Github API for the provided repository
// More: https://docs.github.com/en/rest/reference/repos#releases
func latestGithubRelease(githubOrg, githubRepository string, accessToken stepconf.Secret) (GithubRelease, error) {
url := fmt.Sprintf("https://api.github.com/repos/%s/%s/releases/latest", githubOrg, githubRepository)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return GithubRelease{}, fmt.Errorf("failed to create new request for %s, error: %v", url, err)
}
if string(accessToken) != "" {
req.Header.Set("Authorization", fmt.Sprintf("token %s", string(accessToken)))
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return GithubRelease{}, fmt.Errorf("failed to call the %s, error: %v", url, err)
}
defer func() {
if cerr := resp.Body.Close(); cerr != nil {
log.Warnf("Failed to close response body, error: %v", cerr)
}
}()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return GithubRelease{}, fmt.Errorf("failed to read response body from the %s, error: %v", url, err)
}
if resp.StatusCode != http.StatusOK {
return GithubRelease{}, fmt.Errorf("response status %v, body: %s", resp.StatusCode, string(body))
}
log.Debugf("Response status: %s", resp.Status)
var githubRelease GithubRelease
if err := json.Unmarshal([]byte(body), &githubRelease); err != nil {
return githubRelease, fmt.Errorf("failed to parse latest Github Release JSON, ewrror: %v", err)
}
return githubRelease, nil
}