Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How can I retrieve a pull request from repository slug and pull request number in ScriptRunner?

Infantino Benito May 17, 2023

I need to get a pull request from the project, repository and pull request number with Script Runner. Usually I use the event but in this case I only have the repository slug and pull request ID. 

1 answer

0 votes
Infantino Benito May 18, 2023

After viewing some examples here https://www.programcreek.com/java-api-examples/index.php?api=com.atlassian.bitbucket.pull.PullRequest:

Figured this out:

import com.atlassian.bitbucket.pull.PullRequestSearchRequest
import com.atlassian.bitbucket.util.PageRequestImpl
import com.atlassian.bitbucket.util.PageRequest
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.bitbucket.repository.RepositoryService
import com.atlassian.bitbucket.repository.Repository
import com.atlassian.bitbucket.repository.RefService
import com.atlassian.bitbucket.pull.PullRequest
import static com.atlassian.bitbucket.pull.PullRequestState.OPEN;
import com.atlassian.bitbucket.pull.PullRequestService
import com.atlassian.bitbucket.pull.PullRequestUpdateRequest

 

def repositoryService = ComponentLocator.getComponent(RepositoryService.class);
def refService = ComponentLocator.getComponent(RefService.class);

Repository rep = repositoryService.getBySlug("Project1","Repo1");
int id = rep.getId();

log.warn(id.toString())

def pullRequestService = ComponentLocator.getComponent(PullRequestService)

 

PageRequest page = new PageRequestImpl(0, 10)
PullRequestSearchRequest pullr = new PullRequestSearchRequest.Builder().toRepositoryId(id).state(OPEN).build()
long pullrequestID
int version
log.warn(pullRequestService.search(pullr, page).getValues().toString())
pullRequestService.search(pullr,page).getValues().forEach( val -> {
//log.warn("ID: "+val.id+" Description "+val.getDescription())
pullrequestID = val.id
version = val.version
}
)

log.warn(pullRequestService.getById(id,pullrequestID).getDescription())

@Infantino Benitono need to make a paged request if you have a Project Key, Repository slug and pull request id. The following script should get you a single pull request where :

- Project Key = 'PROJECT_1'

- Repository Slug = 'rep_1'

- Pull Request id = '1'

 

```

import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.bitbucket.repository.RepositoryService
import com.atlassian.bitbucket.pull.PullRequestService

def pullRequestService = ComponentLocator.getComponent(PullRequestService)
def repositoryService = ComponentLocator.getComponent(RepositoryService)

def repository = repositoryService.getBySlug('PROJECT_1', 'rep_1')
def pullRequest = pullRequestService.getById(repository.id, 1)
```
Like # people like this
Infantino Benito May 22, 2023

Thanks, much appreciated

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events