You've already forked sap-jenkins-library
							
							
				mirror of
				https://github.com/SAP/jenkins-library.git
				synced 2025-10-30 23:57:50 +02:00 
			
		
		
		
	Allow to configure sap npm registry in mta builder (#1550)
This commit is contained in:
		| @@ -100,9 +100,19 @@ func runMtaBuild(config mtaBuildOptions, | ||||
|  | ||||
| 	var err error | ||||
|  | ||||
| 	handleSettingsFiles(config, p, httpClient) | ||||
| 	err = handleSettingsFiles(config, p, httpClient) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	handleDefaultNpmRegistry(config, e) | ||||
| 	err = configureNpmRegistry(config.DefaultNpmRegistry, "default", "", e) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	err = configureNpmRegistry(config.SapNpmRegistry, "SAP", "@sap", e) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	mtaYamlFile := "mta.yaml" | ||||
| 	mtaYamlFileExists, err := p.FileExists(mtaYamlFile) | ||||
| @@ -299,16 +309,21 @@ func createMtaYamlFile(mtaYamlFile, applicationName string, p piperutils.FileUti | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func handleDefaultNpmRegistry(config mtaBuildOptions, e execRunner) error { | ||||
| func configureNpmRegistry(registryURI string, registryName string, scope string, e execRunner) error { | ||||
| 	if len(registryURI) == 0 { | ||||
| 		log.Entry().Debugf("No %s npm registry provided via configuration. Leaving npm config untouched.", registryName) | ||||
| 		return nil | ||||
| 	} | ||||
|  | ||||
| 	if len(config.DefaultNpmRegistry) > 0 { | ||||
| 	log.Entry().Debugf("Setting %s npm registry to \"%s\"", registryName, registryURI) | ||||
|  | ||||
| 		log.Entry().Debugf("Setting default npm registry to \"%s\"", config.DefaultNpmRegistry) | ||||
| 		if err := e.RunExecutable("npm", "config", "set", "registry", config.DefaultNpmRegistry); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 	} else { | ||||
| 		log.Entry().Debugf("No default npm registry provided via configuration. Leaving npm config untouched.") | ||||
| 	key := "registry" | ||||
| 	if len(scope) > 0 { | ||||
| 		key = fmt.Sprintf("%s:registry", scope) | ||||
| 	} | ||||
|  | ||||
| 	if err := e.RunExecutable("npm", "config", "set", key, registryURI); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
|   | ||||
| @@ -24,6 +24,7 @@ type mtaBuildOptions struct { | ||||
| 	Platform            string `json:"platform,omitempty"` | ||||
| 	ApplicationName     string `json:"applicationName,omitempty"` | ||||
| 	DefaultNpmRegistry  string `json:"defaultNpmRegistry,omitempty"` | ||||
| 	SapNpmRegistry      string `json:"sapNpmRegistry,omitempty"` | ||||
| 	ProjectSettingsFile string `json:"projectSettingsFile,omitempty"` | ||||
| 	GlobalSettingsFile  string `json:"globalSettingsFile,omitempty"` | ||||
| } | ||||
| @@ -117,6 +118,7 @@ func addMtaBuildFlags(cmd *cobra.Command, stepConfig *mtaBuildOptions) { | ||||
| 	cmd.Flags().StringVar(&stepConfig.Platform, "platform", os.Getenv("PIPER_platform"), "mtaBuildTool 'cloudMbt' only: The target platform to which the mtar can be deployed.") | ||||
| 	cmd.Flags().StringVar(&stepConfig.ApplicationName, "applicationName", os.Getenv("PIPER_applicationName"), "The name of the application which is being built. If the parameter has been provided and no `mta.yaml` exists, the `mta.yaml` will be automatically generated using this parameter and the information (`name` and `version`) from 'package.json` before the actual build starts.") | ||||
| 	cmd.Flags().StringVar(&stepConfig.DefaultNpmRegistry, "defaultNpmRegistry", os.Getenv("PIPER_defaultNpmRegistry"), "Url to the npm registry that should be used for installing npm dependencies.") | ||||
| 	cmd.Flags().StringVar(&stepConfig.SapNpmRegistry, "sapNpmRegistry", os.Getenv("PIPER_sapNpmRegistry"), "Url to the sap npm registry that should be used for installing npm dependencies prefixed with @sap.") | ||||
| 	cmd.Flags().StringVar(&stepConfig.ProjectSettingsFile, "projectSettingsFile", os.Getenv("PIPER_projectSettingsFile"), "Path or url to the mvn settings file that should be used as project settings file.") | ||||
| 	cmd.Flags().StringVar(&stepConfig.GlobalSettingsFile, "globalSettingsFile", os.Getenv("PIPER_globalSettingsFile"), "Path or url to the mvn settings file that should be used as global settings file") | ||||
|  | ||||
| @@ -196,6 +198,14 @@ func mtaBuildMetadata() config.StepData { | ||||
| 						Mandatory:   false, | ||||
| 						Aliases:     []config.Alias{}, | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name:        "sapNpmRegistry", | ||||
| 						ResourceRef: []config.ResourceReference{}, | ||||
| 						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"}, | ||||
| 						Type:        "string", | ||||
| 						Mandatory:   false, | ||||
| 						Aliases:     []config.Alias{}, | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name:        "projectSettingsFile", | ||||
| 						ResourceRef: []config.ResourceReference{}, | ||||
|   | ||||
| @@ -52,6 +52,26 @@ func TestMarBuild(t *testing.T) { | ||||
| 		} | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("Provide SAP npm registry", func(t *testing.T) { | ||||
|  | ||||
| 		e := mock.ExecMockRunner{} | ||||
|  | ||||
| 		options := mtaBuildOptions{ApplicationName: "myApp", MtaBuildTool: "classic", BuildTarget: "CF", SapNpmRegistry: "https://example.sap/npm", MtarName: "myName"} | ||||
|  | ||||
| 		existingFiles := make(map[string]string) | ||||
| 		existingFiles["package.json"] = "{\"name\": \"myName\", \"version\": \"1.2.3\"}" | ||||
| 		fileUtils := MtaTestFileUtilsMock{existingFiles: existingFiles} | ||||
|  | ||||
| 		err := runMtaBuild(options, &cpe, &e, &fileUtils, &httpClient) | ||||
|  | ||||
| 		assert.Nil(t, err) | ||||
|  | ||||
| 		if assert.Len(t, e.Calls, 2) { // the second (unchecked) entry is the mta call | ||||
| 			assert.Equal(t, "npm", e.Calls[0].Exec) | ||||
| 			assert.Equal(t, []string{"config", "set", "@sap:registry", "https://example.sap/npm"}, e.Calls[0].Params) | ||||
| 		} | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("Package json does not exist", func(t *testing.T) { | ||||
|  | ||||
| 		e := mock.ExecMockRunner{} | ||||
|   | ||||
| @@ -84,9 +84,18 @@ spec: | ||||
|         type: string | ||||
|         description: "Url to the npm registry that should be used for installing npm dependencies." | ||||
|         scope: | ||||
|         - PARAMETERS | ||||
|         - STAGES | ||||
|         - STEPS | ||||
|           - PARAMETERS | ||||
|           - STAGES | ||||
|           - STEPS | ||||
|         mandatory: false | ||||
|         default: | ||||
|       - name: sapNpmRegistry | ||||
|         type: string | ||||
|         description: "Url to the sap npm registry that should be used for installing npm dependencies prefixed with @sap." | ||||
|         scope: | ||||
|           - PARAMETERS | ||||
|           - STAGES | ||||
|           - STEPS | ||||
|         mandatory: false | ||||
|         default: | ||||
|       - name: projectSettingsFile | ||||
|   | ||||
		Reference in New Issue
	
	Block a user