github.com/secman-team/gh-api@v1.8.2/pkg/cmd/repo/create/http.go (about)

     1  package create
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	"github.com/secman-team/gh-api/api"
     8  )
     9  
    10  // repoCreateInput represents input parameters for repoCreate
    11  type repoCreateInput struct {
    12  	Name        string `json:"name"`
    13  	Visibility  string `json:"visibility"`
    14  	HomepageURL string `json:"homepageUrl,omitempty"`
    15  	Description string `json:"description,omitempty"`
    16  
    17  	OwnerID string `json:"ownerId,omitempty"`
    18  	TeamID  string `json:"teamId,omitempty"`
    19  
    20  	HasIssuesEnabled bool `json:"hasIssuesEnabled"`
    21  	HasWikiEnabled   bool `json:"hasWikiEnabled"`
    22  }
    23  
    24  type repoTemplateInput struct {
    25  	Name       string `json:"name"`
    26  	Visibility string `json:"visibility"`
    27  	OwnerID    string `json:"ownerId,omitempty"`
    28  
    29  	RepositoryID string `json:"repositoryId,omitempty"`
    30  	Description  string `json:"description,omitempty"`
    31  }
    32  
    33  // repoCreate creates a new GitHub repository
    34  func repoCreate(client *http.Client, hostname string, input repoCreateInput, templateRepositoryID string) (*api.Repository, error) {
    35  	apiClient := api.NewClientFromHTTP(client)
    36  
    37  	if input.TeamID != "" {
    38  		orgID, teamID, err := resolveOrganizationTeam(apiClient, hostname, input.OwnerID, input.TeamID)
    39  		if err != nil {
    40  			return nil, err
    41  		}
    42  		input.TeamID = teamID
    43  		input.OwnerID = orgID
    44  	} else if input.OwnerID != "" {
    45  		orgID, err := resolveOrganization(apiClient, hostname, input.OwnerID)
    46  		if err != nil {
    47  			return nil, err
    48  		}
    49  		input.OwnerID = orgID
    50  	}
    51  
    52  	if templateRepositoryID != "" {
    53  		var response struct {
    54  			CloneTemplateRepository struct {
    55  				Repository api.Repository
    56  			}
    57  		}
    58  
    59  		if input.OwnerID == "" {
    60  			var err error
    61  			input.OwnerID, err = api.CurrentUserID(apiClient, hostname)
    62  			if err != nil {
    63  				return nil, err
    64  			}
    65  		}
    66  
    67  		templateInput := repoTemplateInput{
    68  			Name:         input.Name,
    69  			Visibility:   input.Visibility,
    70  			OwnerID:      input.OwnerID,
    71  			RepositoryID: templateRepositoryID,
    72  		}
    73  
    74  		variables := map[string]interface{}{
    75  			"input": templateInput,
    76  		}
    77  
    78  		err := apiClient.GraphQL(hostname, `
    79  		mutation CloneTemplateRepository($input: CloneTemplateRepositoryInput!) {
    80  			cloneTemplateRepository(input: $input) {
    81  				repository {
    82  					id
    83  					name
    84  					owner { login }
    85  					url
    86  				}
    87  			}
    88  		}
    89  		`, variables, &response)
    90  		if err != nil {
    91  			return nil, err
    92  		}
    93  
    94  		return api.InitRepoHostname(&response.CloneTemplateRepository.Repository, hostname), nil
    95  	}
    96  
    97  	var response struct {
    98  		CreateRepository struct {
    99  			Repository api.Repository
   100  		}
   101  	}
   102  
   103  	variables := map[string]interface{}{
   104  		"input": input,
   105  	}
   106  
   107  	err := apiClient.GraphQL(hostname, `
   108  	mutation RepositoryCreate($input: CreateRepositoryInput!) {
   109  		createRepository(input: $input) {
   110  			repository {
   111  				id
   112  				name
   113  				owner { login }
   114  				url
   115  			}
   116  		}
   117  	}
   118  	`, variables, &response)
   119  	if err != nil {
   120  		return nil, err
   121  	}
   122  
   123  	return api.InitRepoHostname(&response.CreateRepository.Repository, hostname), nil
   124  }
   125  
   126  // using API v3 here because the equivalent in GraphQL needs `read:org` scope
   127  func resolveOrganization(client *api.Client, hostname, orgName string) (string, error) {
   128  	var response struct {
   129  		NodeID string `json:"node_id"`
   130  	}
   131  	err := client.REST(hostname, "GET", fmt.Sprintf("users/%s", orgName), nil, &response)
   132  	return response.NodeID, err
   133  }
   134  
   135  // using API v3 here because the equivalent in GraphQL needs `read:org` scope
   136  func resolveOrganizationTeam(client *api.Client, hostname, orgName, teamSlug string) (string, string, error) {
   137  	var response struct {
   138  		NodeID       string `json:"node_id"`
   139  		Organization struct {
   140  			NodeID string `json:"node_id"`
   141  		}
   142  	}
   143  	err := client.REST(hostname, "GET", fmt.Sprintf("orgs/%s/teams/%s", orgName, teamSlug), nil, &response)
   144  	return response.Organization.NodeID, response.NodeID, err
   145  }