Skip to content

feat: update bot comment when Jenkins finishes #259

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

Closed
wants to merge 1 commit into from
Closed
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
113 changes: 111 additions & 2 deletions lib/push-jenkins-update.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
'use strict'

const url = require('url')
const { exec } = require('child_process')
const { readFile } = require('child_process')

const githubClient = require('./github-client')
const { createPrComment } = require('./github-comment')

function getCICommentPlaceholders(identifier, number) {
return {
status: `<!-- ${identifier}-status:${build.number} -->`,
failure: `<!-- ${identifier}-failure:${build.number} -->`,
}
}

function pushStarted (options, build, cb) {
const pr = findPrInRef(build.ref)

Expand All @@ -17,7 +26,11 @@ function pushStarted (options, build, cb) {
if (build.status === 'pending') {
switch (build.identifier) {
case 'node-test-pull-request':
createPrComment(Object.assign({ number: pr }, options), `CI: ${build.url}`)
const placeholders = getCICommentPlaceholders(build.identifier, build.number);
const body = `CI: ${build.url} ${placeholder.status}

${placeholder.failure}`;
createPrComment(Object.assign({ number: pr }, options), body)
break

case 'node-test-pull-request-lite-pipeline':
Expand Down Expand Up @@ -57,6 +70,51 @@ function pushEnded (options, build, cb) {

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

if (build.identifier === 'node-test-pull-request') {
const placeholders = getCICommentPlaceholders(build.identifier, build.number)

// TODO(mmarchini): should call `cb` when all async operations here finish
findBotCommentWithPlaceholderInPr(Object.assign({
placeholder: placeholder.status
}, optsWithPr), (err, comment) => {
if (err) {
logger.error(err, 'Got error when retrieving GitHub commits for PR')
return
}
if (comment === null) {
logger.info(err, "Couldn't find comment with placeholder")
}

// TODO(mmarchini): emojis?
comment.body = comment.body.replace(placeholder.status, `[${build.status}]`);

// TODO(mmarchini): does Jenkins return failure or failed?
if (build.status.toLowerCase().startsWith('fail') && comment.body.includes(placeholder.failure)) {
// TODO(mmarchini): move this to a separate file on lib
// TODO(mmarchini): how to pass Jenkins authentication to ncu?
// TODO(mmarchini): create proper tmpfile
const tmpfile = `/tmp/${new Date()}-${build.number}.md`
exec(`npx ncu-ci --markdown ${tmpfile} url ${build.url}`, (err, stdout, stderr) => {
// TODO(mmarchini): cleanup tmpfile after use
if (err) {
logger.error(err, 'Got error when retrieving GitHub commits for PR')
return
}
readFile(tmpfile, (err, data) => {
if (err) {
logger.error(err, 'Got error when retrieving GitHub commits for PR')
return
}
comment.body = comment.body.replace(placeholder.failure, data)
editGhComment(comment, logger, () => {})
})
})
} else {
editGhComment(comment, logger, () => {})
}
})
}

findLatestCommitInPr(optsWithPr, (err, latestCommit) => {
if (err) {
logger.error(err, 'Got error when retrieving GitHub commits for PR')
Expand Down Expand Up @@ -109,6 +167,40 @@ function findLatestCommitInPr (options, cb, pageNumber = 1) {
})
}

function findBotCommentWithPlaceholderInPr (options, cb, pageNumber = 1) {
// Pull request comments are fetched via Issue Comments API
githubClient.issues.getComments({
owner: options.owner,
repo: options.repo,
number: options.pr,
// TODO(mmarchini): use a more narrow date
since: new Date('2020-01-01').ToISOString(),
page: pageNumber,
per_page: 100
}, (err, res) => {
if (err) {
return cb(err)
}

for (const comment of (res.data || [])) {
const user = comment.user
const body = comment.body
if (!user || !user.login || user.login !== 'nodejs-github-bot' || !body) continue

if (!body.includes(placeholder)) {
return cb(null, comment);
}
}

const lastPageURL = githubClient.hasLastPage(res)
if (lastPageURL) {
return findBotCommentWithPlaceholderInPr(options, cb, pageNumber + 1)
} else {
return cb(null, null)
}
})
}

function createGhStatus (options, logger, cb) {
githubClient.repos.createStatus({
owner: options.owner,
Expand All @@ -129,9 +221,26 @@ function createGhStatus (options, logger, cb) {
})
}

function editGhComment (options, logger, cb) {
githubClient.repos.createStatus({
owner: options.owner,
repo: options.repo,
id: options.id,
body: options.body,
}, (err, res) => {
if (err) {
logger.error(err, 'Error while updating GitHub PR Comment')
cb(err)
return
}
logger.info('GitHub PR Comment updated')
cb(null)
})
}

function validate (payload) {
const isString = (param) => typeof (payload[param]) === 'string'
return ['identifier', 'status', 'message', 'commit', 'url'].every(isString)
return ['identifier', 'status', 'message', 'commit', 'url', 'number'].every(isString)
}

function pageNumberFromURL (githubUrl) {
Expand Down
Loading