swadhinbiswas Add external CI disable endpoint and docs · 2/19/2026
b6de549
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { beforeEach, describe, expect, it, vi } from "vitest";
const {
canAdminRepoMock,
generateIdMock,
fakeSchema,
} = vi.hoisted(() => ({
canAdminRepoMock: vi.fn(async () => true),
generateIdMock: vi.fn(() => "external_ci_1"),
fakeSchema: {
users: { username: {} },
repositories: { ownerId: {}, name: {}, id: {} },
externalCiIntegrations: { repositoryId: {}, id: {}, updatedAt: {}, tokenHash: {}, name: {} },
} as any,
}));
let mockDb: any;
vi.mock("@/db", () => ({
getDatabase: () => mockDb,
schema: fakeSchema,
}));
vi.mock("@/lib/permissions", () => ({
canAdminRepo: canAdminRepoMock,
}));
vi.mock("@/lib/utils", () => ({
generateId: generateIdMock,
}));
import { DELETE as externalCiDelete, GET as externalCiGet, POST as externalCiPost } from "@/pages/api/repos/[owner]/[repo]/external-ci";
function makeDb(integration: any | null = null) {
return {
query: {
users: {
findFirst: vi.fn(async () => ({ id: "owner-1" })),
},
repositories: {
findFirst: vi.fn(async () => ({ id: "repo-1", ownerId: "owner-1", name: "demo" })),
},
externalCiIntegrations: {
findFirst: vi.fn(async () => integration),
},
},
insert: vi.fn(() => ({
values: vi.fn(async () => undefined),
})),
update: vi.fn(() => ({
set: vi.fn(() => ({
where: vi.fn(async () => undefined),
})),
})),
delete: vi.fn(() => ({
where: vi.fn(async () => undefined),
})),
};
}
async function readJson(response: Response): Promise<any> {
return response.json();
}
describe("external ci route", () => {
beforeEach(() => {
canAdminRepoMock.mockResolvedValue(true);
generateIdMock.mockReturnValue("external_ci_1");
mockDb = makeDb(null);
});
it("returns disabled when integration is missing", async () => {
const response = await externalCiGet({
params: { owner: "owner-1", repo: "demo" },
locals: { user: { id: "user-1", isAdmin: false } },
} as any);
const body = await readJson(response);
expect(response.status).toBe(200);
expect(body?.data?.enabled).toBe(false);
});
it("returns status metadata when integration exists", async () => {
const lastUsedAt = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000);
mockDb = makeDb({
id: "external-ci-1",
repositoryId: "repo-1",
name: "External CI",
createdAt: new Date("2026-02-01T00:00:00Z"),
updatedAt: new Date("2026-02-15T00:00:00Z"),
lastUsedAt,
});
const response = await externalCiGet({
params: { owner: "owner-1", repo: "demo" },
locals: { user: { id: "user-1", isAdmin: false } },
} as any);
const body = await readJson(response);
expect(response.status).toBe(200);
expect(body?.data?.enabled).toBe(true);
expect(body?.data?.status).toBe("active");
expect(typeof body?.data?.checksEndpoint).toBe("string");
});
it("returns rotated=true when token is regenerated", async () => {
mockDb = makeDb({
id: "external-ci-1",
repositoryId: "repo-1",
name: "External CI",
});
const response = await externalCiPost({
params: { owner: "owner-1", repo: "demo" },
locals: { user: { id: "user-1", isAdmin: false } },
request: new Request("http://localhost/api/repos/owner-1/demo/external-ci", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ name: "Renamed CI" }),
}),
} as any);
const body = await readJson(response);
expect(response.status).toBe(201);
expect(typeof body?.data?.token).toBe("string");
expect(body?.data?.rotated).toBe(true);
});
it("disables integration with delete", async () => {
mockDb = makeDb({
id: "external-ci-1",
repositoryId: "repo-1",
name: "External CI",
});
const response = await externalCiDelete({
params: { owner: "owner-1", repo: "demo" },
locals: { user: { id: "user-1", isAdmin: false } },
} as any);
const body = await readJson(response);
expect(response.status).toBe(200);
expect(body?.data?.enabled).toBe(false);
expect(mockDb.delete).toHaveBeenCalledTimes(1);
});
});