-
Notifications
You must be signed in to change notification settings - Fork 3k
feat: Linear integration — attach issue context from the composer #4115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Gigioxx
wants to merge
8
commits into
pingdotgg:main
Choose a base branch
from
Gigioxx:feat/linear-integration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7de523a
feat: Linear integration — attach issue context from the composer
Gigioxx 32d0177
fix: address PR #4115 review findings
Gigioxx efffbae
fix: harden Linear picker attach flow and settings rollback
Gigioxx 51a5559
fix: guard stale issue-attach callbacks and scope settings rollback
Gigioxx d804f47
fix: degrade Linear secret reads gracefully, structure LinearApiError
Gigioxx c287754
refactor: split LinearApiError into per-condition tagged error classes
Gigioxx e0da139
fix: reserve truncation-suffix space in server-side Linear truncate
Gigioxx d243eed
Merge remote-tracking branch 'origin/main' into feat/linear-integration
Gigioxx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,282 @@ | ||
| import * as Context from "effect/Context"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as Layer from "effect/Layer"; | ||
| import * as Schema from "effect/Schema"; | ||
| import { | ||
| type LinearApiError, | ||
| type LinearApiOperation, | ||
| LinearEmptyResponseError, | ||
| type LinearGetIssueInput, | ||
| LinearGraphqlError, | ||
| LinearHttpError, | ||
| type LinearIssueDetail, | ||
| type LinearIssueSummary, | ||
| LinearIssueNotFoundError, | ||
| LinearNotConnectedError, | ||
| LinearRequestError, | ||
| LinearResponseDecodeError, | ||
| type LinearSearchIssuesInput, | ||
| type LinearSearchIssuesResult, | ||
| type LinearStatus, | ||
| } from "@t3tools/contracts"; | ||
| import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/unstable/http"; | ||
|
|
||
| import * as ServerSettings from "../serverSettings.ts"; | ||
|
|
||
| const LINEAR_GRAPHQL_URL = "https://api.linear.app/graphql"; | ||
|
|
||
| const DESCRIPTION_MAX_LENGTH = 3000; | ||
| const COMMENT_BODY_MAX_LENGTH = 800; | ||
|
|
||
| const STATUS_QUERY = `query { viewer { name } organization { name } }`; | ||
|
|
||
| const SEARCH_ISSUES_QUERY = `query($term: String!, $first: Int!) { searchIssues(term: $term, first: $first) { nodes { id identifier title url state { name type } team { key } } } }`; | ||
|
|
||
| const RECENT_ISSUES_QUERY = `query($first: Int!) { issues(first: $first, orderBy: updatedAt) { nodes { id identifier title url state { name type } team { key } } } }`; | ||
|
|
||
| const GET_ISSUE_QUERY = `query($id: String!) { issue(id: $id) { id identifier title url description priorityLabel updatedAt state { name type } team { key } assignee { displayName } labels(first: 50) { nodes { name } } comments(first: 8) { nodes { body createdAt user { name } } } } }`; | ||
|
|
||
| const StatusDataSchema = Schema.Struct({ | ||
| viewer: Schema.NullOr(Schema.Struct({ name: Schema.String })), | ||
| organization: Schema.NullOr(Schema.Struct({ name: Schema.String })), | ||
| }); | ||
|
|
||
| const IssueStateSchema = Schema.NullOr(Schema.Struct({ name: Schema.String, type: Schema.String })); | ||
| const IssueTeamSchema = Schema.NullOr(Schema.Struct({ key: Schema.String })); | ||
|
|
||
| const IssueSummaryNodeSchema = Schema.Struct({ | ||
| id: Schema.String, | ||
| identifier: Schema.String, | ||
| title: Schema.String, | ||
| url: Schema.String, | ||
| state: IssueStateSchema, | ||
| team: IssueTeamSchema, | ||
| }); | ||
|
|
||
| const SearchIssuesDataSchema = Schema.Struct({ | ||
| searchIssues: Schema.Struct({ | ||
| nodes: Schema.Array(IssueSummaryNodeSchema), | ||
| }), | ||
| }); | ||
|
|
||
| const IssuesDataSchema = Schema.Struct({ | ||
| issues: Schema.Struct({ | ||
| nodes: Schema.Array(IssueSummaryNodeSchema), | ||
| }), | ||
| }); | ||
|
|
||
| const GetIssueDataSchema = Schema.Struct({ | ||
| issue: Schema.NullOr( | ||
| Schema.Struct({ | ||
| id: Schema.String, | ||
| identifier: Schema.String, | ||
| title: Schema.String, | ||
| url: Schema.String, | ||
| description: Schema.NullOr(Schema.String), | ||
| priorityLabel: Schema.NullOr(Schema.String), | ||
| updatedAt: Schema.String, | ||
| state: IssueStateSchema, | ||
| team: IssueTeamSchema, | ||
| assignee: Schema.NullOr(Schema.Struct({ displayName: Schema.String })), | ||
| labels: Schema.Struct({ nodes: Schema.Array(Schema.Struct({ name: Schema.String })) }), | ||
| comments: Schema.Struct({ | ||
| nodes: Schema.Array( | ||
| Schema.Struct({ | ||
| body: Schema.String, | ||
| createdAt: Schema.String, | ||
| user: Schema.NullOr(Schema.Struct({ name: Schema.String })), | ||
| }), | ||
| ), | ||
| }), | ||
| }), | ||
| ), | ||
| }); | ||
|
|
||
| const graphqlResponseSchema = <S extends Schema.Top>(dataSchema: S) => | ||
| Schema.Struct({ | ||
| data: Schema.optional(Schema.NullOr(dataSchema)), | ||
| errors: Schema.optional(Schema.Array(Schema.Struct({ message: Schema.String }))), | ||
| }); | ||
|
|
||
| const TRUNCATION_SUFFIX = "… [truncated]"; | ||
|
|
||
| function truncate(value: string, max: number): string { | ||
| return value.length > max | ||
| ? `${value.slice(0, max - TRUNCATION_SUFFIX.length)}${TRUNCATION_SUFFIX}` | ||
| : value; | ||
| } | ||
|
|
||
| function toIssueSummary(node: { | ||
| readonly id: string; | ||
| readonly identifier: string; | ||
| readonly title: string; | ||
| readonly url: string; | ||
| readonly state: { readonly name: string; readonly type: string } | null; | ||
| readonly team: { readonly key: string } | null; | ||
| }): LinearIssueSummary { | ||
| return { | ||
| id: node.id, | ||
| identifier: node.identifier, | ||
| title: node.title, | ||
| url: node.url, | ||
| stateName: node.state?.name ?? "", | ||
| stateType: node.state?.type ?? "", | ||
| teamKey: node.team?.key ?? "", | ||
| }; | ||
| } | ||
|
|
||
| export class LinearApi extends Context.Service< | ||
| LinearApi, | ||
| { | ||
| readonly getStatus: Effect.Effect<LinearStatus, LinearApiError>; | ||
| readonly searchIssues: ( | ||
| input: LinearSearchIssuesInput, | ||
| ) => Effect.Effect<LinearSearchIssuesResult, LinearApiError>; | ||
| readonly getIssue: ( | ||
| input: LinearGetIssueInput, | ||
| ) => Effect.Effect<LinearIssueDetail, LinearApiError>; | ||
| } | ||
| >()("t3/linear/LinearApi") {} | ||
|
|
||
| export const make = Effect.gen(function* () { | ||
| const httpClient = yield* HttpClient.HttpClient; | ||
| const serverSettings = yield* ServerSettings.ServerSettingsService; | ||
|
|
||
| const readApiKey = (operation: LinearApiOperation) => | ||
| serverSettings.getSettings.pipe( | ||
| Effect.map((settings) => settings.linear.apiKey.trim()), | ||
| Effect.mapError((cause) => new LinearRequestError({ operation, cause })), | ||
| ); | ||
|
|
||
| const requireApiKey = (operation: LinearApiOperation) => | ||
| readApiKey(operation).pipe( | ||
| Effect.flatMap((apiKey) => | ||
| apiKey.length === 0 | ||
| ? Effect.fail(new LinearNotConnectedError({ operation })) | ||
| : Effect.succeed(apiKey), | ||
| ), | ||
| ); | ||
|
|
||
| const graphql = <S extends Schema.Top>( | ||
| operation: LinearApiOperation, | ||
| apiKey: string, | ||
| query: string, | ||
| variables: Record<string, unknown>, | ||
| dataSchema: S, | ||
| ): Effect.Effect<S["Type"], LinearApiError, S["DecodingServices"]> => | ||
| httpClient | ||
| .execute( | ||
| HttpClientRequest.post(LINEAR_GRAPHQL_URL).pipe( | ||
| HttpClientRequest.setHeader("authorization", apiKey), | ||
| HttpClientRequest.bodyJsonUnsafe({ query, variables }), | ||
| ), | ||
| ) | ||
| .pipe( | ||
| Effect.mapError((cause) => new LinearRequestError({ operation, cause })), | ||
| Effect.flatMap((response) => | ||
| HttpClientResponse.matchStatus({ | ||
| "2xx": (success) => | ||
| HttpClientResponse.schemaBodyJson(graphqlResponseSchema(dataSchema))(success).pipe( | ||
| Effect.mapError((cause) => new LinearResponseDecodeError({ operation, cause })), | ||
| Effect.flatMap((body): Effect.Effect<S["Type"], LinearApiError> => { | ||
| if (body.errors !== undefined && body.errors.length > 0) { | ||
| return Effect.fail(new LinearGraphqlError({ operation, cause: body.errors })); | ||
| } | ||
| if (body.data === undefined || body.data === null) { | ||
| return Effect.fail(new LinearEmptyResponseError({ operation })); | ||
| } | ||
| return Effect.succeed(body.data); | ||
| }), | ||
| ), | ||
| orElse: (failed) => | ||
| failed.text.pipe( | ||
| Effect.mapError( | ||
| (cause) => new LinearHttpError({ operation, status: failed.status, cause }), | ||
| ), | ||
| Effect.flatMap((bodyText) => | ||
| Effect.fail( | ||
| new LinearHttpError({ operation, status: failed.status, cause: bodyText }), | ||
| ), | ||
| ), | ||
| ), | ||
| })(response), | ||
| ), | ||
| ); | ||
|
|
||
| return LinearApi.of({ | ||
| getStatus: Effect.gen(function* () { | ||
| const apiKey = yield* readApiKey("getStatus"); | ||
| if (apiKey.length === 0) { | ||
| return { connected: false } satisfies LinearStatus; | ||
| } | ||
| const data = yield* graphql("getStatus", apiKey, STATUS_QUERY, {}, StatusDataSchema); | ||
| return { | ||
| connected: true, | ||
| viewerName: data.viewer?.name ?? "", | ||
| organizationName: data.organization?.name ?? "", | ||
| } satisfies LinearStatus; | ||
| }), | ||
| searchIssues: (input) => | ||
| Effect.gen(function* () { | ||
| const apiKey = yield* requireApiKey("searchIssues"); | ||
| const term = input.query.trim(); | ||
| const first = input.first ?? 10; | ||
| if (term.length === 0) { | ||
| const data = yield* graphql( | ||
| "searchIssues", | ||
| apiKey, | ||
| RECENT_ISSUES_QUERY, | ||
| { first }, | ||
| IssuesDataSchema, | ||
| ); | ||
| return { | ||
| issues: data.issues.nodes.map(toIssueSummary), | ||
| } satisfies LinearSearchIssuesResult; | ||
| } | ||
| const data = yield* graphql( | ||
| "searchIssues", | ||
| apiKey, | ||
| SEARCH_ISSUES_QUERY, | ||
| { term, first }, | ||
| SearchIssuesDataSchema, | ||
| ); | ||
| return { | ||
| issues: data.searchIssues.nodes.map(toIssueSummary), | ||
| } satisfies LinearSearchIssuesResult; | ||
| }), | ||
| getIssue: (input) => | ||
| Effect.gen(function* () { | ||
| const apiKey = yield* requireApiKey("getIssue"); | ||
| const data = yield* graphql( | ||
| "getIssue", | ||
| apiKey, | ||
| GET_ISSUE_QUERY, | ||
| { id: input.issueId }, | ||
| GetIssueDataSchema, | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| ); | ||
| const issue = data.issue; | ||
| if (issue === null) { | ||
| return yield* new LinearIssueNotFoundError({ | ||
| operation: "getIssue", | ||
| issueId: input.issueId, | ||
| }); | ||
| } | ||
| return { | ||
| ...toIssueSummary(issue), | ||
| description: | ||
| issue.description === null ? null : truncate(issue.description, DESCRIPTION_MAX_LENGTH), | ||
| priorityLabel: issue.priorityLabel, | ||
| assigneeName: issue.assignee?.displayName ?? null, | ||
| labels: issue.labels.nodes.map((label) => label.name), | ||
| updatedAt: issue.updatedAt, | ||
| comments: issue.comments.nodes.map((comment) => ({ | ||
| authorName: comment.user?.name ?? null, | ||
| body: truncate(comment.body, COMMENT_BODY_MAX_LENGTH), | ||
| createdAt: comment.createdAt, | ||
| })), | ||
| } satisfies LinearIssueDetail; | ||
| }), | ||
| }); | ||
| }); | ||
|
|
||
| export const layer = Layer.effect(LinearApi, make); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.