From d6ffd109b6a01c38f743100e7063fbac09caf85c Mon Sep 17 00:00:00 2001 From: Ompragash Viswanathan Date: Tue, 25 Jun 2024 19:42:58 +0530 Subject: [PATCH 1/3] Added publishBuildInfo feature --- plugin/plugin.go | 108 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 89 insertions(+), 19 deletions(-) diff --git a/plugin/plugin.go b/plugin/plugin.go index 2797777..25dd395 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -7,6 +7,7 @@ package plugin import ( "context" "fmt" + "net/url" "os" "os/exec" "path/filepath" @@ -23,24 +24,25 @@ type Args struct { Level string `envconfig:"PLUGIN_LOG_LEVEL"` // TODO replace or remove - Username string `envconfig:"PLUGIN_USERNAME"` - Password string `envconfig:"PLUGIN_PASSWORD"` - APIKey string `envconfig:"PLUGIN_API_KEY"` - AccessToken string `envconfig:"PLUGIN_ACCESS_TOKEN"` - URL string `envconfig:"PLUGIN_URL"` - Source string `envconfig:"PLUGIN_SOURCE"` - Target string `envconfig:"PLUGIN_TARGET"` - Retries int `envconfig:"PLUGIN_RETRIES"` - Flat string `envconfig:"PLUGIN_FLAT"` - Spec string `envconfig:"PLUGIN_SPEC"` - Threads int `envconfig:"PLUGIN_THREADS"` - SpecVars string `envconfig:"PLUGIN_SPEC_VARS"` - TargetProps string `envconfig:"PLUGIN_TARGET_PROPS"` - Insecure string `envconfig:"PLUGIN_INSECURE"` - PEMFileContents string `envconfig:"PLUGIN_PEM_FILE_CONTENTS"` - PEMFilePath string `envconfig:"PLUGIN_PEM_FILE_PATH"` - BuildNumber string `envconfig:"PLUGIN_BUILD_NUMBER"` - BuildName string `envconfig:"PLUGIN_BUILD_NAME"` + Username string `envconfig:"PLUGIN_USERNAME"` + Password string `envconfig:"PLUGIN_PASSWORD"` + APIKey string `envconfig:"PLUGIN_API_KEY"` + AccessToken string `envconfig:"PLUGIN_ACCESS_TOKEN"` + URL string `envconfig:"PLUGIN_URL"` + Source string `envconfig:"PLUGIN_SOURCE"` + Target string `envconfig:"PLUGIN_TARGET"` + Retries int `envconfig:"PLUGIN_RETRIES"` + Flat string `envconfig:"PLUGIN_FLAT"` + Spec string `envconfig:"PLUGIN_SPEC"` + Threads int `envconfig:"PLUGIN_THREADS"` + SpecVars string `envconfig:"PLUGIN_SPEC_VARS"` + TargetProps string `envconfig:"PLUGIN_TARGET_PROPS"` + Insecure string `envconfig:"PLUGIN_INSECURE"` + PEMFileContents string `envconfig:"PLUGIN_PEM_FILE_CONTENTS"` + PEMFilePath string `envconfig:"PLUGIN_PEM_FILE_PATH"` + BuildNumber string `envconfig:"PLUGIN_BUILD_NUMBER"` + BuildName string `envconfig:"PLUGIN_BUILD_NAME"` + PublishBuildInfo bool `envconfig:"PLUGIN_PUBLISH_BUILD_INFO"` } // Exec executes the plugin. @@ -143,7 +145,75 @@ func Exec(ctx context.Context, args Args) error { trace(cmd) err := cmd.Run() - return err + if err != nil { + return err + } + + // Call publishBuildInfo if PLUGIN_PUBLISH_BUILD_INFO is set to true + if args.PublishBuildInfo { + if err := publishBuildInfo(args); err != nil { + return err + } + } + + return nil +} + +func publishBuildInfo(args Args) error { + if args.BuildName == "" || args.BuildNumber == "" { + return fmt.Errorf("both build name and build number need to be set when publishing build info") + } + + sanitizedURL, err := sanitizeURL(args.URL) + if err != nil { + return err + } + + publishCmdArgs := []string{getJfrogBin(), "rt", "build-publish", args.BuildName, args.BuildNumber, fmt.Sprintf("--url=%s", sanitizedURL)} + + if args.AccessToken != "" { + publishCmdArgs = append(publishCmdArgs, fmt.Sprintf("--access-token=%sPLUGIN_ACCESS_TOKEN", getEnvPrefix())) + } else if args.Username != "" && args.Password != "" { + publishCmdArgs = append(publishCmdArgs, fmt.Sprintf("--user=%sPLUGIN_USERNAME", getEnvPrefix())) + publishCmdArgs = append(publishCmdArgs, fmt.Sprintf("--password=%sPLUGIN_PASSWORD", getEnvPrefix())) + } else { + return fmt.Errorf("either access token or username/password need to be set for publishing build info") + } + + publishCmdStr := strings.Join(publishCmdArgs, " ") + shell, shArg := getShell() + publishCmd := exec.Command(shell, shArg, publishCmdStr) + publishCmd.Env = os.Environ() + publishCmd.Env = append(publishCmd.Env, "JFROG_CLI_OFFER_CONFIG=false") + publishCmd.Stdout = os.Stdout + publishCmd.Stderr = os.Stderr + trace(publishCmd) + + if err := publishCmd.Run(); err != nil { + return fmt.Errorf("error publishing build info: %s", err) + } + + return nil +} + +// sanitizeURL trims the URL to include only up to the '/artifactory/' path. +func sanitizeURL(inputURL string) (string, error) { + parsedURL, err := url.Parse(inputURL) + if err != nil { + return "", fmt.Errorf("invalid URL: %s", inputURL) + } + + // Split the path to extract the portion up to and including "/artifactory/" + parts := strings.SplitN(parsedURL.Path, "/artifactory/", 2) + if len(parts) == 0 { + return "", fmt.Errorf("url does not contain '/artifactory/': %s", inputURL) + } + + // Ensure the base URL ends with "/artifactory/" + parsedURL.Path = parts[0] + "/artifactory/" + + // Rebuild the URL with the sanitized path + return parsedURL.String(), nil } // setAuthParams appends authentication parameters to cmdArgs based on the provided credentials. From 0563d384819a87ee70c459b79b15ee46d668cf3c Mon Sep 17 00:00:00 2001 From: Ompragash Viswanathan Date: Tue, 25 Jun 2024 21:20:06 +0530 Subject: [PATCH 2/3] support double quotes for buildName and buildNumber --- plugin/plugin.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/plugin/plugin.go b/plugin/plugin.go index 25dd395..a597b46 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -169,7 +169,14 @@ func publishBuildInfo(args Args) error { return err } - publishCmdArgs := []string{getJfrogBin(), "rt", "build-publish", args.BuildName, args.BuildNumber, fmt.Sprintf("--url=%s", sanitizedURL)} + publishCmdArgs := []string{ + getJfrogBin(), + "rt", + "build-publish", + "\"" + args.BuildName + "\"", + "\"" + args.BuildNumber + "\"", + fmt.Sprintf("--url=%s", sanitizedURL), + } if args.AccessToken != "" { publishCmdArgs = append(publishCmdArgs, fmt.Sprintf("--access-token=%sPLUGIN_ACCESS_TOKEN", getEnvPrefix())) From 37ee2cc9c1fd8f5b6e5139bb17f0e7b43d5867a5 Mon Sep 17 00:00:00 2001 From: Ompragash Viswanathan Date: Tue, 25 Jun 2024 21:31:11 +0530 Subject: [PATCH 3/3] Updated plugin.go --- plugin/plugin.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/plugin/plugin.go b/plugin/plugin.go index a597b46..c84adce 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -209,17 +209,11 @@ func sanitizeURL(inputURL string) (string, error) { if err != nil { return "", fmt.Errorf("invalid URL: %s", inputURL) } - - // Split the path to extract the portion up to and including "/artifactory/" parts := strings.SplitN(parsedURL.Path, "/artifactory/", 2) if len(parts) == 0 { return "", fmt.Errorf("url does not contain '/artifactory/': %s", inputURL) } - - // Ensure the base URL ends with "/artifactory/" parsedURL.Path = parts[0] + "/artifactory/" - - // Rebuild the URL with the sanitized path return parsedURL.String(), nil }