Skip to content

Commit 4e289b5

Browse files
committed
fix(sdk): parse projectRef, slug, and name correctly in envvars.update inside taskContext (#4264)
1 parent 10ce69d commit 4e289b5

2 files changed

Lines changed: 106 additions & 6 deletions

File tree

packages/trigger-sdk/src/v3/envvars.test.ts

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,103 @@ describe("envvars.update outside a task context", () => {
9090
expect(requests).toHaveLength(0);
9191
});
9292
});
93+
94+
describe("envvars.update inside a task context", () => {
95+
let server: Server | undefined;
96+
let requests: { method?: string; url?: string; body: string }[];
97+
let previousApiUrl: string | undefined;
98+
let previousSecretKey: string | undefined;
99+
let previousAccessToken: string | undefined;
100+
101+
beforeEach(async () => {
102+
requests = [];
103+
previousApiUrl = process.env.TRIGGER_API_URL;
104+
previousSecretKey = process.env.TRIGGER_SECRET_KEY;
105+
previousAccessToken = process.env.TRIGGER_ACCESS_TOKEN;
106+
107+
delete process.env.TRIGGER_SECRET_KEY;
108+
109+
server = createServer((req, res) => {
110+
let body = "";
111+
req.on("data", (chunk) => {
112+
body += chunk;
113+
});
114+
req.on("end", () => {
115+
requests.push({ method: req.method, url: req.url, body });
116+
res.writeHead(200, { "content-type": "application/json" });
117+
res.end(JSON.stringify({ success: true }));
118+
});
119+
});
120+
121+
await new Promise<void>((resolve) => server!.listen(0, "127.0.0.1", resolve));
122+
123+
const { port } = server!.address() as AddressInfo;
124+
process.env.TRIGGER_API_URL = `http://127.0.0.1:${port}`;
125+
process.env.TRIGGER_ACCESS_TOKEN = "tr_test_token";
126+
127+
taskContext.setGlobalLocation({
128+
ctx: {
129+
project: { id: "proj_ctx_id", ref: "proj_ctx_ref", name: "Project Ctx" },
130+
environment: { id: "env_ctx_id", slug: "dev", type: "DEVELOPMENT" },
131+
organization: { id: "org_ctx_id", slug: "org_ctx", title: "Org Ctx" },
132+
run: { id: "run_ctx_id", isTest: false },
133+
task: { id: "task_ctx_id", filePath: "task.ts", exportName: "task" },
134+
135+
},
136+
});
137+
});
138+
139+
afterEach(async () => {
140+
taskContext.clear();
141+
142+
if (previousApiUrl === undefined) {
143+
delete process.env.TRIGGER_API_URL;
144+
} else {
145+
process.env.TRIGGER_API_URL = previousApiUrl;
146+
}
147+
148+
if (previousSecretKey === undefined) {
149+
delete process.env.TRIGGER_SECRET_KEY;
150+
} else {
151+
process.env.TRIGGER_SECRET_KEY = previousSecretKey;
152+
}
153+
154+
if (previousAccessToken === undefined) {
155+
delete process.env.TRIGGER_ACCESS_TOKEN;
156+
} else {
157+
process.env.TRIGGER_ACCESS_TOKEN = previousAccessToken;
158+
}
159+
160+
const running = server;
161+
server = undefined;
162+
163+
if (running) {
164+
await new Promise<void>((resolve, reject) =>
165+
running.close((error) => (error ? reject(error) : resolve()))
166+
);
167+
}
168+
});
169+
170+
it("correctly parses explicit projectRef, slug, name parameters when taskContext exists", async () => {
171+
await expect(update("proj_explicit", "staging", "MY_VAR", { value: "hello" })).resolves.toEqual({
172+
success: true,
173+
});
174+
175+
expect(requests).toHaveLength(1);
176+
expect(requests[0]?.method).toBe("PUT");
177+
expect(requests[0]?.url).toBe("/api/v1/projects/proj_explicit/envvars/staging/MY_VAR");
178+
expect(JSON.parse(requests[0]?.body ?? "")).toEqual({ value: "hello" });
179+
});
180+
181+
it("correctly uses taskContext defaults when only name and params are provided", async () => {
182+
await expect(update("MY_VAR", { value: "hello_context" })).resolves.toEqual({
183+
success: true,
184+
});
185+
186+
expect(requests).toHaveLength(1);
187+
expect(requests[0]?.method).toBe("PUT");
188+
expect(requests[0]?.url).toBe("/api/v1/projects/proj_ctx_ref/envvars/dev/MY_VAR");
189+
expect(JSON.parse(requests[0]?.body ?? "")).toEqual({ value: "hello_context" });
190+
});
191+
});
192+

packages/trigger-sdk/src/v3/envvars.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -305,12 +305,12 @@ export function update(
305305

306306
if (taskContext.ctx) {
307307
if (typeof slugOrParams === "string") {
308-
$projectRef = slugOrParams;
309-
$slug = slugOrParams ?? taskContext.ctx.environment.slug;
310-
$name =
311-
typeof nameOrRequestOptions === "string"
312-
? nameOrRequestOptions
313-
: taskContext.ctx.environment.slug;
308+
$projectRef = projectRefOrName;
309+
$slug = slugOrParams;
310+
if (typeof nameOrRequestOptions !== "string" || !nameOrRequestOptions) {
311+
throw new Error("name is required");
312+
}
313+
$name = nameOrRequestOptions;
314314

315315
if (!params) {
316316
throw new Error("params is required");

0 commit comments

Comments
 (0)