This repository was archived by the owner on May 27, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdispatch-controller.go
More file actions
109 lines (89 loc) · 2.32 KB
/
dispatch-controller.go
File metadata and controls
109 lines (89 loc) · 2.32 KB
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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strings"
)
func getBerarer() string {
ex, err := os.Executable()
if err != nil {
panic(err)
}
exPath := filepath.Dir(ex)
bearer, err := os.ReadFile(exPath + "/bearer.token")
if err != nil {
log.Fatal(err)
}
cleanup := strings.Replace(string(bearer), "\r", "", -1)
cleanup = strings.Replace(string(bearer), "\n", "", -1)
fmt.Println("Bearer: ", string(cleanup))
return cleanup
}
type Dispatch struct {
EventType string `json:"event_type"`
}
type UserInput struct {
UserName string
RepoName string
EventType string
}
func collectFlags() UserInput {
userName := flag.String("userName", "", "Name of user to get repo from")
repoName := flag.String("repoName", "", "Name of repo to target for dispatch")
eventType := flag.String("eventType", "", "Name of event type inside repo")
flag.Parse()
var errSlice []string
if len(*userName) == 0 {
errSlice = append(errSlice, "Please specify git -userName (or repo)")
}
if len(*repoName) == 0 {
errSlice = append(errSlice, "Please specify -repoName")
}
if len(*eventType) == 0 {
errSlice = append(errSlice, "Please specify -eventType")
}
if len(errSlice) > 0 {
for i := 0; i < len(errSlice); i++ {
log.Println(errSlice[i])
}
log.Fatalln("Please provide the above credentials...")
}
userInput := UserInput{
UserName: *userName,
RepoName: *repoName,
EventType: *eventType,
}
fmt.Println("userName to use: ", *userName)
fmt.Println("repoName to target: ", *repoName)
fmt.Println("eventType to start: ", *eventType)
return userInput
}
func main() {
userInput := collectFlags()
bearer := getBerarer()
baseURL := "https://api.github.com/repos/"
url := baseURL + userInput.UserName + "/" + userInput.RepoName + "/" + "dispatches"
fmt.Println(url)
dispatch := Dispatch{
EventType: userInput.EventType,
}
dispatchJSON, _ := json.Marshal(dispatch)
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(dispatchJSON))
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+bearer)
res, err := http.DefaultClient.Do(req)
fmt.Println(res)
if err != nil {
log.Fatal("Could not make POST request")
}
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
fmt.Println(string(body))
}