Skip to content

Commit

Permalink
New JFrog Pipelines commands (#1766)
Browse files Browse the repository at this point in the history
  • Loading branch information
bhanurp authored Jan 26, 2023
1 parent 414474d commit 821a92b
Show file tree
Hide file tree
Showing 11 changed files with 318 additions and 5 deletions.
7 changes: 7 additions & 0 deletions docs/pipelines/status/help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package status

var Usage = []string{"pl status"}

func GetDescription() string {
return "Fetch the latest pipeline run status."
}
14 changes: 14 additions & 0 deletions docs/pipelines/sync/help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package sync

var Usage = []string{"pl sync"}

func GetDescription() string {
return "Sync a pipeline resource."
}

func GetArguments() string {
return ` repository name
Full repository name of the pipeline resource.
branch name
Branch name to trigger sync on.`
}
7 changes: 7 additions & 0 deletions docs/pipelines/syncstatus/help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package syncstatus

var Usage = []string{"pl sync-status"}

func GetDescription() string {
return "Fetch pipeline resource sync status."
}
14 changes: 14 additions & 0 deletions docs/pipelines/trigger/help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package trigger

var Usage = []string{"pl trigger"}

func GetDescription() string {
return "Trigger a manual pipeline run."
}

func GetArguments() string {
return ` pipeline name
Pipeline name to trigger the manual run on.
branch name
Branch name to trigger the manual run on.`
}
7 changes: 7 additions & 0 deletions docs/pipelines/version/help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package version

var Usage = []string{"pl version"}

func GetDescription() string {
return "Show the version of JFrog Pipelines."
}
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ require (

// replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go v1.25.1-0.20230119133624-ec89f6372ca2

// replace github.com/jfrog/jfrog-cli-core/v2 => github.com/jfrog/jfrog-cli-core/v2 v2.27.1-0.20230119133823-3bdc4e58ba37
replace github.com/jfrog/jfrog-cli-core/v2 => github.com/bhanurp/jfrog-cli-core/v2 v2.24.4-0.20230124140516-8788cb7671eb

// replace github.com/jfrog/gofrog => github.com/jfrog/gofrog v1.2.5-0.20221107113836-a4c9225c690e

replace github.com/jfrog/jfrog-client-go => github.com/bhanurp/jfrog-client-go v0.0.0-20230124125655-3987a337ef2f
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bhanurp/jfrog-cli-core/v2 v2.24.4-0.20230124140516-8788cb7671eb h1:Y36cSA6cUWOfnNGcMHObZ66UCdWRrwQ79yeQCQXq+JI=
github.com/bhanurp/jfrog-cli-core/v2 v2.24.4-0.20230124140516-8788cb7671eb/go.mod h1:PnNIsp7leL4XISXxs9UYf6UnylEuEmPaPEHeIDcDg10=
github.com/bhanurp/jfrog-client-go v0.0.0-20230124125655-3987a337ef2f h1:LgYuy4p/NLM+u4AzNNBGmBsiW6Oo51Rz0Hwz5LDa384=
github.com/bhanurp/jfrog-client-go v0.0.0-20230124125655-3987a337ef2f/go.mod h1:q+QWoyufy21pXTzF+c6ddbuEj16AxCTFe/yrvnVzor0=
github.com/bitly/go-simplejson v0.5.0/go.mod h1:cXHtHw4XUPsvGaxgjIAn8PhEWG9NfngEKAMDJEczWVA=
github.com/bits-and-blooms/bitset v1.2.0/go.mod h1:gIdJ4wp64HaoK2YrL1Q5/N7Y16edYb8uY+O0FJTyyDA=
github.com/blang/semver v3.1.0+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
Expand Down Expand Up @@ -528,10 +532,6 @@ github.com/jfrog/build-info-go v1.8.6 h1:N627DBFIQvx1fbvkaRfsBvgQ74aim9i8A+b2QX0
github.com/jfrog/build-info-go v1.8.6/go.mod h1:iSTj26qEX3eUtyAGMH0qKsW4WJT+MceYxLWP9FfiAq4=
github.com/jfrog/gofrog v1.2.5 h1:jCgJC0iGQ8bU7jCC+YEFJTNINyngApIrhd8BjZAVRIE=
github.com/jfrog/gofrog v1.2.5/go.mod h1:o00tSRff6IapTgaCMuX1Cs9MH08Y1JqnsKgRtx91Gc4=
github.com/jfrog/jfrog-cli-core/v2 v2.28.0 h1:5i2fFWs9zFhw4LaMPvl6NIrc9YU664cHNlovynMh4jk=
github.com/jfrog/jfrog-cli-core/v2 v2.28.0/go.mod h1:PnNIsp7leL4XISXxs9UYf6UnylEuEmPaPEHeIDcDg10=
github.com/jfrog/jfrog-client-go v1.26.0 h1:iDV2T/TMAetkqLgNI5Xryr8JTCRy0a2yuo3Qjb1USPU=
github.com/jfrog/jfrog-client-go v1.26.0/go.mod h1:q+QWoyufy21pXTzF+c6ddbuEj16AxCTFe/yrvnVzor0=
github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/jmespath/go-jmespath v0.0.0-20160803190731-bd40a432e4c7/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/joefitzgerald/rainbow-reporter v0.1.0/go.mod h1:481CNgqmVHQZzdIbN52CupLJyoVwB10FQ/IQlF1pdL8=
Expand Down
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/jfrog/jfrog-cli/general/envsetup"
"github.com/jfrog/jfrog-cli/general/project"
"github.com/jfrog/jfrog-cli/missioncontrol"
"github.com/jfrog/jfrog-cli/pipelines"
"github.com/jfrog/jfrog-cli/plugins"
"github.com/jfrog/jfrog-cli/plugins/utils"
"github.com/jfrog/jfrog-cli/scan"
Expand Down Expand Up @@ -174,6 +175,12 @@ func getCommands() []cli.Command {
Subcommands: distribution.GetCommands(),
Category: otherCategory,
},
{
Name: cliutils.CmdPipelines,
Usage: "JFrog Pipelines commands.",
Subcommands: pipelines.GetCommands(),
Category: otherCategory,
},
{
Name: cliutils.CmdCompletion,
Usage: "Generate autocomplete scripts.",
Expand Down
201 changes: 201 additions & 0 deletions pipelines/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
package pipelines

import (
"fmt"
"github.com/jfrog/jfrog-cli-core/v2/common/commands"
corecommon "github.com/jfrog/jfrog-cli-core/v2/docs/common"
pipelines "github.com/jfrog/jfrog-cli-core/v2/pipelines/commands"
coreConfig "github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
"github.com/jfrog/jfrog-cli/docs/common"
"github.com/jfrog/jfrog-cli/docs/pipelines/status"
"github.com/jfrog/jfrog-cli/docs/pipelines/sync"
"github.com/jfrog/jfrog-cli/docs/pipelines/syncstatus"
"github.com/jfrog/jfrog-cli/docs/pipelines/trigger"
"github.com/jfrog/jfrog-cli/docs/pipelines/version"

"github.com/jfrog/jfrog-cli/utils/cliutils"
clientlog "github.com/jfrog/jfrog-client-go/utils/log"
"github.com/urfave/cli"
)

func GetCommands() []cli.Command {
return cliutils.GetSortedCommands(cli.CommandsByName{
{
Name: "status",
Flags: cliutils.GetCommandFlags(cliutils.Status),
Aliases: []string{"s"},
Usage: status.GetDescription(),
HelpName: corecommon.CreateUsage("pl status", status.GetDescription(), status.Usage),
ArgsUsage: common.CreateEnvVars(),
BashComplete: corecommon.CreateBashCompletionFunc(),
Action: func(c *cli.Context) error {
return fetchLatestPipelineRunStatus(c)
},
},
{
Name: "trigger",
Flags: cliutils.GetCommandFlags(cliutils.Trigger),
Aliases: []string{"t"},
Usage: trigger.GetDescription(),
HelpName: corecommon.CreateUsage("pl trigger", trigger.GetDescription(), trigger.Usage),
UsageText: trigger.GetArguments(),
ArgsUsage: common.CreateEnvVars(),
BashComplete: corecommon.CreateBashCompletionFunc(),
Action: func(c *cli.Context) error {
return triggerNewRun(c)
},
},
{
Name: "version",
Flags: cliutils.GetCommandFlags(cliutils.Version),
Aliases: []string{"v"},
Usage: version.GetDescription(),
HelpName: corecommon.CreateUsage("pl version", version.GetDescription(), version.Usage),
BashComplete: corecommon.CreateBashCompletionFunc(),
Action: func(c *cli.Context) error {
return getVersion(c)
},
},
{
Name: "sync",
Flags: cliutils.GetCommandFlags(cliutils.Sync),
Aliases: []string{"sy"},
Usage: sync.GetDescription(),
HelpName: corecommon.CreateUsage("pl sync", sync.GetDescription(), sync.Usage),
UsageText: sync.GetArguments(),
ArgsUsage: common.CreateEnvVars(),
BashComplete: corecommon.CreateBashCompletionFunc(),
Action: func(c *cli.Context) error {
return syncPipelineResources(c)
},
},
{
Name: "sync-status",
Flags: cliutils.GetCommandFlags(cliutils.SyncStatus),
Aliases: []string{"ss"},
Usage: syncstatus.GetDescription(),
HelpName: corecommon.CreateUsage("pl sync-status", syncstatus.GetDescription(), syncstatus.Usage),
ArgsUsage: common.CreateEnvVars(),
BashComplete: corecommon.CreateBashCompletionFunc(),
Action: func(c *cli.Context) error {
return getSyncPipelineResourcesStatus(c)
},
},
})
}

// getMultiBranch parses singleBranch flag and computes whether multiBranch is set to true/false
func getMultiBranch(c *cli.Context) bool {
return !c.Bool("single-branch")
}

// createPipelinesDetailsByFlags creates pipelines configuration details
func createPipelinesDetailsByFlags(c *cli.Context) (*coreConfig.ServerDetails, error) {
plDetails, err := cliutils.CreateServerDetailsWithConfigOffer(c, true, cliutils.CmdPipelines)
if err != nil {
return nil, err
}
if plDetails.DistributionUrl == "" {
return nil, fmt.Errorf("the --pipelines-url option is mandatory")
}
return plDetails, nil
}

// fetchLatestPipelineRunStatus fetches pipeline run status and filters from pipeline-name and branch flags
func fetchLatestPipelineRunStatus(c *cli.Context) error {
clientlog.Info(coreutils.PrintTitle("Fetching pipeline run status"))

// Read flags for status command
pipName := c.String("pipeline-name")
notify := c.Bool("monitor")
branch := c.String("branch")
multiBranch := getMultiBranch(c)
serviceDetails, err := createPipelinesDetailsByFlags(c)
if err != nil {
return err
}
statusCommand := pipelines.NewStatusCommand()
statusCommand.SetBranch(branch).
SetPipeline(pipName).
SetNotify(notify).
SetMultiBranch(multiBranch)

// Set server details
statusCommand.SetServerDetails(serviceDetails)
return commands.Exec(statusCommand)
}

// syncPipelineResources sync pipelines resource
func syncPipelineResources(c *cli.Context) error {
// Get arguments repository name and branch name
repository := c.Args().Get(0)
branch := c.Args().Get(1)
clientlog.Info("Triggering pipeline sync on repository:", repository, "branch:", branch)
serviceDetails, err := createPipelinesDetailsByFlags(c)
if err != nil {
return err
}

// Create new sync command and add filters
syncCommand := pipelines.NewSyncCommand()
syncCommand.SetBranch(branch)
syncCommand.SetRepositoryFullName(repository)
syncCommand.SetServerDetails(serviceDetails)
return commands.Exec(syncCommand)
}

// getSyncPipelineResourcesStatus fetch sync status for a given repository path and branch name
func getSyncPipelineResourcesStatus(c *cli.Context) error {
branch := c.String("branch")
repository := c.String("repository")
clientlog.Info("Fetching pipeline sync status on repository:", repository, "branch:", branch)

// Fetch service details for authentication
serviceDetails, err := createPipelinesDetailsByFlags(c)
if err != nil {
return err
}

// Create sync status command and add filter params
syncStatusCommand := pipelines.NewSyncStatusCommand()
syncStatusCommand.SetBranch(branch)
syncStatusCommand.SetRepoPath(repository)
syncStatusCommand.SetServerDetails(serviceDetails)
return commands.Exec(syncStatusCommand)
}

// getVersion version command handler
func getVersion(c *cli.Context) error {
serviceDetails, err := createPipelinesDetailsByFlags(c)
if err != nil {
return err
}
versionCommand := pipelines.NewVersionCommand()
versionCommand.SetServerDetails(serviceDetails)
return commands.Exec(versionCommand)
}

// triggerNewRun triggers a new run for supplied flag values
func triggerNewRun(c *cli.Context) error {
// Read arguments pipeline name and branch to trigger pipeline run
pipelineName := c.Args().Get(0)
branch := c.Args().Get(1)
multiBranch := getMultiBranch(c)
coreutils.PrintTitle("Triggering pipeline run ")
clientlog.Info("Triggering on pipeline:", pipelineName, "for branch:", branch)

// Get service config details
serviceDetails, err := createPipelinesDetailsByFlags(c)
if err != nil {
return err
}

// Trigger a pipeline run using branch name and pipeline name
triggerCommand := pipelines.NewTriggerCommand()
triggerCommand.SetBranch(branch).
SetPipelineName(pipelineName).
SetServerDetails(serviceDetails).
SetMultiBranch(multiBranch)
return commands.Exec(triggerCommand)
}
1 change: 1 addition & 0 deletions utils/cliutils/cli_consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (
CmdConfig = "config"
CmdOptions = "options"
CmdProject = "project"
CmdPipelines = "pl"

// Download
DownloadMinSplitKb = 5120
Expand Down
53 changes: 53 additions & 0 deletions utils/cliutils/commandsflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,20 @@ const (
IncludeProjects = "include-projects"
ExcludeProjects = "exclude-projects"

// *** JFrog Pipelines Commands' flags ***
// Base flags
branch = "branch"
Trigger = "trigger"
pipelineName = "pipeline-name"
name = "name"
Validate = "validate"
Resources = "resources"
monitor = "monitor"
repository = "repository"
singleBranch = "single-branch"
Sync = "sync"
SyncStatus = "sync-status"

// *** TransferInstall Commands' flags ***
installPluginPrefix = "install-"
installPluginVersion = installPluginPrefix + Version
Expand Down Expand Up @@ -1447,6 +1461,26 @@ var flagsMap = map[string]cli.Flag{
Name: Status,
Usage: "[Default: false] Set to true to show the status of the transfer-files command currently in progress.` `",
},
branch: cli.StringFlag{
Name: branch,
Usage: "[Optional] Branch name to filter.` `",
},
pipelineName: cli.StringFlag{
Name: pipelineName,
Usage: "[Optional] Pipeline name to filter.` `",
},
monitor: cli.BoolFlag{
Name: monitor,
Usage: "[Default: false] Monitor pipeline status.` `",
},
repository: cli.StringFlag{
Name: repository,
Usage: "[Optional] Repository name to filter resource.` `",
},
singleBranch: cli.BoolFlag{
Name: singleBranch,
Usage: "[Default: false] Single branch to filter multi branches and single branch pipelines sources.` `",
},
Stop: cli.BoolFlag{
Name: Stop,
Usage: "[Default: false] Set to true to stop the transfer-files command currently in progress. Useful when running the transfer-files command in the background.` `",
Expand Down Expand Up @@ -1816,6 +1850,25 @@ var commandFlags = map[string][]string{
setupFormat,
},
Intro: {},
// Pipelines commands
Status: {
branch, serverId, pipelineName, monitor, singleBranch,
},
Trigger: {
serverId, singleBranch,
},
Validate: {
Resources, serverId,
},
Version: {
serverId,
},
Sync: {
serverId,
},
SyncStatus: {
branch, repository, serverId,
},
}

func GetCommandFlags(cmd string) []cli.Flag {
Expand Down

0 comments on commit 821a92b

Please sign in to comment.