generated from ExpediaGroup/new-project
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(update-check-result): add update-check-result helper (#578)
- Loading branch information
1 parent
1db42dc
commit 37fe882
Showing
11 changed files
with
267 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
}); | ||
}); |