Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to sync a fork branch with the upstream repository. #2337

Merged
merged 4 commits into from May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
})
}