Skip to content

Commit

Permalink
Implement support for repository visibility parameter (#1471)
Browse files Browse the repository at this point in the history
Fixes #1470.
  • Loading branch information
patrickmarabeas authored Mar 25, 2020
1 parent 7726a3e commit 371b000
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 11 deletions.
8 changes: 8 additions & 0 deletions github/github-accessors.go

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

3 changes: 2 additions & 1 deletion github/github-stringify_test.go

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

3 changes: 3 additions & 0 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ const (

// https://developer.github.com/changes/2019-11-05-deprecated-passwords-and-authorizations-api/
mediaTypeOAuthAppPreview = "application/vnd.github.doctor-strange-preview+json"

// https://developer.github.com/changes/2019-12-03-internal-visibility-changes/
mediaTypeRepositoryVisibilityPreview = "application/vnd.github.nebula-preview+json"
)

// A Client manages communication with the GitHub API.
Expand Down
23 changes: 16 additions & 7 deletions github/repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ type Repository struct {
// TextMatches is only populated from search results that request text matches
// See: search.go and https://developer.github.com/v3/search/#text-match-metadata
TextMatches []*TextMatch `json:"text_matches,omitempty"`

// Visibility is only used for Create and Edit endpoints. The visibility field
// overrides the field parameter when both are used.
// Can be one of public, private or internal.
Visibility *string `json:"visibility,omitempty"`
}

func (r Repository) String() string {
Expand Down Expand Up @@ -295,11 +300,12 @@ type createRepoRequest struct {
Description *string `json:"description,omitempty"`
Homepage *string `json:"homepage,omitempty"`

Private *bool `json:"private,omitempty"`
HasIssues *bool `json:"has_issues,omitempty"`
HasProjects *bool `json:"has_projects,omitempty"`
HasWiki *bool `json:"has_wiki,omitempty"`
IsTemplate *bool `json:"is_template,omitempty"`
Private *bool `json:"private,omitempty"`
Visibility *string `json:"visibility,omitempty"`
HasIssues *bool `json:"has_issues,omitempty"`
HasProjects *bool `json:"has_projects,omitempty"`
HasWiki *bool `json:"has_wiki,omitempty"`
IsTemplate *bool `json:"is_template,omitempty"`

// Creating an organization repository. Required for non-owners.
TeamID *int64 `json:"team_id,omitempty"`
Expand Down Expand Up @@ -334,6 +340,7 @@ func (s *RepositoriesService) Create(ctx context.Context, org string, repo *Repo
Description: repo.Description,
Homepage: repo.Homepage,
Private: repo.Private,
Visibility: repo.Visibility,
HasIssues: repo.HasIssues,
HasProjects: repo.HasProjects,
HasWiki: repo.HasWiki,
Expand All @@ -353,7 +360,8 @@ func (s *RepositoriesService) Create(ctx context.Context, org string, repo *Repo
return nil, nil, err
}

req.Header.Set("Accept", mediaTypeRepositoryTemplatePreview)
acceptHeaders := []string{mediaTypeRepositoryTemplatePreview, mediaTypeRepositoryVisibilityPreview}
req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
r := new(Repository)
resp, err := s.client.Do(ctx, req, r)
if err != nil {
Expand Down Expand Up @@ -469,7 +477,8 @@ func (s *RepositoriesService) Edit(ctx context.Context, owner, repo string, repo
return nil, nil, err
}

req.Header.Set("Accept", mediaTypeRepositoryTemplatePreview)
acceptHeaders := []string{mediaTypeRepositoryTemplatePreview, mediaTypeRepositoryVisibilityPreview}
req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
r := new(Repository)
resp, err := s.client.Do(ctx, req, r)
if err != nil {
Expand Down
9 changes: 6 additions & 3 deletions github/repos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,13 @@ func TestRepositoriesService_Create_user(t *testing.T) {
Archived: Bool(true), // not passed along.
}

wantAcceptHeaders := []string{mediaTypeRepositoryTemplatePreview, mediaTypeRepositoryVisibilityPreview}
mux.HandleFunc("/user/repos", func(w http.ResponseWriter, r *http.Request) {
v := new(createRepoRequest)
json.NewDecoder(r.Body).Decode(v)

testMethod(t, r, "POST")
testHeader(t, r, "Accept", mediaTypeRepositoryTemplatePreview)
testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", "))
want := &createRepoRequest{Name: String("n")}
if !reflect.DeepEqual(v, want) {
t.Errorf("Request body = %+v, want %+v", v, want)
Expand Down Expand Up @@ -313,12 +314,13 @@ func TestRepositoriesService_Create_org(t *testing.T) {
Archived: Bool(true), // not passed along.
}

wantAcceptHeaders := []string{mediaTypeRepositoryTemplatePreview, mediaTypeRepositoryVisibilityPreview}
mux.HandleFunc("/orgs/o/repos", func(w http.ResponseWriter, r *http.Request) {
v := new(createRepoRequest)
json.NewDecoder(r.Body).Decode(v)

testMethod(t, r, "POST")
testHeader(t, r, "Accept", mediaTypeRepositoryTemplatePreview)
testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", "))
want := &createRepoRequest{Name: String("n")}
if !reflect.DeepEqual(v, want) {
t.Errorf("Request body = %+v, want %+v", v, want)
Expand Down Expand Up @@ -551,12 +553,13 @@ func TestRepositoriesService_Edit(t *testing.T) {
i := true
input := &Repository{HasIssues: &i}

wantAcceptHeaders := []string{mediaTypeRepositoryTemplatePreview, mediaTypeRepositoryVisibilityPreview}
mux.HandleFunc("/repos/o/r", func(w http.ResponseWriter, r *http.Request) {
v := new(Repository)
json.NewDecoder(r.Body).Decode(v)

testMethod(t, r, "PATCH")
testHeader(t, r, "Accept", mediaTypeRepositoryTemplatePreview)
testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", "))
if !reflect.DeepEqual(v, input) {
t.Errorf("Request body = %+v, want %+v", v, input)
}
Expand Down

0 comments on commit 371b000

Please sign in to comment.