Skip to content

Commit b777c1b

Browse files
authored
[JENKINS-53535] Make Bitbucket Server, Owner and repository environment variables for Bitbucket Team/Project based jobs
Fix BitbucketEnvVarExtension to be compatible with @DataBoundConstructor annotation. Add getter methods, hashCode and equals method and a own GitSCMExtensionDescriptor Add test case
1 parent aa9c5ca commit b777c1b

File tree

3 files changed

+128
-5
lines changed

3 files changed

+128
-5
lines changed

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/impl/extension/BitbucketEnvVarExtension.java

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
import com.cloudbees.jenkins.plugins.bitbucket.impl.util.URLUtils;
44
import edu.umd.cs.findbugs.annotations.NonNull;
55
import edu.umd.cs.findbugs.annotations.Nullable;
6+
import hudson.Extension;
67
import hudson.plugins.git.GitSCM;
78
import hudson.plugins.git.extensions.GitSCMExtension;
9+
import hudson.plugins.git.extensions.GitSCMExtensionDescriptor;
810
import java.util.Map;
11+
import java.util.Objects;
912
import org.kohsuke.stapler.DataBoundConstructor;
1013

1114
public class BitbucketEnvVarExtension extends GitSCMExtension {
@@ -33,9 +36,60 @@ public BitbucketEnvVarExtension(@Nullable String owner, @NonNull String reposito
3336
*/
3437
@Override
3538
public void populateEnvironmentVariables(GitSCM scm, Map<String, String> env) {
36-
env.put("BITBUCKET_REPOSITORY", repository);
37-
env.put("BITBUCKET_OWNER", owner);
38-
env.put("BITBUCKET_PROJECT_KEY", projectKey);
39-
env.put("BITBUCKET_SERVER_URL", serverURL);
39+
env.put("BITBUCKET_REPOSITORY", getRepository());
40+
env.put("BITBUCKET_OWNER", getOwner());
41+
env.put("BITBUCKET_PROJECT_KEY", getProjectKey());
42+
env.put("BITBUCKET_SERVER_URL", getServerURL());
43+
}
44+
45+
public String getOwner() {
46+
return owner;
47+
}
48+
49+
public String getRepository() {
50+
return repository;
51+
}
52+
53+
public String getProjectKey() {
54+
return projectKey;
55+
}
56+
57+
public String getServerURL() {
58+
return serverURL;
59+
}
60+
61+
@Override
62+
public int hashCode() {
63+
return Objects.hash(owner, projectKey, repository, serverURL);
64+
}
65+
66+
@Override
67+
public boolean equals(Object obj) {
68+
if (this == obj) {
69+
return true;
70+
}
71+
if (obj == null) {
72+
return false;
73+
}
74+
if (getClass() != obj.getClass()) {
75+
return false;
76+
}
77+
BitbucketEnvVarExtension other = (BitbucketEnvVarExtension) obj;
78+
return Objects.equals(owner, other.owner)
79+
&& Objects.equals(projectKey, other.projectKey)
80+
&& Objects.equals(repository, other.repository)
81+
&& Objects.equals(serverURL, other.serverURL);
82+
}
83+
84+
@Extension
85+
// No @Symbol because Pipeline users should not configure this in other ways than this plugin provides
86+
public static class DescriptorImpl extends GitSCMExtensionDescriptor {
87+
/**
88+
* {@inheritDoc}
89+
*/
90+
@Override
91+
public String getDisplayName() {
92+
return "Contribute additional environment variables about the target branch.";
93+
}
4094
}
4195
}

src/test/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSourceTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.cloudbees.jenkins.plugins.bitbucket.client.pullrequest.BitbucketPullRequestCommit;
55
import com.cloudbees.jenkins.plugins.bitbucket.endpoints.AbstractBitbucketEndpoint;
66
import com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketEndpointConfiguration;
7+
import com.cloudbees.jenkins.plugins.bitbucket.impl.extension.BitbucketEnvVarExtension;
78
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
89
import hudson.plugins.git.GitSCM;
910
import hudson.plugins.git.extensions.GitSCMExtension;
@@ -323,6 +324,34 @@ public void test_that_clone_url_does_not_contains_username() {
323324
});
324325
}
325326

327+
@Test
328+
public void verify_envvar() {
329+
BranchSCMHead head = new BranchSCMHead("master");
330+
BitbucketPullRequestCommit commit = new BitbucketPullRequestCommit();
331+
commit.setHash("046d9a3c1532acf4cf08fe93235c00e4d673c1d2");
332+
commit.setDate(new Date());
333+
334+
BitbucketSCMSource instance = new BitbucketSCMSource("amuniz", "test-repo");
335+
BitbucketMockApiFactory.add(instance.getServerUrl(), BitbucketIntegrationClientFactory.getApiMockClient(instance.getServerUrl()));
336+
SCM scm = instance.build(head, new BitbucketGitSCMRevision(head, commit));
337+
assertThat(scm).isInstanceOf(GitSCM.class);
338+
GitSCM gitSCM = (GitSCM) scm;
339+
340+
assertThat(gitSCM.getExtensions())
341+
.isNotEmpty()
342+
.hasAtLeastOneElementOfType(BitbucketEnvVarExtension.class);
343+
344+
BitbucketEnvVarExtension gitExtension = gitSCM.getExtensions().stream()
345+
.filter(BitbucketEnvVarExtension.class::isInstance)
346+
.map(BitbucketEnvVarExtension.class::cast)
347+
.findFirst()
348+
.orElseThrow();
349+
assertThat(gitExtension.getOwner()).isEqualTo("amuniz");
350+
assertThat(gitExtension.getProjectKey()).isEqualTo("PUB");
351+
assertThat(gitExtension.getServerURL()).isEqualTo(instance.getServerUrl());
352+
assertThat(gitExtension.getRepository()).isEqualTo("test-repo");
353+
}
354+
326355
@Test
327356
public void given__instance__when__setTraits_empty__then__traitsEmpty() {
328357
BitbucketSCMSource instance = new BitbucketSCMSource("testing", "test-repo");

src/test/resources/com/cloudbees/jenkins/plugins/bitbucket/client/payload/2.0-repositories-amuniz-test-repos.json

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,50 @@
7676
"type": "user",
7777
"uuid": "{644c7fc2-b15a-4445-9f89-35390694fac9}"
7878
},
79+
"workspace": {
80+
"type": "workspace",
81+
"uuid": "{7d3a178a-a087-4756-b2da-2f9eadf50ba8}",
82+
"name": "Albert Muniz",
83+
"slug": "amunuz",
84+
"links": {
85+
"avatar": {
86+
"href": "https://bitbucket.org/workspaces/amuniz/avatar/?ts=1644525365"
87+
},
88+
"html": {
89+
"href": "https://bitbucket.org/amuniz/"
90+
},
91+
"self": {
92+
"href": "https://api.bitbucket.org/2.0/workspaces/nfalco79"
93+
}
94+
}
95+
},
96+
"project": {
97+
"type": "project",
98+
"key": "PUB",
99+
"uuid": "{ef731d07-06e0-46d2-9b56-2674649b0655}",
100+
"name": "public",
101+
"links": {
102+
"self": {
103+
"href": "https://api.bitbucket.org/2.0/workspaces/amuniz/projects/PUB"
104+
},
105+
"html": {
106+
"href": "https://bitbucket.org/amuniz/workspace/projects/PUB"
107+
},
108+
"avatar": {
109+
"href": "https://bitbucket.org/amuniz/workspace/projects/PUB/avatar/32?ts=1644525770"
110+
}
111+
}
112+
},
79113
"updated_on": "2018-09-21T15:53:38.794718+00:00",
80114
"size": 84351,
81115
"type": "repository",
82116
"slug": "test-repos",
83117
"is_private": false,
84-
"description": ""
118+
"description": "",
119+
"override_settings": {
120+
"default_merge_strategy": false,
121+
"branching_model": false
122+
},
123+
"parent": null,
124+
"enforced_signed_commits": null
85125
}

0 commit comments

Comments
 (0)