Gitkins Light
package main | |
import ( | |
"log" | |
"time" | |
"net/http" | |
"encoding/json" | |
"golang.org/x/oauth2" | |
"github.com/google/go-github/github" | |
) | |
var ( | |
githubToken = "c14XXXXXXXXXXXXXXXXXXXXda6" | |
client *github.Client | |
pending = "pending" | |
success = "success" | |
failure = "error" | |
targetUrl = "https://xyz.ngrok.com/status" | |
pendingDesc = "Build/testing in progress, please wait." | |
successDesc = "Build/testing successful." | |
failureDesc = "Build or Unit Test failed." | |
appName = "TEST-CI" | |
) | |
func main() { | |
// Setup the token for github authentication | |
ts := oauth2.StaticTokenSource( | |
&oauth2.Token{AccessToken: githubToken}, //pfortin-urbn token | |
) | |
tc := oauth2.NewClient(oauth2.NoContext, ts) | |
// Get a client instance from github | |
client = github.NewClient(tc) | |
// Github is now ready to receive information from us!!! | |
//Setup the serve route to receive guthub events | |
http.HandleFunc("/event", EventHandler) | |
// Start the server | |
log.Println("Listening...") | |
log.Fatal(http.ListenAndServe(":8081", nil)) | |
} | |
//This is the event handler for github events. | |
func EventHandler(w http.ResponseWriter, r *http.Request) { | |
event_type := r.Header.Get("X-GitHub-Event") | |
if event_type == "pull_request" { | |
pr_event := new(github.PullRequestEvent) | |
if *pr_event.PullRequest.State == "open" { | |
json.NewDecoder(r.Body).Decode(pr_event) | |
log.Printf("Event Type: %s, Created by: %s\n", event_type, pr_event.PullRequest.Base.User.Login) | |
log.Printf("Message: %s\n", r.Body) | |
status1 := &github.RepoStatus{State: &pending, TargetURL: &targetUrl, Description: &pendingDesc, Context: &appName} | |
client.Repositories.CreateStatus(*pr_event.PullRequest.Base.User.Login, *pr_event.PullRequest.Base.Repo.Name, *pr_event.PullRequest.Head.SHA, status1) | |
// sleep for 30 seconds | |
time.Sleep(30 * time.Second) | |
log.Println("Returning Success") | |
status2 := &github.RepoStatus{State: &success, TargetURL: &targetUrl, Description: &successDesc, Context: &appName} | |
client.Repositories.CreateStatus(*pr_event.PullRequest.Base.User.Login, *pr_event.PullRequest.Base.Repo.Name, *pr_event.PullRequest.Head.SHA, status2) | |
log.Println("Handler exiting...") | |
} else { | |
log.Println("PR state not open or reopen") | |
} | |
} else { | |
log.Printf("Event %s not supported yet.\n", event_type) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment