forked from hammingcube/ci
/
main.go
123 lines (111 loc) · 2.93 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
package main
import (
"encoding/json"
"fmt"
"github.com/CloudCom/firego"
"github.com/google/go-github/github"
"github.com/maddyonline/ci/prepare"
"github.com/phayes/hookserve/hookserve"
"github.com/streamrail/concurrent-map"
"golang.org/x/oauth2"
"log"
"os"
"strings"
"time"
)
const (
PENDING = "pending"
SUCCESS = "success"
ERROR = "error"
FAILURE = "failure"
)
var mymap cmap.ConcurrentMap
func sPtr(s string) *string { return &s }
func wait() {
ticker := time.NewTicker(time.Second)
go func() {
for t := range ticker.C {
fmt.Println("Tick at", t)
}
}()
time.Sleep(time.Second * 15)
ticker.Stop()
fmt.Println("Ticker stopped")
}
func build(commit *hookserve.Event) {
key := strings.Join([]string{commit.Owner, commit.Repo, commit.Commit}, ",")
if _, ok := mymap.Get(key); ok {
return
} else {
mymap.Set(key, "pending")
}
defer mymap.Remove(key)
fmt.Println("Building: ", commit.Owner, commit.Repo, commit.Branch, commit.Commit)
currentStatus := PENDING
buildURL := fmt.Sprintf("https://builds.firebaseio.com/%s/%s/%s/%s", commit.Owner, commit.Repo, commit.Branch, commit.Commit)
f := firego.New(buildURL)
f.Auth(os.Getenv("FIREBASE_SECRET"))
v := map[string]string{
"status": currentStatus,
}
pushedFirego, err := f.Push(v)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", pushedFirego)
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("GH_TOKEN")},
)
tc := oauth2.NewClient(oauth2.NoContext, ts)
client := github.NewClient(tc)
repoStatus, _, err := client.Repositories.CreateStatus(commit.Owner, commit.Repo, commit.Commit,
&github.RepoStatus{
State: sPtr(currentStatus),
TargetURL: sPtr(buildURL),
Description: sPtr("The build started"),
Context: sPtr("ci/builds"),
})
if err != nil {
fmt.Println(err)
}
jsonBytes := prepare.Main(commit.Owner, commit.Repo)
var statusVal map[string]string
json.Unmarshal(jsonBytes, &statusVal)
if statusVal != nil {
fmt.Println("Got the following status: ", statusVal["status"])
currentStatus = statusVal["status"]
}
wait()
pushedFirego.Update(map[string]string{"status": currentStatus})
repoStatus, _, err = client.Repositories.CreateStatus(commit.Owner, commit.Repo, commit.Commit,
&github.RepoStatus{
State: sPtr(currentStatus),
TargetURL: sPtr(buildURL),
Description: sPtr("The build succeeded"),
Context: sPtr("ci/builds"),
})
if err != nil {
fmt.Println(err)
}
fmt.Println(repoStatus)
}
func main() {
// Create a new map.
mymap = cmap.New()
log.Println(os.Getenv("GH_TOKEN"))
server := hookserve.NewServer()
server.Port = 8120
server.Secret = "absolutesecret"
server.GoListenAndServe()
fmt.Printf("Listening on %d\n", server.Port)
for {
select {
case commit := <-server.Events:
fmt.Println("Got: ", commit.Owner, commit.Repo, commit.Branch, commit.Commit)
go func() { build(&commit) }()
default:
time.Sleep(100)
//fmt.Println("No activity...")
}
}
}