Skip to content

Commit

Permalink
fix(set-latest-pipeline-status): support deployment without status (#199
Browse files Browse the repository at this point in the history
)
  • Loading branch information
danadajian authored May 15, 2022
1 parent 3a936ab commit 83c9176
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 57 deletions.
12 changes: 4 additions & 8 deletions dist/0.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/0.index.js.map

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

12 changes: 4 additions & 8 deletions src/helpers/set-latest-pipeline-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ limitations under the License.

import * as core from '@actions/core';
import { DEFAULT_PIPELINE_STATUS, GITHUB_OPTIONS, PRODUCTION_ENVIRONMENT } from '../constants';
import { PipelineState } from '../types';
import { DeploymentStatus, PipelineState } from '../types';
import { context as githubContext } from '@actions/github';
import { octokit } from '../octokit';

Expand All @@ -35,24 +35,20 @@ export const setLatestPipelineStatus = async ({
});
const deployment_id = deployments.find(Boolean)?.id;
if (!deployment_id) {
core.setFailed('No deployments found.');
core.setFailed('No deployments found. There must be a GitHub deployment on your repository before this helper can run.');
throw new Error();
}
const { data: deploymentStatuses } = await octokit.repos.listDeploymentStatuses({
deployment_id,
...githubContext.repo,
...GITHUB_OPTIONS
});
const deploymentStatus = deploymentStatuses.find(Boolean);
if (!deploymentStatus) {
core.setFailed('No deployment statuses found.');
throw new Error();
}
const deploymentStatus = deploymentStatuses.find(Boolean) ?? ({} as DeploymentStatus);
const { state, description, target_url } = deploymentStatus;
return octokit.repos.createCommitStatus({
sha,
context,
state: deploymentStateToPipelineStateMap[state],
state: deploymentStateToPipelineStateMap[state] ?? 'pending',
description,
target_url,
...githubContext.repo
Expand Down
4 changes: 3 additions & 1 deletion src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ import { components, operations } from '@octokit/openapi-types/types';
import { RestEndpointMethodTypes } from '@octokit/plugin-rest-endpoint-methods/dist-types/generated/parameters-and-response-types';

export type PipelineState = operations['repos/create-commit-status']['requestBody']['content']['application/json']['state'];
export type DeploymentState = operations['repos/create-deployment-status']['requestBody']['content']['application/json']['state'];
export type DeploymentStatus = operations['repos/create-deployment-status']['requestBody']['content']['application/json'];
export type DeploymentState = DeploymentStatus['state'];
export type PullRequest = components['schemas']['pull-request'];
export type SimplePullRequest = components['schemas']['pull-request-simple'];
export type PullRequestList = RestEndpointMethodTypes['pulls']['list']['response']['data'];
export type ChangedFilesList = RestEndpointMethodTypes['pulls']['listFiles']['response']['data'];
export type CreateDeploymentResponse = components['schemas']['deployment'];
export type ProjectListResponse = RestEndpointMethodTypes['projects']['listForRepo']['response'];
export type ColumnListResponse = RestEndpointMethodTypes['projects']['listColumns']['response'];

export type GithubError = {
status: number;
message: string;
Expand Down
96 changes: 57 additions & 39 deletions test/helpers/set-latest-pipeline-status.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,62 +32,80 @@ jest.mock('@actions/github', () => ({
}));

const deployment_id = 123;

(octokit.repos.listDeploymentStatuses as unknown as Mocktokit).mockImplementation(async () => ({
(octokit.repos.listDeployments as unknown as Mocktokit).mockImplementation(async () => ({
data: [
{
state: 'success',
description: 'description',
target_url: 'url'
id: deployment_id
},
{
state: 'pending',
description: 'other description'
id: 456
}
]
}));

describe('setLatestDeploymentStatus', () => {
const sha = 'sha';
describe('deployment status found', () => {
beforeEach(() => {
(octokit.repos.listDeploymentStatuses as unknown as Mocktokit).mockImplementation(async () => ({
data: [
{
state: 'success',
description: 'description',
target_url: 'url'
},
{
state: 'pending',
description: 'other description'
}
]
}));
setLatestPipelineStatus({ sha });
});

beforeEach(() => {
(octokit.repos.listDeployments as unknown as Mocktokit).mockImplementation(async () => ({
data: [
{
id: deployment_id
},
{
id: 456
}
]
}));
setLatestPipelineStatus({ sha });
});
it('should call listDeployments with correct params', () => {
expect(octokit.repos.listDeployments).toHaveBeenCalledWith({
environment: PRODUCTION_ENVIRONMENT,
...context.repo,
...GITHUB_OPTIONS
});
});

it('should call listDeployments with correct params', () => {
expect(octokit.repos.listDeployments).toHaveBeenCalledWith({
environment: PRODUCTION_ENVIRONMENT,
...context.repo,
...GITHUB_OPTIONS
it('should call listDeploymentStatuses with correct params', () => {
expect(octokit.repos.listDeploymentStatuses).toHaveBeenCalledWith({
deployment_id,
...context.repo,
...GITHUB_OPTIONS
});
});
});

it('should call listDeploymentStatuses with correct params', () => {
expect(octokit.repos.listDeploymentStatuses).toHaveBeenCalledWith({
deployment_id,
...context.repo,
...GITHUB_OPTIONS
it('should call createCommitStatus with correct params', () => {
expect(octokit.repos.createCommitStatus).toHaveBeenCalledWith({
sha,
context: DEFAULT_PIPELINE_STATUS,
state: 'success',
description: 'description',
target_url: 'url',
...context.repo
});
});
});

it('should call createCommitStatus with correct params', () => {
expect(octokit.repos.createCommitStatus).toHaveBeenCalledWith({
sha,
context: DEFAULT_PIPELINE_STATUS,
state: 'success',
description: 'description',
target_url: 'url',
...context.repo
describe('deployment status not found', () => {
beforeEach(() => {
(octokit.repos.listDeploymentStatuses as unknown as Mocktokit).mockImplementation(async () => ({
data: []
}));
setLatestPipelineStatus({ sha });
});

it('should call createCommitStatus with correct params', () => {
expect(octokit.repos.createCommitStatus).toHaveBeenCalledWith({
sha,
context: DEFAULT_PIPELINE_STATUS,
state: 'pending',
...context.repo
});
});
});
});

0 comments on commit 83c9176

Please sign in to comment.