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
11 changes: 11 additions & 0 deletions packages/app/src/linear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ export const LINEAR_OPTION_QUERIES = {
labels: "query { issueLabels(first: 100) { nodes { id name } } }",
} as const;

type LinearError = { message?: string; extensions?: { code?: string } };

export const isLinearAuthenticationError = (status: number, errors: LinearError[] | undefined): boolean => {
if (status === 401) return true;
return Boolean(errors?.some(error => {
const code = error.extensions?.code?.toUpperCase();
if (code === "UNAUTHENTICATED" || code === "AUTHENTICATION_ERROR") return true;
return /authentication required|not authenticated|access token.*(?:invalid|expired)|(?:invalid|expired).*access token/i.test(error.message ?? "");
}));
};

export const refreshLinearToken = async (refreshToken: string, clientId: string, clientSecret: string): Promise<{ access_token: string; refresh_token?: string }> => {
const response = await fetch("https://api.linear.app/oauth/token", {
method: "POST",
Expand Down
5 changes: 2 additions & 3 deletions packages/app/src/tenant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Mustache from "mustache";
import { decrypt, encrypt } from "./crypto";
import { agentListCommand, agentOutputCommand, agentStartupBlocked, agentStatusCommand, connectionCheckCommand, defaultAgentCommand, exec, garbageCollectPaneCommand, herdrAgentStatus, herdrCheckCommand, paneGetCommand, paneProcessInfoCommand, replaceForegroundCommand, startAgentCommand, startAgentInPaneCommand, stopAgentCommand, validateWorktreeLeaseCommand, type ExeConnection } from "./exe";
import { findOwnedAgent, ownsPane, parseAgent, parseAgentList, parsePaneProcess, type HerdrIdentity } from "./recovery";
import { LINEAR_ISSUE_PROJECT_QUERY, LINEAR_OPTION_QUERIES, LINEAR_PROJECTS_QUERY, refreshLinearToken } from "./linear";
import { LINEAR_ISSUE_PROJECT_QUERY, LINEAR_OPTION_QUERIES, LINEAR_PROJECTS_QUERY, isLinearAuthenticationError, refreshLinearToken } from "./linear";
import { matchingIssue } from "./matcher";
import type { Env, ExeConnectionInput, FilterType, MatchRule, PipeInput, RunState } from "./types";
import { workingDirectoryFor, workspaceNameFor } from "./workspace";
Expand Down Expand Up @@ -789,8 +789,7 @@ export class Tenant extends DurableObject<Env> {
return { response, payload };
};
let result = await request(token);
const message = result.payload.errors?.[0]?.message;
if ((result.response.status === 401 || /authentication required|not authenticated/i.test(message ?? ""))) {
if (isLinearAuthenticationError(result.response.status, result.payload.errors)) {
const refreshed = await this.refreshLinearAccessToken(token);
result = await request(refreshed);
}
Expand Down
14 changes: 13 additions & 1 deletion packages/app/test/linear.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { LINEAR_OPTION_QUERIES, LINEAR_PROJECTS_QUERY, refreshLinearToken } from "../src/linear";
import { LINEAR_OPTION_QUERIES, LINEAR_PROJECTS_QUERY, isLinearAuthenticationError, refreshLinearToken } from "../src/linear";

afterEach(() => vi.restoreAllMocks());

Expand All @@ -19,6 +19,18 @@ describe("Linear queries", () => {
}
});

it("recognizes Linear's expired-token response variants", () => {
expect(isLinearAuthenticationError(401, undefined)).toBe(true);
expect(isLinearAuthenticationError(200, [{ message: "Authentication required, not authenticated" }])).toBe(true);
expect(isLinearAuthenticationError(200, [{ message: "The access token has expired" }])).toBe(true);
expect(isLinearAuthenticationError(200, [{ extensions: { code: "UNAUTHENTICATED" } }])).toBe(true);
});

it("does not refresh for unrelated Linear errors", () => {
expect(isLinearAuthenticationError(200, [{ message: "Query too complex", extensions: { code: "BAD_USER_INPUT" } }])).toBe(false);
expect(isLinearAuthenticationError(403, [{ message: "You do not have permission" }])).toBe(false);
});

it("exchanges a refresh token for a fresh Linear token pair", async () => {
const request = vi.spyOn(globalThis, "fetch").mockResolvedValue(Response.json({ access_token: "access-new", refresh_token: "refresh-new" }));

Expand Down