repositron 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +99 -2
- package/dist/auth/check.js +32 -0
- package/dist/auth/config.js +31 -0
- package/dist/auth/device-flow.js +131 -0
- package/dist/cli.js +72 -0
- package/dist/github/client.js +102 -0
- package/dist/github/fetch.js +110 -0
- package/dist/github/repos.js +156 -0
- package/dist/insights/activity.js +152 -0
- package/dist/insights/bus-factor.js +56 -0
- package/dist/insights/ci.js +97 -0
- package/dist/insights/context.js +1 -0
- package/dist/insights/dependencies.js +300 -0
- package/dist/insights/dependency-check.js +79 -0
- package/dist/insights/dependency-registry.js +63 -0
- package/dist/insights/detectors/adoption.js +13 -0
- package/dist/insights/detectors/black.detector.js +75 -0
- package/dist/insights/detectors/dependabot.detector.js +41 -0
- package/dist/insights/detectors/eslint.detector.js +70 -0
- package/dist/insights/detectors/github-actions.detector.js +34 -0
- package/dist/insights/detectors/go-test.detector.js +68 -0
- package/dist/insights/detectors/golangci-lint.detector.js +64 -0
- package/dist/insights/detectors/jest.detector.js +85 -0
- package/dist/insights/detectors/phpunit.detector.js +74 -0
- package/dist/insights/detectors/pre-commit.detector.js +46 -0
- package/dist/insights/detectors/prettier.detector.js +70 -0
- package/dist/insights/detectors/pytest.detector.js +75 -0
- package/dist/insights/detectors/renovate.detector.js +60 -0
- package/dist/insights/detectors/ruff.detector.js +59 -0
- package/dist/insights/health.js +59 -0
- package/dist/insights/osv.js +116 -0
- package/dist/insights/semver.js +68 -0
- package/dist/insights/tools.js +130 -0
- package/dist/insights/types.js +1 -0
- package/dist/scan.js +281 -0
- package/dist/ui/App.js +59 -0
- package/dist/ui/components/Badge.js +6 -0
- package/dist/ui/components/Header.js +7 -0
- package/dist/ui/components/ProgressBar.js +10 -0
- package/dist/ui/components/Sparkline.js +13 -0
- package/dist/ui/components/StatCard.js +6 -0
- package/dist/ui/components/StatusMessage.js +12 -0
- package/dist/ui/format.js +46 -0
- package/dist/ui/glyphs.js +24 -0
- package/dist/ui/screens/Dashboard.js +34 -0
- package/dist/ui/screens/RepoPicker.js +83 -0
- package/dist/ui/screens/ScanningScreen.js +36 -0
- package/dist/ui/tabs/ActivityTab.js +25 -0
- package/dist/ui/tabs/CiTab.js +23 -0
- package/dist/ui/tabs/DependenciesTab.js +62 -0
- package/dist/ui/tabs/OverviewTab.js +24 -0
- package/dist/ui/tabs/ToolsTab.js +23 -0
- package/dist/ui/theme.js +20 -0
- package/package.json +50 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 repositron contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,100 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Repositron
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
An interactive terminal dashboard for GitHub repository health.
|
|
4
|
+
|
|
5
|
+
Repositron is a local-first CLI. It logs in to GitHub once, lets you pick a
|
|
6
|
+
repository you own or have contributed to, scans it on demand, and presents
|
|
7
|
+
adoption, activity, bus factor, dependency, and CI insights in a
|
|
8
|
+
keyboard-driven terminal UI. There is no database, hosted backend, or worker to
|
|
9
|
+
run.
|
|
10
|
+
|
|
11
|
+
## Quick start
|
|
12
|
+
|
|
13
|
+
Requires Node.js 22 or newer.
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
npx repositron
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
On first run Repositron starts GitHub's OAuth Device Flow:
|
|
20
|
+
|
|
21
|
+
1. It opens GitHub in your browser.
|
|
22
|
+
2. Enter the short code shown in the terminal.
|
|
23
|
+
3. Authorize the request. The token is stored locally for next time.
|
|
24
|
+
|
|
25
|
+
The picker lists repositories you own plus repositories you contributed to in
|
|
26
|
+
the last 12 months. Organization membership alone does not add a repository to
|
|
27
|
+
the list.
|
|
28
|
+
|
|
29
|
+
## Authentication
|
|
30
|
+
|
|
31
|
+
Repositron checks credentials in this order:
|
|
32
|
+
|
|
33
|
+
1. `GITHUB_TOKEN` or `GH_TOKEN`
|
|
34
|
+
2. The saved session at `~/.config/repositron/config.json`
|
|
35
|
+
3. The GitHub CLI config at `~/.config/gh/hosts.yml`
|
|
36
|
+
4. GitHub OAuth Device Flow
|
|
37
|
+
|
|
38
|
+
The device flow requests `public_repo`, `read:user`, and `read:org` scopes. It
|
|
39
|
+
uses GitHub's public client ID by default; set
|
|
40
|
+
`REPOSITRON_GITHUB_CLIENT_ID` or `GITHUB_CLIENT_ID` to use your own OAuth app.
|
|
41
|
+
For local testing, `repositron --token <token>` accepts a personal access token
|
|
42
|
+
without saving it.
|
|
43
|
+
|
|
44
|
+
No token is sent anywhere except the GitHub API. The saved token lives in your
|
|
45
|
+
user config directory, protected by your operating system account permissions.
|
|
46
|
+
|
|
47
|
+
## What gets scanned
|
|
48
|
+
|
|
49
|
+
- Tool adoption across 13 detectors: Jest, ESLint, Prettier, pytest, Black,
|
|
50
|
+
Ruff, pre-commit, GitHub Actions, Dependabot, Renovate, `go test`,
|
|
51
|
+
golangci-lint, and PHPUnit.
|
|
52
|
+
- Bus factor and contributor concentration using the Gini coefficient and
|
|
53
|
+
top 1/3/10 shares.
|
|
54
|
+
- Commit activity by day, week, and contributor over 30, 90, 180, and 365 day
|
|
55
|
+
windows.
|
|
56
|
+
- GitHub Actions success rates, average durations, and recent failures.
|
|
57
|
+
- Direct dependencies parsed from local manifests such as `package.json`,
|
|
58
|
+
`requirements.txt`, `pyproject.toml`, `go.mod`, `Gemfile`, and
|
|
59
|
+
`composer.json`.
|
|
60
|
+
- Latest versions from npm, PyPI, the Go module proxy, RubyGems, and Packagist.
|
|
61
|
+
|
|
62
|
+
Scans are intentionally bounded to keep the CLI responsive: the 30 most recent
|
|
63
|
+
commit diffs, up to 500 commits, up to 200 contributors, up to 300 workflow
|
|
64
|
+
runs, and up to 300 parsed dependencies. Registry lookups run eight at a time
|
|
65
|
+
with a 10 second timeout per request. See
|
|
66
|
+
[docs/insights.md](docs/insights.md) for the metric definitions and their
|
|
67
|
+
limitations.
|
|
68
|
+
|
|
69
|
+
## Terminal rendering
|
|
70
|
+
|
|
71
|
+
The UI uses ANSI colors, rounded borders, thin meters, and Unicode glyphs. It
|
|
72
|
+
does not print emojis. Secondary text is rendered in white rather than dark
|
|
73
|
+
grey so it stays readable on dark terminals. Set `REPOSITRON_ASCII=1` to swap
|
|
74
|
+
meters and marker glyphs for ASCII equivalents on limited terminals.
|
|
75
|
+
|
|
76
|
+
## Install from source
|
|
77
|
+
|
|
78
|
+
```sh
|
|
79
|
+
git clone --branch cli --single-branch https://github.com/zelvarys/repositron-dt.git repositron
|
|
80
|
+
cd repositron
|
|
81
|
+
npm install
|
|
82
|
+
npm run build
|
|
83
|
+
node dist/cli.js
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
During development:
|
|
87
|
+
|
|
88
|
+
```sh
|
|
89
|
+
npm run dev
|
|
90
|
+
npm test
|
|
91
|
+
npm run typecheck
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
See [docs/development.md](docs/development.md) for testing with a personal
|
|
95
|
+
access token and [docs/architecture.md](docs/architecture.md) for the code
|
|
96
|
+
layout.
|
|
97
|
+
|
|
98
|
+
## License
|
|
99
|
+
|
|
100
|
+
MIT
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import { homedir } from 'node:os';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { getStoredToken } from './config.js';
|
|
5
|
+
import { runDeviceFlow } from './device-flow.js';
|
|
6
|
+
export async function readGhCliToken() {
|
|
7
|
+
const configDir = process.env.GH_CONFIG_DIR ?? join(homedir(), '.config', 'gh');
|
|
8
|
+
try {
|
|
9
|
+
const hosts = await readFile(join(configDir, 'hosts.yml'), 'utf8');
|
|
10
|
+
const match = /oauth_token:\s*["']?([^"'\s]+)["']?/.exec(hosts);
|
|
11
|
+
return match?.[1] ?? null;
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export async function resolveAuth() {
|
|
18
|
+
const envToken = process.env.GITHUB_TOKEN ?? process.env.GH_TOKEN;
|
|
19
|
+
if (envToken?.trim()) {
|
|
20
|
+
return { token: envToken.trim(), source: 'env' };
|
|
21
|
+
}
|
|
22
|
+
const stored = getStoredToken();
|
|
23
|
+
if (stored?.trim()) {
|
|
24
|
+
return { token: stored.trim(), source: 'config' };
|
|
25
|
+
}
|
|
26
|
+
const ghToken = await readGhCliToken();
|
|
27
|
+
if (ghToken) {
|
|
28
|
+
return { token: ghToken, source: 'gh-cli' };
|
|
29
|
+
}
|
|
30
|
+
const token = await runDeviceFlow();
|
|
31
|
+
return { token, source: 'device-flow' };
|
|
32
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { homedir } from 'node:os';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import Conf from 'conf';
|
|
4
|
+
export const config = new Conf({
|
|
5
|
+
projectName: 'repositron',
|
|
6
|
+
cwd: join(homedir(), '.config', 'repositron'),
|
|
7
|
+
});
|
|
8
|
+
export function getStoredToken() {
|
|
9
|
+
return config.get('token');
|
|
10
|
+
}
|
|
11
|
+
export function getStoredUsername() {
|
|
12
|
+
return config.get('username');
|
|
13
|
+
}
|
|
14
|
+
export function saveToken(token, username) {
|
|
15
|
+
config.set({
|
|
16
|
+
token,
|
|
17
|
+
savedAt: new Date().toISOString(),
|
|
18
|
+
...(username ? { username } : {}),
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
export function saveUsername(username) {
|
|
22
|
+
config.set('username', username);
|
|
23
|
+
}
|
|
24
|
+
export function clearToken() {
|
|
25
|
+
config.delete('token');
|
|
26
|
+
config.delete('username');
|
|
27
|
+
config.delete('savedAt');
|
|
28
|
+
}
|
|
29
|
+
export function getConfigPath() {
|
|
30
|
+
return config.path;
|
|
31
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
import open from 'open';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { saveToken } from './config.js';
|
|
5
|
+
const DEVICE_CODE_URL = 'https://github.com/login/device/code';
|
|
6
|
+
const ACCESS_TOKEN_URL = 'https://github.com/login/oauth/access_token';
|
|
7
|
+
const DEFAULT_CLIENT_ID = 'Ov23liFJY12v9rTh1fZc';
|
|
8
|
+
const REQUEST_TIMEOUT_MS = 20_000;
|
|
9
|
+
const MIN_POLL_INTERVAL_SECONDS = 5;
|
|
10
|
+
const deviceCodeSchema = z.object({
|
|
11
|
+
device_code: z.string(),
|
|
12
|
+
user_code: z.string(),
|
|
13
|
+
verification_uri: z.string(),
|
|
14
|
+
verification_uri_complete: z.string().optional(),
|
|
15
|
+
expires_in: z.coerce.number(),
|
|
16
|
+
interval: z.coerce.number().optional(),
|
|
17
|
+
});
|
|
18
|
+
const tokenSchema = z.object({
|
|
19
|
+
access_token: z.string().optional(),
|
|
20
|
+
token_type: z.string().optional(),
|
|
21
|
+
scope: z.string().optional(),
|
|
22
|
+
error: z.string().optional(),
|
|
23
|
+
error_description: z.string().optional(),
|
|
24
|
+
interval: z.coerce.number().optional(),
|
|
25
|
+
});
|
|
26
|
+
export class DeviceFlowError extends Error {
|
|
27
|
+
constructor(message) {
|
|
28
|
+
super(message);
|
|
29
|
+
this.name = 'DeviceFlowError';
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export function getClientId() {
|
|
33
|
+
return (process.env.REPOSITRON_GITHUB_CLIENT_ID ??
|
|
34
|
+
process.env.GITHUB_CLIENT_ID ??
|
|
35
|
+
DEFAULT_CLIENT_ID);
|
|
36
|
+
}
|
|
37
|
+
async function postForm(url, body) {
|
|
38
|
+
const response = await fetch(url, {
|
|
39
|
+
method: 'POST',
|
|
40
|
+
headers: {
|
|
41
|
+
Accept: 'application/json',
|
|
42
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
43
|
+
'User-Agent': 'repositron',
|
|
44
|
+
},
|
|
45
|
+
body: new URLSearchParams(body).toString(),
|
|
46
|
+
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
47
|
+
});
|
|
48
|
+
const text = await response.text();
|
|
49
|
+
let data;
|
|
50
|
+
try {
|
|
51
|
+
data = JSON.parse(text);
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
throw new DeviceFlowError(`GitHub returned an unreadable response (${response.status}).`);
|
|
55
|
+
}
|
|
56
|
+
if (!response.ok) {
|
|
57
|
+
const error = data;
|
|
58
|
+
throw new DeviceFlowError(`GitHub device flow failed (${response.status}): ${error.error_description ?? error.error ?? text.slice(0, 160)}`);
|
|
59
|
+
}
|
|
60
|
+
return data;
|
|
61
|
+
}
|
|
62
|
+
function sleep(milliseconds) {
|
|
63
|
+
return new Promise((resolve) => setTimeout(resolve, milliseconds));
|
|
64
|
+
}
|
|
65
|
+
async function requestDeviceCode(clientId) {
|
|
66
|
+
const data = await postForm(DEVICE_CODE_URL, {
|
|
67
|
+
client_id: clientId,
|
|
68
|
+
scope: 'public_repo read:user read:org',
|
|
69
|
+
});
|
|
70
|
+
const parsed = deviceCodeSchema.safeParse(data);
|
|
71
|
+
if (!parsed.success) {
|
|
72
|
+
throw new DeviceFlowError('GitHub returned an invalid device code response.');
|
|
73
|
+
}
|
|
74
|
+
return parsed.data;
|
|
75
|
+
}
|
|
76
|
+
async function requestAccessToken(clientId, deviceCode) {
|
|
77
|
+
const data = await postForm(ACCESS_TOKEN_URL, {
|
|
78
|
+
client_id: clientId,
|
|
79
|
+
device_code: deviceCode,
|
|
80
|
+
grant_type: 'urn:ietf:params:oauth:grant-type:device_code',
|
|
81
|
+
});
|
|
82
|
+
const parsed = tokenSchema.safeParse(data);
|
|
83
|
+
if (!parsed.success) {
|
|
84
|
+
throw new DeviceFlowError('GitHub returned an invalid token response.');
|
|
85
|
+
}
|
|
86
|
+
return parsed.data;
|
|
87
|
+
}
|
|
88
|
+
export async function runDeviceFlow() {
|
|
89
|
+
const clientId = getClientId();
|
|
90
|
+
const device = await requestDeviceCode(clientId);
|
|
91
|
+
const verificationUrl = device.verification_uri_complete ?? device.verification_uri;
|
|
92
|
+
let interval = Math.max(MIN_POLL_INTERVAL_SECONDS, device.interval ?? MIN_POLL_INTERVAL_SECONDS);
|
|
93
|
+
console.log();
|
|
94
|
+
console.log(chalk.bold('GitHub login required'));
|
|
95
|
+
console.log(`1. Open ${chalk.cyan(verificationUrl)}`);
|
|
96
|
+
console.log(`2. Enter code ${chalk.bold.green(device.user_code)}`);
|
|
97
|
+
console.log();
|
|
98
|
+
console.log(chalk.gray('Waiting for authorization...'));
|
|
99
|
+
try {
|
|
100
|
+
await open(verificationUrl);
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
// The URL is printed above; opening a browser is best-effort.
|
|
104
|
+
}
|
|
105
|
+
const expiresAt = Date.now() + device.expires_in * 1000;
|
|
106
|
+
while (Date.now() < expiresAt) {
|
|
107
|
+
const token = await requestAccessToken(clientId, device.device_code);
|
|
108
|
+
if (token.access_token) {
|
|
109
|
+
saveToken(token.access_token);
|
|
110
|
+
console.log(chalk.green('Authorized. Starting Repositron.'));
|
|
111
|
+
return token.access_token;
|
|
112
|
+
}
|
|
113
|
+
if (token.error === 'authorization_pending') {
|
|
114
|
+
await sleep(interval * 1000);
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
if (token.error === 'slow_down') {
|
|
118
|
+
interval += token.interval ?? 5;
|
|
119
|
+
await sleep(interval * 1000);
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
if (token.error === 'expired_token') {
|
|
123
|
+
throw new DeviceFlowError('The device code expired. Run repositron again.');
|
|
124
|
+
}
|
|
125
|
+
if (token.error === 'access_denied') {
|
|
126
|
+
throw new DeviceFlowError('GitHub authorization was denied.');
|
|
127
|
+
}
|
|
128
|
+
throw new DeviceFlowError(`GitHub authorization failed: ${token.error_description ?? token.error ?? 'unknown error'}`);
|
|
129
|
+
}
|
|
130
|
+
throw new DeviceFlowError('The device code expired. Run repositron again.');
|
|
131
|
+
}
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import chalk from 'chalk';
|
|
3
|
+
import { render } from 'ink';
|
|
4
|
+
import meow from 'meow';
|
|
5
|
+
import { createElement } from 'react';
|
|
6
|
+
import { resolveAuth } from './auth/check.js';
|
|
7
|
+
import { clearToken, saveUsername, } from './auth/config.js';
|
|
8
|
+
import { runDeviceFlow } from './auth/device-flow.js';
|
|
9
|
+
import { GitHubAuthError, GitHubClient, getAuthenticatedUser, } from './github/client.js';
|
|
10
|
+
import { App } from './ui/App.js';
|
|
11
|
+
const cli = meow(`
|
|
12
|
+
Usage
|
|
13
|
+
$ repositron
|
|
14
|
+
|
|
15
|
+
Options
|
|
16
|
+
--token <token> Use a GitHub token instead of the saved session
|
|
17
|
+
--help Show this help
|
|
18
|
+
--version Show the version
|
|
19
|
+
|
|
20
|
+
Repositron is an interactive terminal dashboard for GitHub repositories.
|
|
21
|
+
On first run it starts GitHub's device flow and stores the session in
|
|
22
|
+
~/.config/repositron/config.json.
|
|
23
|
+
`, {
|
|
24
|
+
importMeta: import.meta,
|
|
25
|
+
flags: {
|
|
26
|
+
token: { type: 'string' },
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
async function authenticate() {
|
|
30
|
+
let auth = cli.flags.token
|
|
31
|
+
? { token: cli.flags.token, source: 'env' }
|
|
32
|
+
: await resolveAuth();
|
|
33
|
+
try {
|
|
34
|
+
const user = await getAuthenticatedUser(new GitHubClient(auth.token));
|
|
35
|
+
saveUsername(user.login);
|
|
36
|
+
return { token: auth.token, username: user.login };
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
if (!(error instanceof GitHubAuthError))
|
|
40
|
+
throw error;
|
|
41
|
+
if (cli.flags.token || auth.source === 'env') {
|
|
42
|
+
throw new Error('The GitHub token is invalid or expired.');
|
|
43
|
+
}
|
|
44
|
+
clearToken();
|
|
45
|
+
auth = { token: await runDeviceFlow(), source: 'device-flow' };
|
|
46
|
+
const user = await getAuthenticatedUser(new GitHubClient(auth.token));
|
|
47
|
+
saveUsername(user.login);
|
|
48
|
+
return { token: auth.token, username: user.login };
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function clearTerminal() {
|
|
52
|
+
if (process.stdout.isTTY) {
|
|
53
|
+
process.stdout.write('\u001B[2J\u001B[3J\u001B[H');
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
async function main() {
|
|
57
|
+
clearTerminal();
|
|
58
|
+
const auth = await authenticate();
|
|
59
|
+
let instance = null;
|
|
60
|
+
const clearScreen = () => {
|
|
61
|
+
if (!instance)
|
|
62
|
+
return;
|
|
63
|
+
instance.clear();
|
|
64
|
+
clearTerminal();
|
|
65
|
+
};
|
|
66
|
+
instance = render(createElement(App, { ...auth, clearScreen }));
|
|
67
|
+
}
|
|
68
|
+
main().catch((error) => {
|
|
69
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
70
|
+
console.error(chalk.red(`Repositron failed: ${message}`));
|
|
71
|
+
process.exitCode = 1;
|
|
72
|
+
});
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
const API_URL = 'https://api.github.com';
|
|
2
|
+
const DEFAULT_TIMEOUT_MS = 20_000;
|
|
3
|
+
export class GitHubApiError extends Error {
|
|
4
|
+
status;
|
|
5
|
+
constructor(message, status) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = 'GitHubApiError';
|
|
8
|
+
this.status = status;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export class GitHubAuthError extends GitHubApiError {
|
|
12
|
+
constructor(message = 'GitHub rejected the access token.') {
|
|
13
|
+
super(message, 401);
|
|
14
|
+
this.name = 'GitHubAuthError';
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export class GitHubRateLimitError extends GitHubApiError {
|
|
18
|
+
resetAt;
|
|
19
|
+
constructor(resetAt) {
|
|
20
|
+
super(`GitHub rate limit exhausted. Resets at ${resetAt.toISOString()}.`, 403);
|
|
21
|
+
this.name = 'GitHubRateLimitError';
|
|
22
|
+
this.resetAt = resetAt;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export class GitHubTimeoutError extends GitHubApiError {
|
|
26
|
+
constructor(label, timeoutMs) {
|
|
27
|
+
super(`GitHub request timed out after ${timeoutMs}ms: ${label}`, 504);
|
|
28
|
+
this.name = 'GitHubTimeoutError';
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export class GitHubClient {
|
|
32
|
+
token;
|
|
33
|
+
baseUrl;
|
|
34
|
+
timeoutMs;
|
|
35
|
+
fetchImpl;
|
|
36
|
+
constructor(token, options = {}) {
|
|
37
|
+
this.token = token;
|
|
38
|
+
this.baseUrl = options.baseUrl ?? API_URL;
|
|
39
|
+
this.timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
40
|
+
this.fetchImpl = options.fetchImpl ?? fetch;
|
|
41
|
+
}
|
|
42
|
+
async request(path, init = {}) {
|
|
43
|
+
let response;
|
|
44
|
+
try {
|
|
45
|
+
response = await this.fetchImpl(`${this.baseUrl}${path}`, {
|
|
46
|
+
...init,
|
|
47
|
+
headers: {
|
|
48
|
+
Accept: 'application/vnd.github+json',
|
|
49
|
+
Authorization: `Bearer ${this.token}`,
|
|
50
|
+
'X-GitHub-Api-Version': '2022-11-28',
|
|
51
|
+
'User-Agent': 'repositron',
|
|
52
|
+
...(init.headers ?? {}),
|
|
53
|
+
},
|
|
54
|
+
signal: AbortSignal.timeout(this.timeoutMs),
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
if (error instanceof DOMException &&
|
|
59
|
+
(error.name === 'TimeoutError' || error.name === 'AbortError')) {
|
|
60
|
+
throw new GitHubTimeoutError(path, this.timeoutMs);
|
|
61
|
+
}
|
|
62
|
+
throw error;
|
|
63
|
+
}
|
|
64
|
+
if (!response.ok) {
|
|
65
|
+
const body = await response.text();
|
|
66
|
+
const remaining = response.headers.get('x-ratelimit-remaining');
|
|
67
|
+
if (response.status === 401) {
|
|
68
|
+
throw new GitHubAuthError();
|
|
69
|
+
}
|
|
70
|
+
if ((response.status === 403 || response.status === 429) &&
|
|
71
|
+
remaining === '0') {
|
|
72
|
+
const reset = Number(response.headers.get('x-ratelimit-reset'));
|
|
73
|
+
throw new GitHubRateLimitError(new Date(Number.isFinite(reset) ? reset * 1000 : Date.now()));
|
|
74
|
+
}
|
|
75
|
+
throw new GitHubApiError(`GitHub API ${response.status} on ${path}: ${body.slice(0, 200)}`, response.status);
|
|
76
|
+
}
|
|
77
|
+
if (response.status === 204) {
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
return (await response.json());
|
|
81
|
+
}
|
|
82
|
+
get(path) {
|
|
83
|
+
return this.request(path);
|
|
84
|
+
}
|
|
85
|
+
async graphql(query, variables = {}) {
|
|
86
|
+
const result = await this.request('/graphql', {
|
|
87
|
+
method: 'POST',
|
|
88
|
+
headers: { 'Content-Type': 'application/json' },
|
|
89
|
+
body: JSON.stringify({ query, variables }),
|
|
90
|
+
});
|
|
91
|
+
if (result.errors && result.errors.length > 0) {
|
|
92
|
+
throw new GitHubApiError(`GitHub GraphQL error: ${result.errors[0].message}`, 502);
|
|
93
|
+
}
|
|
94
|
+
if (!result.data) {
|
|
95
|
+
throw new GitHubApiError('GitHub GraphQL returned no data', 502);
|
|
96
|
+
}
|
|
97
|
+
return result.data;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
export async function getAuthenticatedUser(client) {
|
|
101
|
+
return client.get('/user');
|
|
102
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { GitHubApiError } from './client.js';
|
|
2
|
+
import { parseDependencies, } from '../insights/dependencies.js';
|
|
3
|
+
function encodePath(path) {
|
|
4
|
+
return path
|
|
5
|
+
.split('/')
|
|
6
|
+
.filter(Boolean)
|
|
7
|
+
.map((segment) => encodeURIComponent(segment))
|
|
8
|
+
.join('/');
|
|
9
|
+
}
|
|
10
|
+
export async function fetchCommits(client, owner, repo, branch, maxPages = 5) {
|
|
11
|
+
const all = [];
|
|
12
|
+
for (let page = 1; page <= maxPages; page += 1) {
|
|
13
|
+
const batch = await client.get(`/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/commits?sha=${encodeURIComponent(branch)}&per_page=100&page=${page}`);
|
|
14
|
+
all.push(...batch);
|
|
15
|
+
if (batch.length < 100)
|
|
16
|
+
break;
|
|
17
|
+
}
|
|
18
|
+
return all;
|
|
19
|
+
}
|
|
20
|
+
export async function fetchCommitDetail(client, owner, repo, sha) {
|
|
21
|
+
return client.get(`/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/commits/${encodeURIComponent(sha)}`);
|
|
22
|
+
}
|
|
23
|
+
export async function fetchContributors(client, owner, repo, maxPages = 2) {
|
|
24
|
+
const all = [];
|
|
25
|
+
for (let page = 1; page <= maxPages; page += 1) {
|
|
26
|
+
const batch = await client.get(`/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/contributors?per_page=100&page=${page}&anon=false`);
|
|
27
|
+
all.push(...batch);
|
|
28
|
+
if (batch.length < 100)
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
return all;
|
|
32
|
+
}
|
|
33
|
+
export async function fetchContents(client, owner, repo, path, ref) {
|
|
34
|
+
const encoded = encodePath(path);
|
|
35
|
+
try {
|
|
36
|
+
const data = await client.get(`/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/contents/${encoded}?ref=${encodeURIComponent(ref)}`);
|
|
37
|
+
return Array.isArray(data) ? data : [data];
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
if (error instanceof GitHubApiError && error.status === 404)
|
|
41
|
+
return [];
|
|
42
|
+
throw error;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
export async function readFileText(client, owner, repo, path, ref) {
|
|
46
|
+
const encoded = encodePath(path);
|
|
47
|
+
try {
|
|
48
|
+
const data = await client.get(`/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/contents/${encoded}?ref=${encodeURIComponent(ref)}`);
|
|
49
|
+
if (Array.isArray(data))
|
|
50
|
+
return null;
|
|
51
|
+
if (data.type !== 'file' || data.encoding !== 'base64')
|
|
52
|
+
return null;
|
|
53
|
+
return Buffer.from(data.content, 'base64').toString('utf8');
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
if (error instanceof GitHubApiError && error.status === 404)
|
|
57
|
+
return null;
|
|
58
|
+
throw error;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
export async function fetchReadme(client, owner, repo, ref) {
|
|
62
|
+
const suffix = ref ? `?ref=${encodeURIComponent(ref)}` : '';
|
|
63
|
+
const data = await client.get(`/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/readme${suffix}`);
|
|
64
|
+
if (!data.content || data.encoding !== 'base64')
|
|
65
|
+
return null;
|
|
66
|
+
return Buffer.from(data.content, 'base64').toString('utf8');
|
|
67
|
+
}
|
|
68
|
+
export async function fetchRepoTree(client, owner, repo, branch) {
|
|
69
|
+
const data = await client.get(`/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/git/trees/${encodeURIComponent(branch)}?recursive=1`);
|
|
70
|
+
return {
|
|
71
|
+
...data,
|
|
72
|
+
tree: Array.isArray(data.tree) ? data.tree : [],
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
export function isDependabotWorkflowRun(run) {
|
|
76
|
+
return [run.name, run.display_title, run.actor?.login, run.path].some((value) => value !== null && value !== undefined && /dependabot/i.test(value));
|
|
77
|
+
}
|
|
78
|
+
export async function fetchWorkflowRuns(client, owner, repo, maxPages = 3) {
|
|
79
|
+
const all = [];
|
|
80
|
+
for (let page = 1; page <= maxPages; page += 1) {
|
|
81
|
+
const batch = await client.get(`/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/actions/runs?per_page=100&page=${page}`);
|
|
82
|
+
const runs = batch.workflow_runs ?? [];
|
|
83
|
+
all.push(...runs.filter((run) => !isDependabotWorkflowRun(run)));
|
|
84
|
+
if (runs.length < 100)
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
return all;
|
|
88
|
+
}
|
|
89
|
+
const DEFAULT_DEPENDENCY_MANIFESTS = [
|
|
90
|
+
'package.json',
|
|
91
|
+
'requirements.txt',
|
|
92
|
+
'requirements-dev.txt',
|
|
93
|
+
'dev-requirements.txt',
|
|
94
|
+
'pyproject.toml',
|
|
95
|
+
'setup.cfg',
|
|
96
|
+
'setup.py',
|
|
97
|
+
'Pipfile',
|
|
98
|
+
'go.mod',
|
|
99
|
+
'Gemfile',
|
|
100
|
+
'composer.json',
|
|
101
|
+
];
|
|
102
|
+
export async function fetchDependencies(client, owner, repo, branch, paths = DEFAULT_DEPENDENCY_MANIFESTS) {
|
|
103
|
+
const configFiles = {};
|
|
104
|
+
for (const path of paths) {
|
|
105
|
+
const content = await readFileText(client, owner, repo, path, branch);
|
|
106
|
+
if (content !== null)
|
|
107
|
+
configFiles[path] = content;
|
|
108
|
+
}
|
|
109
|
+
return parseDependencies(configFiles);
|
|
110
|
+
}
|