| 345 | } |
| 346 | |
| 347 | func createGithubRepository(ctx context.Context, ch *cmdutil.Helper, pollRes *adminv1.GetGithubUserStatusResponse, localGitPath, repoOwner string) (*github.Repository, error) { |
| 348 | githubClient := github.NewTokenClient(ctx, pollRes.AccessToken) |
| 349 | |
| 350 | defaultBranch := "main" |
| 351 | if repoOwner == pollRes.Account { |
| 352 | repoOwner = "" |
| 353 | } |
| 354 | repoName := filepath.Base(localGitPath) |
| 355 | private := true |
| 356 | |
| 357 | var githubRepo *github.Repository |
| 358 | var err error |
| 359 | for i := 1; i <= 10; i++ { |
| 360 | githubRepo, _, err = githubClient.Repositories.Create(ctx, repoOwner, &github.Repository{Name: &repoName, DefaultBranch: &defaultBranch, Private: &private}) |
| 361 | if err == nil { |
| 362 | break |
| 363 | } |
| 364 | if strings.Contains(err.Error(), "authentication") || strings.Contains(err.Error(), "credentials") { |
| 365 | // The users who installed app before we started including repo:write permissions need to accept permissions |
| 366 | // and then only we can create repositories. |
| 367 | return nil, fmt.Errorf("rill app does not have permissions to create github repository. Visit `https://github.com/settings/installations` to accept new permissions or reinstall app and try again") |
| 368 | } |
| 369 | |
| 370 | if !strings.Contains(err.Error(), "name already exists") { |
| 371 | return nil, fmt.Errorf("failed to create repository: %w", err) |
| 372 | } |
| 373 | |
| 374 | ch.Printf("Repository name %q is already taken\n", repoName) |
| 375 | repoName, err = cmdutil.InputPrompt("Please provide alternate name", "") |
| 376 | if err != nil { |
| 377 | return nil, err |
| 378 | } |
| 379 | } |
| 380 | if err != nil { |
| 381 | return nil, fmt.Errorf("failed to create repository: %w", err) |
| 382 | } |
| 383 | |
| 384 | // the create repo API does not wait for repo creation to be fully processed on server. Need to verify by making a get call in a loop |
| 385 | if repoOwner == "" { |
| 386 | repoOwner = pollRes.Account |
| 387 | } |
| 388 | |
| 389 | ch.Print("\nRequest submitted for creating repository. Checking completion status\n") |
| 390 | pollCtx, cancel := context.WithTimeout(ctx, 10*time.Minute) |
| 391 | defer cancel() |
| 392 | for { |
| 393 | select { |
| 394 | case <-pollCtx.Done(): |
| 395 | return nil, pollCtx.Err() |
| 396 | case <-time.After(2 * time.Second): |
| 397 | // Ready to check again. |
| 398 | } |
| 399 | _, _, err := githubClient.Repositories.Get(ctx, repoOwner, repoName) |
| 400 | if err == nil { |
| 401 | break |
| 402 | } |
| 403 | } |
| 404 | |