Last active
July 18, 2023 09:03
-
-
Save rastislavcore/742b22adb7ae32d16ed1b21d5615a243 to your computer and use it in GitHub Desktop.
Search username in all your GitHub projects (limits may apply; Define Token classic and rights: repo:status, repo_deployment, public_repo, repo:invite as GET `token` parameter in URL or fill in into the form.) Use the script mainly locally.
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>GitHub User Search</title> | |
</head> | |
<body> | |
<h1>GitHub User Search</h1> | |
<form id="searchForm"> | |
<label for="username">Username:</label> | |
<input type="text" id="username" name="username" required> | |
<label for="token">Token:</label> | |
<input type="password" id="token" name="token" required> | |
<button type="submit">Search</button> | |
</form> | |
<ul id="results"></ul> | |
<div id="pagination"> | |
<button id="prevPageButton" disabled>Previous Page</button> | |
<span id="pageInfo">Page <input id="currentPage" type="number" min="1"></span> | |
<button id="nextPageButton" disabled>Next Page</button> | |
</div> | |
<script> | |
const urlParams = new URLSearchParams(window.location.search); | |
const usernameParam = urlParams.get('username'); | |
const tokenParam = urlParams.get('token'); | |
document.getElementById('username').value = usernameParam || ''; | |
document.getElementById('token').value = tokenParam || ''; | |
document.getElementById('searchForm').addEventListener('submit', function(event) { | |
event.preventDefault(); | |
const username = document.getElementById('username').value; | |
const token = document.getElementById('token').value; | |
const resultsElement = document.getElementById('results'); | |
const prevPageButton = document.getElementById('prevPageButton'); | |
const nextPageButton = document.getElementById('nextPageButton'); | |
const currentPageInput = document.getElementById('currentPage'); | |
resultsElement.innerHTML = ''; | |
prevPageButton.disabled = true; | |
nextPageButton.disabled = true; | |
const perPage = 10; | |
function fetchRepos(page) { | |
fetch(`https://api.github.com/user/repos?page=${page}&per_page=${perPage}`, { | |
headers: { | |
'Authorization': `token ${token}` | |
} | |
}) | |
.then(response => { | |
// If the Link header contains 'rel="next"', there are more pages to fetch | |
nextPageButton.disabled = !response.headers.get('Link').includes('rel="next"'); | |
prevPageButton.disabled = page === 1; | |
nextPageButton.onclick = () => fetchRepos(page + 1); | |
prevPageButton.onclick = () => fetchRepos(page - 1); | |
currentPageInput.value = page; | |
currentPageInput.onchange = () => fetchRepos(parseInt(currentPageInput.value)); | |
return response.json(); | |
}) | |
.then(repos => { | |
// Sort the repositories by organization name and then by repository name | |
repos.sort((a, b) => { | |
if (a.owner.login !== b.owner.login) { | |
return a.owner.login.localeCompare(b.owner.login); | |
} | |
return a.name.localeCompare(b.name); | |
}); | |
resultsElement.innerHTML = ''; | |
repos.forEach(repo => { | |
fetch(`https://api.github.com/repos/${repo.owner.login}/${repo.name}/collaborators`, { | |
headers: { | |
'Authorization': `token ${token}` | |
} | |
}) | |
.then(response => response.json()) | |
.then(collaborators => { | |
if (Array.isArray(collaborators) && collaborators.some(collaborator => collaborator.login === username)) { | |
const li = document.createElement('li'); | |
li.innerHTML = `User ${username} found in repository <a href="${repo.html_url}" target="_blank">${repo.name}</a> of organization ${repo.owner.login}. | |
<a href="https://github.com/${repo.owner.login}/${repo.name}/settings/access" target="_blank">Repo Members</a>. | |
<a href="https://github.com/orgs/${repo.owner.login}/people" target="_blank">Organization Members</a>.`; | |
resultsElement.appendChild(li); | |
} | |
}); | |
}); | |
}); | |
} | |
fetchRepos(1); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment