-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto.go
More file actions
64 lines (58 loc) · 1.72 KB
/
auto.go
File metadata and controls
64 lines (58 loc) · 1.72 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
"strings"
"github.com/citizensciencecenter/autodeploy/modules"
"github.com/gorilla/mux"
"github.com/spf13/viper"
)
func runHookServer() {
r := mux.NewRouter()
seed := viper.GetInt64("seed")
rand.Seed(seed)
routeID := rand.Uint64()
route := fmt.Sprintf("/%d", routeID)
fmt.Println("Your URL is: " + route)
r.HandleFunc("/", upHandler).Methods("GET")
r.HandleFunc(route, hookHandler).Methods("POST")
log.Fatal(http.ListenAndServe(":9898", r))
}
func upHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte("Autodeploy!\n"))
}
func hookHandler(w http.ResponseWriter, r *http.Request) {
// TODO check token is matching
body, err := ioutil.ReadAll(r.Body)
modules.ErrHandler(err)
hook := modules.TravisResp{}
json.Unmarshal(body, &hook)
w.Header().Set("Content-Type", "application/json")
src := fmt.Sprintf("%s:%s", hook.Repository.Name, hook.Branch)
rc := modules.HookBody{Source: src, Status: "SUCCESS", Stage: "Hook Triggered", Msg: "Hook started"}
ad := modules.AutoDeploy{Config: viper.GetViper(), HookBody: rc, Travis: hook}
ad.Dir = fmt.Sprintf("%s/%s", ad.Config.GetString("git.repo_dir"), hook.Repository.Name)
fmt.Println(ad.Dir)
if strings.Compare(hook.State, "passed") == 0 {
modules.Notify(ad)
w.WriteHeader(200)
w.Write([]byte("{data: Hook started}"))
go modules.InitRepo(hook.Repository.Name, hook.Branch, hook.GS, ad)
} else {
ad.HookBody.Stage = "Travis"
ad.HookBody.Status = "FAILED"
ad.HookBody.Msg = "Tests Failed"
modules.Notify(ad)
w.WriteHeader(500)
w.Write([]byte("{data: Hook Failed}"))
}
}
func main() {
modules.LoadConfig("./config/conf.json")
runHookServer()
}