@@ -553,43 +553,83 @@ class Jira {
553553 }
554554
555555 /**
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
556+ * Extract Jira issue keys from commit messages
557+ * @param {Array<string>|string } commitMessages - Array of commit messages or single commit message string
558+ * @returns {Array<string> } Array of unique Jira issue keys found in commit messages
561559 */
562- async getIssueKeysFromCommitHistory ( fromRef = 'HEAD~50' , toRef = 'HEAD' , cwd = process . cwd ( ) ) {
560+ extractIssueKeysFromCommitMessages ( commitMessages ) {
563561 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- console . log ( commitMessages )
562+ // Handle both array and string inputs
563+ const messages = Array . isArray ( commitMessages ) ? commitMessages . join ( ' ' ) : commitMessages
575564
576565 // Extract Jira issue keys using regex pattern
577566 const jiraKeyPattern = / [ A - Z ] + - [ 0 - 9 ] + / g
578567 const issueKeys = new Set ( )
579568
580- if ( commitMessages ) {
581- const matches = commitMessages . match ( jiraKeyPattern )
569+ if ( messages ) {
570+ const matches = messages . match ( jiraKeyPattern )
582571 if ( matches ) {
583572 matches . forEach ( key => issueKeys . add ( key ) )
584573 }
585574 }
586575
587576 const uniqueKeys = Array . from ( issueKeys )
588- console . log ( `Found ${ uniqueKeys . length } unique Jira issue keys in commit history (${ fromRef } ..${ toRef } ):` , uniqueKeys )
577+ console . log ( `Found ${ uniqueKeys . length } unique Jira issue keys in commit messages:` , uniqueKeys )
578+
579+ return uniqueKeys
580+ } catch ( error ) {
581+ console . error ( 'Error extracting Jira issue keys from commit messages:' , error . message )
582+ return [ ]
583+ }
584+ }
585+
586+ /**
587+ * Extract Jira issue keys from GitHub context (for GitHub Actions)
588+ * @param {Object } context - GitHub Actions context object
589+ * @returns {Array<string> } Array of unique Jira issue keys found in PR/push context
590+ */
591+ extractIssueKeysFromGitHubContext ( context ) {
592+ try {
593+ const issueKeys = new Set ( )
594+ const jiraKeyPattern = / [ A - Z ] + - [ 0 - 9 ] + / g
595+
596+ // Extract from PR title and body
597+ // if (context.payload.pull_request) {
598+ // const pr = context.payload.pull_request
599+ // const prTitle = pr.title || ''
600+ // const prBody = pr.body || ''
601+ //
602+ // const prMatches = (prTitle + ' ' + prBody).match(jiraKeyPattern)
603+ // if (prMatches) {
604+ // prMatches.forEach(key => issueKeys.add(key))
605+ // }
606+ // }
607+
608+ // Extract from commit messages in the payload
609+ if ( context . payload . commits ) {
610+ context . payload . commits . forEach ( commit => {
611+ const commitMessage = commit . message || ''
612+ const commitMatches = commitMessage . match ( jiraKeyPattern )
613+ if ( commitMatches ) {
614+ commitMatches . forEach ( key => issueKeys . add ( key ) )
615+ }
616+ } )
617+ }
618+
619+ // Extract from head commit message
620+ if ( context . payload . head_commit && context . payload . head_commit . message ) {
621+ const headCommitMatches = context . payload . head_commit . message . match ( jiraKeyPattern )
622+ if ( headCommitMatches ) {
623+ headCommitMatches . forEach ( key => issueKeys . add ( key ) )
624+ }
625+ }
626+
627+ const uniqueKeys = Array . from ( issueKeys )
628+ console . log ( `Found ${ uniqueKeys . length } unique Jira issue keys in GitHub context:` , uniqueKeys )
589629
590630 return uniqueKeys
591631 } catch ( error ) {
592- console . error ( 'Error reading git commit history :' , error . message )
632+ console . error ( 'Error extracting Jira issue keys from GitHub context :' , error . message )
593633 return [ ]
594634 }
595635 }
0 commit comments