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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,559,739
Community Members
 
Community Events
185
Community Groups

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

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

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 Infantino Benito likes this

Thanks, much appreciated

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events