Created
September 19, 2024 19:49
-
-
Save chhantyal/55b26b0735cb0e327ad007f0ebfa3e1a to your computer and use it in GitHub Desktop.
Claude.AI generated code that works without any changes!
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
// Prompt: Write a Javascript code that fetches all issues from Jira with a given filter, | |
// finds the last edited value in Description field and reverts to that value | |
import fetch from 'node-fetch'; | |
const JIRA_DOMAIN = 'your-domain.atlassian.net'; | |
const API_TOKEN = 'your-api-token'; | |
const EMAIL = '[email protected]'; | |
const headers = { | |
'Authorization': `Basic ${Buffer.from(`${EMAIL}:${API_TOKEN}`).toString('base64')}`, | |
'Accept': 'application/json', | |
'Content-Type': 'application/json' | |
}; | |
async function fetchAndRevertJiraDescription(jql) { | |
try { | |
const searchResponse = await fetch(`https://${JIRA_DOMAIN}/rest/api/3/search?jql=${encodeURIComponent(jql)}`, { | |
method: 'GET', | |
headers: headers | |
}); | |
const searchData = await searchResponse.json(); | |
if (searchData.issues.length === 0) { | |
console.log('No issues found with the given filter'); | |
return; | |
} | |
for (const issue of searchData.issues) { | |
const historyResponse = await fetch(`https://${JIRA_DOMAIN}/rest/api/3/issue/${issue.key}/changelog`, { | |
method: 'GET', | |
headers: headers | |
}); | |
const historyData = await historyResponse.json(); | |
const descriptionChanges = historyData.values | |
.flatMap(history => history.items) | |
.filter(item => item.field === 'description') | |
.reverse(); | |
if (descriptionChanges.length < 2) { | |
console.log(`No previous description found for issue ${issue.key}`); | |
continue; | |
} | |
const previousDescription = descriptionChanges[1].toString; | |
// Fetch the current issue data | |
const issueResponse = await fetch(`https://${JIRA_DOMAIN}/rest/api/3/issue/${issue.key}`, { | |
method: 'GET', | |
headers: headers | |
}); | |
const issueData = await issueResponse.json(); | |
// Prepare the update payload | |
const updatePayload = { | |
fields: { | |
description: { | |
type: "doc", | |
version: 1, | |
content: [ | |
{ | |
type: "paragraph", | |
content: [ | |
{ | |
text: previousDescription, | |
type: "text" | |
} | |
] | |
} | |
] | |
} | |
} | |
}; | |
// Update the issue with the previous description | |
const updateResponse = await fetch(`https://${JIRA_DOMAIN}/rest/api/3/issue/${issue.key}`, { | |
method: 'PUT', | |
headers: headers, | |
body: JSON.stringify(updatePayload) | |
}); | |
if (updateResponse.ok) { | |
console.log(`Description reverted successfully for issue ${issue.key}`); | |
} else { | |
const errorData = await updateResponse.json(); | |
console.log(`Failed to revert description for issue ${issue.key}. Status: ${updateResponse.status}`); | |
console.log('Error details:', JSON.stringify(errorData, null, 2)); | |
} | |
} | |
} catch (error) { | |
console.error('An error occurred:', error); | |
} | |
} | |
// Example usage | |
const jql = 'project = "YOUR_PROJECT" AND updated >= -7d'; | |
fetchAndRevertJiraDescription(jql); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment