Skip to content

Commit

Permalink
CR comment
Browse files Browse the repository at this point in the history
  • Loading branch information
eyalb4doc committed Dec 25, 2024
1 parent 86de478 commit 3fa3a9d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 20 deletions.
11 changes: 7 additions & 4 deletions artifactory/utils/commandsummary/buildinfosummary.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,14 @@ func generateArtifactUrl(artifact buildInfo.Artifact, module buildInfo.Module) (
if strings.TrimSpace(artifact.OriginalDeploymentRepo) == "" {
return "", nil
}
trackingSection := "packages"
var section summarySection

if module.Type == buildInfo.Generic {
trackingSection = "artifacts"
section = artifactsSection
} else {
section = packagesSection
}
return GenerateArtifactUrl(path.Join(artifact.OriginalDeploymentRepo, artifact.Path), trackingSection)
return GenerateArtifactUrl(path.Join(artifact.OriginalDeploymentRepo, artifact.Path), section)
}

func groupModules(modules []buildInfo.Module) map[string][]buildInfo.Module {
Expand Down Expand Up @@ -327,7 +330,7 @@ func appendBuildInfoRow(tableBuilder *strings.Builder, build *buildInfo.BuildInf
buildName := build.Name + " " + build.Number
buildScanResult := getScanResults(buildName)
if StaticMarkdownConfig.IsExtendedSummary() {
buildInfoUrl, err := addGitHubTrackingToUrl(build.BuildUrl, "buildInfo")
buildInfoUrl, err := addGitHubTrackingToUrl(build.BuildUrl, buildInfoSection)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion artifactory/utils/commandsummary/uploadsummary.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (us *UploadSummary) generateFileTreeMarkdown() (string, error) {
func (us *UploadSummary) buildUiUrl(targetPath string) (string, error) {
// Only build URL if extended summary is enabled
if StaticMarkdownConfig.IsExtendedSummary() {
return GenerateArtifactUrl(targetPath, "artifacts")
return GenerateArtifactUrl(targetPath, artifactsSection)
}
return "", nil
}
16 changes: 12 additions & 4 deletions artifactory/utils/commandsummary/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ const (
artifactoryDockerPackagesUiFormat = "%s/ui/packages/docker:%s/sha256__%s"
)

func GenerateArtifactUrl(pathInRt, trackingSection string) (url string, err error) {
func GenerateArtifactUrl(pathInRt string, section summarySection) (url string, err error) {
if StaticMarkdownConfig.GetPlatformMajorVersion() == 6 {
url = fmt.Sprintf(artifactory6UiFormat, StaticMarkdownConfig.GetPlatformUrl(), pathInRt)
} else {
url = fmt.Sprintf(artifactory7UiFormat, StaticMarkdownConfig.GetPlatformUrl(), pathInRt)
}
url, err = addGitHubTrackingToUrl(url, trackingSection)
url, err = addGitHubTrackingToUrl(url, section)
return
}

Expand All @@ -41,8 +41,16 @@ func fileNameToSha1(fileName string) string {
return hex.EncodeToString(hashBytes)
}

type summarySection string

const (
artifactsSection summarySection = "artifacts"

Check failure on line 47 in artifactory/utils/commandsummary/utils.go

View workflow job for this annotation

GitHub Actions / Static-Check

SA9004: only the first constant in this group has an explicit type (staticcheck)
packagesSection = "packages"
buildInfoSection = "build"
)

// addGitHubTrackingToUrl adds GitHub-related query parameters to a given URL if the GITHUB_WORKFLOW environment variable is set.
func addGitHubTrackingToUrl(urlStr, section string) (string, error) {
func addGitHubTrackingToUrl(urlStr string, section summarySection) (string, error) {
// Check if GITHUB_WORKFLOW environment variable is set
githubWorkflow := os.Getenv("GITHUB_WORKFLOW")
if githubWorkflow == "" {
Expand All @@ -60,7 +68,7 @@ func addGitHubTrackingToUrl(urlStr, section string) (string, error) {
// Get the query parameters and add the GitHub tracking parameters
queryParams := parsedUrl.Query()
queryParams.Set("gh_job_id", githubWorkflow)
queryParams.Set("gh_section", section)
queryParams.Set("gh_section", string(section))
parsedUrl.RawQuery = queryParams.Encode()

// Return the modified URL
Expand Down
22 changes: 11 additions & 11 deletions artifactory/utils/commandsummary/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package commandsummary

import (
"os"
"testing"

testsutils "github.com/jfrog/jfrog-client-go/utils/tests"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -53,58 +53,58 @@ func TestAddGitHubTrackingToUrl(t *testing.T) {
tests := []struct {
name string
url string
section string
section summarySection
envValue string
expectedResult string
expectsError bool
}{
{
"No GITHUB_WORKFLOW set",
"https://example.com/path",
"build",
buildInfoSection,
"",
"https://example.com/path",
false,
},
{
"GITHUB_WORKFLOW set",
"https://example.com/path",
"build",
buildInfoSection,
"workflow123",
"https://example.com/path?gh_job_id=workflow123&gh_section=build",
false,
},
{
"Invalid URL",
":invalid-url",
"build",
buildInfoSection,
"workflow123",
"",
true,
},
{
"URL with existing query parameters",
"https://example.com/path?existing_param=value",
"deploy",
packagesSection,
"workflow123",
"https://example.com/path?existing_param=value&gh_job_id=workflow123&gh_section=deploy",
"https://example.com/path?existing_param=value&gh_job_id=workflow123&gh_section=packages",
false,
},
{
"GITHUB_WORKFLOW with special characters",
"https://example.com/path",
"test",
artifactsSection,
"workflow with spaces & special?characters",
"https://example.com/path?gh_job_id=workflow+with+spaces+%26+special%3Fcharacters&gh_section=test",
"https://example.com/path?gh_job_id=workflow+with+spaces+%26+special%3Fcharacters&gh_section=artifacts",
false,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// Set up the environment variable
cleanup := testsutils.SetEnvWithCallbackAndAssert(t, "GITHUB_WORKFLOW", test.envValue)
defer cleanup()
os.Setenv("GITHUB_WORKFLOW", test.envValue)
defer os.Unsetenv("GITHUB_WORKFLOW")

// Call the function
result, err := addGitHubTrackingToUrl(test.url, test.section)
Expand Down

0 comments on commit 3fa3a9d

Please sign in to comment.