Skip to content

Commit

Permalink
Merge pull request #205 from github/identical-commit-check-fixes
Browse files Browse the repository at this point in the history
Identical commit check fixes
  • Loading branch information
GrantBirki authored Sep 7, 2023
2 parents fe51a70 + a6eb3d5 commit 346835a
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 77 deletions.
42 changes: 22 additions & 20 deletions __tests__/functions/identical-commit-check.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as core from '@actions/core'
import {identicalCommitCheck} from '../../src/functions/identical-commit-check'
import {COLORS} from '../../src/functions/colors'

const setOutputMock = jest.spyOn(core, 'setOutput')
const infoMock = jest.spyOn(core, 'info')
Expand Down Expand Up @@ -36,21 +37,23 @@ beforeEach(() => {
getBranch: jest.fn().mockReturnValue({
data: {
commit: {
sha: 'deadbeef'
sha: 'deadbeef',
commit: {
tree: {
sha: 'deadbeef'
}
}
}
}
}),
listCommits: jest.fn().mockReturnValue({
data: [
{
sha: 'deadbeef',
parents: [
{
sha: 'beefdead'
}
]
getCommit: jest.fn().mockReturnValue({
data: {
commit: {
tree: {
sha: 'deadbeef'
}
}
]
}
}),
listDeployments: jest.fn().mockReturnValue({
data: [
Expand All @@ -63,11 +66,6 @@ beforeEach(() => {
}
}
]
}),
compareCommitsWithBasehead: jest.fn().mockReturnValue({
data: {
status: 'identical'
}
})
}
}
Expand All @@ -79,24 +77,28 @@ test('checks if the default branch sha and deployment sha are identical, and the
await identicalCommitCheck(octokit, context, 'production')
).toStrictEqual(true)
expect(infoMock).toHaveBeenCalledWith(
'latest deployment sha is identical to the latest commit sha'
`🟰 the latest deployment tree sha is ${COLORS.highlight}equal${COLORS.reset} to the default branch tree sha`
)
expect(setOutputMock).toHaveBeenCalledWith('continue', 'false')
expect(setOutputMock).toHaveBeenCalledWith('environment', 'production')
})

test('checks if the default branch sha and deployment sha are identical, and they are not', async () => {
octokit.rest.repos.compareCommitsWithBasehead = jest.fn().mockReturnValue({
octokit.rest.repos.getCommit = jest.fn().mockReturnValue({
data: {
status: 'not identical'
commit: {
tree: {
sha: 'beefdead'
}
}
}
})

expect(
await identicalCommitCheck(octokit, context, 'production')
).toStrictEqual(false)
expect(infoMock).toHaveBeenCalledWith(
'a new deployment will be created based on your configuration'
`🚀 a ${COLORS.success}new deployment${COLORS.reset} will be created based on your configuration`
)
expect(setOutputMock).toHaveBeenCalledWith('continue', 'true')
expect(setOutputMock).toHaveBeenCalledWith('environment', 'production')
Expand Down
76 changes: 34 additions & 42 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.

40 changes: 26 additions & 14 deletions src/functions/identical-commit-check.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as core from '@actions/core'
import {COLORS} from './colors'

// Helper function to check if the current deployment's ref is identical to the merge commit
// :param octokit: the authenticated octokit instance
Expand All @@ -23,8 +24,8 @@ export async function identicalCommitCheck(octokit, context, environment) {
repo,
branch: defaultBranchName
})
const defaultBranchTreeSha = defaultBranchData.commit.tree.sha
core.debug(`default branch three sha: ${defaultBranchTreeSha}`)
const defaultBranchTreeSha = defaultBranchData.commit.commit.tree.sha
core.debug(`default branch tree sha: ${defaultBranchTreeSha}`)

// find the latest deployment with the payload type of branch-deploy
const {data: deploymentsData} = await octokit.rest.repos.listDeployments({
Expand All @@ -41,42 +42,53 @@ export async function identicalCommitCheck(octokit, context, environment) {
var deploymentId
for (const deployment of deploymentsData) {
if (deployment.payload.type === 'branch-deploy') {
latestDeploymentSha = deployment.sha
latestDeploymentTreeSha = deployment.sha
createdAt = deployment.created_at
deploymentId = deployment.id

// get the tree sha of the latest deployment
const commitData = await octokit.rest.repos.getCommit({
owner,
repo,
ref: latestDeploymentSha
ref: latestDeploymentTreeSha
})
latestDeploymentTreeSha = commitData.data.tree.sha
latestDeploymentTreeSha = commitData.data.commit.tree.sha
break
}
}

core.info(`latest deployment sha: ${latestDeploymentTreeSha}`)
core.debug('latest deployment with payload type of "branch-deploy"')
core.debug(`latest deployment sha: ${latestDeploymentTreeSha}`)
core.debug(`latest deployment created at: ${createdAt}`)
core.debug(`latest deployment id: ${deploymentId}`)
core.info(
`🌲 latest default ${COLORS.info}branch${COLORS.reset} tree sha: ${COLORS.info}${defaultBranchTreeSha}${COLORS.reset}`
)
core.info(
`🌲 latest ${COLORS.info}deployment${COLORS.reset} tree sha: ${COLORS.info}${latestDeploymentTreeSha}${COLORS.reset}`
)
core.debug('💡 latest deployment with payload type of "branch-deploy"')
core.debug(`🕛 latest deployment created at: ${createdAt}`)
core.debug(`🧮 latest deployment id: ${deploymentId}`)

// if the latest deployment sha is identical to the latest commit on the default branch then return true
const result = latestDeploymentTreeSha === defaultBranchTreeSha

if (result) {
core.info('latest deployment sha is identical to the latest commit sha')
core.info(
'identical commits will not be deployed again based on your configuration'
`🟰 the latest deployment tree sha is ${COLORS.highlight}equal${COLORS.reset} to the default branch tree sha`
)
core.info(
`🌲 identical commit trees will ${COLORS.highlight}not${COLORS.reset} be re-deployed based on your configuration`
)
core.info(
`✅ deployments for the ${COLORS.highlight}${environment}${COLORS.reset} environment are ${COLORS.success}up to date${COLORS.reset}`
)
core.setOutput('continue', 'false')
core.setOutput('environment', environment)
} else {
core.info(
'latest deployment is not identical to the latest commit on the default branch'
`💡 the latest deployment tree sha is ${COLORS.highlight}not${COLORS.reset} equal to the default branch tree sha`
)
core.info(
`🚀 a ${COLORS.success}new deployment${COLORS.reset} will be created based on your configuration`
)
core.info('a new deployment will be created based on your configuration')
core.setOutput('continue', 'true')
core.setOutput('environment', environment)
}
Expand Down

0 comments on commit 346835a

Please sign in to comment.