Skip to content
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
38 changes: 35 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,22 @@ pipeline {

### `PT_PULL_REQUEST` type

The `PT_PULL_REQUEST` type lists pull requests in the remote repository.
The `PT_PULL_REQUEST` type lists pull requests or merge requests in the remote repository.

Supports:
- GitHub Pull Requests
- Bitbucket Pull Requests
- GitLab Merge Requests

#### GitHub Example

```groovy
pipeline {
agent any
parameters {
gitParameter type: 'PT_PULL_REQUEST',
name: 'A_PULL_REQUEST',
defaultValue: '',
defaultValue: '1818',
description: 'Choose a pull request to checkout',
selectedValue: 'TOP',
sortMode: 'DESCENDING_SMART'
Expand All @@ -146,14 +153,39 @@ pipeline {
stage('Example') {
steps {
checkout scmGit(branches: [[name: "pr/${params.A_PULL_REQUEST}/head"]],
userRemoteConfigs: [[refspec: '+refs/pull/*:refs/remotes/origin/pr/*',
userRemoteConfigs: [[refspec: "+refs/pull/${params.A_PULL_REQUEST}/head:refs/pull/${params.A_PULL_REQUEST}/head",
url: 'https://github.com/jenkinsci/git-plugin.git']])
}
}
}
}
```

#### GitLab Example

```groovy
pipeline {
agent any
parameters {
gitParameter type: 'PT_PULL_REQUEST',
name: 'MERGE_REQUEST',
defaultValue: '',
description: 'Choose a merge request to checkout',
selectedValue: 'TOP',
sortMode: 'DESCENDING_SMART'
}
stages {
stage('Example') {
steps {
checkout scmGit(branches: [[name: "refs/remotes/origin/merge-requests/${params.MERGE_REQUEST}/head"]],
userRemoteConfigs: [[refspec: '+refs/merge-requests/*:refs/remotes/origin/merge-requests/*',
url: 'https://gitlab.com/your-group/your-project.git']])
}
}
}
}
```

### `PT_REVISION` type

The `PT_REVISION` type lists all the revisions in the repository that are part of the `branch`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public class Consts {
public static final String PARAMETER_TYPE_TAG_BRANCH = "PT_BRANCH_TAG";
public static final String PARAMETER_TYPE_PULL_REQUEST = "PT_PULL_REQUEST";

public static final Pattern PULL_REQUEST_REFS_PATTERN = Pattern.compile("refs/pull.*/(\\d+)/[from|head]");
public static final Pattern PULL_REQUEST_REFS_PATTERN =
Pattern.compile("refs/(pull.*|merge-requests)/(\\d+)/(from|head)");

public static final String TEMPORARY_DIRECTORY_PREFIX = "git_parameter_";
public static final String EMPTY_JOB_NAME = "EMPTY_JOB_NAME";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ private Set<String> getPullRequest(GitClient gitClient, String gitUrl) throws Ex
for (String remoteReference : remoteReferences.keySet()) {
Matcher matcher = PULL_REQUEST_REFS_PATTERN.matcher(remoteReference);
if (matcher.find()) {
pullRequestSet.add(matcher.group(1));
pullRequestSet.add(matcher.group(2));
}
}
return pullRequestSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,21 @@ void testCreateValue_StaplerRequest2() {
void matchesWithBitbucketPullRequestRefs() {
Matcher matcher = Consts.PULL_REQUEST_REFS_PATTERN.matcher("refs/pull-requests/186/from");
assertTrue(matcher.find());
assertEquals("186", matcher.group(1));
assertEquals("186", matcher.group(2));
}

@Test
void matchesWithGithubPullRequestRefs() {
Matcher matcher = Consts.PULL_REQUEST_REFS_PATTERN.matcher("refs/pull/45/head");
assertTrue(matcher.find());
assertEquals("45", matcher.group(1));
assertEquals("45", matcher.group(2));
}

@Test
void matchesWithGitLabMergeRequestRefs() {
Matcher matcher = Consts.PULL_REQUEST_REFS_PATTERN.matcher("refs/merge-requests/42/head");
assertTrue(matcher.find());
assertEquals("42", matcher.group(2));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,21 @@ void testCreateValue_StaplerRequest2() {
void matchesWithBitbucketPullRequestRefs() {
Matcher matcher = Consts.PULL_REQUEST_REFS_PATTERN.matcher("refs/pull-requests/186/from");
assertTrue(matcher.find());
assertEquals("186", matcher.group(1));
assertEquals("186", matcher.group(2));
}

@Test
void matchesWithGithubPullRequestRefs() {
Matcher matcher = Consts.PULL_REQUEST_REFS_PATTERN.matcher("refs/pull/45/head");
assertTrue(matcher.find());
assertEquals("45", matcher.group(1));
assertEquals("45", matcher.group(2));
}

@Test
void matchesWithGitLabMergeRequestRefs() {
Matcher matcher = Consts.PULL_REQUEST_REFS_PATTERN.matcher("refs/merge-requests/42/head");
assertTrue(matcher.find());
assertEquals("42", matcher.group(2));
}

@Test
Expand Down
Loading