|
1 | 1 | package pr |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
4 | 5 | "fmt" |
5 | 6 | "io" |
6 | 7 | "os" |
| 8 | + "strings" |
7 | 9 |
|
8 | 10 | "github.com/samber/lo" |
9 | 11 | "github.com/urfave/cli" |
@@ -116,6 +118,22 @@ func (p *Plural) prCommands() []cli.Command { |
116 | 118 | }, |
117 | 119 | }, |
118 | 120 | }, |
| 121 | + { |
| 122 | + Name: "trigger", |
| 123 | + Action: common.LatestVersion(common.RequireArgs(p.handleTriggerPrAutomation, []string{"{name}"})), |
| 124 | + Usage: "trigger PR automation by name with configuration", |
| 125 | + ArgsUsage: "{name}", |
| 126 | + Flags: []cli.Flag{ |
| 127 | + cli.StringSliceFlag{ |
| 128 | + Name: "configuration", |
| 129 | + Usage: "configuration key-value pairs (format: key=value)", |
| 130 | + }, |
| 131 | + cli.StringFlag{ |
| 132 | + Name: "branch", |
| 133 | + Usage: "branch name for the PR", |
| 134 | + }, |
| 135 | + }, |
| 136 | + }, |
119 | 137 | } |
120 | 138 | } |
121 | 139 |
|
@@ -243,3 +261,73 @@ func (p *Plural) handleCreatePrAutomation(c *cli.Context) error { |
243 | 261 | utils.Success("PR %s created successfully\n", pr.ID) |
244 | 262 | return nil |
245 | 263 | } |
| 264 | + |
| 265 | +func (p *Plural) handleTriggerPrAutomation(c *cli.Context) error { |
| 266 | + if err := p.InitConsoleClient(consoleToken, consoleURL); err != nil { |
| 267 | + return err |
| 268 | + } |
| 269 | + |
| 270 | + // Get PR automation by name |
| 271 | + prAutomation, err := p.ConsoleClient.GetPrAutomationByName(c.Args().Get(0)) |
| 272 | + if err != nil { |
| 273 | + return err |
| 274 | + } |
| 275 | + |
| 276 | + // Build context from CLI flags and env vars |
| 277 | + context, err := buildTriggerContext(c) |
| 278 | + if err != nil { |
| 279 | + return err |
| 280 | + } |
| 281 | + |
| 282 | + contextJSON, err := json.Marshal(context) |
| 283 | + if err != nil { |
| 284 | + return err |
| 285 | + } |
| 286 | + |
| 287 | + // Optional branch |
| 288 | + var branch *string |
| 289 | + if b := c.String("branch"); b != "" { |
| 290 | + branch = &b |
| 291 | + } |
| 292 | + |
| 293 | + // Create PR |
| 294 | + pr, err := p.ConsoleClient.CreatePullRequest(prAutomation.ID, branch, lo.ToPtr(string(contextJSON))) |
| 295 | + if err != nil { |
| 296 | + return err |
| 297 | + } |
| 298 | + |
| 299 | + utils.Success("PR %s triggered successfully\n", pr.ID) |
| 300 | + return nil |
| 301 | +} |
| 302 | + |
| 303 | +func buildTriggerContext(c *cli.Context) (map[string]interface{}, error) { |
| 304 | + context := make(map[string]interface{}) |
| 305 | + |
| 306 | + // Parse --configuration flags (strict validation) |
| 307 | + for _, config := range c.StringSlice("configuration") { |
| 308 | + parts := strings.SplitN(config, "=", 2) |
| 309 | + if len(parts) != 2 { |
| 310 | + return nil, fmt.Errorf("invalid configuration format: '%s'. Expected format: key=value", config) |
| 311 | + } |
| 312 | + if strings.TrimSpace(parts[0]) == "" { |
| 313 | + return nil, fmt.Errorf("invalid configuration format: '%s'. Key cannot be empty", config) |
| 314 | + } |
| 315 | + context[parts[0]] = parts[1] |
| 316 | + } |
| 317 | + |
| 318 | + // Parse PLRL_PR_* environment variables (silently skip malformed ones) |
| 319 | + for _, env := range os.Environ() { |
| 320 | + if strings.HasPrefix(env, "PLRL_PR_") { |
| 321 | + parts := strings.SplitN(env, "=", 2) |
| 322 | + if len(parts) == 2 { |
| 323 | + key := strings.ToLower(strings.TrimPrefix(parts[0], "PLRL_PR_")) |
| 324 | + // Only add if not already set by CLI flags (CLI flags take precedence) |
| 325 | + if _, exists := context[key]; !exists { |
| 326 | + context[key] = parts[1] |
| 327 | + } |
| 328 | + } |
| 329 | + } |
| 330 | + } |
| 331 | + |
| 332 | + return context, nil |
| 333 | +} |
0 commit comments