Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 21 additions & 22 deletions .github/workflows/pull-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
48 changes: 5 additions & 43 deletions .github/workflows/scripts/pull-request.js
Original file line number Diff line number Diff line change
@@ -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));
};