|
| 1 | +package trigger |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "regexp" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/profclems/glab/api" |
| 9 | + "github.com/profclems/glab/commands/cmdutils" |
| 10 | + "github.com/profclems/glab/pkg/git" |
| 11 | + |
| 12 | + "github.com/MakeNowJust/heredoc" |
| 13 | + "github.com/spf13/cobra" |
| 14 | + "github.com/xanzy/go-gitlab" |
| 15 | +) |
| 16 | + |
| 17 | +const keyValuePair = ".+:.+" |
| 18 | + |
| 19 | +var re = regexp.MustCompile(keyValuePair) |
| 20 | + |
| 21 | +func getDefaultBranch(f *cmdutils.Factory) string { |
| 22 | + repo, err := f.BaseRepo() |
| 23 | + if err != nil { |
| 24 | + return "master" |
| 25 | + } |
| 26 | + |
| 27 | + remotes, err := f.Remotes() |
| 28 | + if err != nil { |
| 29 | + return "master" |
| 30 | + } |
| 31 | + |
| 32 | + repoRemote, err := remotes.FindByRepo(repo.RepoOwner(), repo.RepoName()) |
| 33 | + if err != nil { |
| 34 | + return "master" |
| 35 | + } |
| 36 | + |
| 37 | + branch, _ := git.GetDefaultBranch(repoRemote.Name) |
| 38 | + |
| 39 | + return branch |
| 40 | +} |
| 41 | + |
| 42 | +func NewCmdRun(f *cmdutils.Factory) *cobra.Command { |
| 43 | + var pipelineRunCmd = &cobra.Command{ |
| 44 | + Use: "trigger [flags]", |
| 45 | + Short: `Trigger a pipeline in the CI`, |
| 46 | + Aliases: []string{"t"}, |
| 47 | + Example: heredoc.Doc(` |
| 48 | + $ glab ci trigger |
| 49 | + $ glab ci trigger -b trunk |
| 50 | + $ glab ci run -b trunk --variables MYKEY:some_value --variables KEY2:another_value |
| 51 | + `), |
| 52 | + Long: ``, |
| 53 | + Args: cobra.ExactArgs(0), |
| 54 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 55 | + var err error |
| 56 | + |
| 57 | + apiClient, err := f.HttpClient() |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + |
| 62 | + repo, err := f.BaseRepo() |
| 63 | + if err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + |
| 67 | + pipelineVars := map[string]string{ |
| 68 | + // Do this, so the |
| 69 | + "CI_PIPELINE_SOURCE" : "trigger", |
| 70 | + } |
| 71 | + |
| 72 | + if customRunPipelineVars, _ := cmd.Flags().GetStringSlice("variables"); len(customRunPipelineVars) > 0 { |
| 73 | + for _, v := range customRunPipelineVars { |
| 74 | + if !re.MatchString(v) { |
| 75 | + return fmt.Errorf("Bad pipeline variable : \"%s\" should be of format KEY:VALUE", v) |
| 76 | + } |
| 77 | + s := strings.SplitN(v, ":", 2) |
| 78 | + pipelineVars[s[0]] = s[1] |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + c := &gitlab.RunPipelineTriggerOptions{ |
| 83 | + Variables: pipelineVars, |
| 84 | + } |
| 85 | + |
| 86 | + if m, _ := cmd.Flags().GetString("branch"); m != "" { |
| 87 | + c.Ref = gitlab.String(m) |
| 88 | + } else { |
| 89 | + c.Ref = gitlab.String(getDefaultBranch(f)) |
| 90 | + } |
| 91 | + |
| 92 | + pipe, err := api.RunPipelineTrigger(apiClient, repo.FullName(), c) |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + |
| 97 | + fmt.Fprintln(f.IO.StdOut, "Ran pipeline (id:", pipe.ID, "), status:", pipe.Status, ", ref:", pipe.Ref, ", weburl: ", pipe.WebURL, ")") |
| 98 | + return nil |
| 99 | + }, |
| 100 | + } |
| 101 | + pipelineRunCmd.Flags().StringP("branch", "b", "", "Run pipeline on branch/ref <string>") |
| 102 | + pipelineRunCmd.Flags().StringSliceP("variables", "", []string{}, "Pass variables to pipeline run") |
| 103 | + |
| 104 | + return pipelineRunCmd |
| 105 | +} |
0 commit comments