Skip to content

Commit

Permalink
feat(update-check-result): add update-check-result helper (#578)
Browse files Browse the repository at this point in the history
  • Loading branch information
danadajian authored Apr 1, 2024
1 parent 1db42dc commit 37fe882
Show file tree
Hide file tree
Showing 11 changed files with 267 additions and 3 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/update-check-result.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Update Check Result

on:
pull_request:
branches: [ main ]
paths:
- 'src/helpers/update-check-result.ts'

jobs:
test:
name: Update Check Result
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- uses: ./
with:
helper: update-check-result
context: Update Check Result
sha: ${{ github.event.pull_request.head.sha }}
state: success
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ Additionally, the following parameters can be used for additional control over t

- Determines whether the pipeline is clear for a PR. This means it will set the "pipeline" commit status to `pending` if there is an in-progress production deployment for the repo, and `success` otherwise.

### [update-check-result](.github/workflows/update-check-result.yml)
-
- Updates the result of a previous PR check tied to a commit status.

## Legal

This project is available under the [Apache 2.0 License](http://www.apache.org/licenses/LICENSE-2.0.html).
Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ inputs:
description: 'The status context or contexts for updating commit status. Multiple contexts must be newline separated'
required: false
state:
description: 'The state for updating commit status. Can be pending, success, or failure'
description: 'The state for updating commit status. Typically will be "success" or "failure"'
required: false
description:
description: 'A description'
Expand Down
125 changes: 125 additions & 0 deletions dist/148.index.js

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

1 change: 1 addition & 0 deletions dist/148.index.js.map

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

2 changes: 1 addition & 1 deletion dist/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ inputs:
description: 'The status context or contexts for updating commit status. Multiple contexts must be newline separated'
required: false
state:
description: 'The state for updating commit status. Can be pending, success, or failure'
description: 'The state for updating commit status. Typically will be "success" or "failure"'
required: false
description:
description: 'A description'
Expand Down
10 changes: 10 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42295,6 +42295,16 @@ var map = {
2000,
832,
0
],
"./update-check-result": [
3148,
832,
148
],
"./update-check-result.ts": [
3148,
832,
148
]
};
function webpackAsyncContext(req) {
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions src/helpers/update-check-result.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright 2021 Expedia, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { HelperInputs } from '../types/generated';
import { context as githubContext } from '@actions/github';
import { octokit } from '../octokit';
import { ChecksUpdateConclusion } from '../types/github';

export class UpdateCheckResult extends HelperInputs {
context = '';
sha = '';
state = '';
description?: string;
}

export const updateCheckResult = async ({ context, sha, state, description }: UpdateCheckResult) => {
const checks = await octokit.checks.listForRef({
ref: sha,
check_name: context,
...githubContext.repo
});
const check_run_id = checks.data.check_runs[0]?.id;
if (!check_run_id) {
throw new Error('Check run not found');
}

return octokit.checks.update({
check_run_id,
conclusion: state as ChecksUpdateConclusion,
output: {
title: description ?? `Check updated to ${state}`,
summary: 'Check updated via update-check-result helper'
},
...githubContext.repo
});
};
1 change: 1 addition & 0 deletions src/types/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type PullRequestBranchesList = RestEndpointMethodTypes['repos']['listBran
export type ChangedFilesList = RestEndpointMethodTypes['pulls']['listFiles']['response']['data'];
export type ProjectListResponse = RestEndpointMethodTypes['projects']['listForRepo']['response'];
export type ColumnListResponse = RestEndpointMethodTypes['projects']['listColumns']['response'];
export type ChecksUpdateConclusion = RestEndpointMethodTypes['checks']['update']['parameters']['conclusion'];

export type GithubError = {
status: number;
Expand Down
55 changes: 55 additions & 0 deletions test/helpers/update-check-result.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Copyright 2021 Expedia, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { context } from '@actions/github';
import { updateCheckResult } from '../../src/helpers/update-check-result';
import { octokit } from '../../src/octokit';

jest.mock('@actions/core');
jest.mock('@actions/github', () => ({
context: { repo: { repo: 'repo', owner: 'owner' }, issue: { number: 123 } },
getOctokit: jest.fn(() => ({
rest: {
checks: {
listForRef: jest.fn(() => ({ data: { check_runs: [{ id: 123 }] } })),
update: jest.fn()
}
}
}))
}));

describe('updateCheckResult', () => {
beforeEach(async () => {
await updateCheckResult({ context: 'My PR Check', sha: 'sha', state: 'success' });
});

it('should call checks.update with correct params', () => {
expect(octokit.checks.listForRef).toHaveBeenCalledWith({
ref: 'sha',
check_name: 'My PR Check',
...context.repo
});
});

it('should call checks.update with correct params', () => {
expect(octokit.checks.update).toHaveBeenCalledWith({
check_run_id: 123,
conclusion: 'success',
output: {
title: 'Check updated to success',
summary: 'Check updated via update-check-result helper'
},
...context.repo
});
});
});

0 comments on commit 37fe882

Please sign in to comment.