2020-12-21 18:13:16 +02:00
// Code generated by piper's step-generator. DO NOT EDIT.
package cmd
import (
"fmt"
"os"
"time"
"github.com/SAP/jenkins-library/pkg/config"
"github.com/SAP/jenkins-library/pkg/log"
2021-05-17 12:14:04 +02:00
"github.com/SAP/jenkins-library/pkg/splunk"
2020-12-21 18:13:16 +02:00
"github.com/SAP/jenkins-library/pkg/telemetry"
2021-10-01 12:49:05 +02:00
"github.com/SAP/jenkins-library/pkg/validation"
2020-12-21 18:13:16 +02:00
"github.com/spf13/cobra"
)
type githubCreateIssueOptions struct {
2021-10-04 12:33:40 +02:00
APIURL string ` json:"apiUrl,omitempty" `
Assignees [ ] string ` json:"assignees,omitempty" `
Body string ` json:"body,omitempty" `
BodyFilePath string ` json:"bodyFilePath,omitempty" `
Owner string ` json:"owner,omitempty" `
Repository string ` json:"repository,omitempty" `
Title string ` json:"title,omitempty" `
Token string ` json:"token,omitempty" `
2020-12-21 18:13:16 +02:00
}
// GithubCreateIssueCommand Create a new GitHub issue.
func GithubCreateIssueCommand ( ) * cobra . Command {
const STEP_NAME = "githubCreateIssue"
metadata := githubCreateIssueMetadata ( )
var stepConfig githubCreateIssueOptions
var startTime time . Time
2021-05-17 12:14:04 +02:00
var logCollector * log . CollectorHook
2020-12-21 18:13:16 +02:00
var createGithubCreateIssueCmd = & cobra . Command {
Use : STEP_NAME ,
Short : "Create a new GitHub issue." ,
Long : ` This step allows you to create a new GitHub issue .
You will be able to use this step for example for regular jobs to report into your repository in case of new security findings . ` ,
PreRunE : func ( cmd * cobra . Command , _ [ ] string ) error {
startTime = time . Now ( )
log . SetStepName ( STEP_NAME )
log . SetVerbose ( GeneralConfig . Verbose )
2021-07-08 15:26:07 +02:00
GeneralConfig . GitHubAccessTokens = ResolveAccessTokens ( GeneralConfig . GitHubTokens )
2020-12-21 18:13:16 +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 )
if err != nil {
log . SetErrorCategory ( log . ErrorConfiguration )
return err
}
log . RegisterSecret ( stepConfig . Token )
if len ( GeneralConfig . HookConfig . SentryConfig . Dsn ) > 0 {
sentryHook := log . NewSentryHook ( GeneralConfig . HookConfig . SentryConfig . Dsn , GeneralConfig . CorrelationID )
log . RegisterHook ( & sentryHook )
}
2021-05-17 12:14:04 +02:00
if len ( GeneralConfig . HookConfig . SplunkConfig . Dsn ) > 0 {
logCollector = & log . CollectorHook { CorrelationID : GeneralConfig . CorrelationID }
log . RegisterHook ( logCollector )
}
2021-10-01 12:49:05 +02:00
validation , err := validation . New ( validation . WithJSONNamesForStructFields ( ) , validation . WithPredefinedErrorMessages ( ) )
if err != nil {
return err
}
if err = validation . ValidateStruct ( stepConfig ) ; err != nil {
log . SetErrorCategory ( log . ErrorConfiguration )
return err
}
2020-12-21 18:13:16 +02:00
return nil
} ,
Run : func ( _ * cobra . Command , _ [ ] string ) {
telemetryData := telemetry . CustomData { }
telemetryData . ErrorCode = "1"
handler := func ( ) {
config . RemoveVaultSecretFiles ( )
telemetryData . Duration = fmt . Sprintf ( "%v" , time . Since ( startTime ) . Milliseconds ( ) )
telemetryData . ErrorCategory = log . GetErrorCategory ( ) . String ( )
telemetry . Send ( & telemetryData )
2021-05-17 12:14:04 +02:00
if len ( GeneralConfig . HookConfig . SplunkConfig . Dsn ) > 0 {
splunk . Send ( & telemetryData , logCollector )
}
2020-12-21 18:13:16 +02:00
}
log . DeferExitHandler ( handler )
defer handler ( )
telemetry . Initialize ( GeneralConfig . NoTelemetry , STEP_NAME )
2021-05-17 12:14:04 +02:00
if len ( GeneralConfig . HookConfig . SplunkConfig . Dsn ) > 0 {
splunk . Initialize ( GeneralConfig . CorrelationID ,
GeneralConfig . HookConfig . SplunkConfig . Dsn ,
GeneralConfig . HookConfig . SplunkConfig . Token ,
GeneralConfig . HookConfig . SplunkConfig . Index ,
GeneralConfig . HookConfig . SplunkConfig . SendLogs )
}
2020-12-21 18:13:16 +02:00
githubCreateIssue ( stepConfig , & telemetryData )
telemetryData . ErrorCode = "0"
log . Entry ( ) . Info ( "SUCCESS" )
} ,
}
addGithubCreateIssueFlags ( createGithubCreateIssueCmd , & stepConfig )
return createGithubCreateIssueCmd
}
func addGithubCreateIssueFlags ( cmd * cobra . Command , stepConfig * githubCreateIssueOptions ) {
cmd . Flags ( ) . StringVar ( & stepConfig . APIURL , "apiUrl" , ` https://api.github.com ` , "Set the GitHub API url." )
2021-10-04 12:33:40 +02:00
cmd . Flags ( ) . StringSliceVar ( & stepConfig . Assignees , "assignees" , [ ] string { ` ` } , "Defines the assignees for the Issue." )
2020-12-21 18:13:16 +02:00
cmd . Flags ( ) . StringVar ( & stepConfig . Body , "body" , os . Getenv ( "PIPER_body" ) , "Defines the content of the issue, e.g. using markdown syntax." )
2021-03-19 14:04:30 +02:00
cmd . Flags ( ) . StringVar ( & stepConfig . BodyFilePath , "bodyFilePath" , os . Getenv ( "PIPER_bodyFilePath" ) , "Defines the path to a file containing the markdown content for the issue. This can be used instead of [`body`](#body)" )
2020-12-21 18:13:16 +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." )
cmd . Flags ( ) . StringVar ( & stepConfig . Title , "title" , os . Getenv ( "PIPER_title" ) , "Defines the title for the Issue." )
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 . MarkFlagRequired ( "apiUrl" )
cmd . MarkFlagRequired ( "owner" )
cmd . MarkFlagRequired ( "repository" )
cmd . MarkFlagRequired ( "title" )
cmd . MarkFlagRequired ( "token" )
}
// retrieve step metadata
func githubCreateIssueMetadata ( ) config . StepData {
var theMetaData = config . StepData {
Metadata : config . StepMetadata {
Name : "githubCreateIssue" ,
Aliases : [ ] config . Alias { } ,
Description : "Create a new GitHub issue." ,
} ,
Spec : config . StepSpec {
Inputs : config . StepInputs {
2021-06-16 08:43:30 +02:00
Secrets : [ ] config . StepSecrets {
{ Name : "githubTokenCredentialsId" , Description : "Jenkins 'Secret text' credentials ID containing token to authenticate to GitHub." , Type : "jenkins" } ,
} ,
2020-12-21 18:13:16 +02:00
Parameters : [ ] config . StepParameters {
{
Name : "apiUrl" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "GENERAL" , "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : true ,
Aliases : [ ] config . Alias { { Name : "githubApiUrl" } } ,
2021-06-16 08:43:30 +02:00
Default : ` https://api.github.com ` ,
2020-12-21 18:13:16 +02:00
} ,
2021-10-04 12:33:40 +02:00
{
Name : "assignees" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "[]string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
Default : [ ] string { ` ` } ,
} ,
2020-12-21 18:13:16 +02:00
{
Name : "body" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
2021-03-19 14:04:30 +02:00
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_body" ) ,
2021-03-19 14:04:30 +02:00
} ,
{
Name : "bodyFilePath" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : false ,
2020-12-21 18:13:16 +02:00
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_bodyFilePath" ) ,
2020-12-21 18:13:16 +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" } } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_owner" ) ,
2020-12-21 18:13:16 +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" } } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_repository" ) ,
2020-12-21 18:13:16 +02:00
} ,
{
Name : "title" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : true ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_title" ) ,
2020-12-21 18:13:16 +02:00
} ,
{
Name : "token" ,
ResourceRef : [ ] config . ResourceReference {
{
Name : "githubTokenCredentialsId" ,
Type : "secret" ,
} ,
{
2021-09-21 13:06:32 +02:00
Name : "githubVaultSecretName" ,
Type : "vaultSecret" ,
Default : "github" ,
2020-12-21 18:13:16 +02:00
} ,
} ,
Scope : [ ] string { "GENERAL" , "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : true ,
Aliases : [ ] config . Alias { { Name : "githubToken" } , { Name : "access_token" } } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_token" ) ,
2020-12-21 18:13:16 +02:00
} ,
} ,
} ,
} ,
}
return theMetaData
}