From 4e1130461f2ae82079481ef2b200bb167943d804 Mon Sep 17 00:00:00 2001 From: Sandu Postaru Date: Mon, 31 Aug 2020 12:57:34 +0200 Subject: [PATCH] Add github labels API wrappers Change-Id: I8312f521b958323529fa0eb5b582085460e585aa Signed-off-by: Sandu Postaru --- vars/github.groovy | 84 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/vars/github.groovy b/vars/github.groovy index d8837ea..93e5aa7 100644 --- a/vars/github.groovy +++ b/vars/github.groovy @@ -1,5 +1,5 @@ def isPullRequest() { - return "${BRANCH_NAME}".contains('PR-'); + return "${BRANCH_NAME}".contains('PR-') } def getPullRequestId() { @@ -54,3 +54,85 @@ def buildAbortedComment() { pullRequestComment(message) } + +def private isDraftCommit() { + def commitMsg = sh (script: 'git log -1 --pretty=%B ${GIT_COMMIT}', returnStdout: true).trim() + + return commitMsg.contains("DRAFT") +} + +def private getExistingLabels() { + def pullRequestId = getPullRequestId() + def projectName = getProjectName() + def apiURL = "https://api.github.com/repos/eclipse/${projectName}/issues/${pullRequestId}/labels" + def labels = "" + + withCredentials([string(credentialsId: '0dea5761-867c-44db-92fa-9304c81a8653', variable: 'password')]) { + def script = """curl ${apiURL} -u "eclipse-capella-bot:${password}" -H "Content-Type: application/json" """ + + labels = sh(script: "${script}", returnStdout: true) + } + + return labels +} + +def private getCustomLabels() { + return ['build-aborted', 'build-unstable', 'build-failed', 'build-successfull', 'build-started'] +} + +def private removeLabel(label) { + def pullRequestId = getPullRequestId() + def projectName = getProjectName() + def apiURL = "https://api.github.com/repos/eclipse/${projectName}/issues/${pullRequestId}/labels/${label}" + + withCredentials([string(credentialsId: '0dea5761-867c-44db-92fa-9304c81a8653', variable: 'password')]) { + sh """curl ${apiURL} -X DELETE -u "eclipse-capella-bot:${password}" -H "Content-Type: application/json" """ + } +} + +def removeCustomPullRequestLabels() { + def pullRequestId = getPullRequestId() + def projectName = getProjectName() + def existingLabels = getExistingLabels() + def customLabels = getCustomLabels() + + customLabels.each { + if(existingLabels.contains("${it}")) { + removeLabel("${it}") + } + } +} + +def private addPullRequestLabel(label) { + def pullRequestId = getPullRequestId() + def projectName = getProjectName() + def apiURL = "https://api.github.com/repos/eclipse/${projectName}/issues/${pullRequestId}/labels" + + withCredentials([string(credentialsId: '0dea5761-867c-44db-92fa-9304c81a8653', variable: 'password')]) { + sh """curl ${apiURL} --data '{"labels":["${label}"]}' -u "eclipse-capella-bot:${password}" --request POST -H "Content-Type: application/json" """ + } +} + +def buildStartedLabel() { + addPullRequestLabel('build-started') +} + +def buildSuccessfullLabel() { + addPullRequestLabel('build-successfull') +} + +def buildFailedLabel() { + addPullRequestLabel('build-failed') +} + +def buildUnstableLabel() { + addPullRequestLabel('build-unstable') +} + +def buildAbortedLabel() { + addPullRequestLabel('build-aborted') +} + +def removeBuildStartedLabel() { + removeLabel('build-started') +}