This repository has been archived by the owner on Jun 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathout.go
118 lines (102 loc) · 3.01 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
package resource
import (
"encoding/json"
"fmt"
"io/ioutil"
"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 status := request.Params.Status; status != "" {
if err := manager.UpdateCommitStatus(version.Commit, request.Params.Context, status); err != nil {
return nil, fmt.Errorf("failed to set status: %s", err)
}
}
// Set comment if specified
if comment := request.Params.Comment; comment != "" {
err = manager.PostComment(version.PR, comment)
if err != nil {
return nil, fmt.Errorf("failed to post comment: %s", err)
}
}
// Set comment from a file
if cf := request.Params.CommentFile; cf != "" {
path := filepath.Join(inputDir, request.Params.CommentFile)
content, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read comment file: %s", err)
}
comment := string(content)
if comment != "" {
err = manager.PostComment(version.PR, 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"`
Context string `json:"context"`
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
}