Skip to content

Commit

Permalink
Merge pull request #29 from GrantBirki/output-fmt
Browse files Browse the repository at this point in the history
Pretty output formatting
  • Loading branch information
GrantBirki authored Mar 19, 2024
2 parents 9bc6139 + fcf1469 commit f908497
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/acceptance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ jobs:
- uses: actions/upload-artifact@v4
with:
name: json-diff
path: ~/diff.json
path: diff.json
retention-days: 1
2 changes: 1 addition & 1 deletion .github/workflows/sample-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ jobs:
- uses: actions/upload-artifact@v4
with:
name: json-diff
path: ~/diff.json
path: diff.json
retention-days: 1
39 changes: 29 additions & 10 deletions __tests__/functions/git-diff.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,13 @@ test('executes gitDiff', async () => {
)
expect(results.files[0].chunks[0].changes[2].type).toBe('DeletedLine')

expect(infoMock).toHaveBeenCalledWith('total files changed (raw diff): 5')
expect(infoMock).toHaveBeenCalledWith('total files changed (json diff): 5')
expect(infoMock).toHaveBeenCalledWith('🏃 starting the git-diff-action')
expect(infoMock).toHaveBeenCalledWith(
'🧮 total detected files changed (raw diff): 5'
)
expect(infoMock).toHaveBeenCalledWith(
'🧮 total detected files changed (json diff): 5'
)
})

test('executes gitDiff with binary files', async () => {
Expand All @@ -76,11 +81,16 @@ test('executes gitDiff with binary files', async () => {
expect(results.files[0].chunks[0].changes[2].type).toBe('DeletedLine')

expect(results.files.length).toBe(7)
expect(infoMock).toHaveBeenCalledWith('🏃 starting the git-diff-action')
expect(infoMock).toHaveBeenCalledWith(
'📂 reading git diff from file: __tests__/fixtures/with-binary-files.diff'
)
expect(infoMock).toHaveBeenCalledWith(
'🧮 total detected files changed (raw diff): 7'
)
expect(infoMock).toHaveBeenCalledWith(
'reading git diff from file: __tests__/fixtures/with-binary-files.diff'
'🧮 total detected files changed (json diff): 7'
)
expect(infoMock).toHaveBeenCalledWith('total files changed (raw diff): 7')
expect(infoMock).toHaveBeenCalledWith('total files changed (json diff): 7')
})

// this test case is a bug test
Expand All @@ -99,13 +109,18 @@ test('executes gitDiff with binary files and --binary flag and breaks (bug test)
expect(lastFile.path).toBe('kv-cache.js')

expect(results.files.length).toBe(4)
expect(infoMock).toHaveBeenCalledWith('🏃 starting the git-diff-action')
expect(infoMock).toHaveBeenCalledWith(
'reading git diff from file: __tests__/fixtures/with-binary-files-and-binary-flag.diff'
'📂 reading git diff from file: __tests__/fixtures/with-binary-files-and-binary-flag.diff'
)

// note that the total files changed is 7, but the json diff only has 4 files
expect(infoMock).toHaveBeenCalledWith('total files changed (raw diff): 7')
expect(infoMock).toHaveBeenCalledWith('total files changed (json diff): 4')
expect(infoMock).toHaveBeenCalledWith(
'🧮 total detected files changed (raw diff): 7'
)
expect(infoMock).toHaveBeenCalledWith(
'🧮 total detected files changed (json diff): 4'
)
})

test('executes gitDiff by using the git binary', async () => {
Expand Down Expand Up @@ -150,8 +165,12 @@ test('executes gitDiff by using the git binary', async () => {
expect(infoMock).toHaveBeenCalledWith(
'max_buffer_size is not defined, using default of 1000000'
)
expect(infoMock).toHaveBeenCalledWith('total files changed (raw diff): 5')
expect(infoMock).toHaveBeenCalledWith('total files changed (json diff): 5')
expect(infoMock).toHaveBeenCalledWith(
'🧮 total detected files changed (raw diff): 5'
)
expect(infoMock).toHaveBeenCalledWith(
'🧮 total detected files changed (json diff): 5'
)
expect(debugMock).toHaveBeenCalledWith(
'running git diff command: git --no-pager diff --no-color --full-index HEAD^1 .'
)
Expand Down
17 changes: 12 additions & 5 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

17 changes: 12 additions & 5 deletions src/functions/git-diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import fs from 'fs'
// If an error occurs, setFailed is called and it returns null
export async function gitDiff() {
try {
core.info('🏃 starting the git-diff-action')

// Get the base branch to use for the diff
const baseBranch = core.getInput('base_branch')
core.debug(`base_branch: ${baseBranch}`)
Expand All @@ -26,7 +28,7 @@ export async function gitDiff() {
core.debug(`git_diff_file: ${gitDiffFile}`)
// If git_diff_file is provided, read the file and return the diff
if (gitDiffFile !== 'false') {
core.info(`reading git diff from file: ${gitDiffFile}`)
core.info(`📂 reading git diff from file: ${gitDiffFile}`)
gitDiff = fs.readFileSync(gitDiffFile, 'utf8')
} else {
// if max_buffer_size is not defined, just use the default
Expand Down Expand Up @@ -65,7 +67,9 @@ export async function gitDiff() {
// if for some reason you have the literal string 'diff --git' in your file...
// ... this will report one more file changed than reality (or more if you have more of those strings in your file)
const totalFilesChanged = gitDiff.split('diff --git').length - 1
core.info(`total files changed (raw diff): ${totalFilesChanged}`)
core.info(
`🧮 total detected files changed (raw diff): ${totalFilesChanged}`
)

// only log the raw diff if the Action is explicitly set to run in debug mode
core.debug(`raw git diff: ${gitDiff}`)
Expand All @@ -77,7 +81,7 @@ export async function gitDiff() {
// Write the raw diff to a file if the path is provided
const rawPath = core.getInput('raw_diff_file_output')
if (rawPath) {
core.debug(`writing raw diff to ${rawPath}`)
core.info(`💾 writing raw diff to: ${rawPath}`)
core.setOutput('raw-diff-path', rawPath)
fs.writeFileSync(rawPath, gitDiff)
}
Expand All @@ -87,7 +91,9 @@ export async function gitDiff() {
const jsonDiff = JSON.stringify(diff)

// log the total amount of files changed in the json diff
core.info(`total files changed (json diff): ${diff.files.length}`)
core.info(
`🧮 total detected files changed (json diff): ${diff.files.length}`
)

// only log the json diff if the Action is explicitly set to run in debug mode
core.debug(`jsonDiff: ${jsonDiff}`)
Expand All @@ -100,11 +106,12 @@ export async function gitDiff() {
// Write the JSON diff to a file if the path is provided
const jsonPath = core.getInput('json_diff_file_output')
if (jsonPath) {
core.debug(`writing json diff to ${jsonPath}`)
core.info(`💾 writing json diff to: ${jsonPath}`)
core.setOutput('json-diff-path', jsonPath)
fs.writeFileSync(jsonPath, jsonDiff)
}

core.info('✅ git-diff-action completed successfully')
return diff
} catch (e) {
core.setFailed(`error getting git diff: ${e}`)
Expand Down

0 comments on commit f908497

Please sign in to comment.