diff --git a/.eslintrc.json b/.eslintrc.json index 8d54013..66de414 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -11,6 +11,7 @@ "eslint-comments/no-use": "off", "import/no-namespace": "off", "no-unused-vars": "off", + "no-console": "off", "@typescript-eslint/no-unused-vars": "error", "@typescript-eslint/explicit-member-accessibility": ["error", {"accessibility": "no-public"}], "@typescript-eslint/no-require-imports": "error", diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 02b965d..0da146b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -2,22 +2,33 @@ name: "build-test" on: # rebuild any PRs and main branch changes pull_request: push: - branches: - - master - - 'releases/*' jobs: - build: # make sure build/ci work properly +# build: # make sure build/ci work properly +# runs-on: ubuntu-latest +# steps: +# - uses: actions/checkout@v1 +# - run: | +# npm install +# npm run all + test: # make sure the action works on a clean machine without building runs-on: ubuntu-latest steps: - - uses: actions/checkout@v1 - - run: | - npm install - npm run all - test: # make sure the action works on a clean machine without building + - uses: hmarr/debug-action@v1.0.0 + - uses: actions/checkout@v2 + - uses: ./ + if: ${{ github.event_name == 'pull_request' }} + with: + path: '**/pmd.xml' + pr: # make sure the action works on a clean machine without building runs-on: ubuntu-latest steps: + - uses: hmarr/debug-action@v1.0.0 - uses: actions/checkout@v2 + with: + ref: ${{ github.event.pull_request.head.sha }} - uses: ./ - with: - path: '**/pmd.xml' \ No newline at end of file + if: ${{ github.event_name == 'pull_request' }} + with: + path: '**/pmd.xml' + ref: ${{ github.event.pull_request.head.sha }} diff --git a/action.yml b/action.yml index c281852..5b36d71 100644 --- a/action.yml +++ b/action.yml @@ -20,6 +20,10 @@ inputs: Also when generating a new PAT, select the least scopes necessary. [Learn more about creating and using encrypted secrets](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/creating-and-using-encrypted-secrets) default: ${{ github.token }} + ref: + description: > + The SHA to post the results to. + default: ${{ github.sha }} runs: using: 'node12' main: 'dist/index.js' diff --git a/dist/index.js b/dist/index.js index 8a3da7c..187661d 100644 --- a/dist/index.js +++ b/dist/index.js @@ -597,6 +597,7 @@ var Inputs; Inputs["Title"] = "title"; Inputs["Path"] = "path"; Inputs["Token"] = "token"; + Inputs["Ref"] = "ref"; })(Inputs = exports.Inputs || (exports.Inputs = {})); @@ -8458,11 +8459,31 @@ function run() { function createCheck(name, title, annotations, numErrors) { return __awaiter(this, void 0, void 0, function* () { const octokit = github_1.getOctokit(core.getInput(constants_1.Inputs.Token)); - const req = Object.assign(Object.assign({}, github_1.context.repo), { ref: github_1.context.sha }); + const head_sha = core.getInput('ref', { required: true }); + const req = Object.assign(Object.assign({}, github_1.context.repo), { ref: head_sha }); + const run_id = Number(process.env['GITHUB_RUN_ID']); + const workflowRun = yield octokit.actions.getWorkflowRun(Object.assign(Object.assign({}, github_1.context.repo), { run_id })); + console.log('Workflow run: %o', workflowRun.data); + // Gotta love Github's crippled API + const checkSuiteUrl = workflowRun.data.check_suite_url; + const checkSuiteId = Number(checkSuiteUrl.substring(checkSuiteUrl.lastIndexOf('/') + 1)); + core.info(`Posting check result for ${head_sha}`); + const suitesRes = yield octokit.checks.listSuitesForRef(req); + // const suitesById = indexBy( + // suite => String(suite.id), + // suitesRes.data.check_suites + // ) const res = yield octokit.checks.listForRef(req); - const existingCheckRun = res.data.check_runs.find(check => check.name === name); + const existingCheckRun = res.data.check_runs.find(check => check.name === name && check.check_suite.id === checkSuiteId); + if (existingCheckRun) { + console.log('Found existing check run %o', existingCheckRun); + } + else { + console.log(' The check suites are %o', suitesRes.data.check_suites); + } if (!existingCheckRun) { - const createRequest = Object.assign(Object.assign({}, github_1.context.repo), { head_sha: github_1.context.sha, name, status: 'completed', conclusion: numErrors === 0 ? 'success' : 'neutral', output: { + const createRequest = Object.assign(Object.assign({}, github_1.context.repo), { head_sha, + name, status: 'completed', conclusion: numErrors === 0 ? 'success' : 'neutral', output: { title, summary: `${numErrors} violation(s) found`, annotations diff --git a/src/constants.ts b/src/constants.ts index 19e0cd9..15e38b9 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -2,5 +2,6 @@ export enum Inputs { Name = 'name', Title = 'title', Path = 'path', - Token = 'token' + Token = 'token', + Ref = 'ref' } diff --git a/src/main.ts b/src/main.ts index cf72f04..de51c49 100644 --- a/src/main.ts +++ b/src/main.ts @@ -56,20 +56,50 @@ async function createCheck( numErrors: number ): Promise { const octokit = getOctokit(core.getInput(Inputs.Token)) + const head_sha = core.getInput('ref', {required: true}) const req = { ...context.repo, - ref: context.sha + ref: head_sha } + const run_id = Number(process.env['GITHUB_RUN_ID']) + + const workflowRun = await octokit.actions.getWorkflowRun({ + ...context.repo, + run_id + }) + + console.log('Workflow run: %o', workflowRun.data) + + // Gotta love Github's crippled API + const checkSuiteUrl = workflowRun.data.check_suite_url + const checkSuiteId = Number( + checkSuiteUrl.substring(checkSuiteUrl.lastIndexOf('/') + 1) + ) + + core.info(`Posting check result for ${head_sha}`) + + const suitesRes = await octokit.checks.listSuitesForRef(req) + // const suitesById = indexBy( + // suite => String(suite.id), + // suitesRes.data.check_suites + // ) + const res = await octokit.checks.listForRef(req) const existingCheckRun = res.data.check_runs.find( - check => check.name === name + check => check.name === name && check.check_suite.id === checkSuiteId ) + if (existingCheckRun) { + console.log('Found existing check run %o', existingCheckRun) + } else { + console.log(' The check suites are %o', suitesRes.data.check_suites) + } + if (!existingCheckRun) { const createRequest = { ...context.repo, - head_sha: context.sha, + head_sha, name, status: 'completed', conclusion: numErrors === 0 ? 'success' : 'neutral',