|
| 1 | +/** |
| 2 | + * MIT No Attribution |
| 3 | + * |
| 4 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 7 | + * the Software without restriction, including without limitation the rights to |
| 8 | + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 9 | + * the Software, and to permit persons to whom the Software is furnished to do so. |
| 10 | + * |
| 11 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 12 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 14 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 15 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 16 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 17 | + * SOFTWARE. |
| 18 | + */ |
| 19 | + |
| 20 | +import { getLinearSecret } from './linear-verify'; |
| 21 | +import { logger } from './logger'; |
| 22 | + |
| 23 | +/** |
| 24 | + * Lambda-side helper for posting comments and reactions onto Linear issues |
| 25 | + * via direct GraphQL. Used by the webhook processor to give users feedback |
| 26 | + * on pre-container failures (guardrail block, concurrency cap, unmapped |
| 27 | + * project, etc.) — paths where the agent never starts and the agent-side |
| 28 | + * Linear MCP / `linear_reactions.py` cannot run. |
| 29 | + * |
| 30 | + * All calls are best-effort. Errors are logged at WARN and swallowed — |
| 31 | + * Linear feedback is advisory and must never gate task-rejection logic. |
| 32 | + */ |
| 33 | + |
| 34 | +const LINEAR_GRAPHQL_URL = 'https://api.linear.app/graphql'; |
| 35 | + |
| 36 | +const REQUEST_TIMEOUT_MS = 5000; |
| 37 | + |
| 38 | +/** Reaction emoji short-code for the failure marker. Matches `EMOJI_FAILURE` in `agent/src/linear_reactions.py`. */ |
| 39 | +const EMOJI_FAILURE = 'x'; |
| 40 | + |
| 41 | +const COMMENT_CREATE_MUTATION = ` |
| 42 | +mutation CreateComment($issueId: String!, $body: String!) { |
| 43 | + commentCreate(input: { issueId: $issueId, body: $body }) { |
| 44 | + success |
| 45 | + } |
| 46 | +} |
| 47 | +`.trim(); |
| 48 | + |
| 49 | +const REACTION_CREATE_MUTATION = ` |
| 50 | +mutation ReactIssue($issueId: String!, $emoji: String!) { |
| 51 | + reactionCreate(input: { issueId: $issueId, emoji: $emoji }) { |
| 52 | + success |
| 53 | + } |
| 54 | +} |
| 55 | +`.trim(); |
| 56 | + |
| 57 | +async function graphqlRequest( |
| 58 | + apiToken: string, |
| 59 | + query: string, |
| 60 | + variables: Record<string, unknown>, |
| 61 | +): Promise<boolean> { |
| 62 | + const controller = new AbortController(); |
| 63 | + const timer = setTimeout(() => controller.abort(), REQUEST_TIMEOUT_MS); |
| 64 | + try { |
| 65 | + const resp = await fetch(LINEAR_GRAPHQL_URL, { |
| 66 | + method: 'POST', |
| 67 | + headers: { |
| 68 | + 'Authorization': apiToken, |
| 69 | + 'Content-Type': 'application/json', |
| 70 | + }, |
| 71 | + body: JSON.stringify({ query, variables }), |
| 72 | + signal: controller.signal, |
| 73 | + }); |
| 74 | + if (!resp.ok) { |
| 75 | + logger.warn('Linear feedback GraphQL non-2xx', { status: resp.status }); |
| 76 | + return false; |
| 77 | + } |
| 78 | + const body = (await resp.json()) as { errors?: unknown }; |
| 79 | + if (body.errors) { |
| 80 | + logger.warn('Linear feedback GraphQL errors', { errors: body.errors }); |
| 81 | + return false; |
| 82 | + } |
| 83 | + return true; |
| 84 | + } catch (err) { |
| 85 | + logger.warn('Linear feedback request failed', { |
| 86 | + error: err instanceof Error ? err.message : String(err), |
| 87 | + }); |
| 88 | + return false; |
| 89 | + } finally { |
| 90 | + clearTimeout(timer); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +async function resolveToken(secretArn: string): Promise<string | null> { |
| 95 | + try { |
| 96 | + return await getLinearSecret(secretArn); |
| 97 | + } catch (err) { |
| 98 | + logger.warn('Linear feedback could not resolve API token', { |
| 99 | + error: err instanceof Error ? err.message : String(err), |
| 100 | + }); |
| 101 | + return null; |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Post a comment onto a Linear issue. Returns true on success, false on any failure |
| 107 | + * (network, auth, GraphQL errors). Never throws — callers proceed regardless. |
| 108 | + */ |
| 109 | +export async function postIssueComment( |
| 110 | + apiTokenSecretArn: string, |
| 111 | + issueId: string, |
| 112 | + body: string, |
| 113 | +): Promise<boolean> { |
| 114 | + const token = await resolveToken(apiTokenSecretArn); |
| 115 | + if (!token) return false; |
| 116 | + return graphqlRequest(token, COMMENT_CREATE_MUTATION, { issueId, body }); |
| 117 | +} |
| 118 | + |
| 119 | +/** |
| 120 | + * Add an emoji reaction onto a Linear issue. Defaults to ❌ — the failure marker |
| 121 | + * the agent uses on the success/failure side. Returns true on success. |
| 122 | + */ |
| 123 | +export async function addIssueReaction( |
| 124 | + apiTokenSecretArn: string, |
| 125 | + issueId: string, |
| 126 | + emoji: string = EMOJI_FAILURE, |
| 127 | +): Promise<boolean> { |
| 128 | + const token = await resolveToken(apiTokenSecretArn); |
| 129 | + if (!token) return false; |
| 130 | + return graphqlRequest(token, REACTION_CREATE_MUTATION, { issueId, emoji }); |
| 131 | +} |
| 132 | + |
| 133 | +/** |
| 134 | + * Convenience: post a feedback comment **and** drop a ❌ reaction in one call. |
| 135 | + * Both calls run in parallel; both are best-effort. Returns void — callers |
| 136 | + * never branch on the result. |
| 137 | + */ |
| 138 | +export async function reportIssueFailure( |
| 139 | + apiTokenSecretArn: string, |
| 140 | + issueId: string, |
| 141 | + message: string, |
| 142 | +): Promise<void> { |
| 143 | + await Promise.allSettled([ |
| 144 | + postIssueComment(apiTokenSecretArn, issueId, message), |
| 145 | + addIssueReaction(apiTokenSecretArn, issueId, EMOJI_FAILURE), |
| 146 | + ]); |
| 147 | +} |
0 commit comments