Skip to content

feat: Add environment variable branch support and enhanced pattern ma… #844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main-enterprise
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 16 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,22 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
log: robot.log,
repo: () => { return { repo: env.ADMIN_REPO, owner: installation.account.login } }
}
return syncAllSettings(nop, context)

// Use environment variable for branch reference, fallback to undefined (main branch)
let ref = process.env.SAFE_SETTINGS_BRANCH || process.env.GITHUB_HEAD_REF || process.env.GITHUB_REF_NAME || process.env.GITHUB_REF

// If we have a ref and it doesn't start with refs/, assume it's a branch name and add refs/heads/
if (ref && !ref.startsWith('refs/')) {
ref = `refs/heads/${ref}`
}
// If it starts with refs/heads/ already, use as-is
// If it's undefined, will use main branch

if (ref) {
robot.log.info(`Using branch reference: ${ref}`)
}

return syncAllSettings(nop, context, context.repo(), ref)
}
return null
}
Expand Down
23 changes: 22 additions & 1 deletion lib/glob.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,28 @@ class Glob {
}

test (input) {
return minimatch(input, this.pattern, this.options)
// Detect if pattern looks like regex (has regex-specific characters)
if (this.isRegexPattern(this.pattern)) {
try {
const regex = new RegExp(this.pattern)
return regex.test(input)
} catch (e) {
// If regex parsing fails, fall back to glob
return minimatch(input, this.pattern, this.options)
}
} else {
// Use glob pattern matching
return minimatch(input, this.pattern, this.options)
}
}

isRegexPattern (pattern) {
// Check for common regex indicators
return pattern.includes('^') ||
pattern.includes('$') ||
pattern.includes('.*') ||
pattern.includes('\\') ||
(pattern.includes('[') && pattern.includes(']') && pattern.includes('^'))
}
}

Expand Down
28 changes: 18 additions & 10 deletions lib/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class Settings {
constructor (nop, context, repo, config, ref, suborg) {
this.ref = ref
this.context = context
this.installation_id = context.payload.installation.id
this.installation_id = context.payload?.installation?.id || context.installation?.id
this.github = context.octokit
this.repo = repo
this.config = config
Expand Down Expand Up @@ -151,8 +151,8 @@ class Settings {
logError (msg) {
this.log.error(msg)
this.errors.push({
owner: this.repo.owner,
repo: this.repo.repo,
owner: this.repo?.owner || 'unknown',
repo: this.repo?.repo || 'unknown',
msg,
plugin: this.constructor.name
})
Expand All @@ -168,6 +168,12 @@ class Settings {
return
}

// Only proceed with PR comment/check run updates if we have webhook context
if (!payload || !payload.check_run) {
this.log.debug('Not in webhook context, skipping PR comment and check run updates')
return
}

// remove duplicate rows in this.results
this.results = this.results.filter((thing, index, self) => {
return index === self.findIndex((t) => {
Expand Down Expand Up @@ -234,7 +240,7 @@ class Settings {

const renderedCommentMessage = await eta.renderString(commetMessageTemplate, stats)

if (env.CREATE_PR_COMMENT === 'true') {
if (env.CREATE_PR_COMMENT === 'true' && payload.check_run?.check_suite?.pull_requests?.length > 0) {
const summary = `
#### :robot: Safe-Settings config changes detected:

Expand All @@ -261,16 +267,16 @@ ${this.results.reduce((x, y) => {
const pullRequest = payload.check_run.check_suite.pull_requests[0]

await this.github.issues.createComment({
owner: payload.repository.owner.login,
repo: payload.repository.name,
owner: payload.repository?.owner?.login || this.repo?.owner || 'unknown',
repo: payload.repository?.name || this.repo?.repo || 'unknown',
issue_number: pullRequest.number,
body: summary.length > 55536 ? `${summary.substring(0, 55536)}... (too many changes to report)` : summary
})
}

const params = {
owner: payload.repository.owner.login,
repo: payload.repository.name,
owner: payload.repository?.owner?.login || this.repo?.owner || 'unknown',
repo: payload.repository?.name || this.repo?.repo || 'unknown',
check_run_id: payload.check_run.id,
status: 'completed',
conclusion: error ? 'failure' : 'success',
Expand Down Expand Up @@ -494,8 +500,10 @@ ${this.results.reduce((x, y) => {
return null
}

const { owner, name } = repository
return this.updateRepos({ owner: owner.login, repo: name })
// Handle both string and object repository owner formats
const owner = typeof repository.owner === 'string' ? repository.owner : repository.owner.login
const { name } = repository
return this.updateRepos({ owner, repo: name })
})
)
})
Expand Down