Skip to content

Instantly share code, notes, and snippets.

@travisperson
Forked from anonymous/file1.txt
Created October 4, 2014 05:10
Show Gist options
  • Save travisperson/4d53a954c30094b50d22 to your computer and use it in GitHub Desktop.
Save travisperson/4d53a954c30094b50d22 to your computer and use it in GitHub Desktop.
package main
import (
"github.com/gin-gonic/gin"
//"github.com/libgit2/git2go"
"fmt"
"os"
"os/exec"
)
type TestRun struct {
CommitHash string
}
type GithubUser struct {
Name string
Email string
Username string
}
type GithubCommit struct {
Id string
Message string
Author GithubUser
Committer GithubUser
}
type GithubRepo struct {
Id int
Url, Name string
}
type EventPush struct {
Ref string
After string
Before string
Created bool
Deleted bool
Forced bool
HeadCommit GithubCommit `json:"head_commit"`
Pusher GithubUser
Repository GithubRepo
}
var TestQueue chan *TestRun
func main () {
r := gin.Default()
var err error
// Check to see if the repo exists
_, err = os.Stat("repo")
// Run clone command if the repo does not exist
if err != nil {
fmt.Println("Checkout the repo to `repo`")
return;
}
//Start a go routine to queue up tests
TestQueue = make(chan *TestRun)
go func() {
var err error
var git_binary string
var python_binary string
git_binary, err = exec.LookPath("git")
if err != nil {
panic(err)
}
python_binary, err = exec.LookPath("python")
if err != nil {
panic(err)
}
for {
select {
case test := <-TestQueue:
//var repo *git.Repository
var err error
/*
fmt.Println(event)
fmt.Printf("ref %s\n", event.Ref)
fmt.Printf("after %s\n", event.After)
fmt.Printf("before %s\n", event.Before)
fmt.Printf("hc:id %s\n", event.HeadCommit.Id)
fmt.Printf("hc:auth:name %s\n", event.HeadCommit.Author.Name)
fmt.Printf("repo url %s\n", event.Repository.Url)
*/
var args []string
var cmd *exec.Cmd
// Pull down master from the remote server to be up to date
args = []string{"pull", "origin", "master"}
cmd = exec.Command(git_binary, args...)
cmd.Dir = "./repo"
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
panic(err)
}
// Checkout the tree
args = []string{"checkout", test.CommitHash}
cmd = exec.Command(git_binary, args...)
cmd.Dir = "./repo"
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
panic(err)
}
args = []string{"scripts/_test.py", "--host=127.0.0.1", "--port=6911", "--tag=" + test.CommitHash}
cmd = exec.Command(python_binary, args...)
cmd.Dir = "./repo"
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
panic(err)
}
// After we are down checkback master for the next run
args = []string{"checkout", "master"}
cmd = exec.Command(git_binary, args...)
cmd.Dir = "./repo"
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
panic(err)
}
}
}
}()
r.GET("/", func(c *gin.Context) {
c.String(200, "Hello World!")
})
r.POST("/", func(c *gin.Context) {
event_type := c.Request.Header.Get("X-Github-Event")
fmt.Println(event_type)
switch event_type {
case "push":
PostEventHandler(c)
default:
c.String(500, "Event not implemented")
}
})
r.Run(":1440")
}
func PostEventHandler(c *gin.Context) {
var event EventPush
if c.Bind(&event) {
var t *TestRun
t = new(TestRun)
t.CommitHash = event.After
TestQueue <- t
c.Abort(200)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment