-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
183 lines (157 loc) · 3.86 KB
/
main.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
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/alecholmez/GoDash/config"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
)
// Project represents a CircleCI project associated with a user
type Project struct {
Name string `json:"reponame"`
User string `json:"username"`
Language string `json:"language"`
VCSType string `json:"vcs_type"`
}
// Repo is where the current build data is from circle
type Repo struct {
Builds []Build
}
// Build contains circle build info
type Build struct {
Commit string `json:"subject"`
Status string `json:"status"`
User User `json:"user"`
Lifecyrcle string `json:"lifecycle"`
Branch string `json:"branch"`
BuildNum int `json:"build_num"`
StartTime string `json:"start_time"`
StopTime string `json:"stop_time"`
}
// User holds info about the user who triggered the build
type User struct {
Login string `json:"login"`
Avatar string `json:"avatar_url"`
Name string `json:"name"`
}
// Info to send from API call
type Info struct {
Name string `json:"reponame"`
Language string `json:"language"`
Build Build `json:"build_info"`
}
const (
apiURL = "https://circleci.com/api/v1.1"
)
var (
conf = flag.String("config", "../etc/settings.toml", "Path to config file")
upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
fmt.Println(r.Header.Get("Origin"))
return true
},
}
)
// Get API Token
var token = os.Getenv("CIRCLE_CI_AUTH_TOKEN")
var client = &http.Client{}
func main() {
flag.Parse()
c := config.Parse(*conf)
mux := mux.NewRouter()
mux.HandleFunc("/dash", Dash)
s := http.Server{
Addr: c.Address,
Handler: handlers.LoggingHandler(os.Stdout, handlers.CORS()(mux)),
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
fmt.Println("Listening. . .")
if err := s.ListenAndServe(); err != nil {
panic(err)
}
}
// Dash is the handler that exposes the polling function
func Dash(w http.ResponseWriter, r *http.Request) {
// Upgrade web handler to a websocket connection
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println("upgrade: ", err)
return
}
defer c.Close()
// Business logic
if token == "" {
err = errors.New("Missing CIRCLE_CI_AUTH_TOKEN")
w.Write([]byte(err.Error()))
return
}
for {
log.Println("Sending current builds. . .")
projects := getProjects(fmt.Sprintf("%s/projects?circle-token=%s", apiURL, token))
var resp struct {
Builds []Info `json:"builds"`
}
for _, project := range projects {
url := fmt.Sprintf("%s/project/%s/%s/%s?circle-token=%s", apiURL, project.VCSType, project.User, project.Name, token)
inf := getBuildInfo(project, url)
resp.Builds = append(resp.Builds, inf)
}
err = c.WriteJSON(resp)
if err != nil {
log.Println("Write: ", err)
}
time.Sleep(time.Second * 10)
}
}
func getBuildInfo(p Project, url string) Info {
// Hit the circleci endpoint for associated projects
req, err := http.NewRequest("GET", url, nil)
if err != nil {
panic(err)
}
req.Header.Add("Accept", "application/json")
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
var repo Repo
err = json.NewDecoder(resp.Body).Decode(&repo.Builds)
if err != nil {
panic(err)
}
// Get the latest build
inf := Info{
Language: p.Language,
Name: p.Name,
Build: repo.Builds[0],
}
return inf
}
func getProjects(url string) []Project {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
panic(err)
}
// Specify json header otherwise circle won't send json
req.Header.Add("Accept", "application/json")
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
var projects []Project
err = json.NewDecoder(resp.Body).Decode(&projects)
if err != nil {
panic(err)
}
return projects
}