Skip to content

Commit 47c9daa

Browse files
committed
refactor: Fix auth token passing
1 parent da717db commit 47c9daa

5 files changed

Lines changed: 38 additions & 19 deletions

File tree

.envrc-example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
use flake .
22

33
export GH_TOKEN=$(gh auth token)
4+
export OBELISK__API__TOKEN=$(obelisk generate token --json | jq -r .token)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ obelisk server verify \
4949

5050
Run it
5151
```sh
52+
export OBELISK__API__TOKEN=$(obelisk generate token --json | jq -r .token)
5253
obelisk server run --server-config server.toml --deployment deployment.toml
5354
```
5455

deployment.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ env_vars = [
7070
[[webhook_endpoint_js.allowed_host]]
7171
pattern = "${OBELISK_API_URL:-http://127.0.0.1:5005}"
7272
methods = ["GET"]
73+
[webhook_endpoint_js.allowed_host.secrets]
74+
env_vars = ["OBELISK__API__TOKEN"]
75+
replace_in = ["headers"]
7376
[[webhook_endpoint_js.allowed_host]]
7477
pattern = "https://api.github.com"
7578
methods = ["GET"]

flake.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
obelisk.packages.${system}.default
2626
gh
2727
just
28+
jq
2829
];
2930
};
3031
devShells.screenshots = pkgs.mkShell {

webhook/show.js

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,23 @@ export default async function handle(request) {
2626
return dashboardPage();
2727
}
2828

29-
async function collectDashboardStatus() {
29+
async function fetchObelisk(urlSuffix) {
3030
const apiBase = process.env["OBELISK_API_URL"] || "http://127.0.0.1:5005";
31-
const listUrl = `${apiBase}/v1/executions?ffqn_prefix=${encodeURIComponent(WORKFLOW_FFQN)}&length=50`;
32-
const listResp = await fetch(listUrl, { headers: { "accept": "application/json" } });
31+
const url = `${apiBase}${urlSuffix}`;
32+
const token = process.env['OBELISK__API__TOKEN'];
33+
if (!token) {
34+
throw new Error("OBELISK__API__TOKEN is required");
35+
}
36+
const headers = {
37+
"accept": "application/json",
38+
authorization: `bearer ${token}`,
39+
};
40+
return await fetch(url, { headers });
41+
}
42+
43+
async function collectDashboardStatus() {
44+
const listUrlSuffix = `/v1/executions?ffqn_prefix=${encodeURIComponent(WORKFLOW_FFQN)}&length=50`;
45+
const listResp = await fetchObelisk(listUrlSuffix);
3346
if (!listResp.ok) {
3447
throw new Error(`Failed to list executions: HTTP ${listResp.status}`);
3548
}
@@ -49,8 +62,8 @@ async function collectDashboardStatus() {
4962
}
5063

5164
const execId = latestFinished.execution_id;
52-
const retUrl = `${apiBase}/v1/executions/${encodeURIComponent(execId)}`;
53-
const retResp = await fetch(retUrl, { headers: { "accept": "application/json" } });
65+
const retUrlSuffix = `/v1/executions/${encodeURIComponent(execId)}`;
66+
const retResp = await fetchObelisk(retUrlSuffix);
5467
if (!retResp.ok) {
5568
throw new Error(`Failed to fetch execution ${execId}: HTTP ${retResp.status}`);
5669
}
@@ -63,8 +76,8 @@ async function collectDashboardStatus() {
6376

6477
const pairs = retVal.ok || [];
6578
const [executionByRepo, mergeByRepo, prByRepo] = await Promise.all([
66-
fetchBumpExecutions(apiBase),
67-
fetchLatestExecutionsByRepo(apiBase, MERGE_FFQN),
79+
fetchBumpExecutions(),
80+
fetchLatestExecutionsByRepo(MERGE_FFQN),
6881
fetchPullRequests(pairs.map(([repo]) => repo)),
6982
]);
7083
return {
@@ -82,18 +95,18 @@ async function collectDashboardStatus() {
8295
};
8396
}
8497

85-
async function fetchBumpExecutions(apiBase) {
86-
const listUrl = `${apiBase}/v1/executions?ffqn_prefix=${encodeURIComponent(BUMP_FFQN)}&show_derived=true&length=100`;
87-
const resp = await fetch(listUrl, { headers: { "accept": "application/json" } });
98+
async function fetchBumpExecutions() {
99+
const listUrlSuffix = `/v1/executions?ffqn_prefix=${encodeURIComponent(BUMP_FFQN)}&show_derived=true&length=100`;
100+
const resp = await fetchObelisk(listUrlSuffix);
88101
if (!resp.ok) {
89102
console.warn("Failed to list bump executions:", resp.status);
90103
return new Map();
91104
}
92105

93106
const executions = await resp.json();
94107
const entries = await Promise.all(executions.map(async (execution) => {
95-
const eventsUrl = `${apiBase}/v1/executions/${encodeURIComponent(execution.execution_id)}/events?version=0&including_cursor=true&length=1`;
96-
const eventsResp = await fetch(eventsUrl, { headers: { "accept": "application/json" } });
108+
const eventsUrlSuffix = `/v1/executions/${encodeURIComponent(execution.execution_id)}/events?version=0&including_cursor=true&length=1`;
109+
const eventsResp = await fetchObelisk(eventsUrlSuffix);
97110
if (!eventsResp.ok) {
98111
return null;
99112
}
@@ -114,8 +127,8 @@ async function fetchBumpExecutions(apiBase) {
114127
|| execution.pending_state?.result_kind !== "ok") {
115128
return;
116129
}
117-
const resultUrl = `${apiBase}/v1/executions/${encodeURIComponent(execution.execution_id)}`;
118-
const resultResp = await fetch(resultUrl, { headers: { "accept": "application/json" } });
130+
const resultUrlSuffix = `/v1/executions/${encodeURIComponent(execution.execution_id)}`;
131+
const resultResp = await fetchObelisk(resultUrlSuffix);
119132
if (!resultResp.ok) {
120133
return;
121134
}
@@ -129,18 +142,18 @@ async function fetchBumpExecutions(apiBase) {
129142
return byRepo;
130143
}
131144

132-
async function fetchLatestExecutionsByRepo(apiBase, ffqn) {
133-
const listUrl = `${apiBase}/v1/executions?ffqn_prefix=${encodeURIComponent(ffqn)}&show_derived=true&length=100`;
134-
const resp = await fetch(listUrl, { headers: { "accept": "application/json" } });
145+
async function fetchLatestExecutionsByRepo(ffqn) {
146+
const listUrlSuffix = `/v1/executions?ffqn_prefix=${encodeURIComponent(ffqn)}&show_derived=true&length=100`;
147+
const resp = await fetchObelisk(listUrlSuffix);
135148
if (!resp.ok) {
136149
console.warn("Failed to list executions:", ffqn, resp.status);
137150
return new Map();
138151
}
139152

140153
const executions = await resp.json();
141154
const entries = await Promise.all(executions.map(async (execution) => {
142-
const eventsUrl = `${apiBase}/v1/executions/${encodeURIComponent(execution.execution_id)}/events?version=0&including_cursor=true&length=1`;
143-
const eventsResp = await fetch(eventsUrl, { headers: { "accept": "application/json" } });
155+
const eventsUrlSuffix = `/v1/executions/${encodeURIComponent(execution.execution_id)}/events?version=0&including_cursor=true&length=1`;
156+
const eventsResp = await fetchObelisk(eventsUrlSuffix);
144157
if (!eventsResp.ok) {
145158
return null;
146159
}

0 commit comments

Comments
 (0)