Last active
August 29, 2015 13:56
-
-
Save pschultz/9094420 to your computer and use it in GitHub Desktop.
Blueprint for a Gitlab webhook receiver. Developed against Gitlab 6.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"log" | |
"net/http" | |
"os" | |
"time" | |
) | |
type Event struct { | |
TotalCommitsCount int `json:"total_commits_count"` | |
Before string `json:"before"` | |
After string `json:"after"` | |
Ref string `json:"ref"` | |
UserId int `json:"user_id"` | |
UserName string `json:"user_name"` | |
ProjectId int `json:"project_id"` | |
Repository Repository `json:"repository"` | |
Commits []Commit `json:"commits"` | |
} | |
type Repository struct { | |
Homepage string `json:"homepage"` | |
Description string `json:"description"` | |
Url string `json:"url"` | |
Name string `json:"name"` | |
} | |
type Commit struct { | |
Author Author `json:"author"` | |
Url string `json:"url"` | |
Time time.Time `json:"timestamp"` | |
Message string `json:"message"` | |
Id string `json:"id"` | |
} | |
type Author struct { | |
Email string `json:"email"` | |
Name string `json:"name"` | |
} | |
func eventHandler(stream chan Event) { | |
for { | |
e := <-stream | |
log.Printf("Received %#v", e) | |
} | |
} | |
func httpHandler(w http.ResponseWriter, r *http.Request) { | |
eventStream := make(chan Event) | |
go eventHandler(eventStream) | |
e := Event{} | |
decoder := json.NewDecoder(r.Body) | |
if err := decoder.Decode(&e); err != nil { | |
w.WriteHeader(400) | |
log.Printf("ERROR: %s", err) | |
} | |
eventStream <- e | |
} | |
func main() { | |
http.HandleFunc("/", httpHandler) | |
addr := os.Getenv("ADDRESS") | |
port := os.Getenv("PORT") | |
if port == "" { | |
port = "8080" | |
} | |
http.ListenAndServe(addr+":"+port, nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment