-
Notifications
You must be signed in to change notification settings - Fork 1
/
out.go
152 lines (132 loc) · 4.16 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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) {
if err := request.Params.Validate(); 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
content, err := ioutil.ReadFile(filepath.Join(path, "version.json"))
if err != nil {
return nil, fmt.Errorf("failed to read version from path: %s", err)
}
if err := json.Unmarshal(content, &version); err != nil {
return nil, fmt.Errorf("failed to unmarshal version from file: %s", err)
}
// Metadata available after a GET step.
var metadata Metadata
content, err = ioutil.ReadFile(filepath.Join(path, "metadata.json"))
if err != nil {
return nil, fmt.Errorf("failed to read metadata from path: %s", err)
}
if err := json.Unmarshal(content, &metadata); err != nil {
return nil, fmt.Errorf("failed to unmarshal metadata from file: %s", err)
}
// Set status if specified
if p := request.Params; p.Status != "" {
description := p.Description
// Set description from a file
if p.DescriptionFile != "" {
content, err := ioutil.ReadFile(filepath.Join(inputDir, p.DescriptionFile))
if err != nil {
return nil, fmt.Errorf("failed to read description file: %s", err)
}
description = string(content)
}
if err := manager.UpdateCommitStatus(version.Commit, p.BaseContext, safeExpandEnv(p.Context), p.Status, safeExpandEnv(p.TargetURL), description); err != nil {
return nil, fmt.Errorf("failed to set status: %s", err)
}
}
// Delete previous comments if specified
if request.Params.DeletePreviousComments {
err = manager.DeletePreviousComments(version.PR)
if err != nil {
return nil, fmt.Errorf("failed to delete previous comments: %s", err)
}
}
// Set comment if specified
if p := request.Params; p.Comment != "" {
err = manager.PostComment(version.PR, safeExpandEnv(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, safeExpandEnv(comment))
if err != nil {
return nil, fmt.Errorf("failed to post comment: %s", err)
}
}
}
return &PutResponse{
Version: version,
Metadata: metadata,
}, 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"`
DescriptionFile string `json:"description_file"`
Description string `json:"description"`
Status string `json:"status"`
CommentFile string `json:"comment_file"`
Comment string `json:"comment"`
DeletePreviousComments bool `json:"delete_previous_comments"`
}
// 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
}
func safeExpandEnv(s string) string {
return os.Expand(s, func(v string) string {
switch v {
case "BUILD_ID", "BUILD_NAME", "BUILD_JOB_NAME", "BUILD_PIPELINE_NAME", "BUILD_TEAM_NAME", "ATC_EXTERNAL_URL":
return os.Getenv(v)
}
return "$" + v
})
}