Skip to content

Commit

Permalink
[JENKINS-53535] Make Bitbucket Server, Owner and repository environme…
Browse files Browse the repository at this point in the history
…nt 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
  • Loading branch information
nfalco79 committed Jan 2, 2025
1 parent aa9c5ca commit dcf169b
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import com.cloudbees.jenkins.plugins.bitbucket.impl.util.URLUtils;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import hudson.Extension;
import hudson.plugins.git.GitSCM;
import hudson.plugins.git.extensions.GitSCMExtension;
import hudson.plugins.git.extensions.GitSCMExtensionDescriptor;
import java.util.Map;
import java.util.Objects;
import org.kohsuke.stapler.DataBoundConstructor;

public class BitbucketEnvVarExtension extends GitSCMExtension {
Expand Down Expand Up @@ -33,9 +36,60 @@ public BitbucketEnvVarExtension(@Nullable String owner, @NonNull String reposito
*/
@Override
public void populateEnvironmentVariables(GitSCM scm, Map<String, String> env) {
env.put("BITBUCKET_REPOSITORY", repository);
env.put("BITBUCKET_OWNER", owner);
env.put("BITBUCKET_PROJECT_KEY", projectKey);
env.put("BITBUCKET_SERVER_URL", serverURL);
env.put("BITBUCKET_REPOSITORY", getRepository());
env.put("BITBUCKET_OWNER", getOwner());
env.put("BITBUCKET_PROJECT_KEY", getProjectKey());
env.put("BITBUCKET_SERVER_URL", getServerURL());
}

public String getOwner() {
return owner;
}

public String getRepository() {
return repository;
}

public String getProjectKey() {
return projectKey;
}

public String getServerURL() {
return serverURL;
}

@Override
public int hashCode() {
return Objects.hash(owner, projectKey, repository, serverURL);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
BitbucketEnvVarExtension other = (BitbucketEnvVarExtension) obj;
return Objects.equals(owner, other.owner)
&& Objects.equals(projectKey, other.projectKey)
&& Objects.equals(repository, other.repository)
&& Objects.equals(serverURL, other.serverURL);
}

@Extension
// No @Symbol because Pipeline users should not configure this in other ways than this plugin provides
public static class DescriptorImpl extends GitSCMExtensionDescriptor {
/**
* {@inheritDoc}
*/
@Override
public String getDisplayName() {
return "Contribute additional environment variables about the target branch.";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.cloudbees.jenkins.plugins.bitbucket.client.pullrequest.BitbucketPullRequestCommit;
import com.cloudbees.jenkins.plugins.bitbucket.endpoints.AbstractBitbucketEndpoint;
import com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketEndpointConfiguration;
import com.cloudbees.jenkins.plugins.bitbucket.impl.extension.BitbucketEnvVarExtension;
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
import hudson.plugins.git.GitSCM;
import hudson.plugins.git.extensions.GitSCMExtension;
Expand Down Expand Up @@ -323,6 +324,34 @@ public void test_that_clone_url_does_not_contains_username() {
});
}

@Test
public void verify_envvar() {
BranchSCMHead head = new BranchSCMHead("master");
BitbucketPullRequestCommit commit = new BitbucketPullRequestCommit();
commit.setHash("046d9a3c1532acf4cf08fe93235c00e4d673c1d2");
commit.setDate(new Date());

BitbucketSCMSource instance = new BitbucketSCMSource("amuniz", "test-repo");
BitbucketMockApiFactory.add(instance.getServerUrl(), BitbucketIntegrationClientFactory.getApiMockClient(instance.getServerUrl()));
SCM scm = instance.build(head, new BitbucketGitSCMRevision(head, commit));
assertThat(scm).isInstanceOf(GitSCM.class);
GitSCM gitSCM = (GitSCM) scm;

assertThat(gitSCM.getExtensions())
.isNotEmpty()
.hasAtLeastOneElementOfType(BitbucketEnvVarExtension.class);

BitbucketEnvVarExtension gitExtension = gitSCM.getExtensions().stream()
.filter(BitbucketEnvVarExtension.class::isInstance)
.map(BitbucketEnvVarExtension.class::cast)
.findFirst()
.orElseThrow();
assertThat(gitExtension.getOwner()).isEqualTo("amuniz");
assertThat(gitExtension.getProjectKey()).isEqualTo("PUB");
assertThat(gitExtension.getServerURL()).isEqualTo(instance.getServerUrl());
assertThat(gitExtension.getRepository()).isEqualTo("test-repo");
}

@Test
public void given__instance__when__setTraits_empty__then__traitsEmpty() {
BitbucketSCMSource instance = new BitbucketSCMSource("testing", "test-repo");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,50 @@
"type": "user",
"uuid": "{644c7fc2-b15a-4445-9f89-35390694fac9}"
},
"workspace": {
"type": "workspace",
"uuid": "{7d3a178a-a087-4756-b2da-2f9eadf50ba8}",
"name": "Albert Muniz",
"slug": "amunuz",
"links": {
"avatar": {
"href": "https://bitbucket.org/workspaces/nfalco79/avatar/?ts=1644525365"
},
"html": {
"href": "https://bitbucket.org/nfalco79/"
},
"self": {
"href": "https://api.bitbucket.org/2.0/workspaces/nfalco79"
}
}
},
"project": {
"type": "project",
"key": "PUB",
"uuid": "{ef731d07-06e0-46d2-9b56-2674649b0655}",
"name": "public",
"links": {
"self": {
"href": "https://api.bitbucket.org/2.0/workspaces/nfalco79/projects/PUB"
},
"html": {
"href": "https://bitbucket.org/nfalco79/workspace/projects/PUB"
},
"avatar": {
"href": "https://bitbucket.org/nfalco79/workspace/projects/PUB/avatar/32?ts=1644525770"
}
}
},
"updated_on": "2018-09-21T15:53:38.794718+00:00",
"size": 84351,
"type": "repository",
"slug": "test-repos",
"is_private": false,
"description": ""
"description": "",
"override_settings": {
"default_merge_strategy": false,
"branching_model": false
},
"parent": null,
"enforced_signed_commits": null
}

0 comments on commit dcf169b

Please sign in to comment.