diff --git a/adaptors/github/commit.go b/adaptors/github/commit.go index 3f32ad3a..31189246 100644 --- a/adaptors/github/commit.go +++ b/adaptors/github/commit.go @@ -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 } @@ -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 { @@ -157,6 +159,7 @@ 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), } @@ -164,16 +167,31 @@ func (c *GitHub) QueryForCommit(ctx context.Context, in QueryForCommitInput) (*Q 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 } diff --git a/adaptors/github/github.go b/adaptors/github/github.go index 35f4ab85..040484e5 100644 --- a/adaptors/github/github.go +++ b/adaptors/github/github.go @@ -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) diff --git a/adaptors/github/mock_github/mock_github.go b/adaptors/github/mock_github/mock_github.go index 6b5b0ad0..8e25bffd 100644 --- a/adaptors/github/mock_github/mock_github.go +++ b/adaptors/github/mock_github/mock_github.go @@ -51,7 +51,7 @@ func (mr *MockInterfaceMockRecorder) CreateBlob(arg0, arg1 interface{}) *gomock. } // CreateBranch mocks base method -func (m *MockInterface) CreateBranch(arg0 context.Context, arg1 git.NewBranch) error { +func (m *MockInterface) CreateBranch(arg0 context.Context, arg1 github.CreateBranchInput) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "CreateBranch", arg0, arg1) ret0, _ := ret[0].(error) diff --git a/usecases/commit/commit.go b/usecases/commit/commit.go index 1ed6a4b1..ff47b31e 100644 --- a/usecases/commit/commit.go +++ b/usecases/commit/commit.go @@ -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) diff --git a/usecases/commit/commit_test.go b/usecases/commit/commit_test.go index d30e97fc..876515cd 100644 --- a/usecases/commit/commit_test.go +++ b/usecases/commit/commit_test.go @@ -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"}, @@ -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) @@ -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) @@ -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) @@ -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)