Skip to content

Commit bed45e7

Browse files
committed
proper values
1 parent 790eb8b commit bed45e7

File tree

1 file changed

+44
-35
lines changed

1 file changed

+44
-35
lines changed

cmd/ctrlc/root/sync/github/pullrequest.go

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ import (
1919

2020
// NewSyncPullRequestCmd creates a new cobra command for syncing GitHub pull requests
2121
func NewSyncPullRequestsCmd() *cobra.Command {
22-
var owner string
23-
var repo string
22+
var repoPath string
2423
var token string
2524
var name string
2625
var states []string
@@ -32,48 +31,56 @@ func NewSyncPullRequestsCmd() *cobra.Command {
3231
# Make sure GitHub credentials are configured via environment variables or token
3332
3433
# Sync all pull requests from a repository
35-
$ ctrlc sync github pull-request --owner myorg --repo myrepo --token ghp_yourtokenhere
34+
$ ctrlc sync github pull-requests --owner myorg --repo myrepo --token ghp_yourtokenhere
3635
3736
# Sync only open pull requests
38-
$ ctrlc sync github pull-request --owner myorg --repo myrepo --state open
37+
$ ctrlc sync github pull-requests --owner myorg --repo myrepo --state open
3938
4039
# Sync only draft pull requests
41-
$ ctrlc sync github pull-request --owner myorg --repo myrepo --state draft
40+
$ ctrlc sync github pull-requests --owner myorg --repo myrepo --state draft
4241
4342
# Sync only merged pull requests
44-
$ ctrlc sync github pull-request --owner myorg --repo myrepo --state merged
43+
$ ctrlc sync github pull-requests --owner myorg --repo myrepo --state merged
4544
4645
# Sync only closed but not merged pull requests
47-
$ ctrlc sync github pull-request --owner myorg --repo myrepo --state closed
46+
$ ctrlc sync github pull-requests --owner myorg --repo myrepo --state closed
4847
4948
# Sync multiple states
50-
$ ctrlc sync github pull-request --owner myorg --repo myrepo --state open --state draft
49+
$ ctrlc sync github pull-requests --owner myorg --repo myrepo --state open --state draft
5150
`),
52-
PreRunE: validateFlags(&owner, &repo, &states),
53-
RunE: runSync(&owner, &repo, &token, &name, &states),
51+
PreRunE: validateFlags(&repoPath, &states),
52+
RunE: runSync(&repoPath, &token, &name, &states),
5453
}
5554

5655
// Add command flags
5756
cmd.Flags().StringVarP(&name, "provider", "p", "", "Name of the resource provider")
58-
cmd.Flags().StringVarP(&owner, "owner", "o", "", "GitHub repository owner (user or organization)")
59-
cmd.Flags().StringVarP(&repo, "repo", "r", "", "GitHub repository name")
57+
cmd.Flags().StringVarP(&repoPath, "repo", "r", "", "GitHub repository name (owner/repo)")
6058
cmd.Flags().StringVarP(&token, "token", "t", "", "GitHub API token (can also be set via GITHUB_TOKEN env var)")
6159
cmd.Flags().StringSliceVarP(&states, "state", "s", []string{"open"}, "Filter pull requests by state: all, open, closed, draft, merged (can be specified multiple times)")
62-
cmd.MarkFlagRequired("owner")
6360
cmd.MarkFlagRequired("repo")
6461

6562
return cmd
6663
}
6764

6865
// validateFlags ensures required flags are set and validates flag combinations
69-
func validateFlags(owner, repo *string, states *[]string) func(cmd *cobra.Command, args []string) error {
66+
func validateFlags(repoPath *string, states *[]string) func(cmd *cobra.Command, args []string) error {
7067
return func(cmd *cobra.Command, args []string) error {
71-
log.Debug("Validating flags", "owner", *owner, "repo", *repo, "states", *states)
68+
// Extract owner from repo string if it's in the format "owner/repo"
69+
var owner string
70+
var repo string
71+
parts := strings.Split(*repoPath, "/")
72+
if len(parts) == 2 {
73+
owner = parts[0]
74+
repo = parts[1]
75+
log.Debug("Extracted owner and repo from repo string", "owner", owner, "repo", repo)
76+
}
77+
78+
log.Debug("Validating flags", "owner", owner, "repo", repo, "states", *states)
7279

73-
if *owner == "" {
74-
return fmt.Errorf("owner is required")
80+
if owner == "" {
81+
return fmt.Errorf("owner is required (use --owner flag or specify repo as 'owner/repo')")
7582
}
76-
if *repo == "" {
83+
if repo == "" {
7784
return fmt.Errorf("repo is required")
7885
}
7986

@@ -109,11 +116,10 @@ func validateFlags(owner, repo *string, states *[]string) func(cmd *cobra.Comman
109116
}
110117

111118
// runSync contains the main sync logic
112-
func runSync(owner, repo, token, name *string, states *[]string) func(cmd *cobra.Command, args []string) error {
119+
func runSync(repoPath, token, name *string, states *[]string) func(cmd *cobra.Command, args []string) error {
113120
return func(cmd *cobra.Command, args []string) error {
114121
log.Info("Syncing GitHub pull requests into Ctrlplane",
115-
"owner", *owner,
116-
"repo", *repo,
122+
"repoPath", *repoPath,
117123
"states", *states)
118124

119125
ctx := context.Background()
@@ -128,8 +134,6 @@ func runSync(owner, repo, token, name *string, states *[]string) func(cmd *cobra
128134
return fmt.Errorf("GitHub token is required (use --token flag or set GITHUB_TOKEN env var)")
129135
}
130136
log.Debug("Found GitHub token in environment")
131-
} else {
132-
log.Debug("Using GitHub token from flag")
133137
}
134138

135139
// Initialize GitHub client
@@ -142,8 +146,13 @@ func runSync(owner, repo, token, name *string, states *[]string) func(cmd *cobra
142146
log.Debug("GitHub client initialized successfully")
143147

144148
// List and process pull requests
145-
log.Debug("Processing pull requests", "owner", *owner, "repo", *repo)
146-
resources, err := processPullRequests(ctx, client, *owner, *repo, *states)
149+
log.Debug("Processing pull requests", "repoPath", *repoPath)
150+
151+
pathSplit := strings.Split(*repoPath, "/")
152+
owner := pathSplit[0]
153+
repo := pathSplit[1]
154+
155+
resources, err := processPullRequests(ctx, client, owner, repo, *states)
147156
if err != nil {
148157
log.Error("Failed to process pull requests", "error", err)
149158
return err
@@ -152,7 +161,7 @@ func runSync(owner, repo, token, name *string, states *[]string) func(cmd *cobra
152161

153162
// Upsert resources to Ctrlplane
154163
log.Debug("Upserting resources to Ctrlplane", "count", len(resources))
155-
return upsertToCtrlplane(ctx, resources, owner, repo, name)
164+
return upsertToCtrlplane(ctx, resources, owner, repo, *name)
156165
}
157166
}
158167

@@ -598,14 +607,14 @@ func initPullRequestMetadata(pr *github.PullRequest, owner, repo string) map[str
598607
var relationshipRules = []api.CreateResourceRelationshipRule{}
599608

600609
// upsertToCtrlplane handles upserting resources to Ctrlplane
601-
func upsertToCtrlplane(ctx context.Context, resources []api.AgentResource, owner, repo, name *string) error {
610+
func upsertToCtrlplane(ctx context.Context, resources []api.AgentResource, owner, repo, name string) error {
602611
log.Debug("Upserting resources to Ctrlplane", "count", len(resources))
603612

604-
if *name == "" {
605-
*name = fmt.Sprintf("github-prs-%s-%s", *owner, *repo)
606-
log.Debug("Using generated provider name", "name", *name)
613+
if name == "" {
614+
name = fmt.Sprintf("github-prs-%s-%s", owner, repo)
615+
log.Debug("Using generated provider name", "name", name)
607616
} else {
608-
log.Debug("Using provided provider name", "name", *name)
617+
log.Debug("Using provided provider name", "name", name)
609618
}
610619

611620
apiURL := viper.GetString("url")
@@ -621,17 +630,17 @@ func upsertToCtrlplane(ctx context.Context, resources []api.AgentResource, owner
621630
return fmt.Errorf("failed to create API client: %w", err)
622631
}
623632

624-
log.Debug("Creating resource provider", "name", *name)
625-
rp, err := api.NewResourceProvider(ctrlplaneClient, workspaceId, *name)
633+
log.Debug("Creating resource provider", "name", name)
634+
rp, err := api.NewResourceProvider(ctrlplaneClient, workspaceId, name)
626635
if err != nil {
627-
log.Error("Failed to create resource provider", "name", *name, "error", err)
636+
log.Error("Failed to create resource provider", "name", name, "error", err)
628637
return fmt.Errorf("failed to create resource provider: %w", err)
629638
}
630639

631640
log.Debug("Adding resource relationship rules", "rules_count", len(relationshipRules))
632641
err = rp.AddResourceRelationshipRule(ctx, relationshipRules)
633642
if err != nil {
634-
log.Error("Failed to add resource relationship rule", "name", *name, "error", err)
643+
log.Error("Failed to add resource relationship rule", "name", name, "error", err)
635644
} else {
636645
log.Debug("Successfully added relationship rules")
637646
}

0 commit comments

Comments
 (0)