-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
142 lines (127 loc) · 4.27 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
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"fmt"
"io"
"net/http"
"os"
"time"
)
func main() {
token, ok := os.LookupEnv("GITHUB_TOKEN")
if !ok {
fmt.Printf("Github token not set via environment variable" +
"Setting token via command line argument")
}
base_url := "https://api.github.com/repos/"
// Checks if Github token is set and owner, repo names are entered
if len(token) > 0 && len(os.Args) != 3 {
panic("Enter Github owner and repo. " +
"Make sure not to have any spaces in between...")
}
// Check if Github token is not entered via environment
// variable or command line argument
if token == "" && len(os.Args) != 4 {
panic("Set Github token as environment variable or " +
"pass it as command line argument")
}
github_owner := os.Args[1]
gibhub_repo := os.Args[2]
if token == "" {
token = os.Args[3]
}
url := base_url + github_owner + "/" + gibhub_repo
unrefined_branch_list := ListBranches(url, token)
RefineBranchList(unrefined_branch_list, url, token, github_owner)
}
func SendRequest(url string, req_method string, token string) string {
client := &http.Client{}
req, err := http.NewRequest(req_method, url, nil)
if err != nil {
// handle error
fmt.Println(err)
}
req.Header.Add("Accept", "application/vnd.github+json")
req.Header.Add("Authorization", "Bearer "+token)
// Change the api version here
req.Header.Add("X-GitHub-Api-Version", "2022-11-28")
resp, err := client.Do(req)
if err != nil {
// handle error
fmt.Println(err)
}
defer resp.Body.Close()
if req_method == "GET" {
body, err := io.ReadAll(resp.Body)
if err != nil {
// handle error
fmt.Println(err)
}
return string(body)
} else if req_method == "DELETE" {
return fmt.Sprint(resp.StatusCode)
}
return ""
}
func ListBranches(url string, token string) map[string]string {
// set protected query as false to get unprotected branches
body := SendRequest(url+"/branches?protected=false", "GET", token)
return DecodeBranchListInJson(body)
}
func GetDefaultBranch(url string, token string) string {
body := SendRequest(url, "GET", token)
df_branch := DecodeDefaultBranch(body)
return df_branch
}
func CheckIfBranchHasOpenPullRequests(url string, owner string, branch string, token string) bool {
body := SendRequest(url+"/pulls?head="+owner+":"+branch, "GET", token)
// api returns '[]' string if branch has no open pr's.
// so string length is checked for 2 characters and it returns 2
// if the length is not equal to 2
return len(body) != 2
}
func LastCommitTimeOfBranchInDays(url string, branch string, token string) int {
body := SendRequest(url+"/commits/heads/"+branch, "GET", token)
t, err := time.Parse(time.RFC3339, DecodeLastCommitTime(body))
if err != nil {
fmt.Println(err)
}
// print(branch + "has last commit in " + strconv.Itoa(int(time.Since(t).Hours()/24)) + " days")
// Calculate the difference between the current date and the specific date in days
return int(time.Since(t).Hours() / 24)
}
func RefineBranchList(bl map[string]string, url string, token string, owner string) {
// This function checks the branch list and filter out default branch,
// branches which has open pull requests, branches which has been merged
// but has the last commit done in less than 10 days
// Refined list contains branches as keys, sha commits as values
rl := make(map[string]string)
default_branch := GetDefaultBranch(url, token)
for k, v := range bl {
// not assigning to default branch if it is a default branch
if k == default_branch {
continue
}
// Branch is only entered into refined list only if it doesn't have pull
// request and last commit is more than 10 days back
if !CheckIfBranchHasOpenPullRequests(url, owner, k, token) && LastCommitTimeOfBranchInDays(url, k, token) > 10 {
rl[k] = v
}
}
// Only display branches if refined list is not empty
if len(rl) > 0 {
fmt.Println("List of branches which are about to be deleted are as follows...")
for k := range rl {
fmt.Println(k)
}
}
for k, v := range rl {
fmt.Println(url + "git/refs/heads/" + k)
status := SendRequest(url+"/git/refs/heads/"+k, "DELETE", token)
if status == "204" {
fmt.Println("Branch deletion with branch name " + k +
" with sha code of " + v + " is successful")
} else {
fmt.Println("Branch deletion of " + k + " is unsuccessful with status " + status)
}
}
}