Created
June 29, 2023 20:00
-
-
Save maikotrindade/c9a2aaa56cb2d4f349cd1493a4e18709 to your computer and use it in GitHub Desktop.
Unwatch Github repos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fetch from 'node-fetch'; | |
import readline from 'readline'; | |
async function unwatchAllRepos() { | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}); | |
const accessToken = await new Promise<string>((resolve) => { | |
rl.question('Enter your GitHub access token: ', (token) => { | |
resolve(token); | |
rl.close(); | |
}); | |
}); | |
const organization = await new Promise<string>((resolve) => { | |
rl.question('Enter the organization name: ', (orgName) => { | |
resolve(orgName); | |
rl.close(); | |
}); | |
}); | |
const headers = { | |
'Authorization': `Bearer ${accessToken}`, | |
'Accept': 'application/vnd.github.v3+json' | |
}; | |
let page = 1; | |
const perPage = 100; | |
while (true) { | |
// Fetch the repositories being watched by the user for the organization | |
const url = `https://api.github.com/users/${organization}/subscriptions?page=${page}&per_page=${perPage}`; | |
const response = await fetch(url, { headers }); | |
if (response.ok) { | |
const repos = await response.json(); | |
if (repos.length === 0) { | |
break; | |
} | |
for (const repo of repos) { | |
// Unwatch the repository | |
const unwatchUrl = `https://api.github.com/repos/${repo.full_name}/subscription`; | |
await fetch(unwatchUrl, { method: 'DELETE', headers }); | |
console.log(`Unwatched repository: ${repo.full_name}`); | |
} | |
page++; | |
} else { | |
console.log(`Failed to fetch repositories: ${response.status}`); | |
break; | |
} | |
} | |
} | |
// Call the function to unwatch repositories | |
unwatchAllRepos().catch((error) => console. Error(error)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment