-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathshould-delete-fork.js
More file actions
149 lines (126 loc) · 3.45 KB
/
Copy pathshould-delete-fork.js
File metadata and controls
149 lines (126 loc) · 3.45 KB
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
'use strict';
const getBranchPRStatus = async (github, repo, branch) => {
try {
const {data: prs} = await github.pulls.list({
owner: repo.parent.owner.login,
repo: repo.parent.name,
state: 'all',
head: `${repo.owner.login}:${branch.name}`,
per_page: 1
});
if (prs.length === 0) {
return null;
}
const pr = prs[0];
if (pr.merged_at) {
return 'merged';
}
if (pr.state === 'closed') {
return 'closed';
}
return 'open';
} catch {
return null;
}
};
const branchIsUseful = async (github, repo, branch, parentBranches) => {
const someParentBranchAtThisSha =
parentBranches.filter(candidate => {
return branch.commit.sha === candidate.commit.sha;
}).length === 1;
if (someParentBranchAtThisSha) {
return false;
}
const prStatus = await getBranchPRStatus(github, repo, branch);
if (prStatus === 'merged') {
return false;
}
if (prStatus === 'open' || prStatus === 'closed') {
return true;
}
// Check if at least one parent branch contains this commit
// Looping through all parent branches is immensely slow,
// let's take a safe shortcut
const sameName = parentBranches.find(b => b.name === branch.name);
const defaultBranch = parentBranches.find(b => b.name === repo.default_branch);
// If branch doesn't exist in parent and has no PR, it's likely a deleted
// upstream branch that was synced - consider it useless
if (!sameName) {
return false;
}
// Build list of branches to check, avoiding duplicates
const branchesToCheck = [sameName];
if (defaultBranch && defaultBranch.name !== sameName.name) {
branchesToCheck.push(defaultBranch);
}
for (const candidate of branchesToCheck) {
try {
const {data: diff} = await github.repos.compareCommits({
owner: repo.parent.owner.login,
repo: repo.parent.name,
base: branch.commit.sha,
head: candidate.name
});
// If parent is not behind us, this parent candidate
// branch contains our branch
if (diff.behind_by) {
return true;
}
} catch (error) {
// If diff can't be found, means our commit is not on this
// parent branch candidate
// Note: error.status can be a number or string depending on the error type
if (Number(error.status) === 404) {
continue;
}
throw error;
}
}
return false;
};
module.exports = async (github, fork) => {
// Grab the repository information
const {data: repo} = await github.repos.get({
owner: fork.owner.login,
repo: fork.name
});
// Grab all branches
const {data: branches} = await github.repos.listBranches({
owner: repo.owner.login,
repo: repo.name,
per_page: 100
});
if (branches.length === 100) {
// There are too many branches,
// dealing with pagination is not supported
return false;
}
// Grab all parent repository branches
let parentbranches;
try {
({data: parentbranches} = await github.repos.listBranches({
owner: repo.parent.owner.login,
repo: repo.parent.name,
per_page: 100
}));
} catch (error) {
// If parent repository was deleted, need to preserve the fork
// Note: error.status can be a number or string depending on the error type
if (error && Number(error.status) === 404) {
return false;
}
throw error;
}
// Compare if for each local branch is useless
if (branches.length > parentbranches.length) {
// Quick common case
return false;
}
for (const branch of branches) {
const useful = await branchIsUseful(github, repo, branch, parentbranches);
if (useful) {
return false;
}
}
return true;
};