diff --git a/.travis.yml b/.travis.yml index 8589272..0138967 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,10 +3,6 @@ go: - 1.8.3 install: true before_install: - - "cd" - - "mv $HOME/gopath/src/github.com/kairen/github-bot $HOME/gopath/src/github-bot" - - "export TRAVIS_BUILD_DIR=$HOME/gopath/src/github-bot" - - "cd $HOME/gopath/src/github-bot" - go get -u github.com/golang/dep/cmd/dep - go get github.com/mattn/goveralls - go get -u github.com/alecthomas/gometalinter diff --git a/Dockerfile b/Dockerfile index 7b7c69d..46eb282 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,15 +3,15 @@ FROM golang:1.8-alpine AS build-env ENV GOPATH "/go" -ADD . /go/src/github-bot -RUN cd /go/src/github-bot && \ +ADD . /go/src/github.com/kairen/github-bot +RUN cd /go/src/github.com/kairen/github-bot && \ go build -o github-bot # Run stage FROM alpine MAINTAINER Kyle Bai(kyle.b@inwinstack.com) -COPY --from=build-env /go/src/github-bot/github-bot /bin/github-bot +COPY --from=build-env /go/src/github.com/kairen/github-bot/github-bot /bin/github-bot COPY ./etc/github-bot /etc/github-bot RUN apk add --no-cache git openssh && \ mkdir /var/lib/github-bot diff --git a/main.go b/main.go index d5da8e0..543fd85 100644 --- a/main.go +++ b/main.go @@ -4,9 +4,10 @@ import ( "errors" "flag" "fmt" - "github-bot/pkg/config" - "github-bot/pkg/webhook" "os" + + "github.com/kairen/github-bot/pkg/config" + "github.com/kairen/github-bot/pkg/webhook" ) var ( diff --git a/pkg/utils/git.go b/pkg/utils/git.go index 72b7bcc..c5883ac 100644 --- a/pkg/utils/git.go +++ b/pkg/utils/git.go @@ -24,6 +24,7 @@ func git(path, argstr string) { argstr = fmt.Sprintf("-C %s ", path) + argstr } args := splitArgs(argstr) + log.Print(args) _, execErr := exec.Command(head, args...).Output() logIfError(execErr) } @@ -35,7 +36,6 @@ func splitArgs(argstr string) []string { // GitClone clone project from url func GitClone(path, url string) { - log.Println("Git remote clone") _, err := os.Stat(path) if os.IsNotExist(err) { argstr := fmt.Sprintf("clone %s %s", url, path) @@ -45,25 +45,21 @@ func GitClone(path, url string) { // GitAddRemote add remote into project func GitAddRemote(path, remote, url string) { - log.Println("Git remote add") argstr := fmt.Sprintf("remote add %s %s", remote, url) git(path, argstr) } // GitFetch fetch remote changes func GitFetch(path, remote string, pid int64) { - log.Println("Git fetch") argstr := fmt.Sprintf("fetch %s refs/pull/%d/head:pr-%d", remote, pid, pid) git(path, argstr) } // GitPushAndDelete push and delete branch func GitPushAndDelete(path, remote string, pid int64) { - log.Println("Git push") argstr := fmt.Sprintf("push %s pr-%d", remote, pid) git(path, argstr) - log.Println("Git branch delete") argstr = fmt.Sprintf("branch -D pr-%d", pid) git(path, argstr) } diff --git a/pkg/utils/git_test.go b/pkg/utils/git_test.go new file mode 100644 index 0000000..ab6d2f2 --- /dev/null +++ b/pkg/utils/git_test.go @@ -0,0 +1,34 @@ +package utils + +import ( + "fmt" + "log" + "os" + "os/exec" + "strings" + "testing" +) + +const ( + head = "git" + path = "/tmp/github-bot" + remote = "kairen" + repos = "https://github.com/kairen/github-bot.git" +) + +func TestGitClone(t *testing.T) { + GitClone(path, repos) + _, err := os.Stat(path + "github-bot") + if !os.IsNotExist(err) { + log.Fatal("Git clone failed.") + } +} + +func TestGitAddRemote(t *testing.T) { + GitAddRemote(path, remote, repos) + args := strings.Split(fmt.Sprintf("-C %s remote show", path), " ") + out, _ := exec.Command(head, args...).Output() + if !strings.Contains(string(out), remote) { + log.Fatalf("Git add remote failed: %s", string(out)) + } +} diff --git a/pkg/webhook/githubhandler.go b/pkg/webhook/githubhandler.go index 0f6d5d4..acde42d 100644 --- a/pkg/webhook/githubhandler.go +++ b/pkg/webhook/githubhandler.go @@ -1,12 +1,13 @@ package webhook import ( - "github-bot/pkg/api" - "github-bot/pkg/config" - "github-bot/pkg/utils" "log" "strings" + "github.com/kairen/github-bot/pkg/api" + "github.com/kairen/github-bot/pkg/config" + "github.com/kairen/github-bot/pkg/utils" + webhooks "gopkg.in/go-playground/webhooks.v3" "gopkg.in/go-playground/webhooks.v3/github" ) diff --git a/pkg/webhook/gitlabhandler.go b/pkg/webhook/gitlabhandler.go index 902730b..1924ce8 100644 --- a/pkg/webhook/gitlabhandler.go +++ b/pkg/webhook/gitlabhandler.go @@ -1,9 +1,10 @@ package webhook import ( - "github-bot/pkg/api" "strconv" + "github.com/kairen/github-bot/pkg/api" + webhooks "gopkg.in/go-playground/webhooks.v3" "gopkg.in/go-playground/webhooks.v3/gitlab" ) diff --git a/pkg/webhook/webhook.go b/pkg/webhook/webhook.go index 20f573d..0827a31 100644 --- a/pkg/webhook/webhook.go +++ b/pkg/webhook/webhook.go @@ -1,10 +1,11 @@ package webhook import ( - "github-bot/pkg/api" "log" "strconv" + "github.com/kairen/github-bot/pkg/api" + webhooks "gopkg.in/go-playground/webhooks.v3" "gopkg.in/go-playground/webhooks.v3/github" "gopkg.in/go-playground/webhooks.v3/gitlab"