forked from KevinSnyderCodes/github-deployment-resource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
in_command.go
111 lines (95 loc) · 2.57 KB
/
in_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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package resource
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
)
type InCommand struct {
github GitHub
writer io.Writer
}
func NewInCommand(github GitHub, writer io.Writer) *InCommand {
return &InCommand{
github: github,
writer: writer,
}
}
func (c *InCommand) Run(destDir string, request InRequest) (InResponse, error) {
err := os.MkdirAll(destDir, 0755)
if err != nil {
return InResponse{}, err
}
id, _ := strconv.ParseInt(request.Version.ID, 10, 64)
fmt.Fprintln(c.writer, "getting deployment")
deployment, err := c.github.GetDeployment(id)
if err != nil {
return InResponse{}, err
}
if deployment == nil {
return InResponse{}, errors.New("no deployment")
}
idPath := filepath.Join(destDir, "id")
err = ioutil.WriteFile(idPath, []byte(request.Version.ID), 0644)
if err != nil {
return InResponse{}, err
}
refPath := filepath.Join(destDir, "ref")
err = ioutil.WriteFile(refPath, []byte(*deployment.Ref), 0644)
if err != nil {
return InResponse{}, err
}
shaPath := filepath.Join(destDir, "sha")
err = ioutil.WriteFile(shaPath, []byte(*deployment.SHA), 0644)
if err != nil {
return InResponse{}, err
}
if deployment.Task != nil && *deployment.Task != "" {
taskPath := filepath.Join(destDir, "task")
err = ioutil.WriteFile(taskPath, []byte(*deployment.Task), 0644)
if err != nil {
return InResponse{}, err
}
}
if deployment.Environment != nil && *deployment.Environment != "" {
envPath := filepath.Join(destDir, "environment")
err = ioutil.WriteFile(envPath, []byte(*deployment.Environment), 0644)
if err != nil {
return InResponse{}, err
}
}
if deployment.Description != nil && *deployment.Description != "" {
descPath := filepath.Join(destDir, "description")
err = ioutil.WriteFile(descPath, []byte(*deployment.Description), 0644)
if err != nil {
return InResponse{}, err
}
}
// Save the whole deployment too I guess.
deploymentPath := filepath.Join(destDir, "deploymentJSON")
deploymentJSON, _ := json.Marshal(deployment)
err = ioutil.WriteFile(deploymentPath, deploymentJSON, 0644)
if err != nil {
return InResponse{}, err
}
fmt.Fprintln(c.writer, "getting deployment statuses list")
statuses, err := c.github.ListDeploymentStatuses(*deployment.ID)
if err != nil {
return InResponse{}, err
}
latestStatus := ""
if len(statuses) > 0 {
latestStatus = *statuses[0].State
}
return InResponse{
Version: Version{
ID: strconv.FormatInt(*deployment.ID, 10),
Statuses: latestStatus,
},
Metadata: metadataFromDeployment(deployment, statuses),
}, nil
}