-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdeployment_out_command.go
92 lines (79 loc) · 2.48 KB
/
deployment_out_command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package resource
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
"strings"
"github.com/google/go-github/v28/github"
)
type DeploymentOutCommand struct {
github GitHub
writer io.Writer
}
func NewDeploymentOutCommand(github GitHub, writer io.Writer) *DeploymentOutCommand {
return &DeploymentOutCommand{
github: github,
writer: writer,
}
}
func (c *DeploymentOutCommand) Run(sourceDir string, request OutRequest) (OutResponse, error) {
if request.Params.Ref == nil {
return OutResponse{}, errors.New("ref is a required parameter")
}
newDeployment := &github.DeploymentRequest{
Ref: request.Params.Ref,
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"),
}
if request.Params.Payload != nil {
payload := *request.Params.Payload
payload["concourse_payload"] = concoursePayload
} else {
request.Params.Payload = &map[string]interface{}{
"concourse_payload": concoursePayload,
}
}
p, err := json.Marshal(request.Params.Payload)
newDeployment.Payload = github.String(string(p))
if request.Params.Task != nil {
newDeployment.Task = request.Params.Task
}
if request.Params.Environment != nil {
newDeployment.Environment = request.Params.Environment
}
if request.Params.Description != nil {
newDeployment.Description = request.Params.Description
}
if request.Params.AutoMerge != nil {
newDeployment.AutoMerge = request.Params.AutoMerge
}
fmt.Fprintln(c.writer, "creating deployment")
deployment, err := c.github.CreateDeployment(newDeployment)
if err != nil {
return OutResponse{}, err
}
return OutResponse{
Version: Version{ID: strconv.FormatInt(*deployment.ID, 10)},
Metadata: metadataFromDeployment(deployment, []*github.DeploymentStatus{}),
}, nil
}
func (c *DeploymentOutCommand) fileContents(path string) (string, error) {
contents, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
return strings.TrimSpace(string(contents)), nil
}