Skip to content

Commit

Permalink
fix: Add git log functionality to generate changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
GagikNav committed Jan 5, 2024
1 parent 082ca92 commit 597c69b
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fs from 'fs'
import * as core from '@actions/core'
import * as github from '@actions/github'
import process from 'process'
import * as exec from '@actions/exec'
import { generateChangelog } from './generateChangelog'

const repoPath = './' // Replace with the path to your Git repository
Expand All @@ -12,11 +13,13 @@ let currentBranch = process.env.GITHUB_REF

// Get the default branch from the github context
let defaultBranch: string = github.context.payload.repository?.default_branch
console.log('Default branch is: ' + defaultBranch)

let changelog: string = ''

// Check if the current branch exists
if (currentBranch) {
console.log('Current branch is: ' + currentBranch)
currentBranch = currentBranch.replace('refs/heads/', '')
} else {
core.setFailed('Could not determine the current branch')
Expand All @@ -41,19 +44,38 @@ if (!fs.existsSync(repoPath)) {
console.error('The specified repository does not exist.')
process.exit(1)
}
async function run() {
let output = ''
let error = ''
const options = {
listeners: {
stdout: (data: Buffer) => {
output += data.toString()
},
stderr: (data: Buffer) => {
error += data.toString()
},
},
}

// @ts-ignore
const git = simpleGit(repoPath)
await exec.exec(
'git',
['log', `--pretty=format:%H %aI %s %D %b %aN %aE`, `${defaultBranch}...${currentBranch}`],
options
)

git.log({ from: defaultBranch, to: currentBranch }, (err, log) => {
if (err) {
console.error(err)
if (error) {
console.error(error)
return
}
const commits = log.all
// Process commits and generate Markdown
changelog = generateChangelog(commits)
})

const log = output.split('\n').map((line) => {
const [hash, date, message, refNames, body, authorName, authorEmail] = line.split(' ')
return { hash, date, message, refNames, body, authorName, authorEmail }
})
// Process commits and generate Markdown
changelog = generateChangelog(log)
}
run()
// Set the CHANGELOG environment variable
core.exportVariable('CHANGELOG', changelog)

0 comments on commit 597c69b

Please sign in to comment.