diff --git a/docs/github-action.md b/docs/github-action.md index 5424b7cc..a7013ee7 100644 --- a/docs/github-action.md +++ b/docs/github-action.md @@ -11,6 +11,15 @@ Follow the [Create the GitHub App](deploy.md#create-the-github-app) guide to cre Running a full-sync with `safe-settings` can be done via `npm run full-sync`. This requires installing Node, such as with [actions/setup-node](https://github.com/actions/setup-node) (see example below). When doing so, the appropriate environment variables must be set (see the [Environment variables](#environment-variables) document for more details). +### GitHub Action Mode + +When running safe-settings in GitHub Actions, you can enable `GITHUB_ACTION_MODE=true` to automatically post PR comments using the built-in GitHub Actions environment variables. When this mode is enabled: + +- `GITHUB_REPOSITORY` (format: `owner/repo`) - automatically injected by GitHub Actions +- `GITHUB_REF` (format: `refs/pull/123/merge` for PRs) - automatically injected by GitHub Actions + +These variables are used to identify the PR and post comments without additional configuration. + ### Example GHA Workflow The below example uses the GHA "cron" feature to run a full-sync every 4 hours. While not required, this example uses the `.github` repo as the `admin` repo (set via `ADMIN_REPO` env var) and the safe-settings configurations are stored in the `safe-settings/` directory (set via `CONFIG_PATH` and `DEPLOYMENT_CONFIG_FILE`). @@ -54,4 +63,7 @@ jobs: ADMIN_REPO: .github CONFIG_PATH: safe-settings DEPLOYMENT_CONFIG_FILE: ${{ github.workspace }}/safe-settings/deployment-settings.yml + # Enable GitHub Action mode to post PR comments using built-in env vars + # GITHUB_REPOSITORY and GITHUB_REF are automatically injected by GitHub Actions + GITHUB_ACTION_MODE: true ``` diff --git a/lib/env.js b/lib/env.js index 94c0ea74..32dd56cd 100644 --- a/lib/env.js +++ b/lib/env.js @@ -6,5 +6,9 @@ module.exports = { CREATE_PR_COMMENT: process.env.CREATE_PR_COMMENT || 'true', CREATE_ERROR_ISSUE: process.env.CREATE_ERROR_ISSUE || 'true', BLOCK_REPO_RENAME_BY_HUMAN: process.env.BLOCK_REPO_RENAME_BY_HUMAN || 'false', - FULL_SYNC_NOP: process.env.FULL_SYNC_NOP === 'true' + FULL_SYNC_NOP: process.env.FULL_SYNC_NOP === 'true', + GITHUB_ACTION_MODE: process.env.GITHUB_ACTION_MODE === 'true', + // GitHub Actions built-in variables + GITHUB_REPOSITORY: process.env.GITHUB_REPOSITORY || '', // format: owner/repo + GITHUB_REF: process.env.GITHUB_REF || '' // format: refs/pull/123/merge for PRs } diff --git a/lib/settings.js b/lib/settings.js index 8d9e07b2..e7f227b1 100644 --- a/lib/settings.js +++ b/lib/settings.js @@ -259,8 +259,7 @@ class Settings { const renderedCommentMessage = await eta.renderString(commetMessageTemplate, stats) - if (env.CREATE_PR_COMMENT === 'true') { - const summary = ` + const summary = ` #### :robot: Safe-Settings config changes detected: ${this.results.reduce((x, y) => { @@ -283,6 +282,29 @@ ${this.results.reduce((x, y) => { }, table)} ` + // In GITHUB_ACTION_MODE, post a PR comment using GitHub Actions built-in env vars + if (env.GITHUB_ACTION_MODE && env.GITHUB_REPOSITORY && env.GITHUB_REF) { + const prMatch = env.GITHUB_REF.match(/refs\/pull\/(\d+)\//) + if (prMatch) { + const [owner, repo] = env.GITHUB_REPOSITORY.split('/') + const prNumber = parseInt(prMatch[1], 10) + + try { + await this.github.issues.createComment({ + owner, + repo, + issue_number: prNumber, + body: summary.length > 55536 ? `${summary.substring(0, 55536)}... (too many changes to report)` : summary + }) + this.log.info(`PR comment posted to ${owner}/${repo}#${prNumber}`) + } catch (e) { + this.log.error(`Failed to post PR comment: ${e.message}`) + } + } + return + } + + if (env.CREATE_PR_COMMENT === 'true') { const pullRequest = payload.check_run.check_suite.pull_requests[0] await this.github.issues.createComment({ @@ -978,6 +1000,7 @@ ${this.results.reduce((x, y) => { } return typeof obj[Symbol.iterator] === 'function' } + } function prettify (obj) {