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

feat(ZNTA-2623) fetch PR body and escape for HTML #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@
<version>1.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
</dependencies>

</project>
35 changes: 23 additions & 12 deletions src/org/zanata/jenkins/PullRequests.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package org.zanata.jenkins

import com.cloudbees.groovy.cps.NonCPS
import groovy.json.JsonSlurper
@Grab(group='commons-lang', module='commons-lang', version='2.6')
import static org.apache.commons.lang.StringEscapeUtils.escapeHtml

/**
* @author Sean Flanigan <a href="mailto:[email protected]">[email protected]</a>
Expand All @@ -15,16 +17,19 @@ class PullRequests implements Serializable {
def job = manager.build.project
// we only want to do this once, to avoid hammering the github api
if (!job.description || !job.description.contains(env.CHANGE_URL)) {
def sourceBranchLabel = getSourceBranchLabel(env.CHANGE_URL, steps)
def abbrTitle = Strings.truncateAtWord(env.CHANGE_TITLE, 50)
def prDesc = """<a title=\"${env.CHANGE_TITLE}\" href=\"${
env.CHANGE_URL
}\">PR #${env.CHANGE_ID} by ${env.CHANGE_AUTHOR}</a>:
|$abbrTitle
|merging ${sourceBranchLabel} to ${env.CHANGE_TARGET}""".
// PR body and source branch info aren't available in env, so we use GitHub API:
def pullRequest = getPullRequest(env.CHANGE_URL, steps)
def sourceBranchLabel = pullRequest?.head?.label ?: "[unknown]"
def prBody = pullRequest?.body ?: "[body not available]"
def quoteSafeTitle = escapeHtml(env.CHANGE_TITLE).replace('"', "'")
def safeTitle = escapeHtml(env.CHANGE_TITLE)
def safeBody = escapeHtml(prBody)
def prDesc = """<a title=\"$quoteSafeTitle\" href=\"${env.CHANGE_URL}\">
|PR #${env.CHANGE_ID} by ${env.CHANGE_AUTHOR}</a>:<br />
|$safeTitle<br />
|merging ${sourceBranchLabel} to ${env.CHANGE_TARGET}<br />
|$safeBody""".
stripMargin()
// ideally we would show eg sourceRepo/featureBranch ⭆ master
// but there is no env var with that info

steps.echo "description: " + prDesc
//currentBuild.description = prDesc
Expand All @@ -46,7 +51,7 @@ class PullRequests implements Serializable {
}

@NonCPS
private static String getSourceBranchLabel(String changeURL, def steps) {
private static Object getPullRequest(String changeURL, def steps) {
steps.echo "checking github api for pull request details"

def tokens = changeURL.tokenize('/')
Expand All @@ -55,9 +60,15 @@ class PullRequests implements Serializable {
def pr = tokens[5]

// FIXME use github credentials to avoid rate limiting
// https://developer.github.com/v3/pulls/#get-a-single-pull-request
def prUrl = new URL(
"https://api.github.com/repos/${org}/${repo}/pulls/${pr}")
def sourceBranchLabel = new JsonSlurper().parseText(prUrl.text).head.label
return sourceBranchLabel
try {
return new JsonSlurper().parseText(prUrl.text)
} catch (Exception e) {
// NB we don't want to lose entire job description because of rate limiting
steps.echo StackTraces.getStackTrace(e)
return null
}
}
}