Skip to content

Commit

Permalink
Add support to sync fork branch with upstream repo (#2337)
Browse files Browse the repository at this point in the history
Fixes: #2335
  • Loading branch information
lkxed committed May 16, 2022
1 parent 78c231a commit 11817b6
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 0 deletions.
32 changes: 32 additions & 0 deletions github/github-accessors.go

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

40 changes: 40 additions & 0 deletions github/github-accessors_test.go

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

34 changes: 34 additions & 0 deletions github/repos_merging.go
Expand Up @@ -18,6 +18,20 @@ type RepositoryMergeRequest struct {
CommitMessage *string `json:"commit_message,omitempty"`
}

// RepoMergeUpstreamRequest represents a request to sync a branch of
// a forked repository to keep it up-to-date with the upstream repository.
type RepoMergeUpstreamRequest struct {
Branch *string `json:"branch,omitempty"`
}

// RepoMergeUpstreamResult represents the result of syncing a branch of
// a forked repository with the upstream repository.
type RepoMergeUpstreamResult struct {
Message *string `json:"message,omitempty"`
MergeType *string `json:"merge_type,omitempty"`
BaseBranch *string `json:"base_branch,omitempty"`
}

// Merge a branch in the specified repository.
//
// GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#merge-a-branch
Expand All @@ -36,3 +50,23 @@ func (s *RepositoriesService) Merge(ctx context.Context, owner, repo string, req

return commit, resp, nil
}

// MergeUpstream syncs a branch of a forked repository to keep it up-to-date
// with the upstream repository.
//
// GitHub API docs: https://docs.github.com/en/rest/reference/branches#sync-a-fork-branch-with-the-upstream-repository
func (s *RepositoriesService) MergeUpstream(ctx context.Context, owner, repo string, request *RepoMergeUpstreamRequest) (*RepoMergeUpstreamResult, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/merge-upstream", owner, repo)
req, err := s.client.NewRequest("POST", u, request)
if err != nil {
return nil, nil, err
}

result := new(RepoMergeUpstreamResult)
resp, err := s.client.Do(ctx, req, result)
if err != nil {
return nil, resp, err
}

return result, resp, nil
}
46 changes: 46 additions & 0 deletions github/repos_merging_test.go
Expand Up @@ -80,3 +80,49 @@ func TestRepositoryMergeRequest_Marshal(t *testing.T) {

testJSONMarshal(t, u, want)
}

func TestRepositoriesService_MergeUpstream(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

input := &RepoMergeUpstreamRequest{
Branch: String("b"),
}

mux.HandleFunc("/repos/o/r/merge-upstream", func(w http.ResponseWriter, r *http.Request) {
v := new(RepoMergeUpstreamRequest)
json.NewDecoder(r.Body).Decode(v)

testMethod(t, r, "POST")
if !cmp.Equal(v, input) {
t.Errorf("Request body = %+v, want %+v", v, input)
}

fmt.Fprint(w, `{"merge_type":"m"}`)
})

ctx := context.Background()
result, _, err := client.Repositories.MergeUpstream(ctx, "o", "r", input)
if err != nil {
t.Errorf("Repositories.MergeUpstream returned error: %v", err)
}

want := &RepoMergeUpstreamResult{MergeType: String("m")}
if !cmp.Equal(result, want) {
t.Errorf("Repositories.MergeUpstream returned %+v, want %+v", result, want)
}

const methodName = "MergeUpstream"
testBadOptions(t, methodName, func() (err error) {
_, _, err = client.Repositories.MergeUpstream(ctx, "\n", "\n", input)
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Repositories.MergeUpstream(ctx, "o", "r", input)
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
return resp, err
})
}

0 comments on commit 11817b6

Please sign in to comment.