forked from digitalocean/github-pr-resource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
out.go
133 lines (115 loc) · 3.22 KB
/
out.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package resource
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
// Put (business logic)
func Put(request PutRequest, manager Github, inputDir string) (*PutResponse, error) {
err := request.Params.Validate()
if err != nil {
return nil, fmt.Errorf("invalid parameters: %s", err)
}
path := filepath.Join(inputDir, request.Params.Path, ".git", "resource")
// Version available after a GET step.
var version Version
b, err := readFile("version", path)
if err != nil {
return nil, err
}
err = json.Unmarshal(b, &version)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal version from file: %s", err)
}
// Metadata available after a GET step.
var metadata Metadata
b, err = readFile("metadata", path)
if err != nil {
return nil, err
}
err = json.Unmarshal(b, &metadata)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal metadata from file: %s", err)
}
// Set status if specified
if p := request.Params; p.Status != "" {
if err := manager.UpdateCommitStatus(version.Commit, p.BaseContext, os.ExpandEnv(p.Context), p.Status, os.ExpandEnv(p.TargetURL), p.Description); err != nil {
return nil, fmt.Errorf("failed to set status: %s", err)
}
}
// Set comment if specified
if p := request.Params; p.Comment != "" {
err = manager.PostComment(version.PR, os.ExpandEnv(p.Comment))
if err != nil {
return nil, fmt.Errorf("failed to post comment: %s", err)
}
}
// Set comment from a file
if p := request.Params; p.CommentFile != "" {
content, err := ioutil.ReadFile(filepath.Join(inputDir, p.CommentFile))
if err != nil {
return nil, fmt.Errorf("failed to read comment file: %s", err)
}
comment := string(content)
if comment != "" {
err = manager.PostComment(version.PR, os.ExpandEnv(comment))
if err != nil {
return nil, fmt.Errorf("failed to post comment: %s", err)
}
}
}
return &PutResponse{
Version: version,
Metadata: metadata,
}, nil
}
func readFile(name, path string) ([]byte, error) {
content, err := ioutil.ReadFile(filepath.Join(path, name+".json"))
if err != nil {
return nil, fmt.Errorf("failed to read %s from path: %s", name, err)
}
return content, nil
}
// PutRequest ...
type PutRequest struct {
Source Source `json:"source"`
Params PutParameters `json:"params"`
}
// PutResponse ...
type PutResponse struct {
Version Version `json:"version"`
Metadata Metadata `json:"metadata,omitempty"`
}
// PutParameters for the resource.
type PutParameters struct {
Path string `json:"path"`
BaseContext string `json:"base_context"`
Context string `json:"context"`
TargetURL string `json:"target_url"`
Description string `json:"description"`
Status string `json:"status"`
CommentFile string `json:"comment_file"`
Comment string `json:"comment"`
}
// Validate the put parameters.
func (p *PutParameters) Validate() error {
if p.Status == "" {
return nil
}
// Make sure we are setting an allowed status
var allowedStatus bool
status := strings.ToLower(p.Status)
allowed := []string{"success", "pending", "failure", "error"}
for _, a := range allowed {
if status == a {
allowedStatus = true
}
}
if !allowedStatus {
return fmt.Errorf("unknown status: %s", p.Status)
}
return nil
}