linkProjectToRepo links a project to a repository
(ctx context.Context, projectId, repoSlug string, verbose bool)
| 344 | |
| 345 | // linkProjectToRepo links a project to a repository |
| 346 | func linkProjectToRepo(ctx context.Context, projectId, repoSlug string, verbose bool) error { |
| 347 | projectLog.Printf("Linking project %s to repository %s", projectId, repoSlug) |
| 348 | console.LogVerbose(verbose, "Linking project to repository: "+repoSlug) |
| 349 | |
| 350 | // Parse repo slug |
| 351 | parts := strings.Split(repoSlug, "/") |
| 352 | if len(parts) != 2 { |
| 353 | return fmt.Errorf("invalid repository format. Expected 'owner/repo', got '%s'", repoSlug) |
| 354 | } |
| 355 | repoOwner := parts[0] |
| 356 | repoName := parts[1] |
| 357 | |
| 358 | // Get repository ID |
| 359 | repoIdQuery := `query($owner: String!, $name: String!) { repository(owner: $owner, name: $name) { id } }` |
| 360 | output, err := workflow.RunGH("Getting repository ID...", "api", "graphql", |
| 361 | "-f", "query="+repoIdQuery, |
| 362 | "-f", "owner="+repoOwner, |
| 363 | "-f", "name="+repoName, |
| 364 | "--jq", ".data.repository.id", |
| 365 | ) |
| 366 | if err != nil { |
| 367 | return fmt.Errorf("repository '%s' not found: %w", repoSlug, err) |
| 368 | } |
| 369 | |
| 370 | repoId := strings.TrimSpace(string(output)) |
| 371 | if repoId == "" { |
| 372 | return errors.New("failed to get repository ID") |
| 373 | } |
| 374 | |
| 375 | // Link project to repository |
| 376 | mutation := `mutation($projectId: ID!, $repositoryId: ID!) { |
| 377 | linkProjectV2ToRepository(input: { projectId: $projectId, repositoryId: $repositoryId }) { |
| 378 | repository { |
| 379 | id |
| 380 | } |
| 381 | } |
| 382 | }` |
| 383 | |
| 384 | _, err = workflow.RunGH("Linking project to repository...", "api", "graphql", |
| 385 | "-f", "query="+mutation, |
| 386 | "-f", "projectId="+projectId, |
| 387 | "-f", "repositoryId="+repoId, |
| 388 | ) |
| 389 | if err != nil { |
| 390 | return fmt.Errorf("failed to link project to repository: %w", err) |
| 391 | } |
| 392 | |
| 393 | console.LogVerbose(verbose, "✓ Linked project to repository "+repoSlug) |
| 394 | return nil |
| 395 | } |
| 396 | |
| 397 | // escapeGraphQLString escapes special characters in GraphQL strings |
| 398 | func escapeGraphQLString(s string) string { |
no test coverage detected