|
1 |
| -// Handle only webhook events |
| 1 | +import forge, { route } from "@forge/api"; |
| 2 | + |
2 | 3 | export const handler = async (event, context) => {
|
3 |
| - console.log("Event: ", event); |
4 |
| - console.log("Context: ", context); |
5 |
| - const { changelog } = event; |
6 |
| - console.log("Changelog: ", changelog); |
| 4 | + // https://developer.atlassian.com/platform/forge/events-reference/life-cycle/ |
| 5 | + console.log("Installation event payload:", event); |
| 6 | + console.log("Context:", context); |
7 | 7 |
|
8 |
| - if (event.type === "avi:jira:created:issue") { |
9 |
| - return handleIssueCreated(event); |
10 |
| - } |
| 8 | + if (event.eventType === "avi:forge:installed:app") { |
| 9 | + console.log("App was installed!"); |
11 | 10 |
|
12 |
| - if (event.type === "avi:jira:updated:issue") { |
13 |
| - return handleIssueUpdated(event); |
14 |
| - } |
15 |
| -}; |
| 11 | + // Extract cloudId and contextToken |
| 12 | + const cloudId = event.context.cloudId; |
| 13 | + const contextToken = event.contextToken; |
16 | 14 |
|
17 |
| -const handleIssueCreated = async (event) => { |
18 |
| - console.log("Issue created"); |
19 |
| -}; |
| 15 | + try { |
| 16 | + let startAt = 0; |
| 17 | + const maxResults = 50; // Number of results per page |
| 18 | + let allProjects = []; |
| 19 | + let isLastPage = false; |
| 20 | + |
| 21 | + while (!isLastPage) { |
| 22 | + // https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-projects/#api-rest-api-3-project-search-get |
| 23 | + const url = route`/rest/api/3/project/search?startAt=${startAt}&maxResults=${maxResults}`; |
| 24 | + const response = await forge |
| 25 | + .asApp() |
| 26 | + .requestJira(url, { headers: { Accept: "application/json" } }); |
| 27 | + |
| 28 | + // Add status code checking and response logging |
| 29 | + if (!response.ok) { |
| 30 | + console.error("API request failed:", { |
| 31 | + status: response.status, |
| 32 | + statusText: response.statusText, |
| 33 | + body: await response.text(), |
| 34 | + }); |
| 35 | + throw new Error(`API request failed with status ${response.status}`); |
| 36 | + } |
| 37 | + |
| 38 | + const result = await response.json(); |
| 39 | + // console.log("Raw API response:", JSON.stringify(result, null, 2)); |
| 40 | + const { values, total, isLast } = result; |
| 41 | + |
| 42 | + allProjects = [...allProjects, ...values]; |
| 43 | + isLastPage = isLast; |
| 44 | + startAt += maxResults; |
| 45 | + |
| 46 | + console.log(`Fetched ${values.length} projects. Total: ${total}. Page complete: ${isLast}`); |
| 47 | + } |
| 48 | + |
| 49 | + console.log( |
| 50 | + "All projects found:", |
| 51 | + allProjects.map((p) => ({ |
| 52 | + id: p.id, |
| 53 | + key: p.key, |
| 54 | + name: p.name, |
| 55 | + })) |
| 56 | + ); |
| 57 | + } catch (error) { |
| 58 | + console.error("Error fetching projects:", error); |
| 59 | + } |
| 60 | + } else if (event.eventType === "avi:forge:upgraded:app") { |
| 61 | + console.log("App was upgraded!"); |
| 62 | + // Handle upgrade logic here |
| 63 | + } |
20 | 64 |
|
21 |
| -const handleIssueUpdated = async (event) => { |
22 |
| - console.log("Issue updated"); |
| 65 | + return { status: 200, body: "Installation event processed" }; |
23 | 66 | };
|
0 commit comments