resolver.define('getOptionsWithIssueCount', async ({ payload }) => {
const { fieldId, projectId, issueType, fieldClause } = payload;
console.log("Fetching options with issue count...", payload);
try {
// Fetch Field Contexts
const fieldContextRes = await api.asApp().requestJira(route`/rest/api/3/field/${fieldId}/context`, {
method: "GET",
headers: { "Accept": "application/json" }
});
if (!fieldContextRes.ok) throw new Error(`Failed to fetch field context: ${fieldContextRes.statusText}`);
const fieldContextData = await fieldContextRes.json();
if (!fieldContextData.values || fieldContextData.values.length === 0) {
console.warn("No field context found.");
return [];
}
const contextId = fieldContextData.values[0].id;
// Fetch Field Options
const optionsRes = await api.asApp().requestJira(route`/rest/api/3/field/${fieldId}/context/${contextId}/option`, {
method: "GET",
headers: { "Accept": "application/json" }
});
if (!optionsRes.ok) throw new Error(`Failed to fetch field options: ${optionsRes.statusText}`);
const optionsData = await optionsRes.json();
const options = optionsData.values || [];
const optionsWithIssueCount = await Promise.all(options.map(async (option) => {
const jql = `project="${projectId}" AND "${fieldClause}"="${option.value}" AND issuetype="${issueType}"`;
console.log("JQL...........", jql)
const searchRes = await api.asApp().requestJira(route`/rest/api/3/search/jql?jql=project="${projectId}" AND "${fieldClause}"="${option.value}" AND issuetype="${issueType}"&maxResults=50`, {
method: "GET",
headers: { "Accept": "application/json" },
});
if (!searchRes.ok) {
console.error(`Failed to fetch issues for option ${option.value}: ${searchRes.statusText}`);
return { ...option, issueCount: 0 };
}
const searchData = await searchRes.json();
return { ...option, issueCount: searchData.total || 0 };
}));
console.log("Final options with issue count:", optionsWithIssueCount);
return optionsWithIssueCount;
} catch (error) {
console.error("Error fetching options with issue count:", error);
return { error: error.message };
}
});
6.
resolver.define("issuesInComponent", async ({ payload }) => {
const {
customFieldData,
customFieldKey,
projectId,
issueType,
startAt = 0, // default to 0
maxResults = 50, // optional override
} = payload;
console.log("Payload for issue fetch:", payload);
const response = await api.asApp().requestJira(
route`/rest/api/3/search/jql?jql=project="${projectId}" AND "${customFieldKey}"="${customFieldData}" AND issuetype="${issueType}"&startAt=${startAt}&maxResults=${maxResults}`,
{
method: "GET",
headers: { "Accept": "application/json" },
}
);
const issues = await response.json();
console.log(`Fetched issues (startAt: ${startAt}):`, issues);
return issues || null;
});
7.resolver.define("loadRequirements", async ({ payload }) => {
try {
const { projectId, issueType, startAt = 0, maxResults = 50 } = payload;
console.log("Payload for Load requirements .....", payload);
const jql = `project = "${projectId}" AND issuetype = "${issueType}"`;
const response = await api.asApp().requestJira(
route`/rest/api/3/search/jql?jql=${jql}&startAt=${startAt}&maxResults=${maxResults}`,
{
method: "GET",
headers: { "Accept": "application/json" },
}
);
const issues = await response.json();
console.log("Fetch loadRequirements......", issues);
return issues || null;
} catch (error) {
console.error("Error in loadRequirements:", error);
return {
error: true,
message: error.message || "Failed to load requirements"
};
}
});
8.
resolver.define('getIssues', async ({ payload }) => {
// Defensive check: Ensure payload exists and has the required fields
if (!payload || !payload.projectKey || !payload.issueType) {
console.error("❌ Error: 'projectKey' or 'issueType' missing from payload.", payload);
throw new Error("Missing required parameters (projectKey or issueType) for getIssues.");
}
// Destructure after the check
const { projectKey, issueType } = payload;
// The JQL construction logic is correct, but ensure issueType is always quoted
const jql = `project=${projectKey} AND issuetype="${issueType}"`;
// Use URL encoding for the jql parameter for safety
const encodedJql = encodeURIComponent(jql);
const response = await api.asApp().requestJira(route`/rest/api/3/search/jql?jql=${encodedJql}`, {
method: "GET",
headers: { "Accept": "application/json" }
});
// Add error handling for the API response
if (!response.ok) {
console.error(`Jira API call failed with status ${response.status}`);
const errorData = await response.text();
throw new Error(`Failed to fetch issues: ${errorData}`);
}
const data = await response.json();
console.log("Data for get issues..........", data)
return data.issues || []; // Prefer returning an empty array over null for consistency
});
Thanks in Advance for your help in clarifying the new approach.!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.