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

Commit

Permalink
Merge 6624847 into 8aa75de
Browse files Browse the repository at this point in the history
  • Loading branch information
duck8823 committed Sep 16, 2018
2 parents 8aa75de + 6624847 commit d02fa61
Show file tree
Hide file tree
Showing 15 changed files with 447 additions and 232 deletions.
18 changes: 12 additions & 6 deletions application/service/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ import (
"gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
)

type TargetSource struct {
URL string
Ref string
SHA plumbing.Hash
}

type Service interface {
Clone(ctx context.Context, dir string, sshURL string, ref string, sha plumbing.Hash) error
Clone(ctx context.Context, dir string, src TargetSource) error
}

type sshGitService struct {
Expand All @@ -25,12 +31,12 @@ func New(sshKeyPath string) (Service, error) {
return &sshGitService{auth: auth}, nil
}

func (s *sshGitService) Clone(ctx context.Context, dir string, sshURL string, ref string, sha plumbing.Hash) error {
func (s *sshGitService) Clone(ctx context.Context, dir string, src TargetSource) error {
gitRepository, err := git.PlainClone(dir, false, &git.CloneOptions{
URL: sshURL,
URL: src.URL,
Auth: s.auth,
Progress: &ProgressLogger{ctx.UUID()},
ReferenceName: plumbing.ReferenceName(ref),
ReferenceName: plumbing.ReferenceName(src.Ref),
Depth: 1,
})
if err != nil {
Expand All @@ -43,8 +49,8 @@ func (s *sshGitService) Clone(ctx context.Context, dir string, sshURL string, re
}

if err := wt.Checkout(&git.CheckoutOptions{
Hash: sha,
Branch: plumbing.ReferenceName(sha.String()),
Hash: src.SHA,
Branch: plumbing.ReferenceName(src.SHA.String()),
Create: true,
}); err != nil {
return errors.WithStack(err)
Expand Down
16 changes: 10 additions & 6 deletions application/service/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ func TestSshGitService_Clone(t *testing.T) {
err := client.Clone(
context.New("test/task", uuid.New(), &url.URL{}),
tempDir,
"git@github.com:duck8823/duci.git",
"refs/heads/master",
plumbing.ZeroHash,
git.TargetSource{
URL: "git@github.com:duck8823/duci.git",
Ref: "refs/heads/master",
SHA: plumbing.ZeroHash,
},
)

// then
Expand All @@ -68,9 +70,11 @@ func TestSshGitService_Clone(t *testing.T) {
err := client.Clone(
context.New("test/task", uuid.New(), &url.URL{}),
wrongPath,
"git@github.com:duck8823/duci.git",
"refs/heads/master",
plumbing.ZeroHash,
git.TargetSource{
URL: "git@github.com:duck8823/duci.git",
Ref: "refs/heads/master",
SHA: plumbing.ZeroHash,
},
)

// then
Expand Down
10 changes: 5 additions & 5 deletions application/service/git/mock_git/git.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions application/service/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/google/go-github/github"
"github.com/pkg/errors"
"golang.org/x/oauth2"
"gopkg.in/src-d/go-git.v4/plumbing"
"path"
)

Expand All @@ -23,7 +22,7 @@ const (

type Service interface {
GetPullRequest(ctx context.Context, repository Repository, num int) (*PullRequest, error)
CreateCommitStatus(ctx context.Context, repo Repository, hash plumbing.Hash, state State, description string) error
CreateCommitStatus(ctx context.Context, src TargetSource, state State, description string) error
}

type serviceImpl struct {
Expand Down Expand Up @@ -62,8 +61,8 @@ func (s *serviceImpl) GetPullRequest(ctx context.Context, repository Repository,
return pr, nil
}

func (s *serviceImpl) CreateCommitStatus(ctx context.Context, repository Repository, hash plumbing.Hash, state State, description string) error {
name := &RepositoryName{repository.GetFullName()}
func (s *serviceImpl) CreateCommitStatus(ctx context.Context, src TargetSource, state State, description string) error {
name := &RepositoryName{src.Repo.GetFullName()}
owner, err := name.Owner()
if err != nil {
return errors.WithStack(err)
Expand Down Expand Up @@ -91,7 +90,7 @@ func (s *serviceImpl) CreateCommitStatus(ctx context.Context, repository Reposit
ctx,
owner,
repo,
hash.String(),
src.SHA.String(),
status,
); err != nil {
logger.Errorf(ctx.UUID(), "Failed to create commit status: %+v", err)
Expand Down
24 changes: 19 additions & 5 deletions application/service/github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,12 @@ func TestService_CreateCommitStatus(t *testing.T) {
Reply(200)

// expect
if err := s.CreateCommitStatus(context.New("test/task", uuid.New(), &url.URL{}), repo, plumbing.Hash{}, github.SUCCESS, ""); err != nil {
if err := s.CreateCommitStatus(
context.New("test/task", uuid.New(), &url.URL{}),
github.TargetSource{Repo: repo, SHA: plumbing.Hash{}},
github.SUCCESS,
"",
); err != nil {
t.Errorf("error must not occurred: but got %+v", err)
}

Expand All @@ -147,7 +152,12 @@ func TestService_CreateCommitStatus(t *testing.T) {
Reply(404)

// expect
if err := s.CreateCommitStatus(context.New("test/task", uuid.New(), &url.URL{}), repo, plumbing.Hash{}, github.SUCCESS, ""); err == nil {
if err := s.CreateCommitStatus(
context.New("test/task", uuid.New(), &url.URL{}),
github.TargetSource{Repo: repo, SHA: plumbing.Hash{}},
github.SUCCESS,
"",
); err == nil {
t.Error("errot must occred. but got nil")
}

Expand All @@ -162,7 +172,12 @@ func TestService_CreateCommitStatus(t *testing.T) {
}

// expect
if err := s.CreateCommitStatus(context.New("test/task", uuid.New(), &url.URL{}), repo, plumbing.Hash{}, github.SUCCESS, ""); err == nil {
if err := s.CreateCommitStatus(
context.New("test/task", uuid.New(), &url.URL{}),
github.TargetSource{Repo: repo, SHA: plumbing.Hash{}},
github.SUCCESS,
"",
); err == nil {
t.Error("errot must occred. but got nil")
}
})
Expand Down Expand Up @@ -195,8 +210,7 @@ func TestService_CreateCommitStatus(t *testing.T) {
// expect
if err := s.CreateCommitStatus(
context.New(taskName, requestID, &url.URL{Scheme: "http", Host: "host:8080"}),
repo,
plumbing.Hash{},
github.TargetSource{Repo: repo, SHA: plumbing.Hash{}},
state,
description,
); err == nil {
Expand Down
11 changes: 5 additions & 6 deletions application/service/github/mock_github/github.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions application/service/github/target_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package github

import (
"github.com/duck8823/duci/application/service/git"
"gopkg.in/src-d/go-git.v4/plumbing"
)

type TargetSource struct {
Repo Repository
Ref string
SHA plumbing.Hash
}

func (s TargetSource) ToGitTargetSource() git.TargetSource {
return git.TargetSource{
URL: s.Repo.GetSSHURL(),
Ref: s.Ref,
SHA: s.SHA,
}
}
44 changes: 44 additions & 0 deletions application/service/github/target_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package github_test

import (
"github.com/duck8823/duci/application/service/git"
"github.com/duck8823/duci/application/service/github"
"github.com/google/go-cmp/cmp"
"gopkg.in/src-d/go-git.v4/plumbing"
"reflect"
"testing"
)

func TestTargetSource_ToGitTargetSource(t *testing.T) {
// given
expected := git.TargetSource{
URL: "url",
Ref: "ref",
SHA: plumbing.Hash{},
}

// and
src := github.TargetSource{
Repo: &MockRepository{FullName: "fullName", SSHURL: expected.URL},
Ref: expected.Ref,
SHA: expected.SHA,
}

// expect
if !reflect.DeepEqual(expected, src.ToGitTargetSource()) {
t.Errorf("must be equals, but not. diff: %+v", cmp.Diff(expected, src.ToGitTargetSource()))
}
}

type MockRepository struct {
FullName string
SSHURL string
}

func (r *MockRepository) GetFullName() string {
return r.FullName
}

func (r *MockRepository) GetSSHURL() string {
return r.SSHURL
}
9 changes: 4 additions & 5 deletions application/service/runner/mock_runner/runner.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d02fa61

Please sign in to comment.