Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
6 changes: 1 addition & 5 deletions pkg/utils/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
Expand All @@ -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)
}
34 changes: 34 additions & 0 deletions pkg/utils/git_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
7 changes: 4 additions & 3 deletions pkg/webhook/githubhandler.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down
3 changes: 2 additions & 1 deletion pkg/webhook/gitlabhandler.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down
3 changes: 2 additions & 1 deletion pkg/webhook/webhook.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down