2020-09-14 18:08:24 +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-09-14 18:08:24 +02:00
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/spf13/cobra"
)
type githubSetCommitStatusOptions struct {
APIURL string ` json:"apiUrl,omitempty" `
CommitID string ` json:"commitId,omitempty" `
Context string ` json:"context,omitempty" `
Description string ` json:"description,omitempty" `
Owner string ` json:"owner,omitempty" `
Repository string ` json:"repository,omitempty" `
Status string ` json:"status,omitempty" `
TargetURL string ` json:"targetUrl,omitempty" `
Token string ` json:"token,omitempty" `
}
// GithubSetCommitStatusCommand Set a status of a certain commit.
func GithubSetCommitStatusCommand ( ) * cobra . Command {
const STEP_NAME = "githubSetCommitStatus"
metadata := githubSetCommitStatusMetadata ( )
var stepConfig githubSetCommitStatusOptions
var startTime time . Time
2021-05-17 12:14:04 +02:00
var logCollector * log . CollectorHook
2020-09-14 18:08:24 +02:00
var createGithubSetCommitStatusCmd = & cobra . Command {
Use : STEP_NAME ,
Short : "Set a status of a certain commit." ,
Long : ` This step allows you to set a status for a certain commit .
Details can be found here : https : //developer.github.com/v3/repos/statuses/.
Typically , following information is set :
* state ( pending , failure , success )
* context
* target URL ( link to details )
It can for example be used to create additional check indicators for a pull request which can be evaluated and also be enforced by GitHub configuration . ` ,
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-09-14 18:08:24 +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 )
}
2020-09-14 18:08:24 +02:00
return nil
} ,
Run : func ( _ * cobra . Command , _ [ ] string ) {
telemetryData := telemetry . CustomData { }
telemetryData . ErrorCode = "1"
handler := func ( ) {
2020-10-26 15:20:04 +02:00
config . RemoveVaultSecretFiles ( )
2020-09-14 18:08:24 +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-09-14 18:08:24 +02:00
telemetry . Send ( & telemetryData )
2021-05-17 12:14:04 +02:00
if len ( GeneralConfig . HookConfig . SplunkConfig . Dsn ) > 0 {
splunk . Send ( & telemetryData , logCollector )
}
2020-09-14 18:08:24 +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-09-14 18:08:24 +02:00
githubSetCommitStatus ( stepConfig , & telemetryData )
telemetryData . ErrorCode = "0"
log . Entry ( ) . Info ( "SUCCESS" )
} ,
}
addGithubSetCommitStatusFlags ( createGithubSetCommitStatusCmd , & stepConfig )
return createGithubSetCommitStatusCmd
}
func addGithubSetCommitStatusFlags ( cmd * cobra . Command , stepConfig * githubSetCommitStatusOptions ) {
cmd . Flags ( ) . StringVar ( & stepConfig . APIURL , "apiUrl" , ` https://api.github.com ` , "Set the GitHub API URL." )
cmd . Flags ( ) . StringVar ( & stepConfig . CommitID , "commitId" , os . Getenv ( "PIPER_commitId" ) , "The commitId for which the status should be set." )
cmd . Flags ( ) . StringVar ( & stepConfig . Context , "context" , os . Getenv ( "PIPER_context" ) , "Label for the status which will for example show up in a pull request." )
cmd . Flags ( ) . StringVar ( & stepConfig . Description , "description" , os . Getenv ( "PIPER_description" ) , "Short description of the status." )
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 . Status , "status" , os . Getenv ( "PIPER_status" ) , "Status which should be set on the commitId." )
cmd . Flags ( ) . StringVar ( & stepConfig . TargetURL , "targetUrl" , os . Getenv ( "PIPER_targetUrl" ) , "Target URL to associate the status with." )
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 ( "commitId" )
cmd . MarkFlagRequired ( "context" )
cmd . MarkFlagRequired ( "owner" )
cmd . MarkFlagRequired ( "repository" )
cmd . MarkFlagRequired ( "status" )
cmd . MarkFlagRequired ( "token" )
}
// retrieve step metadata
func githubSetCommitStatusMetadata ( ) config . StepData {
var theMetaData = config . StepData {
Metadata : config . StepMetadata {
2020-11-20 09:13:59 +02:00
Name : "githubSetCommitStatus" ,
Aliases : [ ] config . Alias { } ,
Description : "Set a status of a certain commit." ,
2020-09-14 18:08:24 +02:00
} ,
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-09-14 18:08:24 +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-09-14 18:08:24 +02:00
} ,
{
2020-09-16 14:50:09 +02:00
Name : "commitId" ,
ResourceRef : [ ] config . ResourceReference {
{
Name : "commonPipelineEnvironment" ,
Param : "git/commitId" ,
} ,
} ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : true ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_commitId" ) ,
2020-09-14 18:08:24 +02:00
} ,
{
Name : "context" ,
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_context" ) ,
2020-09-14 18:08:24 +02:00
} ,
{
Name : "description" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_description" ) ,
2020-09-14 18:08:24 +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" } } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_owner" ) ,
2020-09-14 18:08:24 +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" } } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_repository" ) ,
2020-09-14 18:08:24 +02:00
} ,
{
Name : "status" ,
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_status" ) ,
2020-09-14 18:08:24 +02:00
} ,
{
Name : "targetUrl" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_targetUrl" ) ,
2020-09-14 18:08:24 +02:00
} ,
{
2020-09-16 14:50:09 +02:00
Name : "token" ,
ResourceRef : [ ] config . ResourceReference {
{
Name : "githubTokenCredentialsId" ,
Type : "secret" ,
} ,
2020-12-22 18:57:11 +02:00
{
Name : "" ,
Paths : [ ] string { "$(vaultPath)/github" , "$(vaultBasePath)/$(vaultPipelineName)/github" , "$(vaultBasePath)/GROUP-SECRETS/github" } ,
Type : "vaultSecret" ,
} ,
2020-09-16 14:50:09 +02:00
} ,
Scope : [ ] string { "GENERAL" , "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : true ,
2021-01-27 13:58:47 +02:00
Aliases : [ ] config . Alias { { Name : "githubToken" } , { Name : "access_token" } } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_token" ) ,
2020-09-14 18:08:24 +02:00
} ,
} ,
} ,
} ,
}
return theMetaData
}