Skip to content

Commit 4f93374

Browse files
author
Julius Marminge
committed
fix(source-control): open Azure DevOps PR web URLs
1 parent ce266c3 commit 4f93374

2 files changed

Lines changed: 111 additions & 1 deletion

File tree

‎apps/server/src/sourceControl/AzureDevOpsCli.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ describe("AzureDevOpsCli.layer", () => {
6464

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

93+
it.effect("builds a web URL when Azure returns only the pull request REST URL", () =>
94+
Effect.gen(function* () {
95+
mockRun.mockReturnValueOnce(
96+
Effect.succeed(
97+
processOutput(
98+
// @effect-diagnostics-next-line preferSchemaOverJson:off
99+
JSON.stringify({
100+
pullRequestId: 863,
101+
title: "Fix Azure link",
102+
url: "https://dev.azure.com/saplyai/a8fe4088-ad76-4eda-aaaa-e3329713a097/_apis/git/repositories/16108a25-93a3-4eff-b77d-85e894ee0fe4/pullRequests/863",
103+
repository: {
104+
name: "CV-engine",
105+
project: {
106+
name: "CV-engine",
107+
},
108+
},
109+
sourceRefName: "refs/heads/feature/azure-pr-link",
110+
targetRefName: "refs/heads/main",
111+
status: "active",
112+
}),
113+
),
114+
),
115+
);
116+
117+
const az = yield* AzureDevOpsCli.AzureDevOpsCli;
118+
const result = yield* az.getPullRequest({
119+
cwd: "/repo",
120+
reference: "863",
121+
});
122+
123+
assert.strictEqual(
124+
result.url,
125+
"https://dev.azure.com/saplyai/CV-engine/_git/CV-engine/pullrequest/863",
126+
);
127+
}).pipe(Effect.provide(layer)),
128+
);
129+
92130
it.effect("lists pull requests with Azure status and source branch arguments", () =>
93131
Effect.gen(function* () {
94132
mockRun.mockReturnValueOnce(

‎apps/server/src/sourceControl/azureDevOpsPullRequests.ts‎

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ const AzureDevOpsPullRequestSchema = Schema.Struct({
2121
pullRequestId: PositiveInt,
2222
title: TrimmedNonEmptyString,
2323
url: Schema.optional(Schema.String),
24+
repository: Schema.optional(
25+
Schema.Struct({
26+
name: Schema.optional(Schema.String),
27+
webUrl: Schema.optional(Schema.String),
28+
project: Schema.optional(
29+
Schema.Struct({
30+
name: Schema.optional(Schema.String),
31+
}),
32+
),
33+
}),
34+
),
2435
sourceRefName: TrimmedNonEmptyString,
2536
targetRefName: TrimmedNonEmptyString,
2637
status: Schema.String,
@@ -57,13 +68,74 @@ function normalizeAzureDevOpsPullRequestState(status: string): "open" | "closed"
5768
}
5869
}
5970

71+
function encodeAzureDevOpsPathSegment(segment: string): string {
72+
return encodeURIComponent(segment);
73+
}
74+
75+
function azureDevOpsOrganizationBaseFromRestApiUrl(
76+
value: string | null | undefined,
77+
): string | null {
78+
const rawUrl = trimOptionalString(value);
79+
if (!rawUrl) {
80+
return null;
81+
}
82+
83+
try {
84+
const url = new URL(rawUrl);
85+
const hostname = url.hostname.toLowerCase();
86+
const pathSegments = url.pathname.split("/").filter((segment) => segment.length > 0);
87+
const isAzureRestUrl = pathSegments.some((segment) => segment.toLowerCase() === "_apis");
88+
if (!isAzureRestUrl) {
89+
return null;
90+
}
91+
92+
if (hostname === "dev.azure.com") {
93+
const organization = pathSegments[0];
94+
return organization ? `${url.origin}/${organization}` : null;
95+
}
96+
97+
if (hostname.endsWith(".visualstudio.com")) {
98+
return url.origin;
99+
}
100+
101+
return null;
102+
} catch {
103+
return null;
104+
}
105+
}
106+
107+
function normalizeAzureDevOpsPullRequestUrl(
108+
raw: Schema.Schema.Type<typeof AzureDevOpsPullRequestSchema>,
109+
): string {
110+
const webLink = trimOptionalString(raw._links?.web?.href);
111+
if (webLink) {
112+
return webLink;
113+
}
114+
115+
const repositoryWebUrl = trimOptionalString(raw.repository?.webUrl);
116+
if (repositoryWebUrl) {
117+
return `${repositoryWebUrl.replace(/\/+$/, "")}/pullrequest/${raw.pullRequestId}`;
118+
}
119+
120+
const organizationBase = azureDevOpsOrganizationBaseFromRestApiUrl(raw.url);
121+
const projectName = trimOptionalString(raw.repository?.project?.name);
122+
const repositoryName = trimOptionalString(raw.repository?.name);
123+
if (organizationBase && projectName && repositoryName) {
124+
const encodedProjectName = encodeAzureDevOpsPathSegment(projectName);
125+
const encodedRepositoryName = encodeAzureDevOpsPathSegment(repositoryName);
126+
return `${organizationBase}/${encodedProjectName}/_git/${encodedRepositoryName}/pullrequest/${raw.pullRequestId}`;
127+
}
128+
129+
return trimOptionalString(raw.url) ?? "";
130+
}
131+
60132
function normalizeAzureDevOpsPullRequestRecord(
61133
raw: Schema.Schema.Type<typeof AzureDevOpsPullRequestSchema>,
62134
): NormalizedAzureDevOpsPullRequestRecord {
63135
return {
64136
number: raw.pullRequestId,
65137
title: raw.title,
66-
url: trimOptionalString(raw._links?.web?.href) ?? trimOptionalString(raw.url) ?? "",
138+
url: normalizeAzureDevOpsPullRequestUrl(raw),
67139
baseRefName: normalizeRefName(raw.targetRefName),
68140
headRefName: normalizeRefName(raw.sourceRefName),
69141
state: normalizeAzureDevOpsPullRequestState(raw.status),

0 commit comments

Comments
 (0)