Skip to content

Commit 2936e40

Browse files
authored
feat: Support creating repo with custom properties (#3933)
1 parent 4767af9 commit 2936e40

2 files changed

Lines changed: 64 additions & 15 deletions

File tree

github/repos.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -501,21 +501,22 @@ type createRepoRequest struct {
501501
// Creating an organization repository. Required for non-owners.
502502
TeamID *int64 `json:"team_id,omitempty"`
503503

504-
AutoInit *bool `json:"auto_init,omitempty"`
505-
GitignoreTemplate *string `json:"gitignore_template,omitempty"`
506-
LicenseTemplate *string `json:"license_template,omitempty"`
507-
AllowSquashMerge *bool `json:"allow_squash_merge,omitempty"`
508-
AllowMergeCommit *bool `json:"allow_merge_commit,omitempty"`
509-
AllowRebaseMerge *bool `json:"allow_rebase_merge,omitempty"`
510-
AllowUpdateBranch *bool `json:"allow_update_branch,omitempty"`
511-
AllowAutoMerge *bool `json:"allow_auto_merge,omitempty"`
512-
AllowForking *bool `json:"allow_forking,omitempty"`
513-
DeleteBranchOnMerge *bool `json:"delete_branch_on_merge,omitempty"`
514-
UseSquashPRTitleAsDefault *bool `json:"use_squash_pr_title_as_default,omitempty"`
515-
SquashMergeCommitTitle *string `json:"squash_merge_commit_title,omitempty"`
516-
SquashMergeCommitMessage *string `json:"squash_merge_commit_message,omitempty"`
517-
MergeCommitTitle *string `json:"merge_commit_title,omitempty"`
518-
MergeCommitMessage *string `json:"merge_commit_message,omitempty"`
504+
AutoInit *bool `json:"auto_init,omitempty"`
505+
GitignoreTemplate *string `json:"gitignore_template,omitempty"`
506+
LicenseTemplate *string `json:"license_template,omitempty"`
507+
AllowSquashMerge *bool `json:"allow_squash_merge,omitempty"`
508+
AllowMergeCommit *bool `json:"allow_merge_commit,omitempty"`
509+
AllowRebaseMerge *bool `json:"allow_rebase_merge,omitempty"`
510+
AllowUpdateBranch *bool `json:"allow_update_branch,omitempty"`
511+
AllowAutoMerge *bool `json:"allow_auto_merge,omitempty"`
512+
AllowForking *bool `json:"allow_forking,omitempty"`
513+
DeleteBranchOnMerge *bool `json:"delete_branch_on_merge,omitempty"`
514+
UseSquashPRTitleAsDefault *bool `json:"use_squash_pr_title_as_default,omitempty"`
515+
SquashMergeCommitTitle *string `json:"squash_merge_commit_title,omitempty"`
516+
SquashMergeCommitMessage *string `json:"squash_merge_commit_message,omitempty"`
517+
MergeCommitTitle *string `json:"merge_commit_title,omitempty"`
518+
MergeCommitMessage *string `json:"merge_commit_message,omitempty"`
519+
CustomProperties map[string]any `json:"custom_properties,omitempty"`
519520
}
520521

521522
// Create a new repository. If an organization is specified, the new
@@ -575,6 +576,7 @@ func (s *RepositoriesService) Create(ctx context.Context, org string, repo *Repo
575576
SquashMergeCommitMessage: repo.SquashMergeCommitMessage,
576577
MergeCommitTitle: repo.MergeCommitTitle,
577578
MergeCommitMessage: repo.MergeCommitMessage,
579+
CustomProperties: repo.CustomProperties,
578580
}
579581

580582
req, err := s.client.NewRequest("POST", u, repoReq)

github/repos_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,53 @@ func TestRepositoriesService_Create_org(t *testing.T) {
308308
}
309309
}
310310

311+
func TestRepositoriesService_Create_withCustomProperties(t *testing.T) {
312+
t.Parallel()
313+
client, mux, _ := setup(t)
314+
315+
input := &Repository{
316+
Name: Ptr("n"),
317+
CustomProperties: map[string]any{
318+
"environment": "production",
319+
"team": "backend",
320+
"priority": 1,
321+
},
322+
}
323+
324+
wantAcceptHeaders := []string{mediaTypeRepositoryTemplatePreview, mediaTypeRepositoryVisibilityPreview}
325+
mux.HandleFunc("/orgs/o/repos", func(w http.ResponseWriter, r *http.Request) {
326+
v := new(createRepoRequest)
327+
assertNilError(t, json.NewDecoder(r.Body).Decode(v))
328+
329+
testMethod(t, r, "POST")
330+
testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", "))
331+
want := &createRepoRequest{
332+
Name: Ptr("n"),
333+
CustomProperties: map[string]any{
334+
"environment": "production",
335+
"team": "backend",
336+
"priority": float64(1), // JSON unmarshals numbers as float64
337+
},
338+
}
339+
if !cmp.Equal(v, want) {
340+
t.Errorf("Request body = %+v, want %+v", v, want)
341+
}
342+
343+
fmt.Fprint(w, `{"id":1}`)
344+
})
345+
346+
ctx := t.Context()
347+
repo, _, err := client.Repositories.Create(ctx, "o", input)
348+
if err != nil {
349+
t.Errorf("Repositories.Create returned error: %v", err)
350+
}
351+
352+
want := &Repository{ID: Ptr(int64(1))}
353+
if !cmp.Equal(repo, want) {
354+
t.Errorf("Repositories.Create returned %+v, want %+v", repo, want)
355+
}
356+
}
357+
311358
func TestRepositoriesService_CreateFromTemplate(t *testing.T) {
312359
t.Parallel()
313360
client, mux, _ := setup(t)

0 commit comments

Comments
 (0)