feat(schedule): add option to create a scheduled pipeline with variables

This commit is contained in:
Sebastian Gumprich 2024-01-02 20:31:50 +00:00 committed by Gary Holtz
parent b84ce07873
commit bb69db18a5
8 changed files with 105 additions and 8 deletions

View File

@ -389,14 +389,17 @@ var PipelineJobsWithSha = func(client *gitlab.Client, pid interface{}, sha strin
return PipelineJobsWithID(client, pid, pipelines[0].ID) return PipelineJobsWithID(client, pid, pipelines[0].ID)
} }
var ProjectNamespaceLint = func(client *gitlab.Client, projectID int, content string) (*gitlab.ProjectLintResult, error) { var ProjectNamespaceLint = func(client *gitlab.Client, projectID int, content string, ref string, dryRun bool, includeJobs bool) (*gitlab.ProjectLintResult, error) {
if client == nil { if client == nil {
client = apiClient.Lab() client = apiClient.Lab()
} }
c, _, err := client.Validate.ProjectNamespaceLint( c, _, err := client.Validate.ProjectNamespaceLint(
projectID, projectID,
&gitlab.ProjectNamespaceLintOptions{ &gitlab.ProjectNamespaceLintOptions{
Content: &content, Content: &content,
DryRun: &dryRun,
Ref: &ref,
IncludeJobs: &includeJobs,
}, },
) )
if err != nil { if err != nil {

View File

@ -34,12 +34,25 @@ var RunSchedule = func(client *gitlab.Client, repo string, schedule int, opts ..
return nil return nil
} }
var CreateSchedule = func(client *gitlab.Client, repo string, scheduleOpts *gitlab.CreatePipelineScheduleOptions, opts ...gitlab.RequestOptionFunc) error { var CreateSchedule = func(client *gitlab.Client, repo string, scheduleOpts *gitlab.CreatePipelineScheduleOptions, opts ...gitlab.RequestOptionFunc) (error, *gitlab.PipelineSchedule) {
if client == nil { if client == nil {
client = apiClient.Lab() client = apiClient.Lab()
} }
_, _, err := client.PipelineSchedules.CreatePipelineSchedule(repo, scheduleOpts, opts...) schedule, _, err := client.PipelineSchedules.CreatePipelineSchedule(repo, scheduleOpts, opts...)
if err != nil {
return fmt.Errorf("creating scheduled pipeline status: %w", err), nil
}
return nil, schedule
}
var CreateScheduleVariable = func(client *gitlab.Client, repo string, schedule *gitlab.PipelineSchedule, scheduleVarOpts *gitlab.CreatePipelineScheduleVariableOptions, opts ...gitlab.RequestOptionFunc) error {
if client == nil {
client = apiClient.Lab()
}
_, _, err := client.PipelineSchedules.CreatePipelineScheduleVariable(repo, schedule.ID, scheduleVarOpts, opts...)
if err != nil { if err != nil {
return fmt.Errorf("creating scheduled pipeline status: %w", err) return fmt.Errorf("creating scheduled pipeline status: %w", err)
} }

View File

@ -15,6 +15,12 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var (
ref string
dryRun bool
includeJobs bool
)
func NewCmdLint(f *cmdutils.Factory) *cobra.Command { func NewCmdLint(f *cmdutils.Factory) *cobra.Command {
pipelineCILintCmd := &cobra.Command{ pipelineCILintCmd := &cobra.Command{
Use: "lint", Use: "lint",
@ -37,6 +43,10 @@ func NewCmdLint(f *cmdutils.Factory) *cobra.Command {
}, },
} }
pipelineCILintCmd.Flags().BoolVarP(&dryRun, "dry-run", "", false, "Run pipeline creation simulation.")
pipelineCILintCmd.Flags().BoolVarP(&includeJobs, "include-jobs", "", false, "The response should include the list of jobs that would exist in a static check or pipeline simulation.")
pipelineCILintCmd.Flags().StringVar(&ref, "ref", "", "When dry-run is true, sets the branch or tag context for validating the CI/CD YAML configuration.")
return pipelineCILintCmd return pipelineCILintCmd
} }
@ -87,7 +97,7 @@ func lintRun(f *cmdutils.Factory, path string) error {
fmt.Fprintln(f.IO.StdOut, "Validating...") fmt.Fprintln(f.IO.StdOut, "Validating...")
lint, err := api.ProjectNamespaceLint(apiClient, projectID, string(content)) lint, err := api.ProjectNamespaceLint(apiClient, projectID, string(content), ref, dryRun, includeJobs)
if err != nil { if err != nil {
return err return err
} }

View File

@ -2,6 +2,7 @@ package create
import ( import (
"fmt" "fmt"
"strings"
"gitlab.com/gitlab-org/cli/api" "gitlab.com/gitlab-org/cli/api"
"gitlab.com/gitlab-org/cli/commands/cmdutils" "gitlab.com/gitlab-org/cli/commands/cmdutils"
@ -11,12 +12,14 @@ import (
"github.com/xanzy/go-gitlab" "github.com/xanzy/go-gitlab"
) )
var variableList []string
func NewCmdCreate(f *cmdutils.Factory) *cobra.Command { func NewCmdCreate(f *cmdutils.Factory) *cobra.Command {
scheduleCreateCmd := &cobra.Command{ scheduleCreateCmd := &cobra.Command{
Use: "create [flags]", Use: "create [flags]",
Short: `Schedule a new pipeline.`, Short: `Schedule a new pipeline.`,
Example: heredoc.Doc(` Example: heredoc.Doc(`
glab schedule create --cron "0 * * * *" --description "Describe your pipeline here" --ref "main" glab schedule create --cron "0 * * * *" --description "Describe your pipeline here" --ref "main" --variable "foo:bar" --variable "baz:baz"
`), `),
Long: ``, Long: ``,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
@ -32,11 +35,14 @@ func NewCmdCreate(f *cmdutils.Factory) *cobra.Command {
l := &gitlab.CreatePipelineScheduleOptions{} l := &gitlab.CreatePipelineScheduleOptions{}
variable := &gitlab.CreatePipelineScheduleVariableOptions{}
description, _ := cmd.Flags().GetString("description") description, _ := cmd.Flags().GetString("description")
ref, _ := cmd.Flags().GetString("ref") ref, _ := cmd.Flags().GetString("ref")
cron, _ := cmd.Flags().GetString("cron") cron, _ := cmd.Flags().GetString("cron")
cronTimeZone, _ := cmd.Flags().GetString("cronTimeZone") cronTimeZone, _ := cmd.Flags().GetString("cronTimeZone")
active, _ := cmd.Flags().GetBool("active") active, _ := cmd.Flags().GetBool("active")
variableList, _ = cmd.Flags().GetStringSlice("variable")
l.Description = &description l.Description = &description
l.Ref = &ref l.Ref = &ref
@ -44,10 +50,24 @@ func NewCmdCreate(f *cmdutils.Factory) *cobra.Command {
l.CronTimezone = &cronTimeZone l.CronTimezone = &cronTimeZone
l.Active = &active l.Active = &active
err = api.CreateSchedule(apiClient, repo.FullName(), l) err, schedule := api.CreateSchedule(apiClient, repo.FullName(), l)
if err != nil { if err != nil {
return err return err
} }
for _, v := range variableList {
split := strings.SplitN(v, ":", 2)
if len(split) != 2 {
return fmt.Errorf("Invalid format for --variable: %s", v)
}
variable.Key = &split[0]
variable.Value = &split[1]
err = api.CreateScheduleVariable(apiClient, repo.FullName(), schedule, variable)
if err != nil {
return err
}
}
fmt.Fprintln(f.IO.StdOut, "Created schedule") fmt.Fprintln(f.IO.StdOut, "Created schedule")
return nil return nil
@ -58,6 +78,7 @@ func NewCmdCreate(f *cmdutils.Factory) *cobra.Command {
scheduleCreateCmd.Flags().String("cron", "", "Cron interval pattern") scheduleCreateCmd.Flags().String("cron", "", "Cron interval pattern")
scheduleCreateCmd.Flags().String("cronTimeZone", "UTC", "Cron timezone") scheduleCreateCmd.Flags().String("cronTimeZone", "UTC", "Cron timezone")
scheduleCreateCmd.Flags().Bool("active", true, "Whether or not the schedule is active") scheduleCreateCmd.Flags().Bool("active", true, "Whether or not the schedule is active")
scheduleCreateCmd.Flags().StringSliceVar(&variableList, "variable", []string{}, "Pass variables to schedule in format <key>:<value>")
_ = scheduleCreateCmd.MarkFlagRequired("ref") _ = scheduleCreateCmd.MarkFlagRequired("ref")
_ = scheduleCreateCmd.MarkFlagRequired("cron") _ = scheduleCreateCmd.MarkFlagRequired("cron")

View File

@ -47,6 +47,39 @@ func Test_ScheduleCreate(t *testing.T) {
ExpectedMsg: []string{""}, ExpectedMsg: []string{""},
cli: "--cron '*0 * * * *' --description 'example pipeline'", cli: "--cron '*0 * * * *' --description 'example pipeline'",
}, },
{
Name: "Schedule created but with skipped variable",
wantStderr: "Invalid format for --variable: foo",
wantErr: true,
cli: "--cron '*0 * * * *' --description 'example pipeline' --ref 'main' --variable 'foo'",
httpMocks: []httpMock{
{
http.MethodPost,
"/api/v4/projects/OWNER/REPO/pipeline_schedules",
http.StatusCreated,
`{}`,
},
},
},
{
Name: "Schedule created with variable",
ExpectedMsg: []string{"Created schedule"},
cli: "--cron '*0 * * * *' --description 'example pipeline' --ref 'main' --variable 'foo:bar'",
httpMocks: []httpMock{
{
http.MethodPost,
"/api/v4/projects/OWNER/REPO/pipeline_schedules",
http.StatusCreated,
`{}`,
},
{
http.MethodPost,
"/api/v4/projects/OWNER/REPO/pipeline_schedules/0/variables",
http.StatusCreated,
`{}`,
},
},
},
} }
for _, tc := range testCases { for _, tc := range testCases {

View File

@ -29,6 +29,14 @@ $ glab ci lint path/to/.gitlab-ci.yml
``` ```
## Options
```plaintext
--dry-run Run pipeline creation simulation.
--include-jobs The response should include the list of jobs that would exist in a static check or pipeline simulation.
--ref string When dry-run is true, sets the branch or tag context for validating the CI/CD YAML configuration.
```
## Options inherited from parent commands ## Options inherited from parent commands
```plaintext ```plaintext

View File

@ -29,6 +29,14 @@ $ glab ci lint path/to/.gitlab-ci.yml
``` ```
## Options
```plaintext
--dry-run Run pipeline creation simulation.
--include-jobs The response should include the list of jobs that would exist in a static check or pipeline simulation.
--ref string When dry-run is true, sets the branch or tag context for validating the CI/CD YAML configuration.
```
## Options inherited from parent commands ## Options inherited from parent commands
```plaintext ```plaintext

View File

@ -20,7 +20,7 @@ glab schedule create [flags]
## Examples ## Examples
```plaintext ```plaintext
glab schedule create --cron "0 * * * *" --description "Describe your pipeline here" --ref "main" glab schedule create --cron "0 * * * *" --description "Describe your pipeline here" --ref "main" --variable "foo:bar" --variable "baz:baz"
``` ```
@ -32,6 +32,7 @@ glab schedule create --cron "0 * * * *" --description "Describe your pipeline he
--cronTimeZone string Cron timezone (default "UTC") --cronTimeZone string Cron timezone (default "UTC")
--description string Description of the schedule --description string Description of the schedule
--ref string Target branch or tag --ref string Target branch or tag
--variable strings Pass variables to schedule in format <key>:<value>
``` ```
## Options inherited from parent commands ## Options inherited from parent commands