Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ Additionally, the following parameters can be used for additional control over t
### [notify-pipeline-complete](.github/workflows/notify-pipeline-complete.yml)

- Sets a "pipeline" commit status to green for all open PRs
- Pass `state` (e.g. `failure`) to report a state other than `success` on the deployment and commit statuses. Defaults to `success`.
- Pass `sha` to mark the deployment for that commit instead of the newest deployment in the environment. Commit statuses are still set on open PRs (or merge queue commits when `merge_queue_enabled` is `true`).

### [prepare-queued-pr-for-merge](.github/workflows/prepare-queued-pr-for-merge.yml)

Expand Down
13 changes: 8 additions & 5 deletions dist/helpers/notify-pipeline-complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ var notifyPipelineComplete = async ({
description = DEFAULT_PIPELINE_DESCRIPTION,
environment = PRODUCTION_ENVIRONMENT,
target_url,
merge_queue_enabled
merge_queue_enabled,
state = "success",
sha
}) => {
const { data: deployments } = await octokit.repos.listDeployments({
sha,
environment,
...context.repo
});
Expand All @@ -46,7 +49,7 @@ var notifyPipelineComplete = async ({
await octokit.repos.createDeploymentStatus({
environment,
deployment_id,
state: "success",
state,
description,
target_url,
...context.repo
Expand All @@ -56,7 +59,7 @@ var notifyPipelineComplete = async ({
return import_bluebird.map(mergeQueueCommitHashes, async (sha) => octokit.repos.createCommitStatus({
sha,
context: context2,
state: "success",
state,
description,
target_url,
...context.repo
Expand All @@ -71,7 +74,7 @@ var notifyPipelineComplete = async ({
return import_bluebird.map(commitHashesForOpenPullRequests, async (sha) => octokit.repos.createCommitStatus({
sha,
context: context2,
state: "success",
state,
description,
target_url,
...context.repo
Expand All @@ -82,4 +85,4 @@ export {
notifyPipelineComplete
};

//# debugId=3D89CC9C798FA26064756E2164756E21
//# debugId=3185D1B530D6728564756E2164756E21
6 changes: 3 additions & 3 deletions dist/helpers/notify-pipeline-complete.js.map

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

14 changes: 10 additions & 4 deletions src/helpers/notify-pipeline-complete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ limitations under the License.
*/

import { DEFAULT_PIPELINE_DESCRIPTION, DEFAULT_PIPELINE_STATUS, PRODUCTION_ENVIRONMENT } from '../constants';
import { DeploymentState, PipelineState } from '../types/github';
import { HelperInputs } from '../types/generated';
import { context as githubContext } from '@actions/github';
import { map } from 'bluebird';
Expand All @@ -24,16 +25,21 @@ export class NotifyPipelineComplete extends HelperInputs {
declare environment?: string;
declare target_url?: string;
declare merge_queue_enabled?: string;
declare state?: string;
declare sha?: string;
}

export const notifyPipelineComplete = async ({
context = DEFAULT_PIPELINE_STATUS,
description = DEFAULT_PIPELINE_DESCRIPTION,
environment = PRODUCTION_ENVIRONMENT,
target_url,
merge_queue_enabled
merge_queue_enabled,
state = 'success',
sha
}: NotifyPipelineComplete) => {
const { data: deployments } = await octokit.repos.listDeployments({
sha,
environment,
...githubContext.repo
});
Expand All @@ -42,7 +48,7 @@ export const notifyPipelineComplete = async ({
await octokit.repos.createDeploymentStatus({
environment,
deployment_id,
state: 'success',
state: state as DeploymentState,
description,
target_url,
...githubContext.repo
Expand All @@ -54,7 +60,7 @@ export const notifyPipelineComplete = async ({
octokit.repos.createCommitStatus({
sha,
context,
state: 'success',
state: state as PipelineState,
description,
target_url,
...githubContext.repo
Expand All @@ -72,7 +78,7 @@ export const notifyPipelineComplete = async ({
octokit.repos.createCommitStatus({
sha,
context,
state: 'success',
state: state as PipelineState,
description,
target_url,
...githubContext.repo
Expand Down
132 changes: 132 additions & 0 deletions test/helpers/notify-pipeline-complete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,136 @@ describe('notify-pipeline-complete', () => {
...context.repo
});
});

it('should notify with the provided state for open pull requests', async () => {
await notifyPipelineComplete({ state: 'failure' });

expect(octokit.repos.createCommitStatus).toHaveBeenCalledTimes(3);
expect(octokit.repos.createCommitStatus).toHaveBeenCalledWith({
sha: 'sha 1',
context: DEFAULT_PIPELINE_STATUS,
state: 'failure',
description,
...context.repo
});
expect(octokit.repos.createCommitStatus).toHaveBeenCalledWith({
sha: 'sha 2',
context: DEFAULT_PIPELINE_STATUS,
state: 'failure',
description,
...context.repo
});
expect(octokit.repos.createCommitStatus).toHaveBeenCalledWith({
sha: 'sha 3',
context: DEFAULT_PIPELINE_STATUS,
state: 'failure',
description,
...context.repo
});

expect(octokit.repos.createDeploymentStatus).toHaveBeenCalledWith({
state: 'failure',
environment: PRODUCTION_ENVIRONMENT,
deployment_id: 123,
description,
...context.repo
});
});

it('should notify with the provided state when merge queue is enabled', async () => {
(octokit.repos.listBranches as unknown as Mock<any>).mockImplementation(async ({ page }: { page: number }) =>
page > 1
? { data: [] }
: {
data: [
{
name: 'some-branch',
commit: { sha: 'normal sha 1' }
},
{
name: 'gh-readonly-queue/default-branch/pr-123-79a5ad2b1a46f6b5d77e02573937667979635f27',
commit: { sha: 'merge queue sha 1' }
},
{
name: 'gh-readonly-queue/default-branch/pr-456-79a5ad2b1a46f6b5d77e02573937667979635f27',
commit: { sha: 'merge queue sha 2' }
}
]
}
);
await notifyPipelineComplete({ merge_queue_enabled: 'true', state: 'failure' });

expect(octokit.pulls.list).not.toHaveBeenCalled();

expect(octokit.repos.createCommitStatus).toHaveBeenCalledTimes(2);
expect(octokit.repos.createCommitStatus).toHaveBeenCalledWith({
sha: 'merge queue sha 1',
context: DEFAULT_PIPELINE_STATUS,
state: 'failure',
description,
...context.repo
});
expect(octokit.repos.createCommitStatus).toHaveBeenCalledWith({
sha: 'merge queue sha 2',
context: DEFAULT_PIPELINE_STATUS,
state: 'failure',
description,
...context.repo
});

expect(octokit.repos.createDeploymentStatus).toHaveBeenCalledWith({
state: 'failure',
environment: PRODUCTION_ENVIRONMENT,
deployment_id: 123,
description,
...context.repo
});
});

it('should list deployments without a sha when none is provided', async () => {
await notifyPipelineComplete({});

expect(octokit.repos.listDeployments).toHaveBeenCalledWith({
environment: PRODUCTION_ENVIRONMENT,
...context.repo
});
expect(octokit.repos.listDeployments).not.toHaveBeenCalledWith(expect.objectContaining({ sha: expect.anything() }));
});

it('should mark the deployment for the provided sha', async () => {
(octokit.repos.listDeployments as unknown as Mock<any>).mockImplementationOnce(async () => ({
data: [{ id: 456, environment: 'production' }]
}));
await notifyPipelineComplete({ sha: 'target sha', state: 'failure' });

expect(octokit.repos.listDeployments).toHaveBeenCalledWith({
sha: 'target sha',
environment: PRODUCTION_ENVIRONMENT,
...context.repo
});

expect(octokit.repos.createDeploymentStatus).toHaveBeenCalledWith({
state: 'failure',
environment: PRODUCTION_ENVIRONMENT,
deployment_id: 456,
description,
...context.repo
});

expect(octokit.repos.createCommitStatus).toHaveBeenCalledTimes(3);
expect(octokit.repos.createCommitStatus).toHaveBeenCalledWith({
sha: 'sha 1',
context: DEFAULT_PIPELINE_STATUS,
state: 'failure',
description,
...context.repo
});
expect(octokit.repos.createCommitStatus).not.toHaveBeenCalledWith({
sha: 'target sha',
context: DEFAULT_PIPELINE_STATUS,
state: 'failure',
description,
...context.repo
});
});
});
Loading