-
Notifications
You must be signed in to change notification settings - Fork 1
Enable GitAuto forge to call GitAuto with payload #24
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
Merged
Merged
Changes from all commits
Commits
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
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import Resolver from "@forge/resolver"; | ||
import forge from "@forge/api"; | ||
import { storage } from "@forge/api"; | ||
import { route, storage } from "@forge/api"; | ||
|
||
const resolver = new Resolver(); | ||
|
||
|
@@ -14,7 +14,7 @@ resolver.define("getGithubRepos", async ({ payload }) => { | |
|
||
// https://supabase.com/docs/guides/api/sql-to-rest | ||
const queryParams = new URLSearchParams({ | ||
select: "*", | ||
select: "github_owner_name,github_repo_name,github_owner_id", | ||
jira_site_id: `eq.${cloudId}`, | ||
jira_project_id: `eq.${projectId}`, | ||
}).toString(); | ||
|
@@ -51,26 +51,99 @@ resolver.define("storeRepo", async ({ payload }) => { | |
return await storage.set(key, value); | ||
}); | ||
|
||
// Trigger GitAuto by calling the FastAPI endpoint | ||
resolver.define("triggerGitAuto", async ({ payload }) => { | ||
const { cloudId, projectId, issueId, selectedRepo } = payload; | ||
|
||
// Determine the API endpoint based on environment | ||
const endpoint = process.env.GITAUTO_URL + "/webhook"; | ||
console.log("Endpoint", endpoint); | ||
const endpoint = process.env.GITAUTO_URL + "/jira-webhook"; | ||
const response = await forge.fetch(endpoint, { | ||
method: "POST", | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify({ | ||
cloudId, | ||
projectId, | ||
issueId, | ||
repository: selectedRepo, | ||
}), | ||
body: JSON.stringify({ ...payload }), | ||
}); | ||
|
||
if (!response.ok) throw new Error(`Failed to trigger GitAuto: ${response.status}`); | ||
|
||
return await response.json(); | ||
}); | ||
|
||
// Convert Atlassian Document Format to Markdown | ||
const adfToMarkdown = (adf) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (complexity): Consider extracting common text content logic and flattening the code structure in the ADF to Markdown conversion. The const getTextContent = (content) => {
if (!content) return '';
return content.map(item => item.text || '').join('');
};
const adfToMarkdown = (adf) => {
if (!adf?.content) return '';
return adf.content.map((block) => {
const content = block.content || [];
switch (block.type) {
case 'paragraph':
return getTextContent(content) + '\n\n';
case 'heading':
const level = block.attrs?.level || 1;
return '#'.repeat(level) + ' ' + getTextContent(content) + '\n\n';
case 'bulletList':
return content.map(item => `- ${getTextContent(item.content)}`).join('\n') + '\n\n';
case 'orderedList':
return content.map((item, i) => `${i + 1}. ${getTextContent(item.content)}`).join('\n') + '\n\n';
case 'codeBlock':
return '```\n' + getTextContent(content) + '\n```\n\n';
default:
return '';
}
}).join('').trim();
}; This refactoring:
|
||
if (!adf || !adf.content) return ""; | ||
|
||
return adf.content | ||
.map((block) => { | ||
switch (block.type) { | ||
case "paragraph": | ||
return block.content?.map((item) => item.text || "").join("") + "\n\n"; | ||
case "heading": | ||
const level = block.attrs?.level || 1; | ||
const hashes = "#".repeat(level); | ||
return `${hashes} ${block.content?.map((item) => item.text || "").join("")}\n\n`; | ||
case "bulletList": | ||
return ( | ||
block.content | ||
?.map((item) => `- ${item.content?.map((subItem) => subItem.text || "").join("")}`) | ||
.join("\n") + "\n\n" | ||
); | ||
case "orderedList": | ||
return ( | ||
block.content | ||
?.map( | ||
(item, index) => | ||
`${index + 1}. ${item.content?.map((subItem) => subItem.text || "").join("")}` | ||
) | ||
.join("\n") + "\n\n" | ||
); | ||
case "codeBlock": | ||
return `\`\`\`\n${block.content?.map((item) => item.text || "").join("")}\n\`\`\`\n\n`; | ||
default: | ||
return ""; | ||
} | ||
}) | ||
.join("") | ||
.trim(); | ||
}; | ||
|
||
// Get issue details from Jira | ||
// https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issues/#api-rest-api-3-issue-issueidorkey-get | ||
resolver.define("getIssueDetails", async ({ payload }) => { | ||
const { issueId } = payload; | ||
const response = await forge.asApp().requestJira(route`/rest/api/3/issue/${issueId}`, { | ||
method: "GET", | ||
headers: { "Content-Type": "application/json" }, | ||
}); | ||
if (!response.ok) throw new Error(`Failed to fetch issue details: ${response.status}`); | ||
const data = await response.json(); | ||
// console.log("Jira issue details:", data); | ||
|
||
// Format comments into readable text list | ||
const comments = | ||
data.fields.comment?.comments?.map((comment) => { | ||
const timestamp = new Date(comment.created).toLocaleString(); | ||
return `${comment.author.displayName} (${timestamp}):\n${adfToMarkdown(comment.body)}`; | ||
}) || []; | ||
|
||
return { | ||
// project: { | ||
// id: data.fields.project.id, | ||
// key: data.fields.project.key, | ||
// name: data.fields.project.name, | ||
// }, | ||
issue: { | ||
id: data.id, | ||
key: data.key, | ||
title: data.fields.summary, | ||
body: adfToMarkdown(data.fields.description), | ||
comments: comments, | ||
}, | ||
creator: { | ||
id: data.fields.creator.accountId, | ||
displayName: data.fields.creator.displayName, | ||
email: data.fields.creator.emailAddress, | ||
}, | ||
reporter: { | ||
id: data.fields.reporter.accountId, | ||
displayName: data.fields.reporter.displayName, | ||
email: data.fields.reporter.emailAddress, | ||
}, | ||
}; | ||
}); | ||
|
||
export const handler = resolver.getDefinitions(); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚨 suggestion (security): Consider validating payload fields before sending to external service
Spreading the entire payload without validation could lead to sending unexpected data. Consider explicitly mapping required fields.
Suggested implementation:
For even better validation, you might want to add: