-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobs.go
101 lines (92 loc) · 2.56 KB
/
obs.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
package obs
import (
"bytes"
"encoding/xml"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
)
const baseUrl string = "api.suse.de"
type Client struct {
BaseURL *url.URL
username string
password string
httpClient *http.Client
}
func (c *Client) newRequest(method, path string, body interface{}) (*http.Request, error) {
rel := &url.URL{Path: path}
u := c.BaseURL.ResolveReference(rel)
var buf io.ReadWriter
if body != nil {
buf = new(bytes.Buffer)
err := xml.NewEncoder(buf).Encode(body)
if err != nil {
return nil, err
}
}
req, err := http.NewRequest(method, u.String(), buf)
if err != nil {
return nil, err
}
req.SetBasicAuth(c.username, c.password)
if body != nil {
req.Header.Set("Content-Type", "application/xml")
}
req.Header.Set("Accept", "application/xml")
return req, nil
}
func (c *Client) GetReleaseRequests(group string, state string) ([]ReleaseRequest, error) {
req, err := c.newRequest("GET", "/request", nil)
if err != nil {
return nil, err
}
q := req.URL.Query()
q.Add("view", "collection")
q.Add("group", group)
q.Add("states", state)
req.URL.RawQuery = q.Encode()
var collection Collection
_, err = c.do(req, &collection)
return collection.ReleaseRequests, err
}
func (c *Client) GetPatchinfo(rr ReleaseRequest) (*Patchinfo, error) {
project := rr.Actions[0].Source.Project
/* https://api.suse.de/source/SUSE:Maintenance:11688/patchinfo/_patchinfo */
patchinfo_url := fmt.Sprintf("/source/%v/patchinfo/_patchinfo", project)
req, err := c.newRequest("GET", patchinfo_url, nil)
if err != nil {
return nil, err
}
var patchinfo Patchinfo
_, err = c.do(req, &patchinfo)
return &patchinfo, err
}
func (c *Client) do(req *http.Request, v interface{}) (*http.Response, error) {
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return nil, errors.New(fmt.Sprintf("Got status code: %v for %q\n", resp.StatusCode, req.URL))
}
defer resp.Body.Close()
err = xml.NewDecoder(resp.Body).Decode(v)
return resp, err
}
func NewClient(username string, password string) *Client {
return &Client{
BaseURL: &url.URL{Host: baseUrl, Scheme: "https"},
username: username,
password: password,
httpClient: &http.Client{},
}
}
func GetRepo(rr ReleaseRequest) string {
trgPrjStr := strings.Replace(rr.Actions[0].Target.Project, ":", "_", -1)
srcPrjStr := strings.Replace(rr.Actions[0].Source.Project, ":", ":/", -1)
repo := fmt.Sprintf("http://download.suse.de/ibs/%s/%s/%s.repo", srcPrjStr, trgPrjStr, rr.Actions[0].Source.Project)
return repo
}