stash-pullrequest-builder-plugin icon indicating copy to clipboard operation
stash-pullrequest-builder-plugin copied to clipboard

Feature request: approve PR on successful build

Open osigida opened this issue 8 years ago • 3 comments

it would be nice to approve/reject PR based on the build status: see https://developer.atlassian.com/static/rest/bitbucket-server/4.12.0/bitbucket-rest.html#idp2045632

when the build is successful, Jenkins can push APPROVE status, on failure NEEDS_WORK and then we can have further automation, like require successful build before merge.

osigida avatar Dec 27 '16 13:12 osigida

Hi @osigida

To do this I personally use the stash notifier plugin configured as described in the README and combined with a repository configured to allow PR merges only with successful builds.

Hope this helps.

loicalbertin avatar Jan 05 '17 14:01 loicalbertin

I’m now using the following System groovy script as first build step:

import hudson.model.*
import groovy.json.JsonOutput
import stashpullrequestbuilder.stashpullrequestbuilder.*

// get current thread / Executor
def thr = Thread.currentThread()
// get current build
def build = thr?.executable

// get pull-request plugin impl
def cause = build.getCause(StashCause)
def trigger = build.project.getTrigger(StashBuildTrigger)

def url = "${cause.stashHost}/rest/api/1.0/projects/${cause.destinationRepositoryOwner}/repos/${cause.destinationRepositoryName}/pull-requests/${cause.pullRequestId}/participants/${trigger.username}"

def conn = new URL(url).openConnection()

String userCredentials = "${trigger.username}:${trigger.password}"
String basicAuth = "Basic ${userCredentials.bytes.encodeBase64().toString()}"
conn.setRequestProperty('Authorization', basicAuth)
conn.setRequestMethod('PUT')
conn.setRequestProperty('Content-Type', 'application/json; charset=utf-8')
conn.setRequestProperty('Accept', 'application/json; charset=utf-8')

conn.setDoOutput(true)

String out = JsonOutput.toJson([status: 'NEEDS_WORK'])

conn.connect()

def writer = new OutputStreamWriter(conn.outputStream);
writer.write(out)
writer.close()

And then I use the “Groovy postbuild” plugin to execute the following, almost identical script on successful builds:

import hudson.model.*
import groovy.json.JsonOutput
import stashpullrequestbuilder.stashpullrequestbuilder.*

if(manager.build.result.isBetterOrEqualTo(hudson.model.Result.UNSTABLE)) {
	def build = manager.build

	// get pull-request plugin impl
	def cause = build.getCause(StashCause)
	def trigger = build.project.getTrigger(StashBuildTrigger)

	def url = "${cause.stashHost}/rest/api/1.0/projects/${cause.destinationRepositoryOwner}/repos/${cause.destinationRepositoryName}/pull-requests/${cause.pullRequestId}/participants/${trigger.username}"

	def conn = new URL(url).openConnection()

	String userCredentials = "${trigger.username}:${trigger.password}"
	String basicAuth = "Basic ${userCredentials.bytes.encodeBase64().toString()}"
	conn.setRequestProperty('Authorization', basicAuth)
	conn.setRequestMethod('PUT')
	conn.setRequestProperty('Content-Type', 'application/json; charset=utf-8')
	conn.setRequestProperty('Accept', 'application/json; charset=utf-8')

	conn.setDoOutput(true)

	String out = JsonOutput.toJson([status: 'APPROVED'])

	conn.connect()

	def writer = new OutputStreamWriter(conn.outputStream);
	writer.write(out)
	writer.close()
}

This is a bit of a pain to configure and maintain but works fine. But I will look into the stashNotifier plugin too…

sabberworm avatar Feb 09 '17 10:02 sabberworm

I've opened a PR for this a couple of months ago, though it hasn't been approved yet: https://github.com/nemccarthy/stash-pullrequest-builder-plugin/pull/123

You could build my fork locally and upload the binary into Jenkins. Worked for me, at least.

tettaji avatar Jul 20 '17 08:07 tettaji