diff --git a/.github/workflows/pull-request.yaml b/.github/workflows/pull-request.yaml index ef336d8..bcbb0d8 100644 --- a/.github/workflows/pull-request.yaml +++ b/.github/workflows/pull-request.yaml @@ -8,29 +8,28 @@ jobs: permissions: contents: read - pull-requests: write runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - - - name: Analyze content.txt - id: analyze - uses: actions/github-script@v7 - with: - script: | - const pullRequest = require('./.github/workflows/scripts/pull-request.js') - await pullRequest({ github, context, core }, "", null); - - - if: ${{ steps.analyze.outputs.FOO == 'true' }} - run: - touch foo.txt - - - if: ${{ steps.analyze.outputs.FOO == 'true' }} - uses: actions/upload-artifact@v4 - with: - name: foo - path: foo.txt - if-no-files-found: error - overwrite: true + - uses: actions/checkout@v4 + + - name: Analyze content.txt + id: analyze + uses: actions/github-script@v7 + with: + debug: true + script: | + const pullRequest = require('./.github/workflows/scripts/pull-request.js') + await pullRequest({ github, context, core }, "", null); + + - if: ${{ steps.analyze.outputs.FOO == 'true' }} + run: touch foo.txt + + - if: ${{ steps.analyze.outputs.FOO == 'true' }} + uses: actions/upload-artifact@v4 + with: + name: foo + path: foo.txt + if-no-files-found: error + overwrite: true diff --git a/.github/workflows/scripts/pull-request.js b/.github/workflows/scripts/pull-request.js index 654efc1..65f177f 100644 --- a/.github/workflows/scripts/pull-request.js +++ b/.github/workflows/scripts/pull-request.js @@ -1,53 +1,15 @@ // @ts-check -const { readFile } = require("fs/promises"); -const { join } = require("path"); -const { env } = require("process"); - /** * @param {import('github-script').AsyncFunctionArguments} AsyncFunctionArguments * @param {string} folder * @param {string?} label */ module.exports = async ({ github, context, core }, folder, label) => { - console.log(JSON.stringify(context, null, 2)); - - const file = join(folder, "content.txt"); - - const payload = /** @type {import("@octokit/webhooks-types").PullRequestEvent} */ (context.payload); - - const permissionLevel = await github.rest.repos.getCollaboratorPermissionLevel({ - owner: payload.repository.owner.login, - repo: payload.repository.name, - username: payload.pull_request.user.login, + const branchRules = await github.rest.repos.getBranchRules({ + owner: context.repo.owner, + repo: context.repo.repo, + branch: context.payload.pull_request?.base.ref, }); - - console.log(JSON.stringify(permissionLevel, null, 2)); - - try { - const content = await readFile(file, { encoding: "utf8" }); - console.log(`File '${file}' exists. Content:\n${content}`); - if (content.includes("foo")) { - if (label) { - console.log(`Adding label '${label}'`); - await github.rest.issues.addLabels({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: context.payload.pull_request?.number ?? -1, - labels: [label], - }); - } - console.log('setOutput("FOO", true)'); - core.setOutput("FOO", true); - } else { - console.log('setOutput("FOO", false)'); - core.setOutput("FOO", false); - } - } catch (err) { - if (err.code === "ENOENT" || err.status === "ENOENT") { - console.log(`File '${file}' does not exist`); - } else { - throw err; - } - } + core.info(JSON.stringify(branchRules, null, 2)); };