Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat]: [CI-14921]: plugin changes jfrog #73

Merged
merged 2 commits into from
Oct 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 60 additions & 25 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package plugin
import (
"context"
"fmt"
"log"
"net/url"
"os"
"os/exec"
Expand All @@ -16,6 +17,15 @@ import (
"strings"
)

const (
harnessHTTPProxy = "HARNESS_HTTP_PROXY"
harnessHTTPSProxy = "HARNESS_HTTPS_PROXY"
harnessNoProxy = "HARNESS_NO_PROXY"
httpProxy = "HTTP_PROXY"
httpsProxy = "HTTPS_PROXY"
noProxy = "NO_PROXY"
)

// Args provides plugin execution arguments.
type Args struct {
Pipeline
Expand Down Expand Up @@ -43,10 +53,18 @@ type Args struct {
BuildNumber string `envconfig:"PLUGIN_BUILD_NUMBER"`
BuildName string `envconfig:"PLUGIN_BUILD_NAME"`
PublishBuildInfo bool `envconfig:"PLUGIN_PUBLISH_BUILD_INFO"`
EnableProxy string `envconfig:"PLUGIN_ENABLE_PROXY"`
}

// Exec executes the plugin.
func Exec(ctx context.Context, args Args) error {

enableProxy := parseBoolOrDefault(false, args.EnableProxy)
if enableProxy {
log.Printf("setting proxy config for upload")
setSecureConnectProxies()
}

// write code here
if args.URL == "" {
return fmt.Errorf("url needs to be set")
Expand Down Expand Up @@ -122,8 +140,8 @@ func Exec(ctx context.Context, args Args) error {
} else {
filteredTargetProps := filterTargetProps(args.TargetProps)
if filteredTargetProps != "" {
cmdArgs = append(cmdArgs, fmt.Sprintf("--target-props='%s'", filteredTargetProps))
}
cmdArgs = append(cmdArgs, fmt.Sprintf("--target-props='%s'", filteredTargetProps))
}
if args.Source == "" {
return fmt.Errorf("source file needs to be set")
}
Expand Down Expand Up @@ -206,28 +224,28 @@ func publishBuildInfo(args Args) error {

// Function to filter TargetProps based on criteria
func filterTargetProps(rawProps string) string {
keyValuePairs := strings.Split(rawProps, ",")
validPairs := []string{}

for _, pair := range keyValuePairs {
keyValuePair := strings.SplitN(pair, "=", 2)
if len(keyValuePair) != 2 {
continue // skip if it's not a valid key-value pair
}

key := strings.TrimSpace(keyValuePair[0])
value := strings.TrimSpace(keyValuePair[1])

// Remove single or double quotes from value
trimmedValue := strings.Trim(value, "\"'")
// Check value is not empty, not "null", and not just whitespace
if trimmedValue != "" && strings.ToLower(trimmedValue) != "null" {
validPairs = append(validPairs, key + "=" + value)
}
}

return strings.Join(validPairs, ",")
keyValuePairs := strings.Split(rawProps, ",")
validPairs := []string{}

for _, pair := range keyValuePairs {
keyValuePair := strings.SplitN(pair, "=", 2)
if len(keyValuePair) != 2 {
continue // skip if it's not a valid key-value pair
}

key := strings.TrimSpace(keyValuePair[0])
value := strings.TrimSpace(keyValuePair[1])

// Remove single or double quotes from value
trimmedValue := strings.Trim(value, "\"'")

// Check value is not empty, not "null", and not just whitespace
if trimmedValue != "" && strings.ToLower(trimmedValue) != "null" {
validPairs = append(validPairs, key+"="+value)
}
}

return strings.Join(validPairs, ",")
}

// sanitizeURL trims the URL to include only up to the '/artifactory/' path.
Expand All @@ -246,7 +264,7 @@ func sanitizeURL(inputURL string) (string, error) {

// Always set the path to the first part + "/artifactory/"
parsedURL.Path = parts[0] + "/artifactory/"

return parsedURL.String(), nil
}

Expand Down Expand Up @@ -304,3 +322,20 @@ func parseBoolOrDefault(defaultValue bool, s string) (result bool) {
func trace(cmd *exec.Cmd) {
fmt.Fprintf(os.Stdout, "+ %s\n", strings.Join(cmd.Args, " "))
}

func setSecureConnectProxies() {
copyEnvVariableIfExists(harnessHTTPProxy, httpProxy)
copyEnvVariableIfExists(harnessHTTPSProxy, httpsProxy)
copyEnvVariableIfExists(harnessNoProxy, noProxy)
}

func copyEnvVariableIfExists(src string, dest string) {
srcValue := os.Getenv(src)
if srcValue == "" {
return
}
err := os.Setenv(dest, srcValue)
if err != nil {
log.Printf("Failed to copy env variable from %s to %s with error %v", src, dest, err)
}
}