-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunstar.js
More file actions
169 lines (143 loc) · 5.96 KB
/
unstar.js
File metadata and controls
169 lines (143 loc) · 5.96 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import 'dotenv/config';
import { graphql } from '@octokit/graphql';
class GitHubUnstarer {
constructor() {
if (!process.env.GITHUB_TOKEN) {
throw new Error('GITHUB_TOKEN environment variable is required. Please create a .env file with your GitHub token.');
}
this.graphqlWithAuth = graphql.defaults({
headers: {
authorization: `token ${process.env.GITHUB_TOKEN}`,
},
});
}
async getStarredRepos() {
console.log('📥 Fetching all starred repositories...');
let repos = [];
let hasNextPage = true;
let endCursor = null;
const query = `
query getStarred($cursor: String) {
viewer {
starredRepositories(first: 100, after: $cursor, orderBy: {field: STARRED_AT, direction: ASC}) {
edges {
starredAt
node {
id
nameWithOwner
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
`;
while (hasNextPage) {
try {
const { viewer: { starredRepositories } } = await this.graphqlWithAuth(query, { cursor: endCursor });
const newRepos = starredRepositories.edges.map(edge => ({
...edge.node,
starredAt: edge.starredAt
}));
repos.push(...newRepos);
hasNextPage = starredRepositories.pageInfo.hasNextPage;
endCursor = starredRepositories.pageInfo.endCursor;
console.log(` Fetched ${repos.length} repositories so far...`);
} catch (error) {
console.error('Error fetching starred repositories:', error.message);
throw error;
}
}
console.log(`✅ Total starred repositories: ${repos.length}`);
return repos;
}
async unstarRepo(repoId) {
const mutation = `
mutation unstar($repoId: ID!) {
removeStar(input: {starrableId: $repoId}) {
starrable {
id
}
}
}
`;
await this.graphqlWithAuth(mutation, { repoId });
}
async unstarRepos(repos, count = 100) {
console.log(`🚫 Starting to unstar ${Math.min(count, repos.length)} oldest starred repositories...`);
const reposToUnstar = repos.slice(0, count);
const unstarred = [];
const failed = [];
for (let i = 0; i < reposToUnstar.length; i++) {
const repo = reposToUnstar[i];
try {
await this.unstarRepo(repo.id);
unstarred.push(repo);
console.log(` ✅ Unstarred: ${repo.nameWithOwner} - Starred: ${new Date(repo.starredAt).toLocaleDateString()}`);
await new Promise(resolve => setTimeout(resolve, 200)); // Rate limiting
} catch (error) {
console.error(` ❌ Failed to unstar ${repo.nameWithOwner}:`, error.message);
failed.push({ repo, error: error.message });
}
}
return { unstarred, failed };
}
async run(unstarCount = 100) {
try {
console.log('🚀 GitHub Unstarer Started');
console.log(`Target: Unstar ${unstarCount} oldest starred repositories\n`);
const repos = await this.getStarredRepos();
if (repos.length === 0) {
console.log('❌ You have no starred repositories.');
return;
}
if (repos.length < unstarCount) {
console.log(`⚠️ You only have ${repos.length} starred repositories, which is less than the requested ${unstarCount}.`);
unstarCount = repos.length;
}
console.log('\n📋 Oldest starred repositories:');
repos.slice(0, Math.min(10, unstarCount)).forEach((repo, index) => {
console.log(` ${index + 1}. ${repo.nameWithOwner} - Starred: ${new Date(repo.starredAt).toLocaleDateString()}`);
});
if (unstarCount > 10) {
console.log(` ... and ${unstarCount - 10} more`);
}
console.log('\n⚠️ WARNING: This will unstar repositories permanently!');
console.log('Press Ctrl+C to cancel, or wait 5 seconds to continue...\n');
for (let i = 5; i > 0; i--) {
process.stdout.write(`Starting in ${i} seconds...\r`);
await new Promise(resolve => setTimeout(resolve, 1000));
}
console.log('\n');
const result = await this.unstarRepos(repos, unstarCount);
console.log('\n📊 Summary:');
console.log(`✅ Successfully unstarred: ${result.unstarred.length} repositories`);
if (result.failed.length > 0) {
console.log(`❌ Failed to unstar: ${result.failed.length} repositories`);
result.failed.forEach(({ repo, error }) => {
console.log(` - ${repo.nameWithOwner}: ${error}`);
});
}
console.log('\n🎉 Operation completed!');
} catch (error) {
console.error('❌ Error:', error.message);
process.exit(1);
}
}
}
// Main execution
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
if (import.meta.url.startsWith('file://') && process.argv[1] === __filename) {
const unstarer = new GitHubUnstarer();
const unstarCount = process.argv[2] ? parseInt(process.argv[2]) : 100;
if (isNaN(unstarCount) || unstarCount <= 0) {
console.error('❌ Invalid unstar count. Please provide a positive number.');
process.exit(1);
}
unstarer.run(unstarCount);
}
export default GitHubUnstarer;