|
| 1 | +package main |
| 2 | + |
| 3 | +import( |
| 4 | + "net/http" |
| 5 | + "encoding/json" |
| 6 | + "io/ioutil" |
| 7 | + "os/exec" |
| 8 | + "os" |
| 9 | + "log" |
| 10 | + "errors" |
| 11 | + "strconv" |
| 12 | +) |
| 13 | + |
| 14 | +//GitlabRepository represents repository information from the webhook |
| 15 | +type GitlabRepository struct { |
| 16 | + Name, Url, Description, Home string |
| 17 | +} |
| 18 | + |
| 19 | +//Commit represents commit information from the webhook |
| 20 | +type Commit struct { |
| 21 | + Id, Message, Timestamp, Url string |
| 22 | + Author Author |
| 23 | +} |
| 24 | + |
| 25 | +//Author represents author information from the webhook |
| 26 | +type Author struct { |
| 27 | + Name, Email string |
| 28 | +} |
| 29 | + |
| 30 | +//Webhook represents push information from the webhook |
| 31 | +type Webhook struct { |
| 32 | + Before, After, Ref, User_name string |
| 33 | + User_id, Project_id int |
| 34 | + Repository GitlabRepository |
| 35 | + Commits []Commit |
| 36 | + Total_commits_count int |
| 37 | +} |
| 38 | + |
| 39 | +//ConfigRepository represents a repository from the config file |
| 40 | +type ConfigRepository struct { |
| 41 | + Name string |
| 42 | + Commands []string |
| 43 | +} |
| 44 | + |
| 45 | +//Config represents the config file |
| 46 | +type Config struct { |
| 47 | + Logfile string |
| 48 | + Address string |
| 49 | + Port int64 |
| 50 | + Repositories []ConfigRepository |
| 51 | +} |
| 52 | + |
| 53 | +func PanicIf(err error, what ...string) { |
| 54 | + if(err != nil) { |
| 55 | + if(len(what) == 0) { |
| 56 | + panic(err) |
| 57 | + } |
| 58 | + |
| 59 | + panic(errors.New(err.Error() + what[0])) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +var config Config |
| 64 | + |
| 65 | +func main() { |
| 66 | + //load config |
| 67 | + config := loadConfig() |
| 68 | + |
| 69 | + //open log file |
| 70 | + writer, err := os.OpenFile(config.Logfile, os.O_RDWR|os.O_APPEND, 0666) |
| 71 | + PanicIf(err) |
| 72 | + |
| 73 | + //close logfile on exit |
| 74 | + defer func() { |
| 75 | + writer.Close() |
| 76 | + }() |
| 77 | + |
| 78 | + //setting logging output |
| 79 | + log.SetOutput(writer) |
| 80 | + |
| 81 | + //setting handler |
| 82 | + http.HandleFunc("/", hookHandler) |
| 83 | + |
| 84 | + address := config.Address + ":" + strconv.FormatInt(config.Port, 10) |
| 85 | + |
| 86 | + log.Println("Listening on " + address) |
| 87 | + |
| 88 | + //starting server |
| 89 | + err = http.ListenAndServe(address, nil) |
| 90 | + if(err != nil) { |
| 91 | + log.Println(err) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func loadConfig() Config { |
| 96 | + var file, err = os.Open("config.json") |
| 97 | + PanicIf(err) |
| 98 | + |
| 99 | + // close file on exit and check for its returned error |
| 100 | + defer func() { |
| 101 | + err := file.Close() |
| 102 | + PanicIf(err) |
| 103 | + }() |
| 104 | + |
| 105 | + buffer := make([]byte, 1024) |
| 106 | + count := 0 |
| 107 | + |
| 108 | + count, err = file.Read(buffer) |
| 109 | + PanicIf(err) |
| 110 | + |
| 111 | + err = json.Unmarshal(buffer[:count], &config) |
| 112 | + PanicIf(err) |
| 113 | + |
| 114 | + return config |
| 115 | +} |
| 116 | + |
| 117 | +func hookHandler(w http.ResponseWriter, r *http.Request) { |
| 118 | + defer func() { |
| 119 | + if r := recover(); r != nil { |
| 120 | + log.Println(r) |
| 121 | + } |
| 122 | + }() |
| 123 | + |
| 124 | + var hook Webhook |
| 125 | + |
| 126 | + //read request body |
| 127 | + var data, err = ioutil.ReadAll(r.Body) |
| 128 | + PanicIf(err, "while reading request") |
| 129 | + |
| 130 | + //unmarshal request body |
| 131 | + err = json.Unmarshal(data, &hook) |
| 132 | + PanicIf(err, "while unmarshaling request") |
| 133 | + |
| 134 | + //find matching config for repository name |
| 135 | + for _, repo := range config.Repositories { |
| 136 | + if(repo.Name != hook.Repository.Name) { continue } |
| 137 | + |
| 138 | + //execute commands for repository |
| 139 | + for _, cmd := range repo.Commands { |
| 140 | + var command = exec.Command(cmd) |
| 141 | + err = command.Run() |
| 142 | + if(err != nil) { |
| 143 | + log.Println(err) |
| 144 | + } else { |
| 145 | + log.Println("Executed: " + cmd) |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments