2020-02-21 13:56:09 +02:00
// Code generated by piper's step-generator. DO NOT EDIT.
2019-12-16 18:34:12 +02:00
package cmd
import (
2020-01-31 15:06:08 +02:00
"fmt"
2019-12-16 18:34:12 +02:00
"os"
2020-01-31 15:06:08 +02:00
"time"
2019-12-16 18:34:12 +02:00
"github.com/SAP/jenkins-library/pkg/config"
"github.com/SAP/jenkins-library/pkg/log"
2020-01-29 14:17:54 +02:00
"github.com/SAP/jenkins-library/pkg/telemetry"
2019-12-16 18:34:12 +02:00
"github.com/spf13/cobra"
)
type githubCreatePullRequestOptions struct {
Assignees [ ] string ` json:"assignees,omitempty" `
Base string ` json:"base,omitempty" `
Body string ` json:"body,omitempty" `
APIURL string ` json:"apiUrl,omitempty" `
Head string ` json:"head,omitempty" `
Owner string ` json:"owner,omitempty" `
Repository string ` json:"repository,omitempty" `
ServerURL string ` json:"serverUrl,omitempty" `
Title string ` json:"title,omitempty" `
Token string ` json:"token,omitempty" `
Labels [ ] string ` json:"labels,omitempty" `
}
// GithubCreatePullRequestCommand Create a pull request on GitHub
func GithubCreatePullRequestCommand ( ) * cobra . Command {
2020-04-28 07:42:02 +02:00
const STEP_NAME = "githubCreatePullRequest"
2019-12-16 18:34:12 +02:00
metadata := githubCreatePullRequestMetadata ( )
2020-02-04 11:46:43 +02:00
var stepConfig githubCreatePullRequestOptions
2020-01-31 15:06:08 +02:00
var startTime time . Time
2020-01-15 13:16:25 +02:00
2019-12-16 18:34:12 +02:00
var createGithubCreatePullRequestCmd = & cobra . Command {
2020-04-28 07:42:02 +02:00
Use : STEP_NAME ,
2019-12-16 18:34:12 +02:00
Short : "Create a pull request on GitHub" ,
Long : ` This step allows you to create a pull request on Github .
It can for example be used for GitOps scenarios or for scenarios where you want to have a manual confirmation step which is delegated to a GitHub pull request . ` ,
2020-06-23 18:05:21 +02:00
PreRunE : func ( cmd * cobra . Command , _ [ ] string ) error {
2020-01-31 15:06:08 +02:00
startTime = time . Now ( )
2020-04-28 07:42:02 +02:00
log . SetStepName ( STEP_NAME )
2019-12-16 18:34:12 +02:00
log . SetVerbose ( GeneralConfig . Verbose )
2020-04-28 07:42:02 +02:00
path , _ := os . Getwd ( )
fatalHook := & log . FatalHook { CorrelationID : GeneralConfig . CorrelationID , Path : path }
log . RegisterHook ( fatalHook )
err := PrepareConfig ( cmd , & metadata , STEP_NAME , & stepConfig , config . OpenPiperFile )
2020-04-16 14:37:45 +02:00
if err != nil {
2020-06-24 10:04:05 +02:00
log . SetErrorCategory ( log . ErrorConfiguration )
2020-04-16 14:37:45 +02:00
return err
}
log . RegisterSecret ( stepConfig . Token )
2020-05-05 08:36:24 +02:00
if len ( GeneralConfig . HookConfig . SentryConfig . Dsn ) > 0 {
sentryHook := log . NewSentryHook ( GeneralConfig . HookConfig . SentryConfig . Dsn , GeneralConfig . CorrelationID )
log . RegisterHook ( & sentryHook )
}
2020-04-16 14:37:45 +02:00
return nil
2019-12-16 18:34:12 +02:00
} ,
2020-06-23 18:05:21 +02:00
Run : func ( _ * cobra . Command , _ [ ] string ) {
2020-01-31 15:06:08 +02:00
telemetryData := telemetry . CustomData { }
telemetryData . ErrorCode = "1"
handler := func ( ) {
2020-10-26 15:20:04 +02:00
config . RemoveVaultSecretFiles ( )
2020-01-31 15:06:08 +02:00
telemetryData . Duration = fmt . Sprintf ( "%v" , time . Since ( startTime ) . Milliseconds ( ) )
2020-09-29 13:49:40 +02:00
telemetryData . ErrorCategory = log . GetErrorCategory ( ) . String ( )
2020-01-31 15:06:08 +02:00
telemetry . Send ( & telemetryData )
}
log . DeferExitHandler ( handler )
defer handler ( )
2020-04-28 07:42:02 +02:00
telemetry . Initialize ( GeneralConfig . NoTelemetry , STEP_NAME )
2020-02-04 11:46:43 +02:00
githubCreatePullRequest ( stepConfig , & telemetryData )
2020-01-31 15:06:08 +02:00
telemetryData . ErrorCode = "0"
2020-05-18 21:52:54 +02:00
log . Entry ( ) . Info ( "SUCCESS" )
2019-12-16 18:34:12 +02:00
} ,
}
2020-02-04 11:46:43 +02:00
addGithubCreatePullRequestFlags ( createGithubCreatePullRequestCmd , & stepConfig )
2019-12-16 18:34:12 +02:00
return createGithubCreatePullRequestCmd
}
2020-02-04 11:46:43 +02:00
func addGithubCreatePullRequestFlags ( cmd * cobra . Command , stepConfig * githubCreatePullRequestOptions ) {
cmd . Flags ( ) . StringSliceVar ( & stepConfig . Assignees , "assignees" , [ ] string { } , "Login names of users to which the PR should be assigned to." )
cmd . Flags ( ) . StringVar ( & stepConfig . Base , "base" , os . Getenv ( "PIPER_base" ) , "The name of the branch you want the changes pulled into." )
cmd . Flags ( ) . StringVar ( & stepConfig . Body , "body" , os . Getenv ( "PIPER_body" ) , "The description text of the pull request in markdown format." )
2020-05-25 19:48:59 +02:00
cmd . Flags ( ) . StringVar ( & stepConfig . APIURL , "apiUrl" , ` https://api.github.com ` , "Set the GitHub API url." )
2020-02-04 11:46:43 +02:00
cmd . Flags ( ) . StringVar ( & stepConfig . Head , "head" , os . Getenv ( "PIPER_head" ) , "The name of the branch where your changes are implemented." )
2020-09-14 12:05:12 +02:00
cmd . Flags ( ) . StringVar ( & stepConfig . Owner , "owner" , os . Getenv ( "PIPER_owner" ) , "Name of the GitHub organization." )
cmd . Flags ( ) . StringVar ( & stepConfig . Repository , "repository" , os . Getenv ( "PIPER_repository" ) , "Name of the GitHub repository." )
2020-05-25 19:48:59 +02:00
cmd . Flags ( ) . StringVar ( & stepConfig . ServerURL , "serverUrl" , ` https://github.com ` , "GitHub server url for end-user access." )
2020-02-04 11:46:43 +02:00
cmd . Flags ( ) . StringVar ( & stepConfig . Title , "title" , os . Getenv ( "PIPER_title" ) , "Title of the pull request." )
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" )
cmd . Flags ( ) . StringSliceVar ( & stepConfig . Labels , "labels" , [ ] string { } , "Labels to be added to the pull request." )
2019-12-16 18:34:12 +02:00
cmd . MarkFlagRequired ( "base" )
cmd . MarkFlagRequired ( "body" )
cmd . MarkFlagRequired ( "apiUrl" )
cmd . MarkFlagRequired ( "head" )
cmd . MarkFlagRequired ( "owner" )
cmd . MarkFlagRequired ( "repository" )
cmd . MarkFlagRequired ( "serverUrl" )
cmd . MarkFlagRequired ( "title" )
cmd . MarkFlagRequired ( "token" )
}
// retrieve step metadata
func githubCreatePullRequestMetadata ( ) config . StepData {
var theMetaData = config . StepData {
2020-03-19 18:24:35 +02:00
Metadata : config . StepMetadata {
Name : "githubCreatePullRequest" ,
Aliases : [ ] config . Alias { } ,
} ,
2019-12-16 18:34:12 +02:00
Spec : config . StepSpec {
Inputs : config . StepInputs {
Parameters : [ ] config . StepParameters {
{
2020-01-15 13:16:25 +02:00
Name : "assignees" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "[]string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
2019-12-16 18:34:12 +02:00
} ,
{
2020-01-15 13:16:25 +02:00
Name : "base" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : true ,
Aliases : [ ] config . Alias { } ,
2019-12-16 18:34:12 +02:00
} ,
{
2020-01-15 13:16:25 +02:00
Name : "body" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : true ,
Aliases : [ ] config . Alias { } ,
2019-12-16 18:34:12 +02:00
} ,
{
2020-01-15 13:16:25 +02:00
Name : "apiUrl" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "GENERAL" , "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : true ,
Aliases : [ ] config . Alias { { Name : "githubApiUrl" } } ,
2019-12-16 18:34:12 +02:00
} ,
{
2020-01-15 13:16:25 +02:00
Name : "head" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : true ,
Aliases : [ ] config . Alias { } ,
2019-12-16 18:34:12 +02:00
} ,
{
2020-09-16 14:50:09 +02:00
Name : "owner" ,
ResourceRef : [ ] config . ResourceReference {
{
Name : "commonPipelineEnvironment" ,
Param : "github/owner" ,
} ,
} ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : true ,
Aliases : [ ] config . Alias { { Name : "githubOrg" } } ,
2019-12-16 18:34:12 +02:00
} ,
{
2020-09-16 14:50:09 +02:00
Name : "repository" ,
ResourceRef : [ ] config . ResourceReference {
{
Name : "commonPipelineEnvironment" ,
Param : "github/repository" ,
} ,
} ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : true ,
Aliases : [ ] config . Alias { { Name : "githubRepo" } } ,
2019-12-16 18:34:12 +02:00
} ,
{
2020-01-15 13:16:25 +02:00
Name : "serverUrl" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "GENERAL" , "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : true ,
Aliases : [ ] config . Alias { { Name : "githubServerUrl" } } ,
2019-12-16 18:34:12 +02:00
} ,
{
2020-01-15 13:16:25 +02:00
Name : "title" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : true ,
Aliases : [ ] config . Alias { } ,
2019-12-16 18:34:12 +02:00
} ,
{
2020-09-16 14:50:09 +02:00
Name : "token" ,
ResourceRef : [ ] config . ResourceReference {
{
Name : "githubTokenCredentialsId" ,
Type : "secret" ,
} ,
} ,
Scope : [ ] string { "GENERAL" , "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : true ,
Aliases : [ ] config . Alias { { Name : "githubToken" } } ,
2019-12-16 18:34:12 +02:00
} ,
{
2020-01-15 13:16:25 +02:00
Name : "labels" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "[]string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
2019-12-16 18:34:12 +02:00
} ,
} ,
} ,
} ,
}
return theMetaData
}