-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadinfos.go
More file actions
67 lines (58 loc) · 1.25 KB
/
readinfos.go
File metadata and controls
67 lines (58 loc) · 1.25 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
package main
import (
"io"
"fmt"
"bytes"
"net/url"
"io/ioutil"
"encoding/json"
)
type RepoType int
const (
Github RepoType = iota
Bitbucket
)
type RepoInfos struct {
Name string `json:"name"`
}
type PushInfos struct {
Ref string `json:"ref"`
Repository RepoInfos `json:"repository"`
Type RepoType `json:"-"`
}
type CommitBitbucket struct {
Branch string `json:"branch"`
}
type PushInfosBitbucket struct {
Repository struct {
Name string `json:"name"`
} `json:"repository"`
Commits []CommitBitbucket `json:"commits"`
}
func ReadInfos(body io.ReadCloser, pushInfos *PushInfos) error {
raw, err := ioutil.ReadAll(body)
if err != nil {
return err
}
err = json.Unmarshal(raw, &pushInfos)
if err != nil {
var bitbucket PushInfosBitbucket
err = nil
ss := bytes.SplitN(raw, []byte("="), 2)
if len(ss) != 2 {
return fmt.Errorf("couldn't parse body")
}
s, err := url.QueryUnescape(string(ss[1]))
err = json.Unmarshal([]byte(s), &bitbucket)
if err != nil {
return err
}
commit := bitbucket.Commits[len(bitbucket.Commits)-1]
pushInfos.Ref = fmt.Sprintf("refs/heads/%s", commit.Branch)
pushInfos.Repository.Name = bitbucket.Repository.Name
pushInfos.Type = Bitbucket
} else {
pushInfos.Type = Github
}
return err
}