-
Notifications
You must be signed in to change notification settings - Fork 24
/
gitHub.go
143 lines (107 loc) · 2.73 KB
/
gitHub.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
package pike
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"github.com/google/go-github/v47/github"
"github.com/rs/zerolog/log"
)
const lastOK = 299
// InvokeGithubDispatchEvent uses your gitHub api key (if sufficiently enabled) to invoke a gitHub action workflow.
func InvokeGithubDispatchEvent(repository string, workflowFileName string, branch string) error {
owner, repo, err := SplitHub(repository)
if err != nil {
log.Print(err)
return fmt.Errorf("failed to SplitHub %w", err)
}
url := "https://api.github.com/repos/" + owner + "/" + repo + "/actions/workflows/" + workflowFileName
err2 := VerifyURL(url)
if err2 != nil {
log.Print(err2)
return err2
}
ctx, client := GetGithubClient()
err3 := VerifyBranch(client, owner, repo, branch)
if err3 != nil {
log.Print(err3)
return err3
}
event := github.CreateWorkflowDispatchEventRequest{
Ref: branch,
}
response, err := client.Actions.CreateWorkflowDispatchEventByFileName(
ctx,
owner,
repo,
workflowFileName,
event)
if response == nil {
return fmt.Errorf("query failed")
}
if response.StatusCode > lastOK {
return fmt.Errorf("non success status code %s for %s", response.Status, url)
}
if err != nil {
log.Printf("invoke failed %s", response.Response.Status)
return err
}
myResponse := *response.Response
Header := myResponse.Header
remains := Header["X-Ratelimit-Remaining"]
if len(remains) != 0 {
if left, err := strconv.Atoi(remains[0]); err == nil {
if left == 0 {
return errors.New("you are being gitHub rate limited")
}
log.Printf("Invoked: Github rate limit remaining: %s", remains[0])
}
}
return nil
}
// VerifyBranch checks that a branch exists in a repo.
func VerifyBranch(client *github.Client, owner string, repo string, branch string) error {
ctx := context.Background()
branches, _, err := client.Repositories.ListBranches(ctx, owner, repo, nil)
if err != nil {
return err
}
found := false
for _, stem := range branches {
temp := *stem
if *temp.Name == branch {
found = true
}
}
if found {
return nil
}
return errors.New("branch " + branch + " not found for " + repo)
}
// VerifyURL tests a url.
func VerifyURL(url string) error {
if //goland:noinspection HttpUrlsUsage
strings.Contains(strings.ToLower(url), "http://") {
return errors.New("http is insecure")
}
resp, err := http.Get(url)
if resp == nil {
return errors.New("response was nil")
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
}
}(resp.Body)
if resp.StatusCode > lastOK {
return fmt.Errorf("non success status code %s for %s", resp.Status, url)
}
if err != nil {
log.Printf("failed to reach %s for %s", url, resp.Status)
return err
}
return nil
}