This is an automated email from the ASF dual-hosted git repository. potiuk pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/airflow-checks-action.git
commit 442ad2296fb110373e3fe01c2a3717b546583631 Author: Louis Brunner <[email protected]> AuthorDate: Sat Nov 7 14:50:02 2020 +0000 Select the right SHA for pull_request triggers, fixes #12 (#13) --- .prettierrc.json | 2 +- __tests__/main.test.ts | 60 ++++++++++++++++++++++++++++++++++++++++++++---- dist/index.js | 2 +- src/main.ts | 28 ++++++++++++++++++---- src/namespaces/GitHub.ts | 4 ++++ src/namespaces/Inputs.ts | 10 ++++---- 6 files changed, 90 insertions(+), 16 deletions(-) diff --git a/.prettierrc.json b/.prettierrc.json index bf0887a..45441f4 100644 --- a/.prettierrc.json +++ b/.prettierrc.json @@ -1,5 +1,5 @@ { - "printWidth": 120, + "printWidth": 100, "tabWidth": 2, "useTabs": false, "semi": true, diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index 8f4c49e..e7b1dba 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -1,5 +1,21 @@ import * as cp from 'child_process'; import * as path from 'path'; +import * as fs from 'fs'; +import * as os from 'os'; + +const fakeEvent = (event: Record<string, unknown>, scope: (filename: string) => void): void => { + const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'checks-actions-')); + const filename = path.join(directory, 'github_event.json'); + fs.writeFileSync(filename, JSON.stringify(event)); + try { + scope(filename); + } finally { + fs.unlinkSync(filename); + fs.rmdirSync(directory); + } +}; + +type ExecSyncError = Error & {stdout: Buffer}; // shows how the runner will run a javascript action with env / stdout protocol test('test runs (creation)', () => { @@ -17,7 +33,7 @@ test('test runs (creation)', () => { try { console.log(cp.execSync(`node ${entry}`, options).toString()); } catch (e) { - const error = e as Error & {stdout: Buffer}; + const error = e as ExecSyncError; const output = error.stdout.toString(); console.log(output); expect(output).toMatch(/::debug::Creating a new Run on LB\/ABC@SHA/); @@ -40,7 +56,7 @@ test('test runs (update)', () => { try { console.log(cp.execSync(`node ${entry}`, options).toString()); } catch (e) { - const error = e as Error & {stdout: Buffer}; + const error = e as ExecSyncError; const output = error.stdout.toString(); console.log(output); expect(output).toMatch(/::debug::Updating a Run on LB\/ABC@SHA \(123\)/); @@ -65,7 +81,7 @@ test('test runs (creation on remote repository)', () => { try { console.log(cp.execSync(`node ${entry}`, options).toString()); } catch (e) { - const error = e as Error & {stdout: Buffer}; + const error = e as ExecSyncError; const output = error.stdout.toString(); console.log(output); expect(output).toMatch(/::debug::Creating a new Run on remote\/repo@DEF/); @@ -90,7 +106,7 @@ test('test runs (update on remote repository)', () => { try { console.log(cp.execSync(`node ${entry}`, options).toString()); } catch (e) { - const error = e as Error & {stdout: Buffer}; + const error = e as ExecSyncError; const output = error.stdout.toString(); console.log(output); expect(output).toMatch(/::debug::Updating a Run on remote\/repo@DEF \(123\)/); @@ -115,11 +131,45 @@ test('test rejects invalid repo', () => { try { console.log(cp.execSync(`node ${entry}`, options).toString()); } catch (e) { - const error = e as Error & {stdout: Buffer}; + const error = e as ExecSyncError; const output = error.stdout.toString(); console.log(output); expect(output).toMatch(/::debug::Error: repo needs to be in the {owner}\/{repository} format/); } }); +test('test runs (creation + pull_request)', () => { + const event = { + pull_request: { + head: { + sha: '123', + }, + }, + }; + fakeEvent(event, (filename: string): void => { + const entry = path.join(__dirname, '..', 'lib', 'main.js'); + const options: cp.ExecSyncOptions = { + env: { + GITHUB_REPOSITORY: 'LB/ABC', + GITHUB_SHA: 'SHA', + GITHUB_EVENT_NAME: 'pull_request', + GITHUB_EVENT_PATH: filename, + INPUT_TOKEN: 'ABC', + INPUT_NAME: 'ABC', + INPUT_STATUS: 'completed', + INPUT_CONCLUSION: 'success', + }, + }; + try { + console.log(cp.execSync(`node ${entry}`, options).toString()); + } catch (e) { + const error = e as ExecSyncError; + const output = error.stdout.toString(); + console.log(output); + expect(output).toMatch(/::debug::Creating a new Run on LB\/ABC@123/); + expect(output).toMatch(/::debug::HttpError: Bad credentials/); + } + }); +}); + // TODO: add more diff --git a/dist/index.js b/dist/index.js index 0283cd9..e7efeb5 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1 +1 @@ -module.exports=(()=>{var __webpack_modules__={321:function(e,t,r){"use strict";var s=this&&this.__createBinding||(Object.create?function(e,t,r,s){if(s===undefined)s=r;Object.defineProperty(e,s,{enumerable:true,get:function(){return t[r]}})}:function(e,t,r,s){if(s===undefined)s=r;e[s]=t[r]});var o=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:true,value:t})}:function(e,t){e["default"]=t});var n=this&&this.__importStar||function(e [...] \ No newline at end of file +module.exports=(()=>{var __webpack_modules__={321:function(e,t,r){"use strict";var s=this&&this.__createBinding||(Object.create?function(e,t,r,s){if(s===undefined)s=r;Object.defineProperty(e,s,{enumerable:true,get:function(){return t[r]}})}:function(e,t,r,s){if(s===undefined)s=r;e[s]=t[r]});var o=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:true,value:t})}:function(e,t){e["default"]=t});var n=this&&this.__importStar||function(e [...] \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index ce440ae..c89c2d5 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,6 +1,7 @@ import * as core from '@actions/core'; import * as github from '@actions/github'; import * as Inputs from './namespaces/Inputs'; +import * as GitHub from './namespaces/GitHub'; import {parseInputs} from './inputs'; import {createRun, updateRun} from './checks'; @@ -8,6 +9,27 @@ const isCreation = (inputs: Inputs.Args): inputs is Inputs.ArgsCreate => { return !!(inputs as Inputs.ArgsCreate).name; }; +// prettier-ignore +const prEvents = [ + 'pull_request', + 'pull_request_review', + 'pull_request_review_comment', +]; + +const getSHA = (inputSHA: string | undefined): string => { + let sha = github.context.sha; + if (prEvents.includes(github.context.eventName)) { + const pull = github.context.payload.pull_request as GitHub.PullRequest; + if (pull?.head.sha) { + sha = pull?.head.sha; + } + } + if (inputSHA) { + sha = inputSHA; + } + return sha; +}; + async function run(): Promise<void> { try { core.debug(`Parsing inputs`); @@ -20,7 +42,7 @@ async function run(): Promise<void> { owner: github.context.repo.owner, repo: github.context.repo.repo, }; - let sha = github.context.sha; + const sha = getSHA(inputs.sha); if (inputs.repo) { const repo = inputs.repo.split('/'); @@ -28,10 +50,6 @@ async function run(): Promise<void> { ownership.repo = repo[1]; } - if (inputs.sha) { - sha = inputs.sha; - } - if (isCreation(inputs)) { core.debug(`Creating a new Run on ${ownership.owner}/${ownership.repo}@${sha}`); const id = await createRun(octokit, inputs.name, sha, ownership, inputs); diff --git a/src/namespaces/GitHub.ts b/src/namespaces/GitHub.ts new file mode 100644 index 0000000..cad698f --- /dev/null +++ b/src/namespaces/GitHub.ts @@ -0,0 +1,4 @@ +import {RestEndpointMethodTypes} from '@octokit/rest'; + +// @octokit/rest > Endpoints.d.ts > PullsGetResponseData +export type PullRequest = RestEndpointMethodTypes['pulls']['get']['response']['data']; diff --git a/src/namespaces/Inputs.ts b/src/namespaces/Inputs.ts index 12cdca8..592348f 100644 --- a/src/namespaces/Inputs.ts +++ b/src/namespaces/Inputs.ts @@ -26,18 +26,20 @@ export interface ArgsUpdate extends ArgsBase { export type Args = ArgsCreate | ArgsUpdate; -// ChecksCreateParamsOutputAnnotations[] +// @octokit/rest > Endpoints.d.ts > ChecksCreateParamsOutputAnnotations[] export type Annotations = NonNullable< NonNullable<RestEndpointMethodTypes['checks']['create']['parameters']['output']>['annotations'] >; -// ChecksCreateParamsOutputImages[] +// @octokit/rest > Endpoints.d.ts > ChecksCreateParamsOutputImages[] export type Images = NonNullable< NonNullable<RestEndpointMethodTypes['checks']['create']['parameters']['output']>['images'] >; -// ChecksCreateParamsActions[] -export type Actions = NonNullable<RestEndpointMethodTypes['checks']['create']['parameters']['actions']>; +// @octokit/rest > Endpoints.d.ts > ChecksCreateParamsActions[] +export type Actions = NonNullable< + RestEndpointMethodTypes['checks']['create']['parameters']['actions'] +>; export type Output = { summary: string;
