@@ -6,15 +6,19 @@ import (
66 "fmt"
77 "sort"
88 "strings"
9+ "time"
910
1011 "github.com/cli/cli/internal/ghrepo"
1112)
1213
1314// Repository contains information about a GitHub repo
1415type Repository struct {
15- ID string
16- Name string
17- Owner RepositoryOwner
16+ ID string
17+ Name string
18+ URL string
19+ CloneURL string
20+ CreatedAt time.Time
21+ Owner RepositoryOwner
1822
1923 IsPrivate bool
2024 HasIssuesEnabled bool
@@ -59,7 +63,6 @@ func (r Repository) ViewerCanPush() bool {
5963 }
6064}
6165
62- // GitHubRepo looks up the node ID of a named repository
6366func GitHubRepo (client * Client , repo ghrepo.Interface ) (* Repository , error ) {
6467 query := `
6568 query($owner: String!, $name: String!) {
@@ -78,12 +81,8 @@ func GitHubRepo(client *Client, repo ghrepo.Interface) (*Repository, error) {
7881 }{}
7982 err := client .GraphQL (query , variables , & result )
8083
81- if err != nil || result .Repository .ID == "" {
82- newErr := fmt .Errorf ("failed to determine repository ID for '%s'" , ghrepo .FullName (repo ))
83- if err != nil {
84- newErr = fmt .Errorf ("%s: %w" , newErr , err )
85- }
86- return nil , newErr
84+ if err != nil {
85+ return nil , err
8786 }
8887
8988 return & result .Repository , nil
@@ -190,9 +189,11 @@ func RepoNetwork(client *Client, repos []ghrepo.Interface) (RepoNetworkResult, e
190189
191190// repositoryV3 is the repository result from GitHub API v3
192191type repositoryV3 struct {
193- NodeID string
194- Name string
195- Owner struct {
192+ NodeID string
193+ Name string
194+ CreatedAt time.Time `json:"created_at"`
195+ CloneURL string `json:"clone_url"`
196+ Owner struct {
196197 Login string
197198 }
198199}
@@ -208,11 +209,73 @@ func ForkRepo(client *Client, repo ghrepo.Interface) (*Repository, error) {
208209 }
209210
210211 return & Repository {
211- ID : result .NodeID ,
212- Name : result .Name ,
212+ ID : result .NodeID ,
213+ Name : result .Name ,
214+ CloneURL : result .CloneURL ,
215+ CreatedAt : result .CreatedAt ,
213216 Owner : RepositoryOwner {
214217 Login : result .Owner .Login ,
215218 },
216219 ViewerPermission : "WRITE" ,
217220 }, nil
218221}
222+
223+ // RepoCreateInput represents input parameters for RepoCreate
224+ type RepoCreateInput struct {
225+ Name string `json:"name"`
226+ Visibility string `json:"visibility"`
227+ Homepage string `json:"homepage,omitempty"`
228+ Description string `json:"description,omitempty"`
229+
230+ OwnerID string `json:"ownerId,omitempty"`
231+ TeamID string `json:"teamId,omitempty"`
232+
233+ HasIssuesEnabled bool `json:"hasIssuesEnabled"`
234+ HasWikiEnabled bool `json:"hasWikiEnabled"`
235+ }
236+
237+ // RepoCreate creates a new GitHub repository
238+ func RepoCreate (client * Client , input RepoCreateInput ) (* Repository , error ) {
239+ var response struct {
240+ CreateRepository struct {
241+ Repository Repository
242+ }
243+ }
244+
245+ if input .TeamID != "" {
246+ orgID , teamID , err := resolveOrganizationTeam (client , input .OwnerID , input .TeamID )
247+ if err != nil {
248+ return nil , err
249+ }
250+ input .TeamID = teamID
251+ input .OwnerID = orgID
252+ } else if input .OwnerID != "" {
253+ orgID , err := resolveOrganization (client , input .OwnerID )
254+ if err != nil {
255+ return nil , err
256+ }
257+ input .OwnerID = orgID
258+ }
259+
260+ variables := map [string ]interface {}{
261+ "input" : input ,
262+ }
263+
264+ err := client .GraphQL (`
265+ mutation($input: CreateRepositoryInput!) {
266+ createRepository(input: $input) {
267+ repository {
268+ id
269+ name
270+ owner { login }
271+ url
272+ }
273+ }
274+ }
275+ ` , variables , & response )
276+ if err != nil {
277+ return nil , err
278+ }
279+
280+ return & response .CreateRepository .Repository , nil
281+ }
0 commit comments