Skip to content

Commit

Permalink
Exposing build artifact metadata from maven and npm (#5008)
Browse files Browse the repository at this point in the history
  • Loading branch information
anilkeshav27 authored Aug 27, 2024
1 parent 70d2abf commit 238339c
Show file tree
Hide file tree
Showing 15 changed files with 186 additions and 25 deletions.
43 changes: 42 additions & 1 deletion cmd/mavenBuild.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package cmd

import (
"encoding/json"
"os"
"path"
"path/filepath"
"reflect"
"strings"

"github.com/SAP/jenkins-library/pkg/build"
"github.com/SAP/jenkins-library/pkg/buildsettings"
"github.com/SAP/jenkins-library/pkg/command"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/maven"
"github.com/SAP/jenkins-library/pkg/piperutils"
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/SAP/jenkins-library/pkg/versioning"
"github.com/pkg/errors"

piperhttp "github.com/SAP/jenkins-library/pkg/http"
Expand Down Expand Up @@ -159,7 +162,45 @@ func runMavenBuild(config *mavenBuildOptions, telemetryData *telemetry.CustomDat
mavenOptions.Goals = []string{"deploy"}
mavenOptions.Defines = []string{}
_, err := maven.Execute(&mavenOptions, utils)
return err
if err != nil {
return err
}
if config.CreateBuildArtifactsMetadata {
buildCoordinates := []versioning.Coordinates{}
options := versioning.Options{}
var utils versioning.Utils

matches, _ := fileUtils.Glob("**/pom.xml")
for _, match := range matches {

artifact, err := versioning.GetArtifact("maven", match, &options, utils)
if err != nil {
log.Entry().Warnf("unable to get artifact metdata : %v", err)
} else {
coordinate, err := artifact.GetCoordinates()
if err != nil {
log.Entry().Warnf("unable to get artifact coordinates : %v", err)
} else {
coordinate.BuildPath = filepath.Dir(match)
coordinate.URL = config.AltDeploymentRepositoryURL
buildCoordinates = append(buildCoordinates, coordinate)
}
}
}

if len(buildCoordinates) == 0 {
log.Entry().Warnf("unable to identify artifact coordinates for the maven packages published")
return nil
}

var buildArtifacts build.BuildArtifacts

buildArtifacts.Coordinates = buildCoordinates
jsonResult, _ := json.Marshal(buildArtifacts)
commonPipelineEnvironment.custom.mavenBuildArtifacts = string(jsonResult)
}

return nil
} else {
log.Entry().Infof("publish not detected, ignoring maven deploy")
}
Expand Down
16 changes: 15 additions & 1 deletion cmd/mavenBuild_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions cmd/mavenBuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,25 @@ func TestMavenBuild(t *testing.T) {
assert.Contains(t, mockedUtils.Calls[0].Params, "profile1,profile2")
})

t.Run("mavenBuild should not create build artifacts metadata when CreateBuildArtifactsMetadata is false and Publish is true", func(t *testing.T) {
mockedUtils := newMavenMockUtils()
mockedUtils.AddFile("pom.xml", []byte{})
config := mavenBuildOptions{CreateBuildArtifactsMetadata: false, Publish: true}
err := runMavenBuild(&config, nil, &mockedUtils, &cpe)
assert.Nil(t, err)
assert.Equal(t, mockedUtils.Calls[0].Exec, "mvn")
assert.Contains(t, mockedUtils.Calls[0].Params, "install")
assert.Empty(t, cpe.custom.mavenBuildArtifacts)
})

t.Run("mavenBuild should not create build artifacts metadata when CreateBuildArtifactsMetadata is true and Publish is false", func(t *testing.T) {
mockedUtils := newMavenMockUtils()
mockedUtils.AddFile("pom.xml", []byte{})
config := mavenBuildOptions{CreateBuildArtifactsMetadata: true, Publish: false}
err := runMavenBuild(&config, nil, &mockedUtils, &cpe)
assert.Nil(t, err)
assert.Equal(t, mockedUtils.Calls[0].Exec, "mvn")
assert.Empty(t, cpe.custom.mavenBuildArtifacts)
})

}
22 changes: 20 additions & 2 deletions cmd/npmExecuteScripts.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package cmd

import (
"encoding/json"
"os"

"github.com/SAP/jenkins-library/pkg/build"
"github.com/SAP/jenkins-library/pkg/buildsettings"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/npm"
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/SAP/jenkins-library/pkg/versioning"
)

func npmExecuteScripts(config npmExecuteScriptsOptions, telemetryData *telemetry.CustomData, commonPipelineEnvironment *npmExecuteScriptsCommonPipelineEnvironment) {
Expand Down Expand Up @@ -85,9 +88,11 @@ func runNpmExecuteScripts(npmExecutor npm.Executor, config *npmExecuteScriptsOpt
}
commonPipelineEnvironment.custom.buildSettingsInfo = buildSettingsInfo

buildCoordinates := []versioning.Coordinates{}

if config.Publish {
if len(config.BuildDescriptorList) > 0 {
err = npmExecutor.PublishAllPackages(config.BuildDescriptorList, config.RepositoryURL, config.RepositoryUsername, config.RepositoryPassword, config.PackBeforePublish)
err = npmExecutor.PublishAllPackages(config.BuildDescriptorList, config.RepositoryURL, config.RepositoryUsername, config.RepositoryPassword, config.PackBeforePublish, &buildCoordinates)
if err != nil {
return err
}
Expand All @@ -97,12 +102,25 @@ func runNpmExecuteScripts(npmExecutor npm.Executor, config *npmExecuteScriptsOpt
return err
}

err = npmExecutor.PublishAllPackages(packageJSONFiles, config.RepositoryURL, config.RepositoryUsername, config.RepositoryPassword, config.PackBeforePublish)
err = npmExecutor.PublishAllPackages(packageJSONFiles, config.RepositoryURL, config.RepositoryUsername, config.RepositoryPassword, config.PackBeforePublish, &buildCoordinates)
if err != nil {
return err
}
}
}

if config.CreateBuildArtifactsMetadata {
if len(buildCoordinates) == 0 {
log.Entry().Warnf("unable to identify artifact coordinates for the npm packages published")
return nil
}

var buildArtifacts build.BuildArtifacts

buildArtifacts.Coordinates = buildCoordinates
jsonResult, _ := json.Marshal(buildArtifacts)
commonPipelineEnvironment.custom.npmBuildArtifacts = string(jsonResult)
}

return nil
}
44 changes: 29 additions & 15 deletions cmd/npmExecuteScripts_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cmd/npmExecuteScripts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,5 @@ func TestNpmExecuteScripts(t *testing.T) {
v := os.Getenv("NODE_ENV")
assert.Equal(t, "production", v)
})

}
7 changes: 7 additions & 0 deletions pkg/build/artifact.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package build

import "github.com/SAP/jenkins-library/pkg/versioning"

type BuildArtifacts struct {
Coordinates []versioning.Coordinates
}
3 changes: 2 additions & 1 deletion pkg/npm/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package npm

import (
"fmt"
"github.com/SAP/jenkins-library/pkg/versioning"

"github.com/SAP/jenkins-library/pkg/mock"
)
Expand Down Expand Up @@ -123,6 +124,6 @@ func (n *NpmExecutorMock) CreateBOM(packageJSONFiles []string) error {
}

// CreateBOM mock implementation
func (n *NpmExecutorMock) PublishAllPackages(packageJSONFiles []string, registry, username, password string, packBeforePublish bool) error {
func (n *NpmExecutorMock) PublishAllPackages(packageJSONFiles []string, registry, username, password string, packBeforePublish bool, buildCoordinates *[]versioning.Coordinates) error {
return nil
}
3 changes: 2 additions & 1 deletion pkg/npm/npm.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/SAP/jenkins-library/pkg/command"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/piperutils"
"github.com/SAP/jenkins-library/pkg/versioning"
)

const (
Expand All @@ -34,7 +35,7 @@ type Executor interface {
FindPackageJSONFilesWithScript(packageJSONFiles []string, script string) ([]string, error)
RunScriptsInAllPackages(runScripts []string, runOptions []string, scriptOptions []string, virtualFrameBuffer bool, excludeList []string, packagesList []string) error
InstallAllDependencies(packageJSONFiles []string) error
PublishAllPackages(packageJSONFiles []string, registry, username, password string, packBeforePublish bool) error
PublishAllPackages(packageJSONFiles []string, registry, username, password string, packBeforePublish bool, buildCoordinates *[]versioning.Coordinates) error
SetNpmRegistries() error
CreateBOM(packageJSONFiles []string) error
}
Expand Down
26 changes: 23 additions & 3 deletions pkg/npm/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/SAP/jenkins-library/pkg/log"
CredentialUtils "github.com/SAP/jenkins-library/pkg/piperutils"
"github.com/SAP/jenkins-library/pkg/versioning"
)

type npmMinimalPackageDescriptor struct {
Expand All @@ -31,7 +32,7 @@ func (pd *npmMinimalPackageDescriptor) Scope() string {
}

// PublishAllPackages executes npm publish for all package.json files defined in packageJSONFiles list
func (exec *Execute) PublishAllPackages(packageJSONFiles []string, registry, username, password string, packBeforePublish bool) error {
func (exec *Execute) PublishAllPackages(packageJSONFiles []string, registry, username, password string, packBeforePublish bool, buildCoordinates *[]versioning.Coordinates) error {
for _, packageJSON := range packageJSONFiles {
log.Entry().Infof("triggering publish for %s", packageJSON)

Expand All @@ -43,7 +44,7 @@ func (exec *Execute) PublishAllPackages(packageJSONFiles []string, registry, use
return fmt.Errorf("package.json file '%s' not found: %w", packageJSON, err)
}

err = exec.publish(packageJSON, registry, username, password, packBeforePublish)
err = exec.publish(packageJSON, registry, username, password, packBeforePublish, buildCoordinates)
if err != nil {
return err
}
Expand All @@ -52,7 +53,7 @@ func (exec *Execute) PublishAllPackages(packageJSONFiles []string, registry, use
}

// publish executes npm publish for package.json
func (exec *Execute) publish(packageJSON, registry, username, password string, packBeforePublish bool) error {
func (exec *Execute) publish(packageJSON, registry, username, password string, packBeforePublish bool, buildCoordinates *[]versioning.Coordinates) error {
execRunner := exec.Utils.GetExecRunner()

oldWorkingDirectory, err := exec.Utils.Getwd()
Expand Down Expand Up @@ -202,6 +203,25 @@ func (exec *Execute) publish(packageJSON, registry, username, password string, p
}
}

options := versioning.Options{}
var utils versioning.Utils

artifact, err := versioning.GetArtifact("npm", packageJSON, &options, utils)
if err != nil {
log.Entry().Warnf("unable to get artifact metdata : %v", err)
} else {
coordinate, err := artifact.GetCoordinates()
if err != nil {
log.Entry().Warnf("unable to get artifact coordinates : %v", err)
} else {
coordinate.BuildPath = filepath.Dir(packageJSON)
coordinate.URL = registry
coordinate.Packaging = "tgz"

*buildCoordinates = append(*buildCoordinates, coordinate)
}
}

return nil
}

Expand Down
Loading

0 comments on commit 238339c

Please sign in to comment.