Skip to content

Commit 7617e67

Browse files
committed
temp
1 parent 0e104de commit 7617e67

File tree

2 files changed

+66
-22
lines changed

2 files changed

+66
-22
lines changed

update_jira/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const core = require('@actions/core')
2+
const github = require('@actions/github')
23
const { Octokit } = require('@octokit/rest')
34
const Jira = require('./../utils/jira')
45

@@ -257,7 +258,10 @@ async function handlePushEvent(branch, jiraUtil, githubRepository, githubToken)
257258

258259
try {
259260
// Get issue keys from commit history
260-
const commitHistoryIssues = await jiraUtil.getIssueKeysFromCommitHistory('HEAD~100', 'HEAD')
261+
const commitHistoryIssues = await jiraUtil.extractIssueKeysFromGithubContext(github.context)
262+
console.log(commitHistoryIssues)
263+
264+
return
261265

262266
if (commitHistoryIssues.length > 0) {
263267
console.log(`Found ${commitHistoryIssues.length} issues in staging commit history`)

utils/jira.js

Lines changed: 61 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)