|
| 1 | +import { outputError } from '@app/cli/output' |
| 2 | +import axios, { type AxiosInstance } from 'axios' |
| 3 | + |
| 4 | +export type SentryConfig = { |
| 5 | + url: string |
| 6 | + org: string |
| 7 | + project: string |
| 8 | + authToken: string |
| 9 | +} |
| 10 | + |
| 11 | +export const readSentryConfigFromEnv = (): SentryConfig => { |
| 12 | + const url = (process.env.SENTRY_URL ?? 'https://sentry.io').replace( |
| 13 | + /\/+$/, |
| 14 | + '', |
| 15 | + ) |
| 16 | + const org = process.env.SENTRY_ORG ?? '' |
| 17 | + const project = process.env.SENTRY_PROJECT ?? '' |
| 18 | + const authToken = process.env.SENTRY_AUTH_TOKEN ?? '' |
| 19 | + |
| 20 | + if (!org || !project || !authToken) { |
| 21 | + outputError( |
| 22 | + 'Missing Sentry configuration. Please set SENTRY_ORG, SENTRY_PROJECT, and SENTRY_AUTH_TOKEN.', |
| 23 | + ) |
| 24 | + process.exit(1) |
| 25 | + } |
| 26 | + |
| 27 | + return { url, org, project, authToken } |
| 28 | +} |
| 29 | + |
| 30 | +export const createSentryHttpClient = (config: SentryConfig): AxiosInstance => { |
| 31 | + return axios.create({ |
| 32 | + baseURL: `${config.url}/api/0`, |
| 33 | + headers: { |
| 34 | + Authorization: `Bearer ${config.authToken}`, |
| 35 | + 'Content-Type': 'application/json', |
| 36 | + }, |
| 37 | + timeout: 30000, |
| 38 | + }) |
| 39 | +} |
| 40 | + |
| 41 | +export const parseNextCursorFromLinkHeader = ( |
| 42 | + linkHeader: string | undefined, |
| 43 | +): string | undefined => { |
| 44 | + if (!linkHeader) return undefined |
| 45 | + const parts = linkHeader.split(',') |
| 46 | + for (const part of parts) { |
| 47 | + const segment = part.trim() |
| 48 | + const isNext = /rel="next"/.test(segment) |
| 49 | + const hasResults = /results="true"/.test(segment) |
| 50 | + if (isNext && hasResults) { |
| 51 | + const match = segment.match(/cursor="([^"]+)"/) |
| 52 | + if (match && match[1]) return match[1] |
| 53 | + } |
| 54 | + } |
| 55 | + return undefined |
| 56 | +} |
| 57 | + |
| 58 | +export const listIssueIdsForEnvironment = async ( |
| 59 | + http: AxiosInstance, |
| 60 | + org: string, |
| 61 | + project: string, |
| 62 | + environment: string, |
| 63 | +): Promise<string[]> => { |
| 64 | + const collectedIssueIds: string[] = [] |
| 65 | + let cursor: string | undefined |
| 66 | + do { |
| 67 | + // Per Sentry docs: GET /projects/{org_slug}/{project_slug}/issues/ |
| 68 | + // Filter by environment via the 'query' search param |
| 69 | + const response = await http.get( |
| 70 | + `/projects/${encodeURIComponent(org)}/${encodeURIComponent(project)}/issues/`, |
| 71 | + { |
| 72 | + params: { |
| 73 | + query: `environment:${environment}`, |
| 74 | + limit: 100, |
| 75 | + cursor, |
| 76 | + // Optionally ensure only unresolved |
| 77 | + // query: `environment:${environment}`, |
| 78 | + }, |
| 79 | + }, |
| 80 | + ) |
| 81 | + const issues = Array.isArray(response.data) ? response.data : [] |
| 82 | + for (const issue of issues) { |
| 83 | + if (issue && typeof issue.id === 'string') |
| 84 | + collectedIssueIds.push(issue.id) |
| 85 | + } |
| 86 | + const linkHeader: string | undefined = |
| 87 | + typeof (response.headers as any).get === 'function' |
| 88 | + ? (response.headers as any).get('link') |
| 89 | + : (response.headers as Record<string, string | undefined>).link |
| 90 | + cursor = parseNextCursorFromLinkHeader(linkHeader) |
| 91 | + } while (cursor) |
| 92 | + return collectedIssueIds |
| 93 | +} |
| 94 | + |
| 95 | +export const deleteIssuesByIds = async ( |
| 96 | + http: AxiosInstance, |
| 97 | + issueIds: string[], |
| 98 | +): Promise<{ deletedCount: number; failedCount: number }> => { |
| 99 | + let deletedCount = 0 |
| 100 | + let failedCount = 0 |
| 101 | + for (const issueId of issueIds) { |
| 102 | + try { |
| 103 | + // Per Sentry docs: DELETE /issues/{issue_id}/ |
| 104 | + await http.delete(`/issues/${issueId}/`) |
| 105 | + deletedCount += 1 |
| 106 | + } catch (error) { |
| 107 | + failedCount += 1 |
| 108 | + const message = axios.isAxiosError(error) |
| 109 | + ? `${error.response?.status} ${error.response?.statusText}` |
| 110 | + : String(error) |
| 111 | + outputError(`Failed to delete issue ${issueId}: ${message}`) |
| 112 | + } |
| 113 | + } |
| 114 | + return { deletedCount, failedCount } |
| 115 | +} |
| 116 | + |
| 117 | +export type SentryEnvironmentWithCount = { |
| 118 | + environment: string |
| 119 | + count: number |
| 120 | +} |
| 121 | + |
| 122 | +// Uses Sentry tags API to list environments that appear on issues (by events count) |
| 123 | +// GET /api/0/projects/{org_slug}/{project_slug}/tags/environment/values/ |
| 124 | +export const listEnvironmentsWithIssues = async ( |
| 125 | + http: AxiosInstance, |
| 126 | + org: string, |
| 127 | + project: string, |
| 128 | +): Promise<SentryEnvironmentWithCount[]> => { |
| 129 | + const aggregated = new Map<string, number>() |
| 130 | + let cursor: string | undefined |
| 131 | + do { |
| 132 | + const response = await http.get( |
| 133 | + `/projects/${encodeURIComponent(org)}/${encodeURIComponent(project)}/tags/environment/values/`, |
| 134 | + { |
| 135 | + params: { limit: 100, cursor }, |
| 136 | + }, |
| 137 | + ) |
| 138 | + const values = Array.isArray(response.data) ? response.data : [] |
| 139 | + for (const v of values) { |
| 140 | + const env = typeof v.value === 'string' ? v.value : undefined |
| 141 | + const count = typeof v.count === 'number' ? v.count : 0 |
| 142 | + if (!env) continue |
| 143 | + aggregated.set(env, (aggregated.get(env) ?? 0) + count) |
| 144 | + } |
| 145 | + const linkHeader: string | undefined = |
| 146 | + typeof (response.headers as any).get === 'function' |
| 147 | + ? (response.headers as any).get('link') |
| 148 | + : (response.headers as Record<string, string | undefined>).link |
| 149 | + cursor = parseNextCursorFromLinkHeader(linkHeader) |
| 150 | + } while (cursor) |
| 151 | + |
| 152 | + return Array.from(aggregated.entries()) |
| 153 | + .map(([environment, count]) => ({ environment, count })) |
| 154 | + .sort((a, b) => b.count - a.count) |
| 155 | +} |
| 156 | + |
| 157 | +export const listProjectEnvironments = async ( |
| 158 | + http: AxiosInstance, |
| 159 | + org: string, |
| 160 | + project: string, |
| 161 | +): Promise<string[]> => { |
| 162 | + const environments = new Set<string>() |
| 163 | + let cursor: string | undefined |
| 164 | + do { |
| 165 | + const response = await http.get( |
| 166 | + `/projects/${encodeURIComponent(org)}/${encodeURIComponent(project)}/environments/`, |
| 167 | + { |
| 168 | + params: { limit: 100, cursor }, |
| 169 | + }, |
| 170 | + ) |
| 171 | + const values = Array.isArray(response.data) ? response.data : [] |
| 172 | + for (const v of values) { |
| 173 | + const name = typeof v.name === 'string' ? v.name : undefined |
| 174 | + if (name) environments.add(name) |
| 175 | + } |
| 176 | + const linkHeader: string | undefined = |
| 177 | + typeof (response.headers as any).get === 'function' |
| 178 | + ? (response.headers as any).get('link') |
| 179 | + : (response.headers as Record<string, string | undefined>).link |
| 180 | + cursor = parseNextCursorFromLinkHeader(linkHeader) |
| 181 | + } while (cursor) |
| 182 | + |
| 183 | + return Array.from(environments).sort((a, b) => a.localeCompare(b)) |
| 184 | +} |
| 185 | + |
| 186 | +export const updateProjectEnvironmentVisibility = async ( |
| 187 | + http: AxiosInstance, |
| 188 | + org: string, |
| 189 | + project: string, |
| 190 | + environment: string, |
| 191 | + isHidden: boolean, |
| 192 | +): Promise<{ name: string; isHidden: boolean }> => { |
| 193 | + const response = await http.put( |
| 194 | + `/projects/${encodeURIComponent(org)}/${encodeURIComponent(project)}/environments/${encodeURIComponent(environment)}/`, |
| 195 | + { isHidden }, |
| 196 | + ) |
| 197 | + const name = |
| 198 | + typeof response.data?.name === 'string' ? response.data.name : environment |
| 199 | + const hidden = Boolean(response.data?.isHidden) |
| 200 | + return { name, isHidden: hidden } |
| 201 | +} |
0 commit comments