-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub.go
152 lines (135 loc) · 3.64 KB
/
github.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2018 Julien Schmidt. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be found
// in the LICENSE file.
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strconv"
)
type githubRepos []struct {
Name string `json:"name"`
Description string `json:"description"`
Path string `json:"path"`
}
type createRepo struct {
Name string `json:"name"`
Description string `json:"description"`
Homepage string `json:"homepage"`
HasIssues bool `json:"has_issues"`
HasProjects bool `json:"has_projects"`
HasWiki bool `json:"has_wiki"`
}
type updateRepo struct {
Name string `json:"name"`
Description string `json:"description"`
}
func setGithubHeaders(r *http.Request) {
r.Header.Set("Accept", "application/vnd.github.v3+json")
r.Header.Set("Authorization", "token "+cfg.GithubToken)
r.Header.Set("User-Agent", cfg.UserAgent)
}
func githubPushURL(name string) string {
return "https://" + cfg.GithubToken + "@github.com/" + cfg.OrgName + "/" + name + ".git"
}
func getGithub(path string) (*http.Response, error) {
req, err := http.NewRequest("GET", "https://api.github.com/"+path, nil)
if err != nil {
return nil, err
}
setGithubHeaders(req)
return http.DefaultClient.Do(req)
}
func sendGithub(method, path string, jsonData interface{}) (*http.Response, error) {
var buf bytes.Buffer
err := json.NewEncoder(&buf).Encode(jsonData)
if err != nil {
return nil, err
}
req, err := http.NewRequest(method, "https://api.github.com/"+path, &buf)
if err != nil {
return nil, err
}
setGithubHeaders(req)
req.Header.Set("Content-Type", "application/json; charset=utf-8")
return http.DefaultClient.Do(req)
}
func getGithubRepos() (repos githubRepos, err error) {
page := 1
for {
var newRepos githubRepos
resp, err := getGithub("orgs/" + cfg.OrgName +
"/repos?type=public&per_page=100&page=" + strconv.Itoa(page))
if err != nil {
return nil, err
}
if err = json.NewDecoder(resp.Body).Decode(&newRepos); err != nil {
return nil, err
}
if len(newRepos) < 100 {
if page == 1 {
return newRepos, nil
} else {
return append(repos, newRepos...), nil
}
} else {
repos = append(repos, newRepos...)
page++
}
}
}
func createGithubRepo(id int, name, description, path string) error {
cr := createRepo{
Name: name,
Description: description,
Homepage: cfg.GitlabURL + cfg.OrgName + "/" + name,
HasIssues: true,
HasProjects: false,
HasWiki: false,
}
resp, err := sendGithub("POST", "orgs/"+cfg.OrgName+"/repos", cr)
if err != nil {
return err
}
if resp.StatusCode != 201 {
bs, err := ioutil.ReadAll(resp.Body)
log.Println(name, string(bs), err)
return fmt.Errorf("unexpected response: %d %s", resp.StatusCode, resp.Status)
}
resp.Body.Close()
return pushRepo(id, name)
}
func updateGithubRepo(id int, name, description, path string) error {
ur := updateRepo{
Name: name,
Description: description,
}
resp, err := sendGithub("PATCH", "repos/"+cfg.OrgName+"/"+name, ur)
if err != nil {
return err
}
if resp.StatusCode != 200 {
bs, err := ioutil.ReadAll(resp.Body)
log.Println(name, string(bs), err)
return fmt.Errorf("unexpected response: %s", resp.Status)
}
resp.Body.Close()
return pushRepo(id, name)
}
func deleteGithubRepo(id int, name string) error {
resp, err := sendGithub("DELETE", "repos/"+cfg.OrgName+"/"+name, nil)
if err != nil {
return err
}
if resp.StatusCode != 204 {
bs, err := ioutil.ReadAll(resp.Body)
log.Println(name, string(bs), err)
return fmt.Errorf("unexpected response: %s", resp.Status)
}
resp.Body.Close()
return nil
}