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
25 changes: 15 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { getCLIWarnings, parseCLIArgs } from "./args";
import { log, setStderr } from "./log";
import { pluralize } from "./util";
import { buildUserAgent } from "./user-agent";
import { withRetry } from "./retry";

declare const CLI_VERSION: string;

Expand Down Expand Up @@ -116,6 +117,10 @@ const options: LinearClientOptions = {
const linearClient = new LinearClient(options);
linearClient.client.setHeader("User-Agent", buildUserAgent());

async function apiRequest<T>(query: string, variables?: Record<string, unknown>): Promise<T> {
return withRetry(() => linearClient.client.rawRequest(query, variables)) as Promise<T>;
}

function scanCommits(
commits: CommitContext[],
includePaths: string[] | null,
Expand Down Expand Up @@ -380,7 +385,7 @@ async function updateCommand(): Promise<{
}

async function getLatestRelease(): Promise<Release | null> {
const response = (await linearClient.client.rawRequest(
const response = await apiRequest<AccessKeyLatestReleaseResponse>(
`
query latestReleaseByAccessKey {
latestReleaseByAccessKey {
Expand All @@ -391,7 +396,7 @@ async function getLatestRelease(): Promise<Release | null> {
}
}
`,
)) as AccessKeyLatestReleaseResponse;
);

return response.data.latestReleaseByAccessKey;
}
Expand All @@ -417,15 +422,15 @@ async function getLatestSha(): Promise<string> {
}

async function getPipelineSettings(): Promise<{ includePathPatterns: string[] }> {
const response = (await linearClient.client.rawRequest(
const response = await apiRequest<AccessKeyPipelineSettingsResponse>(
`
query pipelineSettingsByAccessKey {
releasePipelineByAccessKey {
includePathPatterns
}
}
`,
)) as AccessKeyPipelineSettingsResponse;
);

return {
includePathPatterns: response.data.releasePipelineByAccessKey.includePathPatterns ?? [],
Expand All @@ -449,7 +454,7 @@ async function syncRelease(

const { owner, name } = repoInfo ?? {};

const response = (await linearClient.client.rawRequest(
const response = await apiRequest<AccessKeySyncReleaseResponse>(
`
mutation syncReleaseByAccessKey($input: ReleaseSyncInputBase!) {
releaseSyncByAccessKey(input: $input) {
Expand Down Expand Up @@ -487,7 +492,7 @@ async function syncRelease(
debugSink,
},
},
)) as AccessKeySyncReleaseResponse;
);

if (!response.data?.releaseSyncByAccessKey?.release) {
throw new Error("Failed to sync release");
Expand All @@ -502,7 +507,7 @@ async function completeRelease(options: {
}): Promise<{ success: boolean; release: { id: string; name: string; version?: string; url?: string } | null }> {
const { version, commitSha } = options;

const response = (await linearClient.client.rawRequest(
const response = await apiRequest<AccessKeyCompleteReleaseResponse>(
`
mutation releaseCompleteByAccessKey($input: ReleaseCompleteInputBase!) {
releaseCompleteByAccessKey(input: $input) {
Expand All @@ -522,7 +527,7 @@ async function completeRelease(options: {
commitSha,
},
},
)) as AccessKeyCompleteReleaseResponse;
);

return response.data.releaseCompleteByAccessKey;
}
Expand All @@ -539,7 +544,7 @@ async function updateReleaseByPipeline(options: { stage?: string; version?: stri
.filter(Boolean)
.map((s) => s.slice(2))
.join(", ");
const response = (await linearClient.client.rawRequest(
const response = await apiRequest<AccessKeyUpdateByPipelineResponse>(
`
mutation {
releaseUpdateByPipelineByAccessKey(input: { ${inputParts} }) {
Expand All @@ -556,7 +561,7 @@ async function updateReleaseByPipeline(options: { stage?: string; version?: stri
}
}
`,
)) as AccessKeyUpdateByPipelineResponse;
);

const result = response.data.releaseUpdateByPipelineByAccessKey;
return {
Expand Down
151 changes: 151 additions & 0 deletions src/retry.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { LinearError, LinearErrorType, RatelimitedLinearError } from "@linear/sdk";
import { withRetry } from "./retry";

function makeLinearError(type: LinearErrorType, status?: number): LinearError {
const error = new LinearError();
error.type = type;
if (status !== undefined) {
error.status = status;
}
return error;
}

function makeRateLimitedError(retryAfterSeconds?: number): RatelimitedLinearError {
const error = new RatelimitedLinearError();
error.type = LinearErrorType.Ratelimited;
error.status = 429;
if (retryAfterSeconds !== undefined) {
error.retryAfter = retryAfterSeconds;
}
return error;
}

describe("withRetry", () => {
beforeEach(() => {
vi.useFakeTimers();
});

afterEach(() => {
vi.useRealTimers();
});

it("returns the result on first success", async () => {
const fn = vi.fn().mockResolvedValue("ok");
const promise = withRetry(fn);
const result = await promise;
expect(result).toBe("ok");
expect(fn).toHaveBeenCalledTimes(1);
});

it("retries on transient error and succeeds", async () => {
const fn = vi
.fn()
.mockRejectedValueOnce(makeLinearError(LinearErrorType.NetworkError, 500))
.mockResolvedValue("ok");

const promise = withRetry(fn);

// Advance past the 1s delay for the first retry
await vi.advanceTimersByTimeAsync(1000);

const result = await promise;
expect(result).toBe("ok");
expect(fn).toHaveBeenCalledTimes(2);
});

it("retries twice then succeeds on third attempt", async () => {
const fn = vi
.fn()
.mockRejectedValueOnce(makeLinearError(LinearErrorType.NetworkError, 500))
.mockRejectedValueOnce(makeLinearError(LinearErrorType.InternalError, 500))
.mockResolvedValue("ok");

const promise = withRetry(fn);

// First retry after 1s
await vi.advanceTimersByTimeAsync(1000);
// Second retry after 2s
await vi.advanceTimersByTimeAsync(2000);

const result = await promise;
expect(result).toBe("ok");
expect(fn).toHaveBeenCalledTimes(3);
});

it("throws after exhausting all attempts", async () => {
const error = makeLinearError(LinearErrorType.NetworkError, 500);
const fn = vi.fn().mockRejectedValue(error);

const promise = withRetry(fn);
// Attach rejection handler immediately to prevent unhandled rejection
const resultPromise = promise.catch((e) => e);

await vi.advanceTimersByTimeAsync(1000);
await vi.advanceTimersByTimeAsync(2000);

const caught = await resultPromise;
expect(caught).toBe(error);
expect(fn).toHaveBeenCalledTimes(3);
});

it("does not retry non-retryable errors", async () => {
const error = makeLinearError(LinearErrorType.AuthenticationError, 401);
const fn = vi.fn().mockRejectedValue(error);

await expect(withRetry(fn)).rejects.toBe(error);
expect(fn).toHaveBeenCalledTimes(1);
});

it("does not retry GraphQL errors", async () => {
const error = makeLinearError(LinearErrorType.GraphqlError, 200);
const fn = vi.fn().mockRejectedValue(error);

await expect(withRetry(fn)).rejects.toBe(error);
expect(fn).toHaveBeenCalledTimes(1);
});

it("does not retry 4xx errors", async () => {
const error = makeLinearError(LinearErrorType.InvalidInput, 400);
const fn = vi.fn().mockRejectedValue(error);

await expect(withRetry(fn)).rejects.toBe(error);
expect(fn).toHaveBeenCalledTimes(1);
});

it("retries rate limit errors (429) when retryAfter is missing", async () => {
const fn = vi.fn().mockRejectedValueOnce(makeRateLimitedError()).mockResolvedValue("ok");

const promise = withRetry(fn);
await vi.advanceTimersByTimeAsync(1000);

const result = await promise;
expect(result).toBe("ok");
expect(fn).toHaveBeenCalledTimes(2);
});

it("uses retryAfter for rate limit errors when provided", async () => {
const fn = vi.fn().mockRejectedValueOnce(makeRateLimitedError(3)).mockResolvedValue("ok");

const promise = withRetry(fn);

await vi.advanceTimersByTimeAsync(2999);
expect(fn).toHaveBeenCalledTimes(1);

await vi.advanceTimersByTimeAsync(1);
const result = await promise;
expect(result).toBe("ok");
expect(fn).toHaveBeenCalledTimes(2);
});

it("retries non-LinearError exceptions", async () => {
const fn = vi.fn().mockRejectedValueOnce(new Error("fetch failed")).mockResolvedValue("ok");

const promise = withRetry(fn);
await vi.advanceTimersByTimeAsync(1000);

const result = await promise;
expect(result).toBe("ok");
expect(fn).toHaveBeenCalledTimes(2);
});
});
57 changes: 57 additions & 0 deletions src/retry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { LinearError, LinearErrorType, RatelimitedLinearError } from "@linear/sdk";
import { log } from "./log";

const MAX_ATTEMPTS = 3;
const BASE_DELAY_MS = 1000;

const NON_RETRYABLE_TYPES = new Set<LinearErrorType>([
LinearErrorType.AuthenticationError,
LinearErrorType.Forbidden,
LinearErrorType.FeatureNotAccessible,
LinearErrorType.GraphqlError,
LinearErrorType.InvalidInput,
LinearErrorType.UserError,
LinearErrorType.UsageLimitExceeded,
]);

function isRetryable(error: unknown): boolean {
if (error instanceof LinearError) {
if (error.type && NON_RETRYABLE_TYPES.has(error.type)) {
return false;
}
// 4xx (except 429 rate limit) are not retryable
if (error.status && error.status >= 400 && error.status < 500 && error.status !== 429) {
return false;
}
}
return true;
}

function getDelayMs(error: unknown, attempt: number): number {
if (error instanceof RatelimitedLinearError) {
const retryAfterSeconds = error.retryAfter;
if (typeof retryAfterSeconds === "number" && Number.isFinite(retryAfterSeconds) && retryAfterSeconds > 0) {
return Math.ceil(retryAfterSeconds * 1000);
}
}

return BASE_DELAY_MS * 2 ** (attempt - 1);
}

export async function withRetry<T>(fn: () => Promise<T>): Promise<T> {
for (let attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {
try {
return await fn();
} catch (error) {
if (attempt === MAX_ATTEMPTS || !isRetryable(error)) {
throw error;
}
const delay = getDelayMs(error, attempt);
log(`Request failed, retrying (attempt ${attempt + 1}/${MAX_ATTEMPTS})...`);
await new Promise((resolve) => setTimeout(resolve, delay));
}
}

// Unreachable — the loop always returns or throws
throw new Error("Retry logic error");
}