|
| 1 | +import type { JiraSearchUsersParams, JiraSearchUsersResponse } from '@/tools/jira/types' |
| 2 | +import { TIMESTAMP_OUTPUT, USER_OUTPUT_PROPERTIES } from '@/tools/jira/types' |
| 3 | +import { getJiraCloudId, transformUser } from '@/tools/jira/utils' |
| 4 | +import type { ToolConfig } from '@/tools/types' |
| 5 | + |
| 6 | +export const jiraSearchUsersTool: ToolConfig<JiraSearchUsersParams, JiraSearchUsersResponse> = { |
| 7 | + id: 'jira_search_users', |
| 8 | + name: 'Jira Search Users', |
| 9 | + description: |
| 10 | + 'Search for Jira users by email address or display name. Returns matching users with their accountId, displayName, and emailAddress.', |
| 11 | + version: '1.0.0', |
| 12 | + |
| 13 | + oauth: { |
| 14 | + required: true, |
| 15 | + provider: 'jira', |
| 16 | + }, |
| 17 | + |
| 18 | + params: { |
| 19 | + accessToken: { |
| 20 | + type: 'string', |
| 21 | + required: true, |
| 22 | + visibility: 'hidden', |
| 23 | + description: 'OAuth access token for Jira', |
| 24 | + }, |
| 25 | + domain: { |
| 26 | + type: 'string', |
| 27 | + required: true, |
| 28 | + visibility: 'user-only', |
| 29 | + description: 'Your Jira domain (e.g., yourcompany.atlassian.net)', |
| 30 | + }, |
| 31 | + query: { |
| 32 | + type: 'string', |
| 33 | + required: true, |
| 34 | + visibility: 'user-or-llm', |
| 35 | + description: |
| 36 | + 'A query string to search for users. Can be an email address, display name, or partial match.', |
| 37 | + }, |
| 38 | + maxResults: { |
| 39 | + type: 'number', |
| 40 | + required: false, |
| 41 | + visibility: 'user-or-llm', |
| 42 | + description: 'Maximum number of users to return (default: 50, max: 1000)', |
| 43 | + }, |
| 44 | + startAt: { |
| 45 | + type: 'number', |
| 46 | + required: false, |
| 47 | + visibility: 'user-or-llm', |
| 48 | + description: 'The index of the first user to return (for pagination, default: 0)', |
| 49 | + }, |
| 50 | + cloudId: { |
| 51 | + type: 'string', |
| 52 | + required: false, |
| 53 | + visibility: 'hidden', |
| 54 | + description: |
| 55 | + 'Jira Cloud ID for the instance. If not provided, it will be fetched using the domain.', |
| 56 | + }, |
| 57 | + }, |
| 58 | + |
| 59 | + request: { |
| 60 | + url: (params: JiraSearchUsersParams) => { |
| 61 | + if (params.cloudId) { |
| 62 | + const queryParams = new URLSearchParams() |
| 63 | + queryParams.append('query', params.query) |
| 64 | + if (params.maxResults !== undefined) |
| 65 | + queryParams.append('maxResults', String(params.maxResults)) |
| 66 | + if (params.startAt !== undefined) queryParams.append('startAt', String(params.startAt)) |
| 67 | + return `https://api.atlassian.com/ex/jira/${params.cloudId}/rest/api/3/user/search?${queryParams.toString()}` |
| 68 | + } |
| 69 | + return 'https://api.atlassian.com/oauth/token/accessible-resources' |
| 70 | + }, |
| 71 | + method: 'GET', |
| 72 | + headers: (params: JiraSearchUsersParams) => ({ |
| 73 | + Accept: 'application/json', |
| 74 | + Authorization: `Bearer ${params.accessToken}`, |
| 75 | + }), |
| 76 | + }, |
| 77 | + |
| 78 | + transformResponse: async (response: Response, params?: JiraSearchUsersParams) => { |
| 79 | + const fetchUsers = async (cloudId: string) => { |
| 80 | + const queryParams = new URLSearchParams() |
| 81 | + queryParams.append('query', params!.query) |
| 82 | + if (params!.maxResults !== undefined) |
| 83 | + queryParams.append('maxResults', String(params!.maxResults)) |
| 84 | + if (params!.startAt !== undefined) queryParams.append('startAt', String(params!.startAt)) |
| 85 | + |
| 86 | + const usersUrl = `https://api.atlassian.com/ex/jira/${cloudId}/rest/api/3/user/search?${queryParams.toString()}` |
| 87 | + |
| 88 | + const usersResponse = await fetch(usersUrl, { |
| 89 | + method: 'GET', |
| 90 | + headers: { |
| 91 | + Accept: 'application/json', |
| 92 | + Authorization: `Bearer ${params!.accessToken}`, |
| 93 | + }, |
| 94 | + }) |
| 95 | + |
| 96 | + if (!usersResponse.ok) { |
| 97 | + let message = `Failed to search Jira users (${usersResponse.status})` |
| 98 | + try { |
| 99 | + const err = await usersResponse.json() |
| 100 | + message = err?.errorMessages?.join(', ') || err?.message || message |
| 101 | + } catch (_e) {} |
| 102 | + throw new Error(message) |
| 103 | + } |
| 104 | + |
| 105 | + return usersResponse.json() |
| 106 | + } |
| 107 | + |
| 108 | + let data: any |
| 109 | + |
| 110 | + if (!params?.cloudId) { |
| 111 | + const cloudId = await getJiraCloudId(params!.domain, params!.accessToken) |
| 112 | + data = await fetchUsers(cloudId) |
| 113 | + } else { |
| 114 | + if (!response.ok) { |
| 115 | + let message = `Failed to search Jira users (${response.status})` |
| 116 | + try { |
| 117 | + const err = await response.json() |
| 118 | + message = err?.errorMessages?.join(', ') || err?.message || message |
| 119 | + } catch (_e) {} |
| 120 | + throw new Error(message) |
| 121 | + } |
| 122 | + data = await response.json() |
| 123 | + } |
| 124 | + |
| 125 | + const users = Array.isArray(data) ? data.filter(Boolean) : [] |
| 126 | + |
| 127 | + return { |
| 128 | + success: true, |
| 129 | + output: { |
| 130 | + ts: new Date().toISOString(), |
| 131 | + users: users.map((user: any) => ({ |
| 132 | + ...(transformUser(user) ?? { accountId: '', displayName: '' }), |
| 133 | + self: user.self ?? null, |
| 134 | + })), |
| 135 | + total: users.length, |
| 136 | + startAt: params?.startAt ?? 0, |
| 137 | + maxResults: params?.maxResults ?? 50, |
| 138 | + }, |
| 139 | + } |
| 140 | + }, |
| 141 | + |
| 142 | + outputs: { |
| 143 | + ts: TIMESTAMP_OUTPUT, |
| 144 | + users: { |
| 145 | + type: 'array', |
| 146 | + description: 'Array of matching Jira users', |
| 147 | + items: { |
| 148 | + type: 'object', |
| 149 | + properties: { |
| 150 | + ...USER_OUTPUT_PROPERTIES, |
| 151 | + self: { |
| 152 | + type: 'string', |
| 153 | + description: 'REST API URL for this user', |
| 154 | + optional: true, |
| 155 | + }, |
| 156 | + }, |
| 157 | + }, |
| 158 | + }, |
| 159 | + total: { |
| 160 | + type: 'number', |
| 161 | + description: 'Number of users returned in this page (may be less than total matches)', |
| 162 | + }, |
| 163 | + startAt: { type: 'number', description: 'Pagination start index' }, |
| 164 | + maxResults: { type: 'number', description: 'Maximum results per page' }, |
| 165 | + }, |
| 166 | +} |
0 commit comments