mirror of
https://github.com/n8n-io/n8n.git
synced 2026-10-19 16:55:02 +02:00
Add an optional `env` parameter to the shared `writeGithubOutput` in github-helpers.mjs. The default keeps all existing callers unchanged. Delete the local copy in sync-master-to-3x.mjs and import the shared helper. The local copy only differed by that injectable `env`. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
124 lines
3.3 KiB
JavaScript
124 lines
3.3 KiB
JavaScript
import { afterEach, beforeEach, describe, it, mock } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { mkdtempSync, readFileSync } from 'node:fs';
|
|
import { tmpdir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
|
|
/**
|
|
* Run these tests by running
|
|
*
|
|
* node --test --experimental-test-module-mocks ./.github/scripts/github-helpers.test.mjs
|
|
* */
|
|
|
|
let octokitImpl;
|
|
|
|
mock.module('@actions/github', {
|
|
namedExports: {
|
|
getOctokit: () => octokitImpl(),
|
|
},
|
|
});
|
|
|
|
const { postOrUpdateComment, writeGithubOutput } = await import('./github-helpers.mjs');
|
|
|
|
const ORIGINAL_ENV = { ...process.env };
|
|
|
|
describe('postOrUpdateComment', () => {
|
|
beforeEach(() => {
|
|
process.env.GITHUB_TOKEN = 'token';
|
|
process.env.GITHUB_REPOSITORY = 'n8n-io/n8n';
|
|
});
|
|
|
|
afterEach(() => {
|
|
process.env = { ...ORIGINAL_ENV };
|
|
});
|
|
|
|
it('creates a new comment when no existing bot-marker comment is found', async () => {
|
|
const createComment = mock.fn(async () => {});
|
|
const updateComment = mock.fn(async () => {});
|
|
const paginate = mock.fn(async () => [{ id: 1, body: 'unrelated comment' }]);
|
|
octokitImpl = () => ({
|
|
paginate,
|
|
rest: {
|
|
issues: {
|
|
listComments: {},
|
|
createComment,
|
|
updateComment,
|
|
},
|
|
},
|
|
});
|
|
|
|
await postOrUpdateComment(123, 'new body', '<!-- marker -->');
|
|
|
|
assert.equal(updateComment.mock.calls.length, 0);
|
|
assert.equal(createComment.mock.calls.length, 1);
|
|
assert.deepEqual(createComment.mock.calls[0].arguments[0], {
|
|
owner: 'n8n-io',
|
|
repo: 'n8n',
|
|
issue_number: 123,
|
|
body: 'new body',
|
|
});
|
|
assert.deepEqual(paginate.mock.calls[0].arguments[1], {
|
|
owner: 'n8n-io',
|
|
repo: 'n8n',
|
|
issue_number: 123,
|
|
per_page: 100,
|
|
});
|
|
});
|
|
|
|
it('updates the existing comment when a bot-marker comment is found', async () => {
|
|
const createComment = mock.fn(async () => {});
|
|
const updateComment = mock.fn(async () => {});
|
|
octokitImpl = () => ({
|
|
paginate: mock.fn(async () => [{ id: 42, body: '<!-- marker -->\nold body' }]),
|
|
rest: {
|
|
issues: {
|
|
listComments: {},
|
|
createComment,
|
|
updateComment,
|
|
},
|
|
},
|
|
});
|
|
|
|
await postOrUpdateComment(123, 'updated body', '<!-- marker -->');
|
|
|
|
assert.equal(createComment.mock.calls.length, 0);
|
|
assert.equal(updateComment.mock.calls.length, 1);
|
|
assert.deepEqual(updateComment.mock.calls[0].arguments[0], {
|
|
owner: 'n8n-io',
|
|
repo: 'n8n',
|
|
comment_id: 42,
|
|
body: 'updated body',
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('writeGithubOutput', () => {
|
|
it('appends key=value lines to the GITHUB_OUTPUT of the given env', () => {
|
|
const file = join(mkdtempSync(join(tmpdir(), 'github-output-')), 'output.txt');
|
|
|
|
writeGithubOutput(
|
|
{ conflict_pr: 'https://example.test/pr/1', conflict_owners: '' },
|
|
{ GITHUB_OUTPUT: file },
|
|
);
|
|
|
|
assert.equal(
|
|
readFileSync(file, 'utf8'),
|
|
'conflict_pr=https://example.test/pr/1\nconflict_owners=\n',
|
|
);
|
|
});
|
|
|
|
it('does nothing when the given env has no GITHUB_OUTPUT', () => {
|
|
assert.doesNotThrow(() => writeGithubOutput({ conflict_pr: 'https://example.test/pr/1' }, {}));
|
|
});
|
|
|
|
it('falls back to process.env when no env is given', () => {
|
|
const file = join(mkdtempSync(join(tmpdir(), 'github-output-')), 'output.txt');
|
|
process.env.GITHUB_OUTPUT = file;
|
|
|
|
writeGithubOutput({ target_branches: 'release/1.x' });
|
|
|
|
assert.equal(readFileSync(file, 'utf8'), 'target_branches=release/1.x\n');
|
|
delete process.env.GITHUB_OUTPUT;
|
|
});
|
|
});
|