Skip to content

Commit 5bc0871

Browse files
committed
temp
1 parent 9763350 commit 5bc0871

File tree

2 files changed

+191
-18
lines changed

2 files changed

+191
-18
lines changed

update_jira/index.js

Lines changed: 105 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@ const Jira = require('./../utils/jira')
44

55
const statusMap = {
66
'master': {
7-
status: 'Deployed to Production',
7+
status: 'Done',
88
fields: {
9+
resolution: 'Done',
910
// prod release timestamp
1011
customfield_11475: new Date(),
1112
customfield_11473: 'Production',
1213
}
1314
},
1415
'main': {
15-
status: 'Deployed to Production',
16+
status: 'Done',
1617
fields: {
18+
resolution: 'Done',
1719
// prod release timestamp
1820
customfield_11475: new Date(),
1921
customfield_11473: 'Production',
@@ -29,10 +31,9 @@ const statusMap = {
2931
}
3032
},
3133
'dev': {
32-
status: 'Deployed to Staging',
34+
status: 'Deployed to Dev',
3335
fields: {
3436
resolution: 'Done',
35-
customfield_11473: 'Development',
3637
}
3738
}
3839
}
@@ -208,41 +209,117 @@ async function handlePushEvent(branch, jiraUtil, githubRepository, githubToken)
208209

209210
const newStatus = branchConfig.status
210211
const customFields = branchConfig.fields || {}
211-
212212
const preparedFields = await prepareFields(customFields, jiraUtil)
213213

214+
const shouldCheckCommitHistory = ['master', 'main', 'staging'].includes(branch)
215+
214216
const prMatch = commitMessage.match(/#([0-9]+)/)
217+
218+
// Handle staging to production deployment
215219
if ((branch === 'master' || branch === 'main')) {
216-
// Handle special case: staging -> production bulk update
217-
if (commitMessage === 'from coursedog/staging') {
218-
console.log('Bulk updating all Staging issues to Done')
219-
const issues = await jiraUtil.updateByStatus('Deployed to Staging', newStatus, preparedFields)
220-
console.log(`Issues deployed to production: ${issues.length}`)
221-
return
220+
console.log('Production deployment: extracting issues from commit history')
221+
222+
try {
223+
const commitHistoryIssues = await jiraUtil.getIssueKeysFromCommitHistory('HEAD~100', 'HEAD')
224+
if (commitHistoryIssues.length > 0) {
225+
console.log(`Found ${commitHistoryIssues.length} issues in production commit history`)
226+
227+
const updateResults = await jiraUtil.updateIssuesFromCommitHistory(
228+
commitHistoryIssues,
229+
newStatus,
230+
['Blocked', 'Rejected'],
231+
preparedFields
232+
)
233+
234+
console.log(`Production deployment results: ${updateResults.successful} successful, ${updateResults.failed} failed`)
235+
} else {
236+
console.log('No Jira issues found in production commit history')
237+
}
238+
} catch (error) {
239+
console.error('Error processing production commit history:', error.message)
222240
}
223241

224-
// direct from open PR to staging
242+
// Also handle direct PR merges to production
225243
if (prMatch) {
226244
const prNumber = extractPrNumber(commitMessage)
227245
const prUrl = `${repositoryName}/pull/${prNumber}`
228-
if (!prNumber) return
229-
await jiraUtil.updateByPR(prUrl, newStatus, preparedFields)
230-
return
246+
if (prNumber) {
247+
console.log(`Also updating issues from PR ${prUrl} to production status`)
248+
await jiraUtil.updateByPR(prUrl, newStatus, preparedFields)
249+
}
231250
}
251+
return
232252
}
233253

254+
// Handle dev to staging deployment
234255
if (branch === 'staging') {
235-
console.log('Bulk updating all Deployed to Dev issues to Deployed to Staging')
236-
await jiraUtil.updateByStatus('Deployed to Staging', newStatus, preparedFields)
256+
console.log('Staging deployment: extracting issues from commit history')
257+
258+
try {
259+
// Get issue keys from commit history
260+
const commitHistoryIssues = await jiraUtil.getIssueKeysFromCommitHistory('HEAD~100', 'HEAD')
261+
262+
if (commitHistoryIssues.length > 0) {
263+
console.log(`Found ${commitHistoryIssues.length} issues in staging commit history`)
264+
265+
// Update issues found in commit history
266+
const updateResults = await jiraUtil.updateIssuesFromCommitHistory(
267+
commitHistoryIssues,
268+
newStatus,
269+
['Blocked', 'Rejected'],
270+
preparedFields
271+
)
272+
273+
console.log(`Staging deployment results: ${updateResults.successful} successful, ${updateResults.failed} failed`)
274+
} else {
275+
console.log('No Jira issues found in staging commit history')
276+
}
277+
} catch (error) {
278+
console.error('Error processing staging commit history:', error.message)
279+
}
280+
281+
// Also handle direct PR merges to staging
282+
if (prMatch) {
283+
const prNumber = prMatch[1]
284+
const prUrl = `${repositoryName}/pull/${prNumber}`
285+
console.log(`Also updating issues from PR ${prUrl} to staging status`)
286+
await jiraUtil.updateByPR(prUrl, newStatus, preparedFields)
287+
}
288+
return
237289
}
238290

239-
// Handle PR merges (look for PR number in commit message)
291+
// Handle PR merges to other branches (like dev)
240292
if (prMatch) {
241293
const prNumber = prMatch[1]
242294
const prUrl = `${repositoryName}/pull/${prNumber}`
243295
console.log(`Updating issues mentioning PR ${prUrl} to status: ${newStatus}`)
244296
await jiraUtil.updateByPR(prUrl, newStatus, preparedFields)
245297
}
298+
299+
// Additionally, for important branches, check commit history for issue keys
300+
if (shouldCheckCommitHistory) {
301+
try {
302+
// Get issue keys from recent commit history (last 50 commits)
303+
const commitHistoryIssues = await jiraUtil.getIssueKeysFromCommitHistory('HEAD~50', 'HEAD')
304+
305+
if (commitHistoryIssues.length > 0) {
306+
console.log(`Found ${commitHistoryIssues.length} additional issues in commit history for ${branch} branch`)
307+
308+
// Update issues found in commit history
309+
const updateResults = await jiraUtil.updateIssuesFromCommitHistory(
310+
commitHistoryIssues,
311+
newStatus,
312+
['Blocked', 'Rejected'],
313+
preparedFields
314+
)
315+
316+
console.log(`Commit history update results: ${updateResults.successful} successful, ${updateResults.failed} failed`)
317+
}
318+
} catch (error) {
319+
console.error('Error processing commit history:', error.message)
320+
// Don't fail the entire action if commit history processing fails
321+
}
322+
}
246323
}
247324

248325
/**
@@ -263,3 +340,13 @@ function extractJiraIssueKeys(pullRequest) {
263340

264341
return Array.from(keys)
265342
}
343+
344+
/**
345+
* Extract PR number from commit message
346+
* @param {string} commitMessage - Git commit message
347+
* @returns {string|null} PR number or null if not found
348+
*/
349+
function extractPrNumber(commitMessage) {
350+
const prMatch = commitMessage.match(/#([0-9]+)/)
351+
return prMatch ? prMatch[1] : null
352+
}

utils/jira.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,92 @@ class Jira {
552552
return null
553553
}
554554

555+
/**
556+
* Extract Jira issue keys from git commit history
557+
* @param {string} fromRef - Starting git reference (e.g., 'HEAD~10', commit hash)
558+
* @param {string} toRef - Ending git reference (e.g., 'HEAD', commit hash)
559+
* @param {string} cwd - Working directory for git commands (optional)
560+
* @returns {Promise<Array<string>>} Array of unique Jira issue keys found in commit messages
561+
*/
562+
async getIssueKeysFromCommitHistory(fromRef = 'HEAD~50', toRef = 'HEAD', cwd = process.cwd()) {
563+
try {
564+
const { execSync } = require('child_process')
565+
566+
// Get commit messages from git log
567+
const gitCommand = `git log ${fromRef}..${toRef} --pretty=format:"%s %b" --no-merges`
568+
const commitMessages = execSync(gitCommand, {
569+
cwd,
570+
encoding: 'utf8',
571+
stdio: ['pipe', 'pipe', 'ignore'] // Suppress stderr to avoid noise
572+
})
573+
574+
// Extract Jira issue keys using regex pattern
575+
const jiraKeyPattern = /[A-Z]+-[0-9]+/g
576+
const issueKeys = new Set()
577+
578+
if (commitMessages) {
579+
const matches = commitMessages.match(jiraKeyPattern)
580+
if (matches) {
581+
matches.forEach(key => issueKeys.add(key))
582+
}
583+
}
584+
585+
const uniqueKeys = Array.from(issueKeys)
586+
console.log(`Found ${uniqueKeys.length} unique Jira issue keys in commit history (${fromRef}..${toRef}):`, uniqueKeys)
587+
588+
return uniqueKeys
589+
} catch (error) {
590+
console.error('Error reading git commit history:', error.message)
591+
return []
592+
}
593+
}
594+
595+
/**
596+
* Update multiple issues found in commit history to a target status
597+
* @param {Array<string>} issueKeys - Array of Jira issue keys
598+
* @param {string} targetStatus - Target status name
599+
* @param {Array<string>} excludeStates - Array of state names to exclude from paths (optional)
600+
* @param {Object} fields - Additional fields to set during transition
601+
* @returns {Promise<Object>} Summary of update results
602+
*/
603+
async updateIssuesFromCommitHistory(issueKeys, targetStatus, excludeStates = ['Blocked', 'Rejected'], fields = {}) {
604+
if (!issueKeys || issueKeys.length === 0) {
605+
console.log('No issue keys provided for update')
606+
return { successful: 0, failed: 0, errors: [] }
607+
}
608+
609+
console.log(`Updating ${issueKeys.length} issues to status: ${targetStatus}`)
610+
611+
console.log(issueKeys)
612+
613+
return {
614+
successful: false,
615+
failed: issueKeys.length,
616+
errors: [],
617+
}
618+
619+
const results = await Promise.allSettled(
620+
issueKeys.map(issueKey =>
621+
this.transitionIssue(issueKey, targetStatus, excludeStates, fields)
622+
)
623+
)
624+
625+
const successful = results.filter(result => result.status === 'fulfilled').length
626+
const failed = results.filter(result => result.status === 'rejected')
627+
const errors = failed.map(result => result.reason?.message || 'Unknown error')
628+
629+
console.log(`Update summary: ${successful} successful, ${failed.length} failed`)
630+
if (failed.length > 0) {
631+
console.log('Failed updates:', errors)
632+
}
633+
634+
return {
635+
successful,
636+
failed: failed.length,
637+
errors
638+
}
639+
}
640+
555641
/**
556642
* Transition an issue through multiple states to reach target
557643
* @param {string} issueKey - Jira issue key

0 commit comments

Comments
 (0)