Skip to content
Open
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
12 changes: 12 additions & 0 deletions docs/github-action.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`).

Expand Down Expand Up @@ -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
```
6 changes: 5 additions & 1 deletion lib/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
27 changes: 25 additions & 2 deletions lib/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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({
Expand Down Expand Up @@ -978,6 +1000,7 @@ ${this.results.reduce((x, y) => {
}
return typeof obj[Symbol.iterator] === 'function'
}

}

function prettify (obj) {
Expand Down