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
38 changes: 38 additions & 0 deletions apps/server/src/sourceControl/AzureDevOpsCli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ describe("AzureDevOpsCli.layer", () => {

assert.strictEqual(result.number, 42);
assert.strictEqual(result.title, "Add Azure provider");
assert.strictEqual(result.url, "https://dev.azure.com/acme/project/_git/repo/pullrequest/42");
assert.strictEqual(result.baseRefName, "main");
assert.strictEqual(result.headRefName, "feature/source-control");
assert.strictEqual(result.state, "open");
Expand All @@ -89,6 +90,43 @@ describe("AzureDevOpsCli.layer", () => {
}).pipe(Effect.provide(layer)),
);

it.effect("builds a web URL when Azure returns only the pull request REST URL", () =>
Effect.gen(function* () {
mockRun.mockReturnValueOnce(
Effect.succeed(
processOutput(
// @effect-diagnostics-next-line preferSchemaOverJson:off
JSON.stringify({
pullRequestId: 863,
title: "Fix Azure link",
url: "https://dev.azure.com/saplyai/a8fe4088-ad76-4eda-aaaa-e3329713a097/_apis/git/repositories/16108a25-93a3-4eff-b77d-85e894ee0fe4/pullRequests/863",
repository: {
name: "CV-engine",
project: {
name: "CV-engine",
},
},
sourceRefName: "refs/heads/feature/azure-pr-link",
targetRefName: "refs/heads/main",
status: "active",
}),
),
),
);

const az = yield* AzureDevOpsCli.AzureDevOpsCli;
const result = yield* az.getPullRequest({
cwd: "/repo",
reference: "863",
});

assert.strictEqual(
result.url,
"https://dev.azure.com/saplyai/CV-engine/_git/CV-engine/pullrequest/863",
);
}).pipe(Effect.provide(layer)),
);

it.effect("lists pull requests with Azure status and source branch arguments", () =>
Effect.gen(function* () {
mockRun.mockReturnValueOnce(
Expand Down
188 changes: 185 additions & 3 deletions apps/server/src/sourceControl/GitHubSourceControlProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@ import { ChildProcessSpawner } from "effect/unstable/process";

import * as VcsProcess from "../vcs/VcsProcess.ts";
import * as GitHubCli from "./GitHubCli.ts";
import { parseGitHubAuthStatus } from "./gitHubAuthStatus.ts";
import * as GitHubSourceControlProvider from "./GitHubSourceControlProvider.ts";

const processResult = (stdout: string): VcsProcess.VcsProcessOutput => ({
exitCode: ChildProcessSpawner.ExitCode(0),
const processResult = (
stdout: string,
options?: {
readonly stderr?: string;
readonly exitCode?: ChildProcessSpawner.ExitCode;
},
): VcsProcess.VcsProcessOutput => ({
exitCode: options?.exitCode ?? ChildProcessSpawner.ExitCode(0),
stdout,
stderr: "",
stderr: options?.stderr ?? "",
stdoutTruncated: false,
stderrTruncated: false,
});
Expand Down Expand Up @@ -157,3 +164,178 @@ it.effect("creates GitHub PRs through provider-neutral input names", () =>
});
}),
);

it("accepts active authenticated GitHub accounts when another account fails", () => {
const auth = GitHubSourceControlProvider.discovery.parseAuth(
processResult(
JSON.stringify({
hosts: {
"github.com": [
{
state: "success",
active: true,
host: "github.com",
login: "active-user",
tokenSource: "keyring",
gitProtocol: "ssh",
},
{
state: "error",
active: false,
host: "github.com",
login: "stale-user",
tokenSource: "keyring",
gitProtocol: "ssh",
error: "The token in keyring is invalid.",
},
],
},
}),
),
);

assert.deepStrictEqual(
{
status: auth.status,
account: auth.account,
host: auth.host,
},
{
status: "authenticated",
account: Option.some("active-user"),
host: Option.some("github.com"),
},
);
});

it("parses GitHub auth JSON from stdout when stderr has warnings", () => {
const auth = GitHubSourceControlProvider.discovery.parseAuth(
processResult(
JSON.stringify({
hosts: {
"github.com": [
{
state: "success",
active: true,
host: "github.com",
login: "active-user",
tokenSource: "keyring",
gitProtocol: "ssh",
},
],
},
}),
{ stderr: "warning: ignored diagnostic from gh\n" },
),
);

assert.deepStrictEqual(
{
status: auth.status,
account: auth.account,
host: auth.host,
},
{
status: "authenticated",
account: Option.some("active-user"),
host: Option.some("github.com"),
},
);
});

it("parses GitHub auth status accounts by host and active state", () => {
assert.deepStrictEqual(
parseGitHubAuthStatus(
JSON.stringify({
hosts: {
"github.com": [
{
state: "success",
active: true,
host: "github.com",
login: "active-user",
tokenSource: "keyring",
gitProtocol: "ssh",
},
{
state: "error",
active: false,
host: "github.com",
login: "stale-user",
tokenSource: "keyring",
gitProtocol: "ssh",
},
],
"github.example.test": [
{
state: "success",
active: false,
host: "github.example.test",
login: "enterprise-user",
tokenSource: "keyring",
gitProtocol: "ssh",
},
],
},
}),
).accounts,
[
{
host: "github.com",
account: "active-user",
authenticated: true,
active: true,
error: null,
},
{
host: "github.com",
account: "stale-user",
authenticated: false,
active: false,
error: null,
},
{
host: "github.example.test",
account: "enterprise-user",
authenticated: true,
active: false,
error: null,
},
],
);
});

it("reports unauthenticated when GitHub JSON has accounts but none are valid", () => {
const auth = GitHubSourceControlProvider.discovery.parseAuth(
processResult(
JSON.stringify({
hosts: {
"github.com": [
{
state: "error",
active: true,
host: "github.com",
login: "stale-user",
tokenSource: "keyring",
gitProtocol: "ssh",
error: "The token in keyring is invalid.",
},
],
},
}),
),
);

assert.deepStrictEqual(
{
status: auth.status,
host: auth.host,
detail: auth.detail,
},
{
status: "unauthenticated",
host: Option.some("github.com"),
detail: Option.some("The token in keyring is invalid."),
},
);
});
34 changes: 24 additions & 10 deletions apps/server/src/sourceControl/GitHubSourceControlProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from "@t3tools/contracts";

import * as GitHubCli from "./GitHubCli.ts";
import { findAuthenticatedGitHubAccount, parseGitHubAuthStatus } from "./gitHubAuthStatus.ts";
import * as GitHubPullRequests from "./gitHubPullRequests.ts";
import * as SourceControlProvider from "./SourceControlProvider.ts";
import * as SourceControlProviderDiscovery from "./SourceControlProviderDiscovery.ts";
Expand Down Expand Up @@ -51,11 +52,28 @@ function toChangeRequest(summary: GitHubCli.GitHubPullRequestSummary): ChangeReq

function parseGitHubAuth(input: SourceControlProviderDiscovery.SourceControlAuthProbeInput) {
const output = SourceControlProviderDiscovery.combinedAuthOutput(input);
const account = SourceControlProviderDiscovery.matchFirst(output, [
/Logged in to .* account\s+([^\s(]+)/iu,
/Logged in to .* as\s+([^\s(]+)/iu,
]);
const host = SourceControlProviderDiscovery.parseCliHost(output);
const authStatus = parseGitHubAuthStatus(input.stdout);
const authenticatedAccount = findAuthenticatedGitHubAccount(authStatus.accounts);
const host = authenticatedAccount?.host;

if (authenticatedAccount) {
return SourceControlProviderDiscovery.providerAuth({
status: "authenticated",
account: authenticatedAccount.account,
host,
});
}

const failedAccount = authStatus.accounts.find((entry) => entry.active) ?? authStatus.accounts[0];
if (authStatus.parsed) {
return SourceControlProviderDiscovery.providerAuth({
status: "unauthenticated",
host: failedAccount?.host,
detail:
failedAccount?.error ??
"Run `gh auth login` to authenticate GitHub CLI with an active account.",
});
}

if (input.exitCode !== 0) {
return SourceControlProviderDiscovery.providerAuth({
Expand All @@ -67,10 +85,6 @@ function parseGitHubAuth(input: SourceControlProviderDiscovery.SourceControlAuth
});
}

if (account) {
return SourceControlProviderDiscovery.providerAuth({ status: "authenticated", account, host });
}

return SourceControlProviderDiscovery.providerAuth({
status: "unknown",
host,
Expand All @@ -86,7 +100,7 @@ export const discovery = {
label: "GitHub",
executable: "gh",
versionArgs: ["--version"],
authArgs: ["auth", "status"],
authArgs: ["auth", "status", "--json", "hosts"],
parseAuth: parseGitHubAuth,
installHint:
"Install the GitHub command-line tool (`gh`) via https://cli.github.com/ or your package manager (for example `brew install gh`).",
Expand Down
Loading
Loading