@@ -4,16 +4,18 @@ const Jira = require('./../utils/jira')
44
55const 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+ }
0 commit comments