Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #76 from duck8823/feature/wait_commit_status
Browse files Browse the repository at this point in the history
Create commit status when job is queued
  • Loading branch information
duck8823 authored Aug 26, 2018
2 parents 8ed69c9 + 1939553 commit ecca3e9
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 19 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Makefile
build:
vgo build
go build

test:
vgo test -coverprofile cover.out $$(vgo list ./... | grep -v mock_)
vgo tool cover -html cover.out -o cover.html
go test -coverprofile cover.out $$(go list ./... | grep -v mock_)
go tool cover -html cover.out -o cover.html
open cover.html

docker-test:
Expand Down
27 changes: 18 additions & 9 deletions presentation/controller/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
go_github "github.com/google/go-github/github"
"github.com/google/uuid"
"github.com/pkg/errors"
"gopkg.in/src-d/go-git.v4/plumbing"
"net/http"
"net/url"
"regexp"
Expand Down Expand Up @@ -53,7 +54,7 @@ func (c *JobController) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

ctx, repo, ref, command, err := c.parseIssueComment(event, requestId, runtimeUrl)
ctx, repo, head, command, err := c.parseIssueComment(event, requestId, runtimeUrl)
if err == SkipBuild {
logger.Info(requestId, "skip build")
w.WriteHeader(http.StatusOK)
Expand All @@ -64,16 +65,23 @@ func (c *JobController) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
go c.Runner.Run(ctx, repo, ref, command...)

c.GitHub.CreateCommitStatus(ctx, repo, plumbing.NewHash(head.GetSHA()), github.PENDING, "waiting...")
go c.Runner.Run(ctx, repo, head.GetRef(), command...)
case "push":
event := &go_github.PushEvent{}
if err := json.NewDecoder(r.Body).Decode(event); err != nil {
logger.Errorf(requestId, "%+v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

taskName := fmt.Sprintf("%s/push", application.Name)
go c.Runner.Run(context.New(taskName, requestId, runtimeUrl), event.GetRepo(), event.GetRef())
ctx := context.New(taskName, requestId, runtimeUrl)

sha := event.GetHeadCommit().GetSHA()
c.GitHub.CreateCommitStatus(ctx, event.GetRepo(), plumbing.NewHash(sha), github.PENDING, "waiting...")
go c.Runner.Run(ctx, event.GetRepo(), event.GetRef())
default:
message := fmt.Sprintf("payload event type must be issue_comment or push. but %s", githubEvent)
logger.Error(requestId, message)
Expand All @@ -89,26 +97,27 @@ func (c *JobController) parseIssueComment(
event *go_github.IssueCommentEvent,
requestId uuid.UUID,
url *url.URL,
) (ctx context.Context, repo *go_github.Repository, ref string, command []string, err error) {
) (ctx context.Context, repo *go_github.Repository, head *go_github.PullRequestBranch, command []string, err error) {

if !isValidAction(event.Action) {
return nil, nil, "", nil, SkipBuild
return nil, nil, nil, nil, SkipBuild
}

if !regexp.MustCompile("^ci\\s+[^\\s]+").Match([]byte(event.Comment.GetBody())) {
return nil, nil, "", nil, SkipBuild
return nil, nil, nil, nil, SkipBuild
}
phrase := regexp.MustCompile("^ci\\s+").ReplaceAllString(event.Comment.GetBody(), "")
command = strings.Split(phrase, " ")
ctx = context.New(fmt.Sprintf("%s/pr/%s", application.Name, command[0]), requestId, url)

pr, err := c.GitHub.GetPullRequest(ctx, event.GetRepo(), event.GetIssue().GetNumber())
if err != nil {
return nil, nil, "", nil, errors.WithStack(err)
return nil, nil, nil, nil, errors.WithStack(err)
}

repo = event.GetRepo()
ref = fmt.Sprintf("refs/heads/%s", pr.GetHead().GetRef())
return ctx, repo, ref, command, err
head = pr.GetHead()
return ctx, repo, head, command, err
}

func isValidAction(action *string) bool {
Expand Down
52 changes: 45 additions & 7 deletions presentation/controller/webhooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,13 @@ func TestJobController_ServeHTTP(t *testing.T) {
githubService.EXPECT().GetPullRequest(gomock.Any(), gomock.Any(), gomock.Any()).
AnyTimes().
Return(&github.PullRequest{
Head: &github.PullRequestBranch{},
Head: &github.PullRequestBranch{
SHA: new(string),
},
}, nil)
githubService.EXPECT().CreateCommitStatus(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
AnyTimes().
Return(nil)

// and
handler := &controller.JobController{Runner: runner, GitHub: githubService}
Expand Down Expand Up @@ -104,7 +109,11 @@ func TestJobController_ServeHTTP(t *testing.T) {

githubService := mock_github.NewMockService(ctrl)
githubService.EXPECT().GetPullRequest(gomock.Any(), gomock.Any(), gomock.Any()).
AnyTimes().
Return(nil, errors.New("error occur"))
githubService.EXPECT().CreateCommitStatus(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
AnyTimes().
Return(nil)

// and
handler := &controller.JobController{Runner: runner, GitHub: githubService}
Expand Down Expand Up @@ -136,13 +145,26 @@ func TestJobController_ServeHTTP(t *testing.T) {
runner.EXPECT().Run(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Times(1)

// and
handler := &controller.JobController{Runner: runner}
githubService := mock_github.NewMockService(ctrl)
githubService.EXPECT().GetPullRequest(gomock.Any(), gomock.Any(), gomock.Any()).
AnyTimes().
Return(&github.PullRequest{
Head: &github.PullRequestBranch{
SHA: new(string),
},
}, nil)
githubService.EXPECT().CreateCommitStatus(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
AnyTimes().
Return(nil)

// and
handler := &controller.JobController{Runner: runner, GitHub: githubService}

s := httptest.NewServer(handler)
defer s.Close()

// and
payload := createPushPayload(t, "test/repo", "master")
payload := createPushPayload(t, "test/repo", "master", "")

req := httptest.NewRequest("POST", "/", payload)
req.Header.Set("X-GitHub-Delivery", requestId.String())
Expand All @@ -164,7 +186,21 @@ func TestJobController_ServeHTTP(t *testing.T) {
runner := mock_runner.NewMockRunner(ctrl)
runner.EXPECT().Run(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Times(0)

handler := &controller.JobController{Runner: runner}
// and
githubService := mock_github.NewMockService(ctrl)
githubService.EXPECT().GetPullRequest(gomock.Any(), gomock.Any(), gomock.Any()).
AnyTimes().
Return(&github.PullRequest{
Head: &github.PullRequestBranch{
SHA: new(string),
},
}, nil)
githubService.EXPECT().CreateCommitStatus(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
AnyTimes().
Return(nil)

// and
handler := &controller.JobController{Runner: runner, GitHub: githubService}

s := httptest.NewServer(handler)
defer s.Close()
Expand Down Expand Up @@ -287,12 +323,11 @@ func TestJobController_ServeHTTP(t *testing.T) {
func createIssueCommentPayload(t *testing.T, action, comment string) io.Reader {
t.Helper()

number := 1
event := &github.IssueCommentEvent{
Repo: &github.Repository{},
Action: &action,
Issue: &github.Issue{
Number: &number,
Number: new(int),
},
Comment: &github.IssueComment{
Body: &comment,
Expand All @@ -305,14 +340,17 @@ func createIssueCommentPayload(t *testing.T, action, comment string) io.Reader {
return bytes.NewReader(payload)
}

func createPushPayload(t *testing.T, repoName, ref string) io.Reader {
func createPushPayload(t *testing.T, repoName, ref string, sha string) io.Reader {
t.Helper()

event := github.PushEvent{
Repo: &github.PushEventRepository{
FullName: &repoName,
},
Ref: &ref,
HeadCommit: &github.PushEventCommit{
SHA: &sha,
},
}
payload, err := json.Marshal(event)
if err != nil {
Expand Down

0 comments on commit ecca3e9

Please sign in to comment.