SOLVED: Updating pull request data

scriptrunner
pullrequest

#1

Hi,

I’m trying to update a pull request in Bitbucket, using ScriptRunner hooked up to “PullRequestOpenRequestedEvent” with the next code:

package pullrequest.handler

import com.atlassian.bitbucket.pull.PullRequest
import com.atlassian.bitbucket.pull.PullRequestService
import com.atlassian.bitbucket.pull.PullRequestUpdateRequest
import com.atlassian.bitbucket.pull.PullRequestUpdateRequest.Builder

PullRequestService prService
PullRequest pullRequest = event.getPullRequest()
assert pullRequest

if (pullRequest.getDescription() != null) {
    String newDescription = "MARKER\n" + pullRequest.getDescription() + "\nMARKER"
    PullRequestUpdateRequest.Builder.description(newDescription)
    PullRequestUpdateRequest updatedpullRequest = PullRequestUpdateRequest.Builder.build()
    prService.update(updatedpullRequest)
}

And while code looks legit and passes static checks, it actually fails with incredibly weird error:

2018-05-23 14:09:29,358 ERROR [https-jsse-nio-8443-exec-8] user @5SLT31x849x2008x0 gjcwq5 10.136.252.68,10.136.2.4 "POST /mvc/projects/~test/repos/test/pull-requests HTTP/1.1" c.o.s.b.DynamicEventListenerInvoker Event handler failed: event: com.atlassian.bitbucket.event.pull.PullRequestOpenRequestedEvent file: <inline script>
groovy.lang.MissingMethodException: No signature of method: static com.atlassian.bitbucket.pull.PullRequestUpdateRequest$Builder.description() is applicable for argument types: (java.lang.String) values: [MARKER
TEST
MARKER]
Possible solutions: description(java.lang.String)
        at pullrequest.handler.Script423.run(Script423.groovy:13)

So basically it says that java.lang.String isn’t java.lang.String :anguished:

Any ideas what could be wrong here? The official documentation for the version I’m using says

@Nonnull PullRequestUpdateRequest.Builder	description(String value)

so it is a String.


#2

Hey @yevgen.lasman,

I think you’ll need to create an instance of the builder. Something like:

package pullrequest.handler

import com.atlassian.bitbucket.pull.PullRequest
import com.atlassian.bitbucket.pull.PullRequestService
import com.atlassian.bitbucket.pull.PullRequestUpdateRequest
import com.atlassian.bitbucket.pull.PullRequestUpdateRequest.Builder

PullRequestService prService
PullRequest pullRequest = event.getPullRequest()
assert pullRequest

if (pullRequest.getDescription() != null) {
    String newDescription = "MARKER\n" + pullRequest.getDescription() + "\nMARKER"
    PullRequestUpdateRequest updatedPullRequest = 
        new PullRequestUpdateRequest.Builder(pullRequest, pullRequest.getVersion())
         .description(newDescription)
         .build()
    prService.update(updatedPullRequest)
}

Also, I haven’t tested this, but very likely you’ll need to update the PR after the pull request is already created, which is not the case when PullRequestOpenRequestedEvent is fired.
You’ll probably want to look at PullRequestOpenedEvent instead.

PullRequestOpenRequestedEvent is really only for pre-create checks, which may want to cancel the pull request’s creation.

Felix


#3

Apparently it doesn’t work without .title(pullRequest.getTitle()) addition between description and build.
But it doesn’t help either and fails with the error

c.o.s.b.DynamicEventListenerInvoker Event handler failed: event: com.atlassian.bitbucket.event.pull.PullRequestOpenedEvent file: <inline script>
java.lang.NullPointerException: Cannot invoke method update() on null object
        at pullrequest.handler.Script480.run(Script480.groovy:20)

which I assume because updatedPullRequest appears to be a null object, i.e. builder doesn’t actually return a proper object. Any ideas why it could be?

UPD Moreover, if I output to log updatedPullRequest.getTitle() or updatedPullRequest.getTitle() they do have proper value. And only prService.update(updatedPullRequest) fails treating updatedPullRequest as a null object.


#4

You’re hitting a ton of imprecise English in those error messages! I think the new error is because prService is null, and the updatedPullRequest isn’t. I don’t really know Groovy or ScriptRunner, but based on https://scriptrunner.adaptavist.com/5.0.0/bitbucket/, I think you need to replace

PullRequestService prService

with

PullRequestService prService = ComponentLocator.getComponent(PullRequestService)

#5

Thank you, I definitely was missing ComponentLocator.getComponent!