-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathfork-cleaner.go
193 lines (173 loc) · 4.72 KB
/
fork-cleaner.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
// Package forkcleaner provides functions to find and remove unused forks.
package forkcleaner
import (
"context"
"fmt"
"log"
"net/http"
"strings"
"time"
"github.com/google/go-github/v50/github"
)
const pageSize = 100
type RepositoryWithDetails struct {
Name string
ParentName string
RepoURL string
Private bool
ParentDeleted bool
ParentDMCATakeDown bool
Forks int
Stars int
OpenPRs int
CommitsAhead int
LastUpdate time.Time
}
// FindAllForks lists all the forks for the current user.
func FindAllForks(ctx context.Context, client *github.Client, login string, skipUpstream bool) ([]*RepositoryWithDetails, error) {
var forks []*RepositoryWithDetails
repos, err := getAllRepos(ctx, client, login)
if err != nil {
return forks, nil
}
for _, r := range repos {
login := r.GetOwner().GetLogin()
name := r.GetName()
// Get repository as List omits parent information.
repo, resp, err := client.Repositories.Get(ctx, login, name)
switch resp.StatusCode {
case http.StatusForbidden:
// no access, ignore
continue
case http.StatusUnavailableForLegalReasons:
// fork DCMA taken down, so will the parent
forks = append(forks, buildDetails(r, nil, nil, resp.StatusCode))
continue
}
if err != nil {
return forks, fmt.Errorf("failed to get repository: %s: %w", repo.GetFullName(), err)
}
if skipUpstream {
forks = append(forks, buildDetails(repo, nil, nil, resp.StatusCode))
continue
}
parent := repo.GetParent()
// get parent's Issues
issues, err := getIssues(ctx, client, login, parent)
if err != nil {
return forks, fmt.Errorf("failed to get repository's issues: %s: %w", parent.GetFullName(), err)
}
// compare Commits with parent
commits, resp, err := client.Repositories.CompareCommits(
ctx,
parent.GetOwner().GetLogin(),
parent.GetName(),
parent.GetDefaultBranch(),
fmt.Sprintf("%s:%s", login, repo.GetDefaultBranch()),
&github.ListOptions{},
)
if err != nil && resp.StatusCode != 404 {
return forks, fmt.Errorf("failed to compare repository with parent: %s: %w", repo.GetFullName(), err)
}
forks = append(forks, buildDetails(repo, issues, commits, resp.StatusCode))
}
return forks, nil
}
func buildDetails(repo *github.Repository, issues []*github.Issue, commits *github.CommitsComparison, code int) *RepositoryWithDetails {
var openPrs, aheadBy int
for _, issue := range issues {
if issue.IsPullRequest() {
openPrs++
}
}
if commits != nil {
aheadBy = commits.GetAheadBy()
}
return &RepositoryWithDetails{
Name: repo.GetFullName(),
ParentName: repo.GetParent().GetFullName(),
RepoURL: repo.GetURL(),
Private: repo.GetPrivate(),
ParentDeleted: code == http.StatusNotFound,
ParentDMCATakeDown: code == http.StatusUnavailableForLegalReasons,
Forks: repo.GetForksCount(),
Stars: repo.GetStargazersCount(),
OpenPRs: openPrs,
CommitsAhead: aheadBy,
LastUpdate: repo.GetUpdatedAt().Time,
}
}
func getAllRepos(
ctx context.Context,
client *github.Client,
login string,
) ([]*github.Repository, error) {
var allRepos []*github.Repository
opts := &github.SearchOptions{
Sort: "created",
Order: "asc",
TextMatch: false,
ListOptions: github.ListOptions{
PerPage: pageSize,
},
}
for {
repos, resp, err := client.Search.Repositories(ctx, "owner:"+login+" fork:only", opts)
if err != nil {
return allRepos, err
}
allRepos = append(allRepos, repos.Repositories...)
if resp.NextPage == 0 {
break
}
opts.ListOptions.Page = resp.NextPage
}
return allRepos, nil
}
func getIssues(
ctx context.Context,
client *github.Client,
login string,
repo *github.Repository,
) ([]*github.Issue, error) {
var allIssues []*github.Issue
opts := &github.IssueListByRepoOptions{
ListOptions: github.ListOptions{
PerPage: pageSize,
},
Creator: login,
}
for {
issues, resp, err := client.Issues.ListByRepo(
ctx,
repo.GetOwner().GetLogin(),
repo.GetName(),
opts,
)
if err != nil {
return allIssues, err
}
allIssues = append(allIssues, issues...)
if resp.NextPage == 0 {
break
}
opts.ListOptions.Page = resp.NextPage
}
return allIssues, nil
}
// Delete delete the given list of forks.
func Delete(
ctx context.Context,
client *github.Client,
deletions []*RepositoryWithDetails,
) error {
for _, repo := range deletions {
parts := strings.Split(repo.Name, "/")
log.Println("deleting repository:", repo.Name)
_, err := client.Repositories.Delete(ctx, parts[0], parts[1])
if err != nil {
return fmt.Errorf("couldn't delete repository: %s: %w", repo.Name, err)
}
}
return nil
}