github.com/jaylevin/jenkins-library@v1.230.4/cmd/githubPublishRelease_generated.go (about)

     1  // Code generated by piper's step-generator. DO NOT EDIT.
     2  
     3  package cmd
     4  
     5  import (
     6  	"fmt"
     7  	"os"
     8  	"time"
     9  
    10  	"github.com/SAP/jenkins-library/pkg/config"
    11  	"github.com/SAP/jenkins-library/pkg/log"
    12  	"github.com/SAP/jenkins-library/pkg/splunk"
    13  	"github.com/SAP/jenkins-library/pkg/telemetry"
    14  	"github.com/SAP/jenkins-library/pkg/validation"
    15  	"github.com/spf13/cobra"
    16  )
    17  
    18  type githubPublishReleaseOptions struct {
    19  	AddClosedIssues       bool     `json:"addClosedIssues,omitempty"`
    20  	AddDeltaToLastRelease bool     `json:"addDeltaToLastRelease,omitempty"`
    21  	APIURL                string   `json:"apiUrl,omitempty"`
    22  	AssetPath             string   `json:"assetPath,omitempty"`
    23  	AssetPathList         []string `json:"assetPathList,omitempty"`
    24  	Commitish             string   `json:"commitish,omitempty"`
    25  	ExcludeLabels         []string `json:"excludeLabels,omitempty"`
    26  	Labels                []string `json:"labels,omitempty"`
    27  	Owner                 string   `json:"owner,omitempty"`
    28  	PreRelease            bool     `json:"preRelease,omitempty"`
    29  	ReleaseBodyHeader     string   `json:"releaseBodyHeader,omitempty"`
    30  	Repository            string   `json:"repository,omitempty"`
    31  	ServerURL             string   `json:"serverUrl,omitempty"`
    32  	TagPrefix             string   `json:"tagPrefix,omitempty"`
    33  	Token                 string   `json:"token,omitempty"`
    34  	UploadURL             string   `json:"uploadUrl,omitempty"`
    35  	Version               string   `json:"version,omitempty"`
    36  }
    37  
    38  // GithubPublishReleaseCommand Publish a release in GitHub
    39  func GithubPublishReleaseCommand() *cobra.Command {
    40  	const STEP_NAME = "githubPublishRelease"
    41  
    42  	metadata := githubPublishReleaseMetadata()
    43  	var stepConfig githubPublishReleaseOptions
    44  	var startTime time.Time
    45  	var logCollector *log.CollectorHook
    46  	var splunkClient *splunk.Splunk
    47  	telemetryClient := &telemetry.Telemetry{}
    48  
    49  	var createGithubPublishReleaseCmd = &cobra.Command{
    50  		Use:   STEP_NAME,
    51  		Short: "Publish a release in GitHub",
    52  		Long: `This step creates a tag in your GitHub repository together with a release.
    53  The release can be filled with text plus additional information like:
    54  
    55  * Closed pull request since last release
    56  * Closed issues since last release
    57  * Link to delta information showing all commits since last release
    58  
    59  The result looks like
    60  
    61  ![Example release](../images/githubRelease.png)`,
    62  		PreRunE: func(cmd *cobra.Command, _ []string) error {
    63  			startTime = time.Now()
    64  			log.SetStepName(STEP_NAME)
    65  			log.SetVerbose(GeneralConfig.Verbose)
    66  
    67  			GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
    68  
    69  			path, _ := os.Getwd()
    70  			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
    71  			log.RegisterHook(fatalHook)
    72  
    73  			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
    74  			if err != nil {
    75  				log.SetErrorCategory(log.ErrorConfiguration)
    76  				return err
    77  			}
    78  			log.RegisterSecret(stepConfig.Token)
    79  
    80  			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
    81  				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
    82  				log.RegisterHook(&sentryHook)
    83  			}
    84  
    85  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    86  				splunkClient = &splunk.Splunk{}
    87  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
    88  				log.RegisterHook(logCollector)
    89  			}
    90  
    91  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
    92  			if err != nil {
    93  				return err
    94  			}
    95  			if err = validation.ValidateStruct(stepConfig); err != nil {
    96  				log.SetErrorCategory(log.ErrorConfiguration)
    97  				return err
    98  			}
    99  
   100  			return nil
   101  		},
   102  		Run: func(_ *cobra.Command, _ []string) {
   103  			stepTelemetryData := telemetry.CustomData{}
   104  			stepTelemetryData.ErrorCode = "1"
   105  			handler := func() {
   106  				config.RemoveVaultSecretFiles()
   107  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
   108  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
   109  				stepTelemetryData.PiperCommitHash = GitCommit
   110  				telemetryClient.SetData(&stepTelemetryData)
   111  				telemetryClient.Send()
   112  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   113  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   114  				}
   115  			}
   116  			log.DeferExitHandler(handler)
   117  			defer handler()
   118  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
   119  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   120  				splunkClient.Initialize(GeneralConfig.CorrelationID,
   121  					GeneralConfig.HookConfig.SplunkConfig.Dsn,
   122  					GeneralConfig.HookConfig.SplunkConfig.Token,
   123  					GeneralConfig.HookConfig.SplunkConfig.Index,
   124  					GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   125  			}
   126  			githubPublishRelease(stepConfig, &stepTelemetryData)
   127  			stepTelemetryData.ErrorCode = "0"
   128  			log.Entry().Info("SUCCESS")
   129  		},
   130  	}
   131  
   132  	addGithubPublishReleaseFlags(createGithubPublishReleaseCmd, &stepConfig)
   133  	return createGithubPublishReleaseCmd
   134  }
   135  
   136  func addGithubPublishReleaseFlags(cmd *cobra.Command, stepConfig *githubPublishReleaseOptions) {
   137  	cmd.Flags().BoolVar(&stepConfig.AddClosedIssues, "addClosedIssues", false, "If set to `true`, closed issues and merged pull-requests since the last release will added below the `releaseBodyHeader`")
   138  	cmd.Flags().BoolVar(&stepConfig.AddDeltaToLastRelease, "addDeltaToLastRelease", false, "If set to `true`, a link will be added to the release information that brings up all commits since the last release.")
   139  	cmd.Flags().StringVar(&stepConfig.APIURL, "apiUrl", `https://api.github.com`, "Set the GitHub API url.")
   140  	cmd.Flags().StringVar(&stepConfig.AssetPath, "assetPath", os.Getenv("PIPER_assetPath"), "Path to a release asset which should be uploaded to the list of release assets.")
   141  	cmd.Flags().StringSliceVar(&stepConfig.AssetPathList, "assetPathList", []string{}, "List of paths to a release asset which should be uploaded to the list of release assets.")
   142  	cmd.Flags().StringVar(&stepConfig.Commitish, "commitish", `master`, "Target git commitish for the release")
   143  	cmd.Flags().StringSliceVar(&stepConfig.ExcludeLabels, "excludeLabels", []string{}, "Allows to exclude issues with dedicated list of labels.")
   144  	cmd.Flags().StringSliceVar(&stepConfig.Labels, "labels", []string{}, "Labels to include in issue search.")
   145  	cmd.Flags().StringVar(&stepConfig.Owner, "owner", os.Getenv("PIPER_owner"), "Name of the GitHub organization.")
   146  	cmd.Flags().BoolVar(&stepConfig.PreRelease, "preRelease", false, "If set to `true` the release will be marked as Pre-release.")
   147  	cmd.Flags().StringVar(&stepConfig.ReleaseBodyHeader, "releaseBodyHeader", os.Getenv("PIPER_releaseBodyHeader"), "Content which will appear for the release.")
   148  	cmd.Flags().StringVar(&stepConfig.Repository, "repository", os.Getenv("PIPER_repository"), "Name of the GitHub repository.")
   149  	cmd.Flags().StringVar(&stepConfig.ServerURL, "serverUrl", `https://github.com`, "GitHub server url for end-user access.")
   150  	cmd.Flags().StringVar(&stepConfig.TagPrefix, "tagPrefix", ``, "Defines a prefix to be added to the tag.")
   151  	cmd.Flags().StringVar(&stepConfig.Token, "token", os.Getenv("PIPER_token"), "GitHub personal access token as per https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line")
   152  	cmd.Flags().StringVar(&stepConfig.UploadURL, "uploadUrl", `https://uploads.github.com`, "Set the GitHub API url.")
   153  	cmd.Flags().StringVar(&stepConfig.Version, "version", os.Getenv("PIPER_version"), "Define the version number which will be written as tag as well as release name.")
   154  
   155  	cmd.MarkFlagRequired("apiUrl")
   156  	cmd.MarkFlagRequired("owner")
   157  	cmd.MarkFlagRequired("repository")
   158  	cmd.MarkFlagRequired("serverUrl")
   159  	cmd.MarkFlagRequired("token")
   160  	cmd.MarkFlagRequired("uploadUrl")
   161  	cmd.MarkFlagRequired("version")
   162  }
   163  
   164  // retrieve step metadata
   165  func githubPublishReleaseMetadata() config.StepData {
   166  	var theMetaData = config.StepData{
   167  		Metadata: config.StepMetadata{
   168  			Name:        "githubPublishRelease",
   169  			Aliases:     []config.Alias{},
   170  			Description: "Publish a release in GitHub",
   171  		},
   172  		Spec: config.StepSpec{
   173  			Inputs: config.StepInputs{
   174  				Secrets: []config.StepSecrets{
   175  					{Name: "githubTokenCredentialsId", Description: "Jenkins 'Secret text' credentials ID containing token to authenticate to GitHub.", Type: "jenkins"},
   176  				},
   177  				Parameters: []config.StepParameters{
   178  					{
   179  						Name:        "addClosedIssues",
   180  						ResourceRef: []config.ResourceReference{},
   181  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   182  						Type:        "bool",
   183  						Mandatory:   false,
   184  						Aliases:     []config.Alias{},
   185  						Default:     false,
   186  					},
   187  					{
   188  						Name:        "addDeltaToLastRelease",
   189  						ResourceRef: []config.ResourceReference{},
   190  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   191  						Type:        "bool",
   192  						Mandatory:   false,
   193  						Aliases:     []config.Alias{},
   194  						Default:     false,
   195  					},
   196  					{
   197  						Name:        "apiUrl",
   198  						ResourceRef: []config.ResourceReference{},
   199  						Scope:       []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"},
   200  						Type:        "string",
   201  						Mandatory:   true,
   202  						Aliases:     []config.Alias{{Name: "githubApiUrl"}},
   203  						Default:     `https://api.github.com`,
   204  					},
   205  					{
   206  						Name:        "assetPath",
   207  						ResourceRef: []config.ResourceReference{},
   208  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   209  						Type:        "string",
   210  						Mandatory:   false,
   211  						Aliases:     []config.Alias{},
   212  						Default:     os.Getenv("PIPER_assetPath"),
   213  					},
   214  					{
   215  						Name:        "assetPathList",
   216  						ResourceRef: []config.ResourceReference{},
   217  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   218  						Type:        "[]string",
   219  						Mandatory:   false,
   220  						Aliases:     []config.Alias{},
   221  						Default:     []string{},
   222  					},
   223  					{
   224  						Name:        "commitish",
   225  						ResourceRef: []config.ResourceReference{},
   226  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   227  						Type:        "string",
   228  						Mandatory:   false,
   229  						Aliases:     []config.Alias{},
   230  						Default:     `master`,
   231  					},
   232  					{
   233  						Name:        "excludeLabels",
   234  						ResourceRef: []config.ResourceReference{},
   235  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   236  						Type:        "[]string",
   237  						Mandatory:   false,
   238  						Aliases:     []config.Alias{},
   239  						Default:     []string{},
   240  					},
   241  					{
   242  						Name:        "labels",
   243  						ResourceRef: []config.ResourceReference{},
   244  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   245  						Type:        "[]string",
   246  						Mandatory:   false,
   247  						Aliases:     []config.Alias{},
   248  						Default:     []string{},
   249  					},
   250  					{
   251  						Name: "owner",
   252  						ResourceRef: []config.ResourceReference{
   253  							{
   254  								Name:  "commonPipelineEnvironment",
   255  								Param: "github/owner",
   256  							},
   257  						},
   258  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   259  						Type:      "string",
   260  						Mandatory: true,
   261  						Aliases:   []config.Alias{{Name: "githubOrg"}},
   262  						Default:   os.Getenv("PIPER_owner"),
   263  					},
   264  					{
   265  						Name:        "preRelease",
   266  						ResourceRef: []config.ResourceReference{},
   267  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   268  						Type:        "bool",
   269  						Mandatory:   false,
   270  						Aliases:     []config.Alias{},
   271  						Default:     false,
   272  					},
   273  					{
   274  						Name:        "releaseBodyHeader",
   275  						ResourceRef: []config.ResourceReference{},
   276  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   277  						Type:        "string",
   278  						Mandatory:   false,
   279  						Aliases:     []config.Alias{},
   280  						Default:     os.Getenv("PIPER_releaseBodyHeader"),
   281  					},
   282  					{
   283  						Name: "repository",
   284  						ResourceRef: []config.ResourceReference{
   285  							{
   286  								Name:  "commonPipelineEnvironment",
   287  								Param: "github/repository",
   288  							},
   289  						},
   290  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   291  						Type:      "string",
   292  						Mandatory: true,
   293  						Aliases:   []config.Alias{{Name: "githubRepo"}},
   294  						Default:   os.Getenv("PIPER_repository"),
   295  					},
   296  					{
   297  						Name:        "serverUrl",
   298  						ResourceRef: []config.ResourceReference{},
   299  						Scope:       []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"},
   300  						Type:        "string",
   301  						Mandatory:   true,
   302  						Aliases:     []config.Alias{{Name: "githubServerUrl"}},
   303  						Default:     `https://github.com`,
   304  					},
   305  					{
   306  						Name:        "tagPrefix",
   307  						ResourceRef: []config.ResourceReference{},
   308  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   309  						Type:        "string",
   310  						Mandatory:   false,
   311  						Aliases:     []config.Alias{},
   312  						Default:     ``,
   313  					},
   314  					{
   315  						Name: "token",
   316  						ResourceRef: []config.ResourceReference{
   317  							{
   318  								Name: "githubTokenCredentialsId",
   319  								Type: "secret",
   320  							},
   321  
   322  							{
   323  								Name:    "githubVaultSecretName",
   324  								Type:    "vaultSecret",
   325  								Default: "github",
   326  							},
   327  						},
   328  						Scope:     []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"},
   329  						Type:      "string",
   330  						Mandatory: true,
   331  						Aliases:   []config.Alias{{Name: "githubToken"}, {Name: "access_token"}},
   332  						Default:   os.Getenv("PIPER_token"),
   333  					},
   334  					{
   335  						Name:        "uploadUrl",
   336  						ResourceRef: []config.ResourceReference{},
   337  						Scope:       []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"},
   338  						Type:        "string",
   339  						Mandatory:   true,
   340  						Aliases:     []config.Alias{{Name: "githubUploadUrl"}},
   341  						Default:     `https://uploads.github.com`,
   342  					},
   343  					{
   344  						Name: "version",
   345  						ResourceRef: []config.ResourceReference{
   346  							{
   347  								Name:  "commonPipelineEnvironment",
   348  								Param: "artifactVersion",
   349  							},
   350  						},
   351  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   352  						Type:      "string",
   353  						Mandatory: true,
   354  						Aliases:   []config.Alias{},
   355  						Default:   os.Getenv("PIPER_version"),
   356  					},
   357  				},
   358  			},
   359  		},
   360  	}
   361  	return theMetaData
   362  }