Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update deps, add a dev mode, collect sboms #13

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ jobs:
- run: node index.js
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- run: du -hs public/*
- run: gzip -9v public/sboms.json
- run: du -hs public/*
- run: tar -cf public.tar ./public
- uses: actions/[email protected]
with:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
public/repos.json
public/sboms.json
61 changes: 55 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

import yaml from "js-yaml";
import Octokat from "octokat";
import { Octokit } from "@octokit/core";
import Promise from "bluebird";
import { writeFileSync } from "fs";

const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN,
});

const octo = new Octokat({
token: process.env.GITHUB_TOKEN,
});
Expand All @@ -25,13 +30,15 @@ const formatResult = (result) => {
};

const fetchAll = async (org, args) => {
let response = await octo.orgs(org).repos.fetch({ per_page: 100 });
let response = await octo
.orgs(org)
.repos.fetch({ per_page: process.env.NODE_ENV === "dev" ? 10 : 100 });
let aggregate = [response];

console.log(`fetched page 1 for ${org}`);
let i = 1;
await Promise.delay(50); //slow down to appease github rate limiting
while (response.nextPage) {
while (response.nextPage && process.env.NODE_ENV !== "dev") {
i++;
response = await response.nextPage();
console.log(`fetched page ${i} for ${org}`);
Expand All @@ -49,18 +56,60 @@ const allDepartments = yaml.safeLoad(
).text()
);

const UKDepartments = [].concat(
allDepartments["U.K. Councils"],
allDepartments["U.K. Central"]
);
const UKDepartments =
process.env.NODE_ENV === "dev"
? ["ukhomeoffice"]
: [].concat(
allDepartments["U.K. Councils"],
allDepartments["U.K. Central"]
);

const allReposForAllUKDepartments = await Promise.mapSeries(
UKDepartments,
fetchAll
);

const fetchAllSboms = async (repo) => {
await Promise.delay(5000); //slow down to appease github rate limiting
console.log(`Collecting SBOM for ${repo.owner.login}/${repo.name}`);
try {
return (
await octokit.request("GET /repos/{owner}/{repo}/dependency-graph/sbom", {
owner: repo.owner.login,
repo: repo.name,
headers: {
"X-GitHub-Api-Version": "2022-11-28",
},
})
).data.sbom;
} catch (e) {
if (e.status != 404) {
throw e;
}
}
};

const formatSboms = (sbom) => {
return {
name: sbom.name,
packages: sbom?.packages?.map((pkg) => {
return `${pkg.name}@${pkg.versionInfo}`;
}),
};
};

const formattedResults = allReposForAllUKDepartments.flat(2).map(formatResult);

const allSbomsForAllUKDepartments = await Promise.mapSeries(
allReposForAllUKDepartments.flat(2),
fetchAllSboms
);
const formattedSboms = allSbomsForAllUKDepartments
.filter((sbom) => sbom?.name)
.map(formatSboms);

console.log("writing results to file");

writeFileSync("./public/repos.json", JSON.stringify(formattedResults));
writeFileSync("./public/sboms.json", JSON.stringify(formattedSboms));
console.log("done");
Loading