Skip to content
Draft
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ See [CONTRIBUTING.md](CONTRIBUTING.md).
- **`GITHUB_WEBHOOK_SECRET`**<br>
The webhook secret that GitHub signs the POSTed payloads with. This is created when the webhook is defined. The default is `hush-hush`.
- **`JENKINS_WORKER_IPS`**<br>
List of valid Jenkins worker IPs allowed to push PR status updates, split by comma: `192.168.1.100,192.168.1.101`.
Required list of valid Jenkins worker IPs allowed to push PR status updates, split by comma:
`192.168.1.100,192.168.1.101`. Jenkins callbacks are rejected when this variable is not set.
- **`JENKINS_API_CREDENTIALS`** (optional)<br>
For scripts that communicate with Jenkins on http://ci.nodejs.org. The Jenkins API token is visible on
your own profile page `https://ci.nodejs.org/user/<YOUR_GITHUB_USERNAME>/configure`, by clicking the
Expand Down
132 changes: 132 additions & 0 deletions lib/jenkins-ci-results.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { Writable } from 'node:stream'

import CLI from '@node-core/utils/lib/cli.js'
import Request from '@node-core/utils/lib/request.js'
import { PRBuild } from '@node-core/utils/lib/ci/build-types/pr_build.js'

import { createPrComment } from './github-comment.js'

const maxCommentLength = 60_000

function findLastDetailsCloseOutsideFence (markdown) {
let inCodeFence = false
let lastDetailsClose = -1
let offset = 0

for (const line of markdown.split('\n')) {
if (line.trimStart().startsWith('```')) {
inCodeFence = !inCodeFence
} else if (!inCodeFence) {
const closeIndex = line.lastIndexOf('</details>')
if (closeIndex !== -1) {
lastDetailsClose = offset + closeIndex + '</details>'.length
}
}
offset += line.length + 1
}

return lastDetailsClose
}

export function extractBuildNumber (build) {
// e.g. https://ci.nodejs.org/job/node-test-pull-request/21633/
const match = /\/job\/node-test-pull-request\/(\d+)/.exec(build.url || '')
return match ? parseInt(match[1], 10) : null
}

export async function fetchResultsMarkdown (buildNumber, destination) {
// node-core-utils insists on writing progress output to a stream
const nullStream = new Writable({ write (chunk, encoding, callback) { callback() } })
const cli = new CLI(nullStream)
const request = new Request({
jenkins: Buffer.from(process.env.JENKINS_API_CREDENTIALS).toString('base64')
})

const prBuild = new PRBuild(cli, request, buildNumber)
const results = await prBuild.getResults()
throwOnTopLevelRetrievalFailure(results)

validateBuildDestination(prBuild.params, destination)
return prBuild.formatAsMarkdown()
}

export function throwOnTopLevelRetrievalFailure (results) {
if (!Array.isArray(results)) return

const requestFailure = results.find(failure => failure?.type === 'NCU_FAILURE')
if (requestFailure) {
throw new Error(`Unable to retrieve Jenkins CI results: ${requestFailure.reason}`)
}
}

export function validateBuildDestination (params, { owner, repo, pr }) {
const matches =
params.TARGET_GITHUB_ORG?.toLowerCase() === owner.toLowerCase() &&
params.TARGET_REPO_NAME?.toLowerCase() === repo.toLowerCase() &&
String(params.PR_ID) === String(pr)

if (!matches) {
throw new Error('Jenkins build does not belong to the destination pull request')
}
}

export function limitResultsMarkdown (markdown, buildUrl) {
if (markdown.length <= maxCommentLength) return markdown

const notice = `\n\n---\nCI results were truncated. [View the complete results in Jenkins](${buildUrl}).`
const availableLength = maxCommentLength - notice.length
let truncated = markdown.slice(0, availableLength)

// node-core-utils puts detailed failures in <details> blocks containing
// fenced code. Avoid cutting a block in half and producing broken Markdown.
const firstDetails = truncated.indexOf('<details>')
const lastCompleteDetails = findLastDetailsCloseOutsideFence(truncated)
if (firstDetails !== -1) {
if (lastCompleteDetails === -1) {
const closing = '\n... diagnostic output truncated ...\n```\n</details>'
const openingFence = markdown.indexOf('```', firstDetails)
const excerptLength = availableLength - closing.length

truncated = openingFence !== -1 && openingFence < excerptLength
? markdown.slice(0, excerptLength).trimEnd() + closing
: truncated.slice(0, firstDetails)
} else {
truncated = truncated.slice(0, lastCompleteDetails)
}
}

return truncated.trimEnd() + notice
}

export async function postBuildResults (options, build, fetchResults = fetchResultsMarkdown) {
const { pr } = options
const buildNumber = extractBuildNumber(build)

const traceFields = { pr, job: build.identifier, gitRef: build.ref, buildNumber }
const logger = options.logger.child(traceFields, true)

if (!process.env.JENKINS_API_CREDENTIALS) {
logger.info('JENKINS_API_CREDENTIALS is not set, skipping CI results comment')
return
}

if (buildNumber === null) {
logger.warn('Unable to find build number in build URL, skipping CI results comment')
return
}

const markdown = await fetchResults(buildNumber, {
owner: options.owner,
repo: options.repo,
pr
})

if (!markdown) {
logger.warn('Got no CI results back from node-core-utils, skipping CI results comment')
return
}

const comment = limitResultsMarkdown(markdown, build.url)
await createPrComment({ issue_number: pr, ...options, logger }, comment)
logger.info('Jenkins CI results comment posted')
}
7 changes: 4 additions & 3 deletions lib/jenkins-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ const enabledRepos = process.env.JENKINS_ENABLED_REPOS
? process.env.JENKINS_ENABLED_REPOS.split(',')
: ['citgm', 'http-parser', 'node', 'node-auto-test']

const listOfKnownJenkinsIps = process.env.JENKINS_WORKER_IPS ? process.env.JENKINS_WORKER_IPS.split(',') : []

function isKnownJenkinsIp (req) {
const ip = req.connection.remoteAddress.split(':').pop()
const listOfKnownJenkinsIps = process.env.JENKINS_WORKER_IPS
? process.env.JENKINS_WORKER_IPS.split(',')
: []

if (listOfKnownJenkinsIps.length && !listOfKnownJenkinsIps.includes(ip)) {
if (!listOfKnownJenkinsIps.includes(ip)) {
req.log.warn({ ip }, 'Ignoring, not allowed to push Jenkins updates')
return false
}
Expand Down
9 changes: 9 additions & 0 deletions lib/push-jenkins-update.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import githubClient from './github-client.js'
import { createPrComment } from './github-comment.js'
import { postBuildResults } from './jenkins-ci-results.js'

export function pushStarted (options, build, cb) {
const pr = findPrInRef(build.ref)
Expand Down Expand Up @@ -54,6 +55,14 @@ export function pushEnded (options, build, cb) {

const optsWithPr = Object.assign({ pr }, options)

if (build.identifier === 'node-test-pull-request' && build.status === 'failure') {
// fetching the CI results involves many Jenkins API calls, so let them
// finish in the background instead of holding up the status update
postBuildResults(optsWithPr, build).catch((err) => {
logger.error(err, 'Error while posting Jenkins CI results')
})
}

findLatestCommitInPr(optsWithPr).then(latestCommit => {
const statusOpts = Object.assign({
sha: latestCommit.sha,
Expand Down
Loading
Loading