Skip to content
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

pipeline: check inputs in post-build-status-update #981

Closed
wants to merge 5 commits into from
Closed
Changes from 4 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
50 changes: 28 additions & 22 deletions jenkins/pipelines/post-build-status-update.jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,41 @@ pipeline {
agent { label 'jenkins-workspace' }

parameters {
string(defaultValue: 'node', description: 'GitHub repository', name: 'REPO')
string(defaultValue: '', description: 'test/aix, linter, etc.', name: 'IDENTIFIER')
string(defaultValue: '', description: 'pending, success, unstable, failure', name: 'STATUS')
string(defaultValue: '', description: 'URL for upstream Jenkins job', name: 'URL')
string(defaultValue: '', description: 'Current commit being tested in upstream Jenkins job', name: 'COMMIT')
string(defaultValue: '', description: 'Current branch being tested in upstream Jenkins job', name: 'REF')
string(name: 'REPO', defaultValue: 'node', description: 'GitHub repository')
string(name: 'IDENTIFIER', defaultValue: '', description: 'test/aix, linter, etc.')
string(name: 'STATUS, 'defaultValue: '', description: 'pending, success, unstable, failure')
string(name: 'URL, 'defaultValue: '', description: 'URL for upstream Jenkins job')
string(name: 'COMMIT, 'defaultValue: '', description: 'Current commit being tested in upstream Jenkins job')
string(name: 'REF, 'defaultValue: '', description: 'Current branch being tested in upstream Jenkins job')
booleanParam(name: 'POST_STATUS_TO_PR', defaultValue: 'false', description: 'Whether the PR should be updated.')
}

stages {
stage('Send status report') {
steps {
validateParams(params)
sendBuildStatus(params.REPO, params.IDENTIFIER, params.STATUS, params.URL, params.COMMIT, params.REF)
sendBuildStatus(params)
}
}
}
}

def sendBuildStatus(repo, identifier, status, url, commit, ref) {
def sendBuildStatus(params) {
// Fail loudly if upstream job is configured wrong.
if (params.any{ it.value == '' }) {
error('All parameter fields are required.')
}

// Fail quietly if upstream job shouldn't be posting to GitHub.
if (params.REF.contains('refs/pull/') || params.POST_STATUS_TO_PR != 'true') {

This comment was marked as off-topic.

println "Skipping status update (REF: ${params.REF}, " +
"POST_STATUS_TO_PR: ${params.POST_STATUS_TO_PR})."
return
}

def path = ""
def message = ""

def status = params.STATUS
if (status == "pending") {
path = "start"
message = "running tests"
Expand All @@ -48,24 +61,17 @@ def sendBuildStatus(repo, identifier, status, url, commit, ref) {
}

def buildPayload = JsonOutput.toJson([
'identifier': identifier,
'status': status,
'url': url,
'commit': commit,
'ref': ref,
'identifier': params.IDENTIFIER,
'status': params.STATUS,
'url': params.URL,
'commit': params.COMMIT,
'ref': params.REF,
'message': message
])

def script = "curl -s -o /dev/null --connect-timeout 5 -X POST " +
"-H 'Content-Type: application/json' -d '${buildPayload}' " +
"http://github-bot.nodejs.org:3333/${repo}/jenkins/${path}"
"http://github-bot.nodejs.org:3333/${params.REPO}/jenkins/${path}"

sh(returnStdout: true, script: script)
}

def validateParams(params) {
if (params.IDENTIFIER == '' || params.STATUS == '' || params.URL == '' ||
params.COMMIT == '' || params.REF == '') {
error('All parameter fields are required.')
}
}