Skip to content

Commit

Permalink
refactor postDeployMessage() and pass the commit sha all the way …
Browse files Browse the repository at this point in the history
…through to the `post-deploy-message()` function so that is available as a template variable for deployment messages
  • Loading branch information
GrantBirki committed Dec 6, 2024
1 parent ae602fc commit 15744bb
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 128 deletions.
69 changes: 27 additions & 42 deletions __tests__/functions/post-deploy-message.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ var status
var noop
var ref
var approved_reviews_count
var sha
var data

beforeEach(() => {
jest.clearAllMocks()
Expand All @@ -28,6 +30,7 @@ beforeEach(() => {
status = 'success'
noop = false
ref = 'test-ref'
sha = 'abc123'
approved_reviews_count = '4'

context = {
Expand All @@ -44,18 +47,23 @@ beforeEach(() => {
}
}
}

data = {
environment: environment,
environment_url: environment_url,
status: status,
noop: noop,
ref: ref,
sha: sha,
approved_reviews_count: approved_reviews_count
}
})

test('successfully constructs a post deploy message with the defaults', async () => {
expect(
await postDeployMessage(
context, // context
environment, // environment
environment_url, // environment_url
status, // status
noop, // noop
ref, // ref
approved_reviews_count // approved_reviews_count
data
)
).toStrictEqual(
dedent(`
Expand All @@ -68,15 +76,11 @@ test('successfully constructs a post deploy message with the defaults', async ()
})

test('successfully constructs a post deploy message with the defaults during a "noop" deploy', async () => {
data.noop = true
expect(
await postDeployMessage(
context, // context
environment, // environment
environment_url, // environment_url
status, // status
true, // noop
ref, // ref
approved_reviews_count // approved_reviews_count
data
)
).toStrictEqual(
dedent(`
Expand All @@ -87,15 +91,11 @@ test('successfully constructs a post deploy message with the defaults during a "
})

test('successfully constructs a post deploy message with the defaults during a deployment failure', async () => {
data.status = 'failure'
expect(
await postDeployMessage(
context, // context
environment, // environment
environment_url, // environment_url
'failure', // status
noop, // noop
ref, // ref
approved_reviews_count // approved_reviews_count
data
)
).toStrictEqual(
dedent(`
Expand All @@ -106,15 +106,11 @@ test('successfully constructs a post deploy message with the defaults during a d
})

test('successfully constructs a post deploy message with the defaults during a deployment with an unknown status', async () => {
data.status = 'unknown'
expect(
await postDeployMessage(
context, // context
environment, // environment
environment_url, // environment_url
'unknown', // status
noop, // noop
ref, // ref
approved_reviews_count // approved_reviews_count
data
)
).toStrictEqual(
dedent(`
Expand All @@ -126,15 +122,11 @@ test('successfully constructs a post deploy message with the defaults during a d

test('successfully constructs a post deploy message with the defaults during a deployment with an unknown status and the DEPLOY_MESSAGE_PATH is unset', async () => {
process.env.INPUT_DEPLOY_MESSAGE_PATH = ''
data.status = 'unknown'
expect(
await postDeployMessage(
context, // context
environment, // environment
environment_url, // environment_url
'unknown', // status
noop, // noop
ref, // ref
approved_reviews_count // approved_reviews_count
data
)
).toStrictEqual(
dedent(`
Expand All @@ -152,12 +144,7 @@ test('successfully constructs a post deploy message with a custom env var', asyn
expect(
await postDeployMessage(
context, // context
environment, // environment
environment_url, // environment_url
status, // status
noop, // noop
ref, // ref
approved_reviews_count // approved_reviews_count
data
)
).toStrictEqual(
dedent(`
Expand All @@ -181,12 +168,7 @@ test('successfully constructs a post deploy message with a custom markdown file'
expect(
await postDeployMessage(
context, // context
environment, // environment
environment_url, // environment_url
status, // status
noop, // noop
ref, // ref
approved_reviews_count // approved_reviews_count
data
)
).toStrictEqual(
dedent(`### Deployment Results :rocket:
Expand All @@ -198,13 +180,16 @@ test('successfully constructs a post deploy message with a custom markdown file'
- \`status\` - The status of the deployment (String) - \`success\`, \`failure\`, or \`unknown\`
- \`noop\` - Whether or not the deployment is a noop (Boolean)
- \`ref\` - The ref of the deployment (String)
- \`sha\` - The exact commit SHA of the deployment (String)
- \`actor\` - The GitHub username of the actor who triggered the deployment (String)
- \`approved_reviews_count\` - The number of approved reviews on the pull request at the time of deployment (String of a number)
Here is an example:
monalisa deployed branch \`test-ref\` to the **production** environment. This deployment was a success :rocket:.
The exact commit sha that was used for the deployment was \`${sha}\`.
You can view the deployment [here](https://example.com).
Expand Down
3 changes: 2 additions & 1 deletion __tests__/functions/post-deploy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,12 @@ beforeEach(() => {
}

data = {
sha: 'abc123',
ref: 'test-ref',
comment_id: 123,
reaction_id: 12345,
status: 'success',
message: 'test-message',
ref: 'test-ref',
noop: false,
deployment_id: 456,
environment: 'production',
Expand Down
4 changes: 4 additions & 0 deletions __tests__/functions/post.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const validInputs = {
}

const validStates = {
sha: 'abc123',
ref: 'test-ref',
comment_id: '123',
noop: 'false',
Expand Down Expand Up @@ -61,6 +62,9 @@ beforeEach(() => {

test('successfully runs post() Action logic', async () => {
expect(await post()).toBeUndefined()
expect(infoMock).toHaveBeenCalledWith(
`🧑‍🚀 commit SHA: ${COLORS.highlight}${validStates.sha}${COLORS.reset}`
)
})

test('successfully runs post() Action logic when environment_url is not defined', async () => {
Expand Down
3 changes: 3 additions & 0 deletions __tests__/templates/test_deployment_message.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ The following variables are available to use in this template:
- `status` - The status of the deployment (String) - `success`, `failure`, or `unknown`
- `noop` - Whether or not the deployment is a noop (Boolean)
- `ref` - The ref of the deployment (String)
- `sha` - The exact commit SHA of the deployment (String)
- `actor` - The GitHub username of the actor who triggered the deployment (String)
- `approved_reviews_count` - The number of approved reviews on the pull request at the time of deployment (String of a number)

Here is an example:

{{ actor }} deployed branch `{{ ref }}` to the **{{ environment }}** environment. This deployment was a {{ status }} {{ ":rocket:" if status === "success" else ":cry:" }}.

The exact commit sha that was used for the deployment was `{{ sha }}`.

{% if environment_url %}You can view the deployment [here]({{ environment_url }}).{% endif %}

{% if noop %}This was a noop deployment.{% endif %}
Expand Down
84 changes: 42 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.

Loading

0 comments on commit 15744bb

Please sign in to comment.