Skip to content
Merged
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
4 changes: 4 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ updates:
directory: /
schedule:
interval: monthly
cooldown:
default-days: 5
groups:
github-actions:
patterns:
Expand All @@ -14,6 +16,8 @@ updates:
versioning-strategy: widen
schedule:
interval: weekly
cooldown:
default-days: 5
groups:
dependencies:
dependency-type: "production"
Expand Down
92 changes: 46 additions & 46 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
name: Node.js CI
on:
push:
branches: master
pull_request:
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [20.x]
fail-fast: false
steps:
- name: Harden Runner
uses: step-security/harden-runner@0634a2670c59f64b4a01f0f96f84700a4088b9f0 # v2.12.0
with:
egress-policy: audit
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: npm install
- name: Run tests
run: npm run test
automerge:
if: >
github.event_name == 'pull_request' && github.event.pull_request.user.login == 'dependabot[bot]'
needs:
- test
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Merge Dependabot PR
uses: fastify/github-action-merge-dependabot@e820d631adb1d8ab16c3b93e5afe713450884a4a # v3.11.1
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
name: Node.js CI

on:
push:
branches: master
pull_request:

permissions:
contents: read

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [24.x]
fail-fast: false
steps:
- name: Harden Runner
uses: step-security/harden-runner@0634a2670c59f64b4a01f0f96f84700a4088b9f0 # v2.12.0
with:
egress-policy: audit

- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: npm install --ignore-scripts
- name: Run tests
run: npm run test
automerge:
if: >
github.event_name == 'pull_request' && github.event.pull_request.user.login == 'dependabot[bot]'
needs:
- test
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Merge Dependabot PR
uses: fastify/github-action-merge-dependabot@e820d631adb1d8ab16c3b93e5afe713450884a4a # v3.11.1
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
2 changes: 2 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
package-lock=false
ignore-scripts=true
save-exact=true
19 changes: 8 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
"exports": "./dist/index.js",
"types": "./dist/index.d.ts",
"scripts": {
"build": "rimraf ./dist && tsc",
"build": "tsc",
"prepublishOnly": "npm run build",
"test": "glob -c \"tsx --test\" \"./test/**/*.spec.ts\"",
"test": "node --test ./test/**/*.spec.ts",
"coverage": "c8 -r html npm test",
"lint": "eslint src test"
},
Expand Down Expand Up @@ -42,19 +42,16 @@
},
"homepage": "https://github.com/dashlog/fetch-github-repositories#readme",
"devDependencies": {
"@openally/config.eslint": "^2.1.0",
"@openally/config.typescript": "^1.0.3",
"@openally/config.eslint": "2.2.0",
"@openally/config.typescript": "^1.2.1",
"@types/http-link-header": "^1.0.7",
"@types/node": "^22.15.3",
"@types/node": "25.0.3",
"c8": "^10.1.3",
"glob": "^11.0.2",
"rimraf": "^6.0.1",
"tsx": "^4.19.4",
"typescript": "^5.8.3"
"typescript": "5.9.3"
},
"dependencies": {
"@myunisoft/httpie": "^5.0.1",
"combine-async-iterators": "^3.0.0",
"http-link-header": "^1.1.3"
"http-link-header": "^1.1.3",
"undici": "7.16.0"
}
}
105 changes: 105 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Import Third-party Dependencies
import { request } from "undici";
import linkHeader from "http-link-header";
import combineAsyncIterators from "combine-async-iterators";

// Import Internal Dependencies
import type {
Repository,
UserOrg
} from "./github.types.ts";

// CONSTANTS
const kGithubURL = new URL("https://api.github.com/");

export interface FetchOptions {
/**
* @default fetch-github-repo
*/
agent?: string;
token?: string | null;
/**
* @default users
*/
kind?: "users" | "orgs";
/**
* Fetch the repositories of all orgs for a given user
* @default false
*/
fetchUserOrgs?: boolean;
}

export async function* fetchLazy(
namespace: string,
options: FetchOptions = Object.create(null)
): AsyncIterableIterator<Repository> {
if (typeof namespace !== "string") {
throw new TypeError("namespace argument must be typeof string");
}

const {
agent = "fetch-github-repo",
token = null,
kind = "users",
fetchUserOrgs = false
} = options;

const headers = {
"User-Agent": agent,
Accept: "application/vnd.github.v3.raw",
...(typeof token === "string" ? { Authorization: `token ${token}` } : {})
};

let currURL: string | null = `${kind}/${namespace}/repos`;
while (currURL) {
const { body, headers: currHeaders } = await request(
new URL(currURL, kGithubURL),
{ headers }
);
yield* (await body.json() as Repository[]);

currURL = getNextPageUrl(currHeaders);
}

if (kind === "users" && fetchUserOrgs) {
const { body } = await request(
new URL(`users/${namespace}/orgs`, kGithubURL),
{ headers }
);
const data = await body.json() as UserOrg[];

const iterators = data.map(
(row) => fetchLazy(row.login, { agent, token, kind: "orgs" })
);
for await (const repo of combineAsyncIterators(...iterators)) {
yield repo;
}
}
}

export async function fetch(
namespace: string,
options: FetchOptions = Object.create(null)
): Promise<Repository[]> {
const repositories: Repository[] = [];
for await (const repo of fetchLazy(namespace, options)) {
repositories.push(repo);
}

return repositories;
}

function getNextPageUrl(
headers: Record<string, string | string[] | undefined>
): string | null {
const linkValue = headers.link;
if (linkValue === undefined) {
return null;
}

const { refs } = linkHeader.parse(linkValue.toString());

return refs.find(
(row) => row.rel === "next" || row.rel === "last"
)?.uri ?? null;
}
120 changes: 120 additions & 0 deletions src/github.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
export interface Repository {
id: number;
node_id: string;
name: string;
full_name: string;
private: boolean;
owner: {
login: string;
id: number;
node_id: string;
avatar_url: string;
gravatar_id: string;
url: string;
html_url: string;
followers_url: string;
following_url: string;
gists_url: string;
starred_url: string;
subscriptions_url: string;
organizations_url: string;
repos_url: string;
events_url: string;
received_events_url: string;
type: "User" | "Organization";
site_admin: boolean;
};
html_url: string;
description: string;
fork: boolean;
url: string;
forks_url: string;
keys_url: string;
collaborators_url: string;
teams_url: string;
hooks_url: string;
issue_events_url: string;
events_url: string;
assignees_url: string;
branches_url: string;
tags_url: string;
blobs_url: string;
git_tags_url: string;
git_refs_url: string;
trees_url: string;
statuses_url: string;
languages_url: string;
stargazers_url: string;
contributors_url: string;
subscribers_url: string;
subscription_url: string;
commits_url: string;
git_commits_url: string;
comments_url: string;
issue_comment_url: string;
contents_url: string;
compare_url: string;
merges_url: string;
archive_url: string;
downloads_url: string;
issues_url: string;
pulls_url: string;
milestones_url: string;
notifications_url: string;
labels_url: string;
releases_url: string;
deployments_url: string;
created_at: string;
updated_at: string;
pushed_at: string;
git_url: string;
ssh_url: string;
clone_url: string;
svn_url: string;
homepage: string;
size: number;
stargazers_count: number;
watchers_count: number;
language: null | string;
has_issues: boolean;
has_projects: boolean;
has_downloads: boolean;
has_wiki: boolean;
has_pages: boolean;
forks_count: number;
mirror_url: string;
archived: boolean;
disabled: boolean;
open_issues_count: number;
license: {
key: string;
name: string;
spdx_id: string;
url: string;
node_id: string;
} | null;
forks: number;
open_issues: number;
watchers: number;
default_branch: string;
permissions?: {
admin: true;
push: true;
pull: true;
};
}

export interface UserOrg {
login: string;
id: number;
node_id: string;
url: string;
repos_url: string;
events_url: string;
hooks_url: string;
issues_url: string;
members_url: string;
public_members_url: string;
avatar_url: string;
description: string;
}
Loading