forked from ahume/github-deployment-resource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
out_command.go
82 lines (69 loc) · 1.76 KB
/
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
package resource
import (
"errors"
"fmt"
"io"
"io/ioutil"
"strconv"
"strings"
"github.com/ahume/go-github/github"
)
type OutCommand struct {
github GitHub
writer io.Writer
}
func NewOutCommand(github GitHub, writer io.Writer) *OutCommand {
return &OutCommand{
github: github,
writer: writer,
}
}
func (c *OutCommand) Run(sourceDir string, request OutRequest) (OutResponse, error) {
if request.Params.ID == "" {
return OutResponse{}, errors.New("id is a required parameter")
}
if request.Params.State == "" {
return OutResponse{}, errors.New("state is a required parameter")
}
idInt, err := strconv.Atoi(request.Params.ID)
if err != nil {
return OutResponse{}, err
}
fmt.Fprintln(c.writer, "getting deployment")
deployment, err := c.github.GetDeployment(idInt)
if err != nil {
return OutResponse{}, err
}
newStatus := &github.DeploymentStatusRequest{
State: github.String(request.Params.State),
Description: github.String(request.Params.Description),
}
fmt.Fprintln(c.writer, "creating deployment status")
_, err = c.github.CreateDeploymentStatus(*deployment.ID, newStatus)
if err != nil {
return OutResponse{}, err
}
fmt.Fprintln(c.writer, "getting deployment statuses list")
statuses, err := c.github.ListDeploymentStatuses(*deployment.ID)
if err != nil {
return OutResponse{}, err
}
latestStatus := ""
if len(statuses) > 0 {
latestStatus = *statuses[0].State
}
return OutResponse{
Version: Version{
ID: request.Params.ID,
Statuses: latestStatus,
},
Metadata: metadataFromDeployment(deployment, statuses),
}, nil
}
func (c *OutCommand) fileContents(path string) (string, error) {
contents, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
return strings.TrimSpace(string(contents)), nil
}