Add support for GitHub Enterprise

pull/31/head
stefanprodan 5 years ago
parent 34ea89c3be
commit f5b738198f

@ -44,6 +44,9 @@ the bootstrap command will perform an upgrade if needed.`,
# Run bootstrap for a public repository on a personal account # Run bootstrap for a public repository on a personal account
tk bootstrap github --owner=<user> --repository=<repo name> --private=false --personal=true tk bootstrap github --owner=<user> --repository=<repo name> --private=false --personal=true
# Run bootstrap for a private repo hosted on GitHub Enterprise
tk bootstrap github --owner=<organization> --repository=<repo name> --hostname=<domain>
`, `,
RunE: bootstrapGitHubCmdRun, RunE: bootstrapGitHubCmdRun,
} }
@ -54,6 +57,7 @@ var (
ghInterval time.Duration ghInterval time.Duration
ghPersonal bool ghPersonal bool
ghPrivate bool ghPrivate bool
ghHostname string
) )
const ( const (
@ -62,6 +66,7 @@ const (
ghInstallManifest = "toolkit.yaml" ghInstallManifest = "toolkit.yaml"
ghSourceManifest = "toolkit-source.yaml" ghSourceManifest = "toolkit-source.yaml"
ghKustomizationManifest = "toolkit-kustomization.yaml" ghKustomizationManifest = "toolkit-kustomization.yaml"
ghDefaultHostname = "github.com"
) )
func init() { func init() {
@ -70,6 +75,7 @@ func init() {
bootstrapGitHubCmd.Flags().BoolVar(&ghPersonal, "personal", false, "is personal repository") bootstrapGitHubCmd.Flags().BoolVar(&ghPersonal, "personal", false, "is personal repository")
bootstrapGitHubCmd.Flags().BoolVar(&ghPrivate, "private", true, "is private repository") bootstrapGitHubCmd.Flags().BoolVar(&ghPrivate, "private", true, "is private repository")
bootstrapGitHubCmd.Flags().DurationVar(&ghInterval, "interval", time.Minute, "sync interval") bootstrapGitHubCmd.Flags().DurationVar(&ghInterval, "interval", time.Minute, "sync interval")
bootstrapGitHubCmd.Flags().StringVar(&ghHostname, "hostname", ghDefaultHostname, "GitHub hostname")
bootstrapCmd.AddCommand(bootstrapGitHubCmd) bootstrapCmd.AddCommand(bootstrapGitHubCmd)
} }
@ -79,7 +85,7 @@ func bootstrapGitHubCmdRun(cmd *cobra.Command, args []string) error {
return fmt.Errorf("%s environment variable not found", ghTokenName) return fmt.Errorf("%s environment variable not found", ghTokenName)
} }
ghURL := fmt.Sprintf("https://github.com/%s/%s", ghOwner, ghRepository) ghURL := fmt.Sprintf("https://%s/%s/%s", ghHostname, ghOwner, ghRepository)
if ghOwner == "" || ghRepository == "" { if ghOwner == "" || ghRepository == "" {
return fmt.Errorf("owner and repository are required") return fmt.Errorf("owner and repository are required")
} }
@ -99,8 +105,8 @@ func bootstrapGitHubCmdRun(cmd *cobra.Command, args []string) error {
defer cancel() defer cancel()
// create GitHub repository if doesn't exists // create GitHub repository if doesn't exists
logAction("connecting to GitHub") logAction("connecting to %s", ghHostname)
if err := createGitHubRepository(ctx, ghOwner, ghRepository, ghToken, ghPrivate, ghPersonal); err != nil { if err := createGitHubRepository(ctx, ghHostname, ghOwner, ghRepository, ghToken, ghPrivate, ghPersonal); err != nil {
return err return err
} }
@ -199,14 +205,22 @@ func bootstrapGitHubCmdRun(cmd *cobra.Command, args []string) error {
return nil return nil
} }
func createGitHubRepository(ctx context.Context, owner, name, token string, isPrivate, isPersonal bool) error { func createGitHubRepository(ctx context.Context, hostname, owner, name, token string, isPrivate, isPersonal bool) error {
autoInit := true autoInit := true
auth := github.BasicAuthTransport{ auth := github.BasicAuthTransport{
Username: "git", Username: "git",
Password: token, Password: token,
} }
gh := github.NewClient(auth.Client()) gh := github.NewClient(auth.Client())
if hostname != ghDefaultHostname {
baseURL := fmt.Sprintf("https://%s/api/v3/", hostname)
uploadURL := fmt.Sprintf("https://%s/api/uploads/", hostname)
if g, err := github.NewEnterpriseClient(baseURL, uploadURL, auth.Client()); err == nil {
gh = g
} else {
return fmt.Errorf("github client error: %w", err)
}
}
org := "" org := ""
if !isPersonal { if !isPersonal {
org = owner org = owner

Loading…
Cancel
Save