-
Notifications
You must be signed in to change notification settings - Fork 354
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JENKINS-53535] Make Bitbucket Server, Owner and repository environme…
…nt variables for Bitbucket Team/Project based jobs (#949) Add as environment variables some bitbucket useful informations like: - repository name (slug); - owner name (slug); - project key; - the server URL.
- Loading branch information
Showing
21 changed files
with
194 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...java/com/cloudbees/jenkins/plugins/bitbucket/impl/extension/BitbucketEnvVarExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.cloudbees.jenkins.plugins.bitbucket.impl.extension; | ||
|
||
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.plugins.git.GitSCM; | ||
import hudson.plugins.git.extensions.GitSCMExtension; | ||
import java.util.Map; | ||
|
||
public class BitbucketEnvVarExtension extends GitSCMExtension { | ||
|
||
private final String owner; | ||
private final String repository; | ||
private final String projectKey; | ||
private final String serverURL; | ||
|
||
public BitbucketEnvVarExtension(@Nullable String owner, @NonNull String repository, @Nullable String projectKey, @NonNull String serverURL) { | ||
this.owner = owner; | ||
this.repository = repository; | ||
this.projectKey = projectKey; | ||
this.serverURL = URLUtils.removeAuthority(serverURL); | ||
} | ||
|
||
/** | ||
* Contribute additional environment variables about the target branch. | ||
* Since source branch could be from a forked repository, for which the | ||
* credentials in use are not allowed to do nothing, is discarded. | ||
* | ||
* @param scm GitSCM used as reference | ||
* @param env environment variables to be added | ||
*/ | ||
@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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/main/java/com/cloudbees/jenkins/plugins/bitbucket/impl/util/URLUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.cloudbees.jenkins.plugins.bitbucket.impl.util; | ||
|
||
import edu.umd.cs.findbugs.annotations.CheckForNull; | ||
import edu.umd.cs.findbugs.annotations.Nullable; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
|
||
public final class URLUtils { | ||
|
||
private URLUtils() { | ||
} | ||
|
||
@Nullable | ||
public static String removeAuthority(@CheckForNull String url) { | ||
if (url != null) { | ||
try { | ||
URL linkURL = new URL(url); | ||
URL cleanURL = new URL(linkURL.getProtocol(), linkURL.getHost(), linkURL.getPort(), linkURL.getFile()); | ||
return cleanURL.toExternalForm(); | ||
} catch (MalformedURLException e) { | ||
// do nothing, URL can not be parsed, leave as is | ||
} | ||
} | ||
return url; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.