-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo_and_delete.go
208 lines (183 loc) · 5.92 KB
/
go_and_delete.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"context"
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
"os"
"strings"
"github.com/google/go-github/v51/github"
"golang.org/x/oauth2"
)
var (
githubClient *github.Client
clientID = os.Getenv("OAUTH_CLIENT_ID") // Set your client ID here
clientSecret = os.Getenv("OAUTH_CLIENT_SECRET") // Set your client secret here
redirectURI = "http://localhost:8989/callback"
tpl = `
<!DOCTYPE html>
<html>
<head>
<title>Bulk GitHub Repo Deleter</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; background-color: #f4f4f9; }
h1 { color: #333; text-align: center; }
form { margin: 20px auto; width: 50%; padding: 20px; background: #fff; border-radius: 5px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); }
input[type='checkbox'] { margin-right: 10px; }
input[type='submit'] { background-color: #28a745; color: white; padding: 10px 20px; border: none; border-radius: 5px; cursor: pointer; }
input[type='submit']:hover { background-color: #218838; }
.repo { transition: all 0.3s ease; }
.repo:hover { background-color: #e8f0fe; cursor: pointer; }
.pagination { text-align: center; margin: 20px 0; }
.pagination a { margin: 0 5px; text-decoration: none; padding: 5px 10px; background: #007bff; color: white; border-radius: 3px; }
.pagination a:hover { background: #0056b3; }
</style>
</head>
<body>
<h1>Bulk GitHub Repo Deleter</h1>
{{if .IsLoggedIn}}
<form method="POST" action="/delete">
{{range .Repos}}
<div class="repo">
<input type="checkbox" name="repos" value="{{.}}"> {{.}}<br>
</div>
{{end}}
<br>
<input type="submit" value="Delete Selected Repos">
</form>
<form action="/logout" method="POST">
<input type="submit" value="Logout">
</form>
{{else}}
<a href="/login">Login with GitHub</a>
{{end}}
<div class="pagination">
{{if .HasPrev}}
<a href="/?page={{.PrevPage}}">Previous</a>
{{end}}
{{if .HasNext}}
<a href="/?page={{.NextPage}}">Next</a>
{{end}}
</div>
</body>
</html>
`
)
// Handle login
func loginHandler(w http.ResponseWriter, r *http.Request) {
url := fmt.Sprintf("https://github.com/login/oauth/authorize?client_id=%s&scope=repo,delete_repo&redirect_uri=%s", clientID, redirectURI)
http.Redirect(w, r, url, http.StatusFound)
}
// Exchange code for access token
func exchangeCodeForToken(code string) string {
url := "https://github.com/login/oauth/access_token"
body := fmt.Sprintf("client_id=%s&client_secret=%s&code=%s", clientID, clientSecret, code)
req, _ := http.NewRequest("POST", url, strings.NewReader(body))
req.Header.Set("Accept", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatalf("Failed to get token: %v", err)
}
defer resp.Body.Close()
var result map[string]interface{}
json.NewDecoder(resp.Body).Decode(&result)
token, ok := result["access_token"].(string)
if !ok {
log.Fatalf("Failed to extract access token: %v", result)
}
return token
}
// Handle callback
func callbackHandler(w http.ResponseWriter, r *http.Request) {
code := r.URL.Query().Get("code")
if code == "" {
http.Error(w, "Code not found", http.StatusBadRequest)
return
}
token := exchangeCodeForToken(code)
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
client := oauth2.NewClient(context.Background(), ts)
githubClient = github.NewClient(client)
http.Redirect(w, r, "/", http.StatusFound)
}
// Home Page
func homeHandler(w http.ResponseWriter, r *http.Request) {
if githubClient == nil {
http.Redirect(w, r, "/login", http.StatusFound)
return
}
page := 1
if p := r.URL.Query().Get("page"); p != "" {
fmt.Sscanf(p, "%d", &page)
}
options := &github.RepositoryListOptions{
ListOptions: github.ListOptions{PerPage: 10, Page: page},
}
repos, resp, err := githubClient.Repositories.List(context.Background(), "", options)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
repoNames := []string{}
for _, repo := range repos {
repoNames = append(repoNames, *repo.FullName)
}
data := struct {
Repos []string
HasPrev bool
HasNext bool
PrevPage int
NextPage int
IsLoggedIn bool
}{
Repos: repoNames,
HasPrev: resp.PrevPage > 0,
HasNext: resp.NextPage > 0,
PrevPage: resp.PrevPage,
NextPage: resp.NextPage,
IsLoggedIn: githubClient != nil,
}
t := template.Must(template.New("index").Parse(tpl))
t.Execute(w, data)
}
// Delete selected repositories
func deleteHandler(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
repos := r.Form["repos"]
for _, repo := range repos {
parts := strings.Split(repo, "/")
owner, repoName := parts[0], parts[1]
_, err := githubClient.Repositories.Delete(context.Background(), owner, repoName)
if err != nil {
if strings.Contains(err.Error(), "403 Must have admin rights") {
fmt.Fprintf(w, "You don't have admin rights to delete %s. Please try logging in with an account that has admin rights for this repository.<br>", repo)
} else {
fmt.Fprintf(w, "Failed to delete %s: %s<br>", repo, err)
}
} else {
fmt.Fprintf(w, "Deleted: %s<br>", repo)
}
}
fmt.Fprintf(w, "<a href='/'>Go Back</a>")
}
// Logout handler
func logoutHandler(w http.ResponseWriter, r *http.Request) {
githubClient = nil
// Redirect to the GitHub logout URL to clear the session on GitHub's side
logoutURL := "https://github.com/logout"
http.Redirect(w, r, logoutURL, http.StatusFound)
}
func main() {
http.HandleFunc("/", homeHandler)
http.HandleFunc("/login", loginHandler)
http.HandleFunc("/callback", callbackHandler)
http.HandleFunc("/delete", deleteHandler)
http.HandleFunc("/logout", logoutHandler)
fmt.Println("Starting server on :8989")
log.Fatal(http.ListenAndServe(":8989", nil))
}