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
Comment on lines +66 to +67
GITHUB_ACTION_MODE: true
Comment on lines +66 to +68
```
6 changes: 5 additions & 1 deletion lib/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ module.exports = {
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',
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
Comment on lines +10 to +13
Comment on lines +10 to +13
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

Comment on lines +10 to +13
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

GHE_HOST: process.env.GHE_HOST,
GHE_PROTOCOL: process.env.GHE_PROTOCOL,
GHE_PROTOCOL: process.env.GHE_PROTOCOL
}
37 changes: 35 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,39 @@ ${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}`)
}
} else {
this.log.warn
? this.log.warn(`GITHUB_ACTION_MODE is set but GITHUB_REF ('${env.GITHUB_REF}') does not reference a pull request; skipping PR comment and continuing to complete check run.`)
: this.log.info(`GITHUB_ACTION_MODE is set but GITHUB_REF ('${env.GITHUB_REF}') does not reference a pull request; skipping PR comment and continuing to complete check run.`)
Comment on lines +304 to +306
Comment on lines +304 to +306
}
} else if (env.GITHUB_ACTION_MODE && (!env.GITHUB_REPOSITORY || !env.GITHUB_REF)) {
const missingVars = [
!env.GITHUB_REPOSITORY && 'GITHUB_REPOSITORY',
!env.GITHUB_REF && 'GITHUB_REF'
].filter(Boolean).join(' and ')
const message = `GITHUB_ACTION_MODE is set but required environment variable(s) ${missingVars} are missing or empty; skipping PR comment and continuing to complete check run.`
this.log.warn ? this.log.warn(message) : this.log.info(message)
Comment on lines +304 to +314
}

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 @@ -979,6 +1011,7 @@ ${this.results.reduce((x, y) => {
}
return typeof obj[Symbol.iterator] === 'function'
}

}

function prettify (obj) {
Expand Down
Loading