-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
131 lines (117 loc) · 3.44 KB
/
main.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
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"encoding/json"
"fmt"
"os"
"strings"
bitbucket "github.com/ktrysmt/go-bitbucket"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
var (
// required args/flags
owner = kingpin.Flag("owner", "bitbucket owner").Short('o').Required().String()
reposlug = kingpin.Flag("repo", "git repo").Required().Short('r').String()
sourceBranch = kingpin.Flag("sourceBranch", "source pr branch").Short('s').Required().String()
title = kingpin.Flag("title", "title of pr").Required().Short('t').String()
// non-required args/flags
closeSource = kingpin.Flag("closeSource", "boolean switch to close source branch after merge").Default("true").Bool()
destinationBranch = kingpin.Arg("destinationBranch", "destination pr branch (default is master)").Default("master").String()
description = kingpin.Flag("description", "description in the pr body").Short('d').String()
)
func main() {
c := &bitbucket.Client{}
kingpin.Parse()
if os.Getenv("BB_AUTH") == "basic" {
c = bitbucket.NewBasicAuth(os.Getenv("user"), os.Getenv("secret"))
} else if os.Getenv("BB_AUTH") == "oauth" {
c = bitbucket.NewOAuthClientCredentials(os.Getenv("user"), os.Getenv("secret"))
} else {
fmt.Println("ERROR: cannot continue without authentication option. Set environment variable BB_AUTH")
os.Exit(1)
}
var reviewers []string
r := os.Getenv("reviewers")
if r != "" {
reviewers = strings.Split(r, ",")
}
opt := &bitbucket.PullRequestsOptions{
Owner: *owner,
RepoSlug: *reposlug,
SourceBranch: *sourceBranch,
DestinationBranch: *destinationBranch,
Title: *title,
Description: *description,
CloseSourceBranch: *closeSource,
Reviewers: reviewers,
}
res, err := c.Repositories.PullRequests.Create(opt)
if err != nil {
fmt.Printf("%v", res)
panic(err)
}
// fmt.Println(reflect.TypeOf(res).PkgPath(), reflect.TypeOf(res).Name())
// myMap := reflect.ValueOf(res)
// for _, e := range myMap.MapKeys() {
// thing := e["test"]
// //fmt.Println(v)
// }
// m := make(map[string]interface{})
// myMap := reflect.ValueOf(res)
// for _, e := range myMap.MapKeys() {
// m[e.String()] = myMap.Elem()
// }
// fmt.Println(m)
byteData, _ := json.Marshal(res)
//var t Message
//json.Unmarshal(byteData, &t.Data)
// fmt.Println(string(byteData))
jsonMap := make(map[string]interface{})
err = json.Unmarshal(byteData, &jsonMap)
if err != nil {
panic(err)
}
// fetchkey("html", jsonMap)
a := getMap("links", jsonMap)
b := getMap("html", a)
d := b["href"]
fmt.Println(d)
}
// dumpmap to stdout...is messy
func dumpMap(space string, m map[string]interface{}) {
for k, v := range m {
if mv, ok := v.(map[string]interface{}); ok {
fmt.Printf("{ \"%v\": \n", k)
dumpMap(space+"\t", mv)
fmt.Printf("}\n")
} else {
fmt.Printf("%v %v : %v\n", space, k, v)
}
}
}
func getMap(key string, m map[string]interface{}) map[string]interface{} {
for k, v := range m {
if k == key {
thing, ok := v.(map[string]interface{})
if !ok {
// Can't assert, handle error.
fmt.Println("cant assert onthing at all")
}
return thing
}
}
return nil
}
// fetchkey to stdout...is messy
func fetchkey(space string, m map[string]interface{}) {
for k, v := range m {
if k != space {
if mv, ok := v.(map[string]interface{}); ok {
fetchkey(space, mv)
} else {
fmt.Printf("%v %v : %v\n", space, k, v)
}
} else {
fmt.Printf("%v %v : %v\n", space, k, v)
}
}
}