Skip to content

Instantly share code, notes, and snippets.

@WritingPanda
Last active September 12, 2024 00:10
Show Gist options
  • Save WritingPanda/890d461b23c9827b97e09043f5390e86 to your computer and use it in GitHub Desktop.
Save WritingPanda/890d461b23c9827b97e09043f5390e86 to your computer and use it in GitHub Desktop.
Script to get all repos with CodeQL compatible languages
GITHUB_TOKEN=YOUR TOKEN HERE
// You will need a github token to use this script
// GitHub token will need the repo permissions and organization read permissions
// Put the token in the .env file
// Get the package.json file and run npm install
// Replace "YOUR ORG NAME HERE" with your org name
// Output should look something like this
// Organization: (your org name)
// Number of Repos: 2536
// Number of CodeQL Compatible Repos: 1522
// Uncomment the last line of this file if you want to see the full list of
// repos with CodeQL compatible languages
// May or may not be useful for you as it can be a long list
import { Octokit } from "octokit";
import "dotenv/config";
const ORG_NAME = "YOUR ORG NAME HERE";
const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN,
org: ORG_NAME,
});
let outputData = {
numberOfRepos: 0,
numberOfCodeQLCompatibleRepos: 0,
listOfCodeQLCompatibleRepos: [],
};
const codeQLLanguages = ["C++", "C", "C#", "Go", "Java", "JavaScript", "Python", "TypeScript", "Vue"];
const iterator = octokit.paginate.iterator("GET /orgs/{org}/repos", {
org: ORG_NAME,
headers: {
"X-GitHub-Api-Version": "2022-11-28"
},
});
let listOfRepos = [];
for await (const { data } of iterator) {
for (const repo of data) {
listOfRepos = [...listOfRepos, {
id: repo.id,
name: repo.name,
languages: await octokit.request("GET /repos/{org}/{repo}/languages", {
org: ORG_NAME,
repo: repo.name,
headers: {
"X-GitHub-Api-Version": "2022-11-28"
}
}).then(response => response.data)
}];
}
}
outputData.numberOfRepos = listOfRepos.length;
for (const repo of listOfRepos) {
for (const language of Object.keys(repo.languages)) {
if (codeQLLanguages.includes(language)) {
outputData.numberOfCodeQLCompatibleRepos += 1;
outputData.listOfCodeQLCompatibleRepos.push(repo.name);
break;
}
}
};
console.log(`Organization: ${ORG_NAME}`);
console.log(`Number of Repos: ${outputData.numberOfRepos}`);
console.log(`Number of CodeQL Compatible Repos: ${outputData.numberOfCodeQLCompatibleRepos}`);
// console.log(`List of CodeQL Compatible Repos: ${outputData.listOfRepos}`);
{
"name": "get-languages-in-org",
"version": "1.0.0",
"description": "Get all repos in an org, get the count, and then the count of repos with codeql-supported languages",
"main": "index.js",
"type": "module",
"scripts": {
"start": "node index.js"
},
"author": "Leonardo Quimbaya",
"license": "ISC",
"dependencies": {
"dotenv": "^16.4.5",
"octokit": "^4.0.2",
"typescript": "^5.6.2"
},
"devDependencies": {
"@types/node": "^22.5.4"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment