From cde1a4f4abc2378010f44fd415ff8e3362c7d2ed Mon Sep 17 00:00:00 2001 From: Mario Siegenthaler Date: Mon, 1 Mar 2021 12:58:15 +0100 Subject: [PATCH] add log url and env url --- README.md | 4 ++++ concourse_metadata.go | 30 ++++++++++++++++++++++++++++++ deployment_out_command.go | 12 +----------- out_command.go | 7 +++++-- resources.go | 23 ++++++++++++----------- 5 files changed, 52 insertions(+), 24 deletions(-) create mode 100644 concourse_metadata.go diff --git a/README.md b/README.md index bfbf73a..cfeb6c7 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,8 @@ Create a new Deployment, or update a given Deployment with a new DeploymentStatu * `description`: *Optional.* The description of the deployment. +* `environment_url`: *Optional.* URL of the deployment + * `auto_merge`: *Optional.* Whether to auto-merge the repository's default branch into the deployment ref. Defaults to true. * `payload`: *Optional.* Additional data about the deployment. @@ -103,6 +105,8 @@ NB: If there are duplicate keys in data from `payload` and `payload_path`, then * `task`: *Optional.* The name of the task for the deployment. +The log url is automatically set to the url of the concourse build. + ##### Reading values from files All of the above parameters can be used to pass the name of a file to read the applicable value diff --git a/concourse_metadata.go b/concourse_metadata.go new file mode 100644 index 0000000..4f37e8d --- /dev/null +++ b/concourse_metadata.go @@ -0,0 +1,30 @@ +package resource + +import ( + "fmt" + "os" +) + +type ConcourseMetadata struct { + ATCExternalURL string `json:"atc_external_url"` + BuildID string `json:"build_id"` + BuildJobName string `json:"build_job_name"` + BuildName string `json:"build_name"` + BuildPipelineName string `json:"build_pipeline_name"` + BuildTeamName string `json:"build_team_name"` + BuildURL string `json:"build_url"` +} + +func GetConcourseMetadata() ConcourseMetadata { + buildURL := fmt.Sprintf("%v/teams/%v/pipelines/%v/jobs/%v/builds/%v", + os.Getenv("ATC_EXTERNAL_URL"), os.Getenv("BUILD_TEAM_NAME"), os.Getenv("BUILD_PIPELINE_NAME"), os.Getenv("BUILD_JOB_NAME"), os.Getenv("BUILD_NAME")) + return ConcourseMetadata{ + ATCExternalURL: os.Getenv("ATC_EXTERNAL_URL"), + BuildID: os.Getenv("BUILD_ID"), + BuildJobName: os.Getenv("BUILD_JOB_NAME"), + BuildName: os.Getenv("BUILD_NAME"), + BuildPipelineName: os.Getenv("BUILD_PIPELINE_NAME"), + BuildTeamName: os.Getenv("BUILD_TEAM_NAME"), + BuildURL: buildURL, + } +} diff --git a/deployment_out_command.go b/deployment_out_command.go index 6a16f9c..bfec1e1 100644 --- a/deployment_out_command.go +++ b/deployment_out_command.go @@ -6,7 +6,6 @@ import ( "fmt" "io" "io/ioutil" - "os" "strconv" "strings" @@ -35,16 +34,7 @@ func (c *DeploymentOutCommand) Run(sourceDir string, request OutRequest) (OutRes RequiredContexts: &[]string{}, } - concoursePayload := map[string]interface{}{ - "build_id": os.Getenv("BUILD_ID"), - "build_name": os.Getenv("BUILD_NAME"), - "build_job_name": os.Getenv("BUILD_JOB_NAME"), - "build_pipeline_name": os.Getenv("BUILD_PIPELINE_NAME"), - "build_team_name": os.Getenv("BUILD_TEAM_NAME"), - "build_url": fmt.Sprintf("%v/teams/%v/pipelines/%v/jobs/%v/builds/%v", - os.Getenv("ATC_EXTERNAL_URL"), os.Getenv("BUILD_TEAM_NAME"), os.Getenv("BUILD_PIPELINE_NAME"), os.Getenv("BUILD_JOB_NAME"), os.Getenv("BUILD_NAME")), - "atc_external_url": os.Getenv("ATC_EXTERNAL_URL"), - } + concoursePayload := GetConcourseMetadata() if request.Params.Payload != nil && *request.Params.Payload != nil { payload := *request.Params.Payload diff --git a/out_command.go b/out_command.go index 5cd12e2..be431cc 100644 --- a/out_command.go +++ b/out_command.go @@ -41,9 +41,12 @@ func (c *OutCommand) Run(sourceDir string, request OutRequest) (OutResponse, err return OutResponse{}, err } + concourseMetadata := GetConcourseMetadata() newStatus := &github.DeploymentStatusRequest{ - State: request.Params.State, - Description: request.Params.Description, + State: request.Params.State, + Description: request.Params.Description, + LogURL: &concourseMetadata.BuildURL, + EnvironmentURL: request.Params.EnvironmentURL, } if request.Params.LogURL != nil && *request.Params.LogURL != "" { diff --git a/resources.go b/resources.go index 61446a5..de4fa1c 100644 --- a/resources.go +++ b/resources.go @@ -52,17 +52,18 @@ type OutResponse struct { } type OutParams struct { - Type *string `json:"type"` - ID *string - Ref *string - Environment *string - Task *string - State *string - Description *string - AutoMerge *bool - Payload *map[string]interface{} - PayloadPath *string `json:"payload_path"` - LogURL *string + Type *string `json:"type"` + ID *string + Ref *string + Environment *string + Task *string + State *string + Description *string + AutoMerge *bool + Payload *map[string]interface{} + PayloadPath *string `json:"payload_path"` + LogURL *string + EnvironmentURL *string `json:"environment_url,omitempty"` RawID json.RawMessage `json:"id"` RawState json.RawMessage `json:"state"`