From 1298a04a15fd470054b42deb1dd6e94446d9c55b Mon Sep 17 00:00:00 2001 From: cenfun Date: Sat, 27 Jul 2024 14:19:13 +0800 Subject: [PATCH] fix test pr --- .github/workflows/test-pr.yml | 8 +++- test/test-pr.js | 72 ++++++++++++++++++++--------------- 2 files changed, 49 insertions(+), 31 deletions(-) diff --git a/.github/workflows/test-pr.yml b/.github/workflows/test-pr.yml index b535fbe8..835792b3 100644 --- a/.github/workflows/test-pr.yml +++ b/.github/workflows/test-pr.yml @@ -13,6 +13,7 @@ on: permissions: contents: read + pull-requests: write jobs: @@ -33,6 +34,11 @@ jobs: - run: npm run build - run: npm run test-pr env: - myToken: ${{ secrets.GITHUB_TOKEN }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} FORCE_COLOR: true + + - name: Generate list using Markdown + run: | + cat docs/pr/coverage-summary.md >> $GITHUB_STEP_SUMMARY + cat docs/pr/coverage-details.md >> $GITHUB_STEP_SUMMARY diff --git a/test/test-pr.js b/test/test-pr.js index 902e95c1..07f2b849 100644 --- a/test/test-pr.js +++ b/test/test-pr.js @@ -10,30 +10,39 @@ const getPullRequestChanges = async () => { return []; } - console.log('pull_request', github.context.payload.pull_request); - - // This should be a token with access to your repository scoped in as a secret. - // The YML workflow will need to set myToken with the GitHub Secret Token - // myToken: ${{ secrets.GITHUB_TOKEN }} - // https://help.github.com/en/actions/automating-your-workflow-with-github-actions/authenticating-with-the-github_token#about-the-github_token-secret - const myToken = core.getInput('myToken'); - console.log('myToken', `${myToken}`.length); - - // const octokit = github.getOctokit(myToken); - - // // You can also pass in additional options as a second parameter to getOctokit - // // const octokit = github.getOctokit(myToken, {userAgent: "MyActionVersion1"}); - - // const { data } = await octokit.rest.pulls.get({ - // owner: 'octokit', - // repo: 'rest.js', - // pull_number: 123, - // mediaType: { - // format: 'diff' - // } - // }); - - // return data; + // console.log('pull_request', github.context.payload.pull_request); + + /** + * env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + */ + + const octokit = github.getOctokit(process.env.GITHUB_TOKEN); + const prNumber = github.context.payload.pull_request.number; + core.startGroup(`Fetching list of changed files for PR#${prNumber} from Github API`); + + const iterator = octokit.paginate.iterator( + octokit.rest.pulls.listFiles, { + owner: github.context.repo.owner, + repo: github.context.repo.repo, + pull_number: prNumber, + per_page: 100 + } + ); + + const files = []; + for await (const response of iterator) { + core.info(`Received ${response.data.length} items`); + + for (const file of response.data) { + core.debug(`[${file.status}] ${file.filename}`); + if (['added', 'modified'].includes(file.status)) { + files.push(file.filename); + } + } + } + + return files; }; @@ -72,12 +81,15 @@ const test = async () => { outputDir: './docs/pr', - sourceFilter: { - '**/src/**': true - }, - - onEnd: (coverageResults) => { - + sourceFilter: (sourcePath) => { + if (prChanges.length) { + for (const file of prChanges) { + if (sourcePath.includes(file)) { + return true; + } + } + } + return false; } };