Skip to content

Commit

Permalink
Use createRef mutation of v4 API (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
int128 authored Jan 23, 2021
1 parent 24bc3c5 commit 04d21a9
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 30 deletions.
32 changes: 25 additions & 7 deletions adaptors/github/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ type QueryForCommitOutput struct {
ParentDefaultBranchTreeSHA git.TreeSHA
ParentRefCommitSHA git.CommitSHA // empty if the parent ref does not exist
ParentRefTreeSHA git.TreeSHA // empty if the parent ref does not exist
TargetRepositoryNodeID InternalRepositoryNodeID
TargetBranchCommitSHA git.CommitSHA // empty if the branch does not exist
TargetBranchTreeSHA git.TreeSHA // empty if the branch does not exist
}
Expand Down Expand Up @@ -126,6 +127,7 @@ func (c *GitHub) QueryForCommit(ctx context.Context, in QueryForCommitInput) (*Q
} `graphql:"parentRepository: repository(owner: $parentOwner, name: $parentRepo)"`

TargetRepository struct {
ID githubv4.ID
Ref struct {
Target struct {
Commit struct {
Expand Down Expand Up @@ -157,23 +159,39 @@ func (c *GitHub) QueryForCommit(ctx context.Context, in QueryForCommitInput) (*Q
ParentDefaultBranchTreeSHA: git.TreeSHA(q.ParentRepository.DefaultBranchRef.Target.Commit.Tree.Oid),
ParentRefCommitSHA: git.CommitSHA(q.ParentRepository.ParentRef.Target.Commit.Oid),
ParentRefTreeSHA: git.TreeSHA(q.ParentRepository.ParentRef.Target.Commit.Tree.Oid),
TargetRepositoryNodeID: q.TargetRepository.ID,
TargetBranchCommitSHA: git.CommitSHA(q.TargetRepository.Ref.Target.Commit.Oid),
TargetBranchTreeSHA: git.TreeSHA(q.TargetRepository.Ref.Target.Commit.Tree.Oid),
}
c.Logger.Debugf("Returning the repository: %+v", out)
return &out, nil
}

type CreateBranchInput struct {
RepositoryNodeID InternalRepositoryNodeID
BranchName git.BranchName
CommitSHA git.CommitSHA
}

// CreateBranch creates a branch and returns nil or an error.
func (c *GitHub) CreateBranch(ctx context.Context, n git.NewBranch) error {
c.Logger.Debugf("Creating a branch %+v", n)
_, _, err := c.Client.CreateRef(ctx, n.Repository.Owner, n.Repository.Name, &github.Reference{
Ref: github.String(n.BranchName.QualifiedName().String()),
Object: &github.GitObject{SHA: github.String(string(n.CommitSHA))},
})
if err != nil {
func (c *GitHub) CreateBranch(ctx context.Context, in CreateBranchInput) error {
c.Logger.Debugf("Creating a branch %+v", in.BranchName)
v := githubv4.CreateRefInput{
RepositoryID: in.RepositoryNodeID,
Name: githubv4.String(in.BranchName.QualifiedName().String()),
Oid: githubv4.GitObjectID(in.CommitSHA),
}
var m struct {
CreateRef struct {
Ref struct {
Name string
}
} `graphql:"createRef(input: $input)"`
}
if err := c.Client.Mutate(ctx, &m, v, nil); err != nil {
return xerrors.Errorf("GitHub API error: %w", err)
}
c.Logger.Debugf("Got the result: %+v", m)
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion adaptors/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Interface interface {
CreateFork(ctx context.Context, id git.RepositoryID) (*git.RepositoryID, error)

QueryForCommit(ctx context.Context, in QueryForCommitInput) (*QueryForCommitOutput, error)
CreateBranch(ctx context.Context, branch git.NewBranch) error
CreateBranch(ctx context.Context, in CreateBranchInput) error
UpdateBranch(ctx context.Context, branch git.NewBranch, force bool) error
CreateCommit(ctx context.Context, commit git.NewCommit) (git.CommitSHA, error)

Expand Down
2 changes: 1 addition & 1 deletion adaptors/github/mock_github/mock_github.go

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

11 changes: 6 additions & 5 deletions usecases/commit/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,12 @@ func (u *Commit) createNewBranch(ctx context.Context, in Input, files []fs.File,
}

u.Logger.Debugf("Creating a branch (%s)", in.TargetBranchName)
if err := u.GitHub.CreateBranch(ctx, git.NewBranch{
Repository: in.TargetRepository,
BranchName: in.TargetBranchName,
CommitSHA: commit.CommitSHA,
}); err != nil {
createBranchIn := github.CreateBranchInput{
RepositoryNodeID: q.TargetRepositoryNodeID,
BranchName: in.TargetBranchName,
CommitSHA: commit.CommitSHA,
}
if err := u.GitHub.CreateBranch(ctx, createBranchIn); err != nil {
return xerrors.Errorf("error while creating %s branch: %w", in.TargetBranchName, err)
}
u.Logger.Infof("Created a branch (%s)", in.TargetBranchName)
Expand Down
38 changes: 22 additions & 16 deletions usecases/commit/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
var parentRepositoryID = git.RepositoryID{Owner: "upstream", Name: "repo"}
var targetRepositoryID = git.RepositoryID{Owner: "owner", Name: "repo"}

var targetRepositoryNodeID = github.InternalRepositoryNodeID("OwnerRepo")

var thePathFilter = gomock.AssignableToTypeOf(&pathFilter{})
var theFiles = []fs.File{
{Path: "file1"},
Expand Down Expand Up @@ -91,12 +93,13 @@ func TestCommitToBranch_Do(t *testing.T) {
CurrentUserName: "current",
ParentDefaultBranchCommitSHA: "masterCommitSHA",
ParentDefaultBranchTreeSHA: "masterTreeSHA",
TargetRepositoryNodeID: targetRepositoryNodeID,
}, nil)
gitHub.EXPECT().
CreateBranch(ctx, git.NewBranch{
Repository: targetRepositoryID,
BranchName: "topic",
CommitSHA: "commitSHA",
CreateBranch(ctx, github.CreateBranchInput{
RepositoryNodeID: targetRepositoryNodeID,
BranchName: "topic",
CommitSHA: "commitSHA",
}).
Return(nil).
Times(c.branchOperationTimes)
Expand Down Expand Up @@ -138,12 +141,13 @@ func TestCommitToBranch_Do(t *testing.T) {
CurrentUserName: "current",
ParentDefaultBranchCommitSHA: "masterCommitSHA",
ParentDefaultBranchTreeSHA: "masterTreeSHA",
TargetRepositoryNodeID: targetRepositoryNodeID,
}, nil)
gitHub.EXPECT().
CreateBranch(ctx, git.NewBranch{
Repository: targetRepositoryID,
BranchName: "topic",
CommitSHA: "commitSHA",
CreateBranch(ctx, github.CreateBranchInput{
RepositoryNodeID: targetRepositoryNodeID,
BranchName: "topic",
CommitSHA: "commitSHA",
}).
Return(nil).
Times(c.branchOperationTimes)
Expand Down Expand Up @@ -223,12 +227,13 @@ func TestCommitToBranch_Do(t *testing.T) {
CurrentUserName: "current",
ParentDefaultBranchCommitSHA: "masterCommitSHA",
ParentDefaultBranchTreeSHA: "masterTreeSHA",
TargetRepositoryNodeID: targetRepositoryNodeID,
}, nil)
gitHub.EXPECT().
CreateBranch(ctx, git.NewBranch{
Repository: targetRepositoryID,
BranchName: "topic",
CommitSHA: "commitSHA",
CreateBranch(ctx, github.CreateBranchInput{
RepositoryNodeID: targetRepositoryNodeID,
BranchName: "topic",
CommitSHA: "commitSHA",
}).
Return(nil).
Times(c.branchOperationTimes)
Expand Down Expand Up @@ -311,12 +316,13 @@ func TestCommitToBranch_Do(t *testing.T) {
ParentDefaultBranchTreeSHA: "masterTreeSHA",
ParentRefCommitSHA: "developCommitSHA",
ParentRefTreeSHA: "developTreeSHA",
TargetRepositoryNodeID: targetRepositoryNodeID,
}, nil)
gitHub.EXPECT().
CreateBranch(ctx, git.NewBranch{
Repository: targetRepositoryID,
BranchName: "topic",
CommitSHA: "commitSHA",
CreateBranch(ctx, github.CreateBranchInput{
RepositoryNodeID: targetRepositoryNodeID,
BranchName: "topic",
CommitSHA: "commitSHA",
}).
Return(nil).
Times(c.branchOperationTimes)
Expand Down

0 comments on commit 04d21a9

Please sign in to comment.