@jmanuelcorral/bond-app 0.1.5 → 0.2.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/README.md +280 -23
- package/app/brand/TOKENS.md +5 -5
- package/app/layouts/bond.json +10 -14
- package/app/mods/bond-agents/api.js +35 -0
- package/app/mods/bond-agents/branch.svg +1 -0
- package/app/mods/bond-agents/brand.js +53 -2
- package/app/mods/bond-agents/config.js +4 -0
- package/app/mods/bond-agents/eye-off.svg +1 -0
- package/app/mods/bond-agents/eye.svg +1 -0
- package/app/mods/bond-agents/main.js +308 -327
- package/app/mods/bond-agents/model.js +376 -5
- package/app/mods/bond-board/api.js +35 -0
- package/app/mods/bond-board/branch.svg +1 -0
- package/app/mods/bond-board/brand.js +53 -2
- package/app/mods/bond-board/config.js +4 -0
- package/app/mods/bond-board/main.js +1000 -449
- package/app/mods/bond-board/model.js +170 -28
- package/app/mods/bond-pane/close.svg +1 -0
- package/app/mods/bond-pane/hide.svg +1 -0
- package/app/mods/bond-pane/main.js +103 -0
- package/app/mods/bond-pane/mod.json +1 -0
- package/app/mods/bond-pane/model.js +18 -0
- package/app/mods/bond-pane/split-down.svg +1 -0
- package/app/mods/bond-pane/split-right.svg +1 -0
- package/app/mods/bond-pane/unzoom.svg +1 -0
- package/app/mods/bond-pane/zoom.svg +1 -0
- package/app/mods/bond-settings/api.js +35 -0
- package/app/mods/bond-settings/brand.js +53 -2
- package/app/mods/bond-settings/config.js +4 -0
- package/app/mods/bond-settings/main.js +1216 -272
- package/app/mods/bond-settings/mod.json +1 -1
- package/app/mods/bond-status/api.js +35 -0
- package/app/mods/bond-status/brand.js +53 -2
- package/app/mods/bond-status/config.js +4 -0
- package/app/mods/bond-status/main.js +37 -15
- package/app/mods/bond-status/model.js +19 -1
- package/app/mods/bond-tabs/brand.js +53 -2
- package/app/mods/bond-tabs/main.js +45 -3
- package/app/mods/bond-titlebar/brand.js +53 -2
- package/app/mods/bond-titlebar/main.js +85 -29
- package/app/mods/bond-titlebar/version.js +3 -0
- package/app/themes/bond-dark.json +5 -5
- package/app/themes/bond-light.json +5 -5
- package/package.json +1 -1
- package/runner/Dockerfile +16 -2
- package/runner/SPIKE-BOND.md +6 -2
- package/runner/entrypoint.sh +16 -0
- package/runner/setup.sh +439 -8
- package/runner/versions.env +32 -1
- package/scripts/app-install.ts +322 -27
- package/src/cli.ts +562 -21
- package/src/config.ts +15 -3
- package/src/core/agy-login.ts +683 -0
- package/src/core/delivery.ts +125 -6
- package/src/core/doctor.ts +748 -0
- package/src/core/hub-session.ts +108 -91
- package/src/core/hub.ts +40 -8
- package/src/core/orchestrator.ts +1481 -187
- package/src/core/paths.ts +107 -0
- package/src/core/permissions.ts +143 -0
- package/src/core/pr-source.ts +147 -0
- package/src/core/repo-host.ts +60 -0
- package/src/core/service-windows.ts +180 -0
- package/src/core/service.ts +861 -0
- package/src/core/setup-install.ts +1380 -0
- package/src/core/setup.ts +62 -1
- package/src/core/watchdog.ts +6 -6
- package/src/db/database.ts +54 -7
- package/src/db/settings-repo.ts +332 -7
- package/src/db/ticket-repo.ts +73 -25
- package/src/index.ts +4 -4
- package/src/runner/azure-devops-client.ts +273 -0
- package/src/runner/docker-client.ts +93 -9
- package/src/runner/github-client.ts +148 -3
- package/src/runner/hc-client.ts +33 -5
- package/src/runner/image-builder.ts +123 -5
- package/src/runner/jira-client.ts +368 -0
- package/src/runner/tool-catalog.ts +84 -0
- package/src/server/app.ts +467 -8
- package/src/types.ts +140 -2
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import os from 'node:os';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
export interface PathEnv {
|
|
5
|
+
platform?: NodeJS.Platform;
|
|
6
|
+
homeDir?: string;
|
|
7
|
+
env?: NodeJS.ProcessEnv;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function resolveCtx(ctx: PathEnv = {}): { platform: NodeJS.Platform; homeDir: string; env: NodeJS.ProcessEnv } {
|
|
11
|
+
return {
|
|
12
|
+
platform: ctx.platform ?? process.platform,
|
|
13
|
+
homeDir: ctx.homeDir ?? os.homedir(),
|
|
14
|
+
env: ctx.env ?? process.env,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Default bond-app data directory (`~/.bond-app` on Linux/macOS, `%USERPROFILE%\.bond-app` on Windows).
|
|
20
|
+
* Overridden by `BOND_APP_DATA_DIR`.
|
|
21
|
+
*/
|
|
22
|
+
export function defaultDataDir(ctx: PathEnv = {}): string {
|
|
23
|
+
const { homeDir, env } = resolveCtx(ctx);
|
|
24
|
+
if (env.BOND_APP_DATA_DIR) {
|
|
25
|
+
return env.BOND_APP_DATA_DIR;
|
|
26
|
+
}
|
|
27
|
+
return path.join(homeDir, '.bond-app');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Default herdr-app configuration directory.
|
|
32
|
+
*
|
|
33
|
+
* - Linux / macOS: `~/.config/herdr-app`
|
|
34
|
+
* - Windows: `%APPDATA%\herdr-app` (unconfirmed from the Windows binary; `herdr-app` on Linux reports
|
|
35
|
+
* `~/.config/herdr-app (HERDR_APP_HOME picks another)`). If your Windows installation uses
|
|
36
|
+
* `%USERPROFILE%\.config\herdr-app`, set `HERDR_APP_HOME` or pass `--home`.
|
|
37
|
+
*/
|
|
38
|
+
export function defaultHerdrAppHome(ctx: PathEnv = {}): string {
|
|
39
|
+
const { platform, homeDir, env } = resolveCtx(ctx);
|
|
40
|
+
if (env.HERDR_APP_HOME) {
|
|
41
|
+
return env.HERDR_APP_HOME;
|
|
42
|
+
}
|
|
43
|
+
if (platform === 'win32') {
|
|
44
|
+
const appData = env.APPDATA || path.join(homeDir, 'AppData', 'Roaming');
|
|
45
|
+
return path.join(appData, 'herdr-app');
|
|
46
|
+
}
|
|
47
|
+
return path.join(homeDir, '.config', 'herdr-app');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Directory where user-installed binaries (`herdr-app`, `herdr-core`, `hc`) are placed.
|
|
52
|
+
* - Linux / macOS: `~/.local/bin`
|
|
53
|
+
* - Windows: `%LOCALAPPDATA%\Programs\bond`
|
|
54
|
+
*/
|
|
55
|
+
export function defaultLocalBinDir(ctx: PathEnv = {}): string {
|
|
56
|
+
const { platform, homeDir, env } = resolveCtx(ctx);
|
|
57
|
+
if (platform === 'win32') {
|
|
58
|
+
const localAppData = env.LOCALAPPDATA || path.join(homeDir, 'AppData', 'Local');
|
|
59
|
+
return path.join(localAppData, 'Programs', 'bond');
|
|
60
|
+
}
|
|
61
|
+
return path.join(homeDir, '.local', 'bin');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Directory where the extracted `herdr-app` bundle is stored.
|
|
66
|
+
* - Linux / macOS: `~/.local/share/herdr-app`
|
|
67
|
+
* - Windows: `%LOCALAPPDATA%\Programs\bond\herdr-app`
|
|
68
|
+
*/
|
|
69
|
+
export function defaultHerdrAppInstallDir(ctx: PathEnv = {}): string {
|
|
70
|
+
const { platform, homeDir, env } = resolveCtx(ctx);
|
|
71
|
+
if (platform === 'win32') {
|
|
72
|
+
return path.join(defaultLocalBinDir({ platform, homeDir, env }), 'herdr-app');
|
|
73
|
+
}
|
|
74
|
+
return path.join(homeDir, '.local', 'share', 'herdr-app');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Base data directory used for the optional «bond» desktop launcher.
|
|
79
|
+
* - Linux / macOS: `$XDG_DATA_HOME` if absolute, else `~/.local/share`
|
|
80
|
+
* - Windows: `%APPDATA%` (parent of `Microsoft\Windows\Start Menu\Programs\bond.lnk`)
|
|
81
|
+
*/
|
|
82
|
+
export function defaultLauncherDataHome(ctx: PathEnv = {}): string {
|
|
83
|
+
const { platform, homeDir, env } = resolveCtx(ctx);
|
|
84
|
+
if (platform === 'win32') {
|
|
85
|
+
return env.APPDATA || path.join(homeDir, 'AppData', 'Roaming');
|
|
86
|
+
}
|
|
87
|
+
const xdg = env.XDG_DATA_HOME;
|
|
88
|
+
if (xdg && path.isAbsolute(xdg)) {
|
|
89
|
+
return xdg;
|
|
90
|
+
}
|
|
91
|
+
return path.join(homeDir, '.local', 'share');
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Windows Start Menu Programs directory (`%APPDATA%\Microsoft\Windows\Start Menu\Programs`).
|
|
96
|
+
*/
|
|
97
|
+
export function windowsStartMenuProgramsDir(ctx: PathEnv = {}): string {
|
|
98
|
+
const base = defaultLauncherDataHome({ ...ctx, platform: 'win32' });
|
|
99
|
+
return path.join(base, 'Microsoft', 'Windows', 'Start Menu', 'Programs');
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Windows Start Menu shortcut path (`%APPDATA%\Microsoft\Windows\Start Menu\Programs\bond.lnk`).
|
|
104
|
+
*/
|
|
105
|
+
export function windowsStartMenuShortcutPath(ctx: PathEnv = {}): string {
|
|
106
|
+
return path.join(windowsStartMenuProgramsDir(ctx), 'bond.lnk');
|
|
107
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import { execFile, execFileSync } from 'node:child_process';
|
|
4
|
+
import { promisify } from 'node:util';
|
|
5
|
+
|
|
6
|
+
const execFileAsync = promisify(execFile);
|
|
7
|
+
|
|
8
|
+
export interface AclCommandResult {
|
|
9
|
+
ok: boolean;
|
|
10
|
+
stdout: string;
|
|
11
|
+
stderr: string;
|
|
12
|
+
error?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type AsyncAclRunner = (cmd: string, args: string[], timeoutMs?: number) => Promise<AclCommandResult>;
|
|
16
|
+
export type SyncAclRunner = (cmd: string, args: string[]) => AclCommandResult;
|
|
17
|
+
|
|
18
|
+
export interface RestrictOptions {
|
|
19
|
+
platform?: NodeJS.Platform;
|
|
20
|
+
username?: string;
|
|
21
|
+
runCommand?: AsyncAclRunner;
|
|
22
|
+
runSync?: SyncAclRunner;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function currentWindowsUsername(override?: string): string {
|
|
26
|
+
if (override) return override;
|
|
27
|
+
if (process.env.USERNAME) return process.env.USERNAME;
|
|
28
|
+
try {
|
|
29
|
+
return os.userInfo().username;
|
|
30
|
+
} catch {
|
|
31
|
+
return '';
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Build arguments for `icacls` to restrict a file or directory to the current user only:
|
|
37
|
+
* `icacls <path> /inheritance:r /grant:r "<user>:F"`
|
|
38
|
+
*/
|
|
39
|
+
export function buildIcaclsGrantArgs(targetPath: string, username: string): string[] {
|
|
40
|
+
return [targetPath, '/inheritance:r', '/grant:r', `${username}:F`];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function defaultSyncRunner(cmd: string, args: string[]): AclCommandResult {
|
|
44
|
+
try {
|
|
45
|
+
const stdout = execFileSync(cmd, args, { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'], timeout: 10_000 });
|
|
46
|
+
return { ok: true, stdout: stdout || '', stderr: '' };
|
|
47
|
+
} catch (err: unknown) {
|
|
48
|
+
const e = err as { stdout?: string; stderr?: string; message?: string };
|
|
49
|
+
return {
|
|
50
|
+
ok: false,
|
|
51
|
+
stdout: typeof e.stdout === 'string' ? e.stdout : '',
|
|
52
|
+
stderr: typeof e.stderr === 'string' ? e.stderr : '',
|
|
53
|
+
error: e.message || String(err),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function defaultAsyncRunner(cmd: string, args: string[], timeoutMs = 10_000): Promise<AclCommandResult> {
|
|
59
|
+
try {
|
|
60
|
+
const { stdout, stderr } = await execFileAsync(cmd, args, { encoding: 'utf8', timeout: timeoutMs });
|
|
61
|
+
return { ok: true, stdout: stdout || '', stderr: stderr || '' };
|
|
62
|
+
} catch (err: unknown) {
|
|
63
|
+
const e = err as { stdout?: string; stderr?: string; message?: string };
|
|
64
|
+
return {
|
|
65
|
+
ok: false,
|
|
66
|
+
stdout: typeof e.stdout === 'string' ? e.stdout : '',
|
|
67
|
+
stderr: typeof e.stderr === 'string' ? e.stderr : '',
|
|
68
|
+
error: e.message || String(err),
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Restrict permissions on `targetPath`:
|
|
75
|
+
* - Linux/macOS: `chmodSync(targetPath, mode)` (e.g. `0o600` or `0o700`)
|
|
76
|
+
* - Windows: `icacls <targetPath> /inheritance:r /grant:r "<username>:F"`
|
|
77
|
+
*/
|
|
78
|
+
export function restrictPathSync(targetPath: string, mode: 0o600 | 0o700, opts: RestrictOptions = {}): void {
|
|
79
|
+
const platform = opts.platform ?? process.platform;
|
|
80
|
+
if (platform !== 'win32') {
|
|
81
|
+
fs.chmodSync(targetPath, mode);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
const username = currentWindowsUsername(opts.username);
|
|
85
|
+
if (!username) return;
|
|
86
|
+
const runSync = opts.runSync ?? defaultSyncRunner;
|
|
87
|
+
runSync('icacls', buildIcaclsGrantArgs(targetPath, username));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Async version of `restrictPathSync`, supporting injectable async `runCommand`.
|
|
92
|
+
*/
|
|
93
|
+
export async function restrictPath(targetPath: string, mode: 0o600 | 0o700, opts: RestrictOptions = {}): Promise<void> {
|
|
94
|
+
const platform = opts.platform ?? process.platform;
|
|
95
|
+
if (platform !== 'win32') {
|
|
96
|
+
fs.chmodSync(targetPath, mode);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
const username = currentWindowsUsername(opts.username);
|
|
100
|
+
if (!username) return;
|
|
101
|
+
const runCommand = opts.runCommand ?? defaultAsyncRunner;
|
|
102
|
+
await runCommand('icacls', buildIcaclsGrantArgs(targetPath, username));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Parse `icacls <path>` output and verify that only `username` has ACL entries.
|
|
107
|
+
*
|
|
108
|
+
* Typical `icacls` output:
|
|
109
|
+
* ```
|
|
110
|
+
* C:\Users\jose\.bond-app\token DESKTOP\jose:(F)
|
|
111
|
+
* BUILTIN\Users:(R)
|
|
112
|
+
* Successfully processed 1 files; Failed processing 0 files
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
export function checkIcaclsOutput(stdout: string, username: string): { ok: boolean; principals: string[] } {
|
|
116
|
+
const principals: string[] = [];
|
|
117
|
+
const lines = stdout.split(/\r?\n/);
|
|
118
|
+
|
|
119
|
+
for (const rawLine of lines) {
|
|
120
|
+
const trimmed = rawLine.trim();
|
|
121
|
+
if (!trimmed) continue;
|
|
122
|
+
// Skip summary lines (English / Spanish / other locales typically have digits + semicolon)
|
|
123
|
+
if (/^(Successfully processed|Se procesaron)/i.test(trimmed)) continue;
|
|
124
|
+
|
|
125
|
+
// Match `<principal>:(<perms>)` at the end of the line
|
|
126
|
+
const match = /([^\s]+):\([^)]+\)\s*$/.exec(trimmed);
|
|
127
|
+
if (match) {
|
|
128
|
+
principals.push(match[1]);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (principals.length === 0) {
|
|
133
|
+
return { ok: false, principals };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const targetLower = username.toLowerCase();
|
|
137
|
+
const onlyCurrentUser = principals.every((p) => {
|
|
138
|
+
const lower = p.toLowerCase();
|
|
139
|
+
return lower === targetLower || lower.endsWith(`\\${targetLower}`);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
return { ok: onlyCurrentUser, principals };
|
|
143
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plan bond-app-ticket-desde-pr P1: a ticket can start from existing remote work — an open
|
|
3
|
+
* pull request or a remote branch, on GitHub or Azure Repos — instead of a new branch.
|
|
4
|
+
* This module only reads URLs (no network): which host, repo, PR number or branch they name,
|
|
5
|
+
* and the ticket id a PR gives (D3).
|
|
6
|
+
*/
|
|
7
|
+
import { azureRepoUrl, parseAzureRepoUrl, type AzureRepoRef } from './repo-host.ts';
|
|
8
|
+
import { validateBranchName } from '../types.ts';
|
|
9
|
+
|
|
10
|
+
export type SourceHost = 'github' | 'azure';
|
|
11
|
+
|
|
12
|
+
/** What a source URL names: a PR (by number) or a branch, in one repo. */
|
|
13
|
+
export interface SourceRef {
|
|
14
|
+
host: SourceHost;
|
|
15
|
+
kind: 'pr' | 'branch';
|
|
16
|
+
/** The repo's canonical clone URL (GitHub `https://github.com/o/r.git`, Azure dev.azure.com). */
|
|
17
|
+
repo_url: string;
|
|
18
|
+
number?: number;
|
|
19
|
+
branch?: string;
|
|
20
|
+
github?: { owner: string; repo: string };
|
|
21
|
+
azure?: AzureRepoRef;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface SourceHubRef {
|
|
25
|
+
url: string;
|
|
26
|
+
name: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface SourceHubRepoStatus {
|
|
30
|
+
name: string;
|
|
31
|
+
url: string | null;
|
|
32
|
+
base: string;
|
|
33
|
+
branch_exists: boolean;
|
|
34
|
+
pr_url: string | null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface SourceHubInfo {
|
|
38
|
+
url: string;
|
|
39
|
+
name: string;
|
|
40
|
+
base: string;
|
|
41
|
+
repos: SourceHubRepoStatus[];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** What bond-app resolved from a source URL (P1 `GET /prs/resolve`). */
|
|
45
|
+
export interface SourceInfo {
|
|
46
|
+
kind: 'pr' | 'branch';
|
|
47
|
+
host: SourceHost;
|
|
48
|
+
/** The PR's web URL, or the branch URL as given (normalised). */
|
|
49
|
+
url: string;
|
|
50
|
+
repo_url: string;
|
|
51
|
+
repo_label: string;
|
|
52
|
+
number?: number;
|
|
53
|
+
title: string;
|
|
54
|
+
body: string;
|
|
55
|
+
/** The PR's source branch (or the branch itself): the ticket's target_branch. */
|
|
56
|
+
head: string;
|
|
57
|
+
/** The PR's target branch (or the repo's default one): the ticket's base_branch. */
|
|
58
|
+
base: string;
|
|
59
|
+
/** 'open' for an active PR or an existing branch; 'closed'/'merged' cannot back a ticket. */
|
|
60
|
+
state: 'open' | 'closed' | 'merged';
|
|
61
|
+
draft: boolean;
|
|
62
|
+
/** The PR comes from a fork: no push to its branch, so it cannot back a ticket. */
|
|
63
|
+
is_fork: boolean;
|
|
64
|
+
/** The repo is a bond hub (hub.toml at the head branch). */
|
|
65
|
+
is_hub: boolean;
|
|
66
|
+
author?: string;
|
|
67
|
+
/** P5 (D8): known hubs that list this repo (when the PR is of a regular repo). */
|
|
68
|
+
hubs?: SourceHubRef[];
|
|
69
|
+
/** P5 (D8): the hub's repos and their branch/PR status for `head`. */
|
|
70
|
+
hub?: SourceHubInfo;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** One row of the PR search (P1 `GET /prs/search`). */
|
|
74
|
+
export interface PullRequestHit {
|
|
75
|
+
url: string;
|
|
76
|
+
host: SourceHost;
|
|
77
|
+
number: number;
|
|
78
|
+
title: string;
|
|
79
|
+
repo_label: string;
|
|
80
|
+
author?: string;
|
|
81
|
+
updated_at?: string;
|
|
82
|
+
draft: boolean;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const GH_NAME = '[A-Za-z0-9_.-]+';
|
|
86
|
+
const GH_PR = new RegExp(`^https://github\\.com/(${GH_NAME})/(${GH_NAME})/pull/([1-9][0-9]*)(?:/[A-Za-z0-9/_-]*)?(?:[?#].*)?$`);
|
|
87
|
+
const GH_TREE = new RegExp(`^https://github\\.com/(${GH_NAME})/(${GH_NAME})/tree/([^?#]+?)/?(?:[?#].*)?$`);
|
|
88
|
+
|
|
89
|
+
function decode(s: string): string | null {
|
|
90
|
+
try {
|
|
91
|
+
return decodeURIComponent(s);
|
|
92
|
+
} catch {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* A PR or branch URL of GitHub or Azure Repos, or null:
|
|
99
|
+
* - `https://github.com/<o>/<r>/pull/<n>[/files…]`, `https://github.com/<o>/<r>/tree/<branch>`;
|
|
100
|
+
* - `…/_git/<repo>/pullrequest/<n>`, `…/_git/<repo>?version=GB<branch>` (dev.azure.com or
|
|
101
|
+
* visualstudio.com).
|
|
102
|
+
*/
|
|
103
|
+
export function parseSourceUrl(raw: string): SourceRef | null {
|
|
104
|
+
const url = String(raw ?? '').trim();
|
|
105
|
+
if (!url || url.length > 2000) return null;
|
|
106
|
+
let m = GH_PR.exec(url);
|
|
107
|
+
if (m) {
|
|
108
|
+
const repo = m[2].replace(/\.git$/, '');
|
|
109
|
+
return { host: 'github', kind: 'pr', repo_url: `https://github.com/${m[1]}/${repo}.git`, number: parseInt(m[3], 10), github: { owner: m[1], repo } };
|
|
110
|
+
}
|
|
111
|
+
m = GH_TREE.exec(url);
|
|
112
|
+
if (m) {
|
|
113
|
+
const branch = decode(m[3]);
|
|
114
|
+
const repo = m[2].replace(/\.git$/, '');
|
|
115
|
+
if (!branch || !validateBranchName(branch)) return null;
|
|
116
|
+
return { host: 'github', kind: 'branch', repo_url: `https://github.com/${m[1]}/${repo}.git`, branch, github: { owner: m[1], repo } };
|
|
117
|
+
}
|
|
118
|
+
// Azure: split the repo part from the PR suffix or the ?version= query.
|
|
119
|
+
const [beforeQuery, query = ''] = url.split('#')[0].split('?', 2);
|
|
120
|
+
const pr = /^(.*\/_git\/[^/]+)\/pullrequest\/([1-9][0-9]*)\/?$/.exec(beforeQuery);
|
|
121
|
+
const repoPart = pr ? pr[1] : beforeQuery;
|
|
122
|
+
const az = parseAzureRepoUrl(repoPart);
|
|
123
|
+
if (!az) return null;
|
|
124
|
+
const repoUrl = azureRepoUrl(az);
|
|
125
|
+
if (pr) return { host: 'azure', kind: 'pr', repo_url: repoUrl, number: parseInt(pr[2], 10), azure: az };
|
|
126
|
+
const version = new URLSearchParams(query).get('version') ?? '';
|
|
127
|
+
if (!version.startsWith('GB')) return null;
|
|
128
|
+
const branch = version.slice(2);
|
|
129
|
+
if (!branch || !validateBranchName(branch)) return null;
|
|
130
|
+
return { host: 'azure', kind: 'branch', repo_url: repoUrl, branch, azure: az };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const slug = (s: string) => s.replace(/[^A-Za-z0-9_.-]/g, '-').replace(/^[-.]+|[-.]+$/g, '');
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* D3: the id of a ticket that comes from a PR — `gh-<owner>-<repo>-pr-<n>` (GitHub numbers
|
|
137
|
+
* PRs per repo, like the issue ids) or `az-<org>-pr-<n>` (Azure numbers them per
|
|
138
|
+
* organization). Null for a branch (the ticket keeps a uuid id) or an id over 80 chars.
|
|
139
|
+
*/
|
|
140
|
+
export function prTicketId(ref: SourceRef | null): string | null {
|
|
141
|
+
if (!ref || ref.kind !== 'pr' || !ref.number) return null;
|
|
142
|
+
const id = ref.host === 'github'
|
|
143
|
+
? `gh-${slug(ref.github!.owner)}-${slug(ref.github!.repo)}-pr-${ref.number}`
|
|
144
|
+
: `az-${slug(ref.azure!.org.toLowerCase())}-pr-${ref.number}`;
|
|
145
|
+
const clean = id.replace(/\.{2,}/g, '-');
|
|
146
|
+
return clean.length <= 80 ? clean : null;
|
|
147
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { validateGitHubUrl } from '../types.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Plan bond-app-azure-jira P1: where a ticket's repo lives. GitHub (as always) or Azure
|
|
5
|
+
* Repos (Azure DevOps). Hubs and repo lists stay GitHub only; a single-repo ticket may be
|
|
6
|
+
* either.
|
|
7
|
+
*/
|
|
8
|
+
export type RepoHost = 'github' | 'azure';
|
|
9
|
+
|
|
10
|
+
export interface AzureRepoRef {
|
|
11
|
+
/** https://dev.azure.com/<org> (the API and git root). */
|
|
12
|
+
orgUrl: string;
|
|
13
|
+
org: string;
|
|
14
|
+
project: string;
|
|
15
|
+
repo: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const NAME = '[A-Za-z0-9][A-Za-z0-9_.-]{0,99}';
|
|
19
|
+
// Projects and repos may carry spaces (URL-encoded as %20) and a few other characters.
|
|
20
|
+
const SEG = "[A-Za-z0-9_.~%'()+-]{1,200}";
|
|
21
|
+
const DEV_AZURE = new RegExp(`^https://(?:${NAME}@)?dev\\.azure\\.com/(${NAME})/(${SEG})/_git/(${SEG}?)/?$`);
|
|
22
|
+
const VISUALSTUDIO = new RegExp(`^https://(?:${NAME}@)?(${NAME})\\.visualstudio\\.com/(?:DefaultCollection/)?(${SEG})/_git/(${SEG}?)/?$`);
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* An Azure Repos clone URL, in either the dev.azure.com form or the older
|
|
26
|
+
* <org>.visualstudio.com one, with or without the `<user>@` git adds. Null otherwise.
|
|
27
|
+
*/
|
|
28
|
+
export function parseAzureRepoUrl(url: string): AzureRepoRef | null {
|
|
29
|
+
const u = String(url ?? '').trim();
|
|
30
|
+
const m = DEV_AZURE.exec(u) ?? VISUALSTUDIO.exec(u);
|
|
31
|
+
if (!m) return null;
|
|
32
|
+
const [, org, project, repoRaw] = m;
|
|
33
|
+
const repo = repoRaw.replace(/\.git$/, '');
|
|
34
|
+
if (!repo || project.includes('..') || repo.includes('..')) return null;
|
|
35
|
+
return { orgUrl: `https://dev.azure.com/${org}`, org, project, repo };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The canonical clone URL of an Azure repo (dev.azure.com, no user part). */
|
|
39
|
+
export function azureRepoUrl(ref: AzureRepoRef): string {
|
|
40
|
+
return `${ref.orgUrl}/${ref.project}/_git/${ref.repo}`;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function repoHostOf(url: string): RepoHost | null {
|
|
44
|
+
if (validateGitHubUrl(url)) return 'github';
|
|
45
|
+
if (parseAzureRepoUrl(url)) return 'azure';
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** A repo a single-repo ticket can work on: GitHub or Azure Repos. */
|
|
50
|
+
export function validateRepoUrl(url: string): boolean {
|
|
51
|
+
return repoHostOf(url) !== null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** «owner/repo» or «org/project/repo», for messages and the delete preview. */
|
|
55
|
+
export function repoLabel(url: string): string {
|
|
56
|
+
const az = parseAzureRepoUrl(url);
|
|
57
|
+
if (az) return `${az.org}/${decodeURIComponent(az.project)}/${decodeURIComponent(az.repo)}`;
|
|
58
|
+
const gh = /github\.com\/([^/]+)\/([^/]+?)(?:\.git)?$/.exec(url);
|
|
59
|
+
return gh ? `${gh[1]}/${gh[2]}` : url;
|
|
60
|
+
}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { execFile } from 'node:child_process';
|
|
3
|
+
import { promisify } from 'node:util';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
|
|
6
|
+
import type { CommandOutput } from './doctor.ts';
|
|
7
|
+
|
|
8
|
+
const execFileAsync = promisify(execFile);
|
|
9
|
+
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..');
|
|
10
|
+
|
|
11
|
+
export const DEFAULT_WINDOWS_TASK_NAME = 'bond-app';
|
|
12
|
+
|
|
13
|
+
export type WindowsServiceRunner = (cmd: string, args: string[], timeoutMs?: number) => Promise<CommandOutput>;
|
|
14
|
+
|
|
15
|
+
export interface WindowsServiceOptions {
|
|
16
|
+
taskName?: string;
|
|
17
|
+
nodePath?: string;
|
|
18
|
+
binPath?: string;
|
|
19
|
+
dataDir?: string;
|
|
20
|
+
port?: number;
|
|
21
|
+
host?: string;
|
|
22
|
+
runCommand?: WindowsServiceRunner;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface WindowsTaskStatus {
|
|
26
|
+
installed: boolean;
|
|
27
|
+
running: boolean;
|
|
28
|
+
taskName: string;
|
|
29
|
+
state?: string;
|
|
30
|
+
raw?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function defaultRunCommand(cmd: string, args: string[], timeoutMs = 15_000): Promise<CommandOutput> {
|
|
34
|
+
try {
|
|
35
|
+
const { stdout, stderr } = await execFileAsync(cmd, args, {
|
|
36
|
+
encoding: 'utf8',
|
|
37
|
+
timeout: timeoutMs,
|
|
38
|
+
});
|
|
39
|
+
return { ok: true, stdout: stdout || '', stderr: stderr || '' };
|
|
40
|
+
} catch (err: unknown) {
|
|
41
|
+
const e = err as { stdout?: string; stderr?: string; message?: string };
|
|
42
|
+
return {
|
|
43
|
+
ok: false,
|
|
44
|
+
stdout: typeof e.stdout === 'string' ? e.stdout : '',
|
|
45
|
+
stderr: typeof e.stderr === 'string' ? e.stderr : '',
|
|
46
|
+
error: e.message || String(err),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function winQuote(arg: string): string {
|
|
52
|
+
return `"${arg.replace(/"/g, '\\"')}"`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Build the `/TR` command string for `schtasks /Create` using `node` and the
|
|
57
|
+
* absolute path to `bin/bond-app.js`.
|
|
58
|
+
*/
|
|
59
|
+
export function buildTaskCommand(opts: WindowsServiceOptions = {}): string {
|
|
60
|
+
const nodePath = opts.nodePath ?? process.execPath;
|
|
61
|
+
const binPath = path.resolve(opts.binPath ?? path.join(repoRoot, 'bin', 'bond-app.js'));
|
|
62
|
+
const parts = [winQuote(nodePath), winQuote(binPath), 'start'];
|
|
63
|
+
if (opts.port !== undefined) {
|
|
64
|
+
parts.push('--port', String(opts.port));
|
|
65
|
+
}
|
|
66
|
+
if (opts.host) {
|
|
67
|
+
parts.push('--host', winQuote(opts.host));
|
|
68
|
+
}
|
|
69
|
+
if (opts.dataDir) {
|
|
70
|
+
parts.push('--data-dir', winQuote(path.resolve(opts.dataDir)));
|
|
71
|
+
}
|
|
72
|
+
return parts.join(' ');
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function buildCreateArgs(opts: WindowsServiceOptions = {}): string[] {
|
|
76
|
+
const taskName = opts.taskName ?? DEFAULT_WINDOWS_TASK_NAME;
|
|
77
|
+
return ['/Create', '/SC', 'ONLOGON', '/TN', taskName, '/TR', buildTaskCommand(opts), '/F'];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function buildDeleteArgs(taskName: string = DEFAULT_WINDOWS_TASK_NAME): string[] {
|
|
81
|
+
return ['/Delete', '/TN', taskName, '/F'];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function buildQueryArgs(taskName: string = DEFAULT_WINDOWS_TASK_NAME): string[] {
|
|
85
|
+
return ['/Query', '/TN', taskName, '/FO', 'LIST', '/V'];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function buildRunArgs(taskName: string = DEFAULT_WINDOWS_TASK_NAME): string[] {
|
|
89
|
+
return ['/Run', '/TN', taskName];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function buildEndArgs(taskName: string = DEFAULT_WINDOWS_TASK_NAME): string[] {
|
|
93
|
+
return ['/End', '/TN', taskName];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Parse `schtasks /Query /TN <name> /FO LIST /V` output.
|
|
98
|
+
*/
|
|
99
|
+
export function parseSchtasksQuery(stdout: string): { installed: boolean; running: boolean; state?: string } {
|
|
100
|
+
const statusMatch = /^(?:Status|Estado)\s*:\s*(.+)$/im.exec(stdout);
|
|
101
|
+
const state = statusMatch ? statusMatch[1].trim() : undefined;
|
|
102
|
+
const lower = (state ?? '').toLowerCase();
|
|
103
|
+
const running = lower === 'running' || lower.includes('ejecuci');
|
|
104
|
+
return { installed: true, running, state };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export async function installWindowsTask(
|
|
108
|
+
opts: WindowsServiceOptions = {}
|
|
109
|
+
): Promise<{ ok: boolean; taskName: string; command: string; detail: string }> {
|
|
110
|
+
const taskName = opts.taskName ?? DEFAULT_WINDOWS_TASK_NAME;
|
|
111
|
+
const runCommand = opts.runCommand ?? defaultRunCommand;
|
|
112
|
+
const command = buildTaskCommand(opts);
|
|
113
|
+
const res = await runCommand('schtasks', buildCreateArgs(opts));
|
|
114
|
+
return {
|
|
115
|
+
ok: res.ok,
|
|
116
|
+
taskName,
|
|
117
|
+
command,
|
|
118
|
+
detail: res.ok
|
|
119
|
+
? `scheduled task '${taskName}' created (ONLOGON)`
|
|
120
|
+
: res.stderr.trim() || res.error || 'schtasks /Create failed',
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export async function uninstallWindowsTask(
|
|
125
|
+
opts: Pick<WindowsServiceOptions, 'taskName' | 'runCommand'> = {}
|
|
126
|
+
): Promise<{ ok: boolean; taskName: string; detail: string }> {
|
|
127
|
+
const taskName = opts.taskName ?? DEFAULT_WINDOWS_TASK_NAME;
|
|
128
|
+
const runCommand = opts.runCommand ?? defaultRunCommand;
|
|
129
|
+
const res = await runCommand('schtasks', buildDeleteArgs(taskName));
|
|
130
|
+
return {
|
|
131
|
+
ok: res.ok,
|
|
132
|
+
taskName,
|
|
133
|
+
detail: res.ok
|
|
134
|
+
? `scheduled task '${taskName}' deleted`
|
|
135
|
+
: res.stderr.trim() || res.error || 'schtasks /Delete failed',
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export async function queryWindowsTask(
|
|
140
|
+
opts: Pick<WindowsServiceOptions, 'taskName' | 'runCommand'> = {}
|
|
141
|
+
): Promise<WindowsTaskStatus> {
|
|
142
|
+
const taskName = opts.taskName ?? DEFAULT_WINDOWS_TASK_NAME;
|
|
143
|
+
const runCommand = opts.runCommand ?? defaultRunCommand;
|
|
144
|
+
const res = await runCommand('schtasks', buildQueryArgs(taskName));
|
|
145
|
+
if (!res.ok) {
|
|
146
|
+
return { installed: false, running: false, taskName, raw: res.stderr || res.error };
|
|
147
|
+
}
|
|
148
|
+
const parsed = parseSchtasksQuery(res.stdout);
|
|
149
|
+
return {
|
|
150
|
+
...parsed,
|
|
151
|
+
taskName,
|
|
152
|
+
raw: res.stdout,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export async function startWindowsTask(
|
|
157
|
+
opts: Pick<WindowsServiceOptions, 'taskName' | 'runCommand'> = {}
|
|
158
|
+
): Promise<{ ok: boolean; taskName: string; detail: string }> {
|
|
159
|
+
const taskName = opts.taskName ?? DEFAULT_WINDOWS_TASK_NAME;
|
|
160
|
+
const runCommand = opts.runCommand ?? defaultRunCommand;
|
|
161
|
+
const res = await runCommand('schtasks', buildRunArgs(taskName));
|
|
162
|
+
return {
|
|
163
|
+
ok: res.ok,
|
|
164
|
+
taskName,
|
|
165
|
+
detail: res.ok ? `scheduled task '${taskName}' started` : res.stderr.trim() || res.error || 'schtasks /Run failed',
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export async function stopWindowsTask(
|
|
170
|
+
opts: Pick<WindowsServiceOptions, 'taskName' | 'runCommand'> = {}
|
|
171
|
+
): Promise<{ ok: boolean; taskName: string; detail: string }> {
|
|
172
|
+
const taskName = opts.taskName ?? DEFAULT_WINDOWS_TASK_NAME;
|
|
173
|
+
const runCommand = opts.runCommand ?? defaultRunCommand;
|
|
174
|
+
const res = await runCommand('schtasks', buildEndArgs(taskName));
|
|
175
|
+
return {
|
|
176
|
+
ok: res.ok,
|
|
177
|
+
taskName,
|
|
178
|
+
detail: res.ok ? `scheduled task '${taskName}' stopped` : res.stderr.trim() || res.error || 'schtasks /End failed',
|
|
179
|
+
};
|
|
180
|
+
}
|