Skip to content

Commit 4cd45ae

Browse files
committed
Change the interface of the PullRequest resource to be more granular.
Currently the PullRequest resource produces one large json object representing the entire PR. This works, but makes it harder to write simple programs to manipulate this object. This commit changes this interface to produce a set of files, each representing a part of the initial pull request. This makes it easier for programs to do simple tasks like add labels or comments.
1 parent bb85571 commit 4cd45ae

File tree

5 files changed

+490
-41
lines changed

5 files changed

+490
-41
lines changed

cmd/pullrequest-init/disk.go

+178
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
/*
2+
Copyright 2019 The Tekton Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"encoding/json"
21+
"io/ioutil"
22+
"os"
23+
"path/filepath"
24+
"strconv"
25+
)
26+
27+
// This file contains functions used to write a PR to disk. The representation is as follows:
28+
29+
// /workspace/
30+
// /workspace/<resource>/
31+
// /workspace/<resource>/labels/
32+
// /workspace/<resource>/labels/<label>
33+
// /workspace/<resource>/status/
34+
// /workspace/<resource>/status/<status>
35+
// /workspace/<resource>/comments/
36+
// /workspace/<resource>/comments/<comment>
37+
// /workspace/<resource>/head
38+
// /workspace/<resource>/base
39+
40+
// ToDisk converts a PullRequest object to an on-disk representation at the specified path.
41+
func ToDisk(pr *PullRequest, path string) error {
42+
labelsPath := filepath.Join(path, "labels")
43+
commentsPath := filepath.Join(path, "comments")
44+
statusesPath := filepath.Join(path, "status")
45+
46+
// Setup subdirs
47+
for _, p := range []string{labelsPath, commentsPath, statusesPath} {
48+
if err := os.MkdirAll(p, 0755); err != nil {
49+
return err
50+
}
51+
}
52+
53+
// Start with comments
54+
for _, c := range pr.Comments {
55+
commentPath := filepath.Join(commentsPath, strconv.FormatInt(c.ID, 10))
56+
b, err := json.Marshal(c)
57+
if err != nil {
58+
return err
59+
}
60+
if err := ioutil.WriteFile(commentPath, b, 0700); err != nil {
61+
return err
62+
}
63+
}
64+
65+
// Now labels
66+
for _, l := range pr.Labels {
67+
labelPath := filepath.Join(labelsPath, l.Text)
68+
if err := ioutil.WriteFile(labelPath, []byte{}, 0700); err != nil {
69+
return err
70+
}
71+
}
72+
73+
// Now status
74+
for _, s := range pr.Statuses {
75+
statusPath := filepath.Join(statusesPath, s.ID)
76+
b, err := json.Marshal(s)
77+
if err != nil {
78+
return err
79+
}
80+
if err := ioutil.WriteFile(statusPath, b, 0700); err != nil {
81+
return err
82+
}
83+
}
84+
85+
// Now refs
86+
writeRef := func(name string, r *GitReference) error {
87+
b, err := json.Marshal(r)
88+
if err != nil {
89+
return err
90+
}
91+
return ioutil.WriteFile(filepath.Join(path, name), b, 0700)
92+
}
93+
94+
if err := writeRef("head", pr.Head); err != nil {
95+
return err
96+
}
97+
if err := writeRef("base", pr.Base); err != nil {
98+
return err
99+
}
100+
return nil
101+
}
102+
103+
// FromDisk outputs a PullRequest object from an on-disk representation at the specified path.
104+
func FromDisk(path string) (*PullRequest, error) {
105+
labelsPath := filepath.Join(path, "labels")
106+
commentsPath := filepath.Join(path, "comments")
107+
statusesPath := filepath.Join(path, "status")
108+
109+
pr := PullRequest{}
110+
111+
// Start with comments
112+
fis, err := ioutil.ReadDir(commentsPath)
113+
if err != nil {
114+
return nil, err
115+
}
116+
for _, fi := range fis {
117+
b, err := ioutil.ReadFile(filepath.Join(commentsPath, fi.Name()))
118+
if err != nil {
119+
return nil, err
120+
}
121+
comment := Comment{}
122+
if err := json.Unmarshal(b, &comment); err != nil {
123+
return nil, err
124+
}
125+
pr.Comments = append(pr.Comments, &comment)
126+
}
127+
128+
// Now Labels
129+
fis, err = ioutil.ReadDir(labelsPath)
130+
if err != nil {
131+
return nil, err
132+
}
133+
for _, fi := range fis {
134+
label := Label{fi.Name()}
135+
pr.Labels = append(pr.Labels, &label)
136+
}
137+
138+
// Now statuses
139+
fis, err = ioutil.ReadDir(statusesPath)
140+
if err != nil {
141+
return nil, err
142+
}
143+
144+
for _, fi := range fis {
145+
b, err := ioutil.ReadFile(filepath.Join(statusesPath, fi.Name()))
146+
if err != nil {
147+
return nil, err
148+
}
149+
status := Status{}
150+
if err := json.Unmarshal(b, &status); err != nil {
151+
return nil, err
152+
}
153+
pr.Statuses = append(pr.Statuses, &status)
154+
}
155+
156+
// Finally refs
157+
readRef := func(name string) (*GitReference, error) {
158+
b, err := ioutil.ReadFile(filepath.Join(path, name))
159+
if err != nil {
160+
return nil, err
161+
}
162+
ref := GitReference{}
163+
if err := json.Unmarshal(b, &ref); err != nil {
164+
return nil, err
165+
}
166+
return &ref, nil
167+
}
168+
169+
pr.Base, err = readRef("base")
170+
if err != nil {
171+
return nil, err
172+
}
173+
pr.Head, err = readRef("head")
174+
if err != nil {
175+
return nil, err
176+
}
177+
return &pr, nil
178+
}

0 commit comments

Comments
 (0)