forked from golang/review
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.go
More file actions
70 lines (63 loc) · 1.45 KB
/
api.go
File metadata and controls
70 lines (63 loc) · 1.45 KB
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
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
)
const (
sourceHost = ".googlesource.com"
reviewSuffix = "-review"
)
var notFound = errors.New("not found")
func getChange(origin, id string) (*Change, error) {
u, err := url.Parse(origin)
if err != nil {
return nil, fmt.Errorf("parsing origin URL: %v", err)
}
if !strings.HasSuffix(u.Host, sourceHost) {
return nil, fmt.Errorf("origin URL not on %v", sourceHost)
}
u.Host = strings.TrimSuffix(u.Host, sourceHost) + reviewSuffix + sourceHost
u.Path = "/changes/" + id
r, err := http.Get(u.String())
if err != nil {
return nil, err
}
defer r.Body.Close()
if r.StatusCode == http.StatusNotFound {
return nil, notFound
}
if r.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status: %v", r.Status)
}
br := bufio.NewReader(r.Body)
br.ReadSlice('\n') // throw away first line
var c Change
if err := json.NewDecoder(br).Decode(&c); err != nil {
return nil, err
}
u.Path = fmt.Sprintf("/%v", c.Number)
c.URL = u.String()
return &c, nil
}
type Change struct {
ChangeId string `json:"change_id"`
Subject string
Status string
Number int `json:"_number"`
Messages []*Message
URL string
}
type Message struct {
Author struct {
Name string
}
Message string
}