diff --git a/.github/workflows/automation-group-issues.yml b/.github/workflows/automation-group-issues.yml index a350d83..43dd458 100644 --- a/.github/workflows/automation-group-issues.yml +++ b/.github/workflows/automation-group-issues.yml @@ -16,7 +16,7 @@ on: jobs: issue-open: name: Issue Open - if: ${{ github.event_name == 'issues' && contains(fromJSON('["opened","reopened"]'), github.event.action) }} + if: ${{ github.event_name == 'issues' && (contains(fromJSON('["opened","reopened"]'), github.event.action) || (github.event.action == 'edited' && github.event.changes.body)) }} uses: learningequality/.github/.github/workflows/manage-issue-header.yml@main permissions: contents: read diff --git a/.github/workflows/automation.yml b/.github/workflows/automation.yml index 8c7e19a..bdbefd2 100644 --- a/.github/workflows/automation.yml +++ b/.github/workflows/automation.yml @@ -42,7 +42,7 @@ jobs: SLACK_COMMUNITY_NOTIFICATIONS_WEBHOOK_URL: ${{ secrets.SLACK_COMMUNITY_NOTIFICATIONS_WEBHOOK_URL }} issues: name: Issues - if: ${{ (github.event_name == 'issues' && contains(fromJSON('["opened","reopened"]'), github.event.action)) || (github.event_name == 'issues' && contains(fromJSON('["labeled","unlabeled"]'), github.event.action) && github.event.label.name == 'help wanted') || (github.event_name == 'issues' && github.event.action == 'labeled' && (github.event.label.name == 'good first issue' || github.event.label.name == 'help wanted')) || (github.event_name == 'issues' && contains(fromJSON('["assigned","unassigned"]'), github.event.action)) || (github.event_name == 'issues' && (github.event.action == 'opened' || (contains(fromJSON('["edited","reopened"]'), github.event.action) && contains(github.event.issue.labels.*.name, 'needs-template')))) }} + if: ${{ (github.event_name == 'issues' && (contains(fromJSON('["opened","reopened"]'), github.event.action) || (github.event.action == 'edited' && github.event.changes.body))) || (github.event_name == 'issues' && contains(fromJSON('["labeled","unlabeled"]'), github.event.action) && github.event.label.name == 'help wanted') || (github.event_name == 'issues' && github.event.action == 'labeled' && (github.event.label.name == 'good first issue' || github.event.label.name == 'help wanted')) || (github.event_name == 'issues' && contains(fromJSON('["assigned","unassigned"]'), github.event.action)) || (github.event_name == 'issues' && (github.event.action == 'opened' || (contains(fromJSON('["edited","reopened"]'), github.event.action) && contains(github.event.issue.labels.*.name, 'needs-template')))) }} uses: learningequality/.github/.github/workflows/automation-group-issues.yml@main permissions: contents: read diff --git a/automation-registry.yml b/automation-registry.yml index dee6802..be65c36 100644 --- a/automation-registry.yml +++ b/automation-registry.yml @@ -93,8 +93,8 @@ automations: leaf: manage-issue-header.yml on: issues: - types: [opened, reopened] - if: "github.event_name == 'issues' && contains(fromJSON('[\"opened\",\"reopened\"]'), github.event.action)" + types: [opened, reopened, edited] + if: "github.event_name == 'issues' && (contains(fromJSON('[\"opened\",\"reopened\"]'), github.event.action) || (github.event.action == 'edited' && github.event.changes.body))" secrets: LE_BOT_APP_ID: required LE_BOT_PRIVATE_KEY: required diff --git a/scripts/manage-issue-header.js b/scripts/manage-issue-header.js index 5bb8c6e..23f6eaf 100644 --- a/scripts/manage-issue-header.js +++ b/scripts/manage-issue-header.js @@ -54,7 +54,7 @@ module.exports = async ({ github, context, core }) => { header = isIssueHelpWanted(issue) ? HELP_WANTED_HEADER : NON_HELP_WANTED_HEADER; break; case 'reopened': - // check for pre-existing 'help wanted' label + case 'edited': header = isIssueHelpWanted(issue) ? HELP_WANTED_HEADER : NON_HELP_WANTED_HEADER; break; case 'labeled': @@ -94,8 +94,11 @@ module.exports = async ({ github, context, core }) => { const currentBody = issue.data.body || ''; - let newBody = clearHeader(currentBody); - newBody = header + newBody; + const newBody = header + clearHeader(currentBody); + if (newBody === currentBody) { + core.info('Header is already up to date.'); + return; + } await github.rest.issues.update({ owner: repoOwner, diff --git a/scripts/manage-issue-header.test.js b/scripts/manage-issue-header.test.js new file mode 100644 index 0000000..3f5a76e --- /dev/null +++ b/scripts/manage-issue-header.test.js @@ -0,0 +1,58 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); + +const manageIssueHeader = require('./manage-issue-header'); + +function fakeGithub(body) { + const updates = []; + return { + updates, + rest: { + issues: { + get: async () => ({ data: { body } }), + update: async ({ body: updated }) => updates.push(updated), + }, + }, + }; +} + +function fakeCore() { + const failures = []; + return { failures, info() {}, setFailed: message => failures.push(message) }; +} + +function fakeContext(action, labels = []) { + return { + repo: { owner: 'learningequality', repo: 'kolibri' }, + payload: { action, issue: { number: 7, labels: labels.map(name => ({ name })) } }, + }; +} + +async function run(action, body, labels) { + const github = fakeGithub(body); + const core = fakeCore(); + await manageIssueHeader({ github, context: fakeContext(action, labels), core }); + assert.deepEqual(core.failures, []); + return github.updates; +} + +test('an edit that drops the header restores it', async () => { + const [body] = await run('edited', 'Steps to reproduce', ['help wanted']); + + assert.match(body, /^[\s\S]*This issue is open for contribution/); + assert.match(body, /\n\nSteps to reproduce$/); +}); + +test('an edit that keeps the header leaves the body alone', async () => { + const [withHeader] = await run('edited', 'Steps to reproduce', []); + + assert.deepEqual(await run('edited', withHeader, []), []); +}); + +test('an edit replaces a header that no longer matches the labels', async () => { + const [notOpen] = await run('edited', 'Steps to reproduce', []); + const [open] = await run('edited', notOpen, ['help wanted']); + + assert.match(open, /This issue is open for contribution/); + assert.doesNotMatch(open, /not open for contribution/); +});