forked from KevinSnyderCodes/github-deployment-resource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeployment_out_command.go
83 lines (69 loc) · 1.97 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
package resource
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"strconv"
"strings"
"github.com/shipt/go-github/v32/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 := GetConcourseMetadata()
if request.Params.Payload != nil && *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
}