-
Notifications
You must be signed in to change notification settings - Fork 143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow TokenMacro in Stash server URL Fixes [JENKINS-31117] #153
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -132,6 +132,7 @@ public class StashNotifier extends Notifier implements SimpleBuildStep { | |
|
||
// public members ---------------------------------------------------------- | ||
|
||
@Override | ||
public BuildStepMonitor getRequiredMonitorService() { | ||
return BuildStepMonitor.NONE; | ||
} | ||
|
@@ -376,19 +377,16 @@ protected Collection<String> lookupCommitSha1s( | |
* | ||
* @param logger the logger to log messages to | ||
* @param run | ||
* @param stashServer | ||
* @return the HttpClient | ||
*/ | ||
protected HttpClient getHttpClient(PrintStream logger, Run<?, ?> run) throws Exception { | ||
protected HttpClient getHttpClient(PrintStream logger, Run<?, ?> run, String stashServer) throws Exception { | ||
boolean ignoreUnverifiedSSL = ignoreUnverifiedSSLPeer; | ||
String stashServer = stashServerBaseUrl; | ||
|
||
DescriptorImpl descriptor = getDescriptor(); | ||
|
||
CertificateCredentials certificateCredentials = getCredentials(CertificateCredentials.class, run.getParent()); | ||
|
||
if ("".equals(stashServer) || stashServer == null) { | ||
stashServer = descriptor.getStashRootUrl(); | ||
} | ||
|
||
URL url = new URL(stashServer); | ||
HttpClientBuilder builder = HttpClientBuilder.create(); | ||
RequestConfig.Builder requestBuilder = RequestConfig.custom(); | ||
|
@@ -452,6 +450,7 @@ private SSLContext buildSslContext(boolean ignoreUnverifiedSSL, Credentials cred | |
} | ||
if (ignoreUnverifiedSSL) { | ||
TrustStrategy easyStrategy = new TrustStrategy() { | ||
@Override | ||
public boolean isTrusted(X509Certificate[] chain, String authType) | ||
throws CertificateException { | ||
return true; | ||
|
@@ -562,7 +561,7 @@ public ListBoxModel doFillCredentialsIdItems(@AncestorInPath Item project) { | |
} | ||
|
||
public String getStashRootUrl() { | ||
if ((stashRootUrl == null) || (stashRootUrl.trim().equals(""))) { | ||
if ((stashRootUrl == null) || (stashRootUrl.trim().isEmpty())) { | ||
return null; | ||
} else { | ||
return stashRootUrl; | ||
|
@@ -610,13 +609,13 @@ public FormValidation doCheckStashServerBaseUrl( | |
|
||
// calculate effective url from global and local config | ||
String url = value; | ||
if ((url != null) && (!url.trim().equals(""))) { | ||
if ((url != null) && (!url.trim().isEmpty())) { | ||
url = url.trim(); | ||
} else { | ||
url = stashRootUrl != null ? stashRootUrl.trim() : null; | ||
} | ||
|
||
if ((url == null) || url.equals("")) { | ||
if ((url == null) || url.isEmpty()) { | ||
return FormValidation.error( | ||
"Please specify a valid URL here or in the global " | ||
+ "configuration"); | ||
|
@@ -633,10 +632,12 @@ public FormValidation doCheckStashServerBaseUrl( | |
} | ||
|
||
@SuppressWarnings("rawtypes") | ||
@Override | ||
public boolean isApplicable(Class<? extends AbstractProject> aClass) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Notify Stash Instance"; | ||
} | ||
|
@@ -687,9 +688,13 @@ protected NotificationResult notifyStash( | |
final StashBuildState state) throws Exception { | ||
HttpEntity stashBuildNotificationEntity | ||
= newStashBuildNotificationEntity(run, state, listener); | ||
|
||
String stashURL= expandStashURL(run, listener); | ||
|
||
logger.println("Notifying Stash at \""+stashURL+"\""); | ||
|
||
HttpPost req = createRequest(stashBuildNotificationEntity, run.getParent(), commitSha1); | ||
HttpClient client = getHttpClient(logger, run); | ||
HttpPost req = createRequest(stashBuildNotificationEntity, run.getParent(), commitSha1, stashURL); | ||
HttpClient client = getHttpClient(logger, run, stashURL); | ||
try { | ||
HttpResponse res = client.execute(req); | ||
if (res.getStatusLine().getStatusCode() != 204) { | ||
|
@@ -708,7 +713,7 @@ protected NotificationResult notifyStash( | |
* | ||
* @param clazz The type of {@link com.cloudbees.plugins.credentials.Credentials} to return. | ||
* @param project The hierarchical project context within which the credentials are searched for. | ||
* @return The first credentials of the given type that are found withing the project hierarchy, or null otherwise. | ||
* @return The first credentials of the given type that are found within the project hierarchy, or null otherwise. | ||
*/ | ||
private <T extends Credentials> T getCredentials(final Class<T> clazz, final Item project) { | ||
|
||
|
@@ -777,19 +782,16 @@ protected <C extends Credentials> List<C> lookupCredentials(Class<C> type, Item | |
* @param stashBuildNotificationEntity a entity containing the parameters | ||
* for Stash | ||
* @param commitSha1 the SHA1 of the commit that was built | ||
* @param url | ||
* @return the HTTP POST request to the Stash build API | ||
*/ | ||
protected HttpPost createRequest( | ||
final HttpEntity stashBuildNotificationEntity, | ||
final Item project, | ||
final String commitSha1) throws AuthenticationException { | ||
|
||
String url = stashServerBaseUrl; | ||
DescriptorImpl descriptor = getDescriptor(); | ||
|
||
if ("".equals(url) || url == null) | ||
url = descriptor.getStashRootUrl(); | ||
|
||
final Item project, | ||
final String commitSha1, | ||
final String url) throws AuthenticationException { | ||
|
||
|
||
HttpPost req = new HttpPost( | ||
url | ||
+ "/rest/build-status/1.0/commits/" | ||
|
@@ -815,6 +817,28 @@ protected HttpPost createRequest( | |
return req; | ||
} | ||
|
||
private String expandStashURL(Run<?, ?> run, final TaskListener listener) { | ||
String url = stashServerBaseUrl; | ||
DescriptorImpl descriptor = getDescriptor(); | ||
if ("".equals(url) || url == null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be |
||
url = descriptor.getStashRootUrl(); | ||
} | ||
|
||
try { | ||
if (!(run instanceof AbstractBuild<?, ?>)) { | ||
url = TokenMacro.expandAll(run, new FilePath(run.getRootDir()), listener, url); | ||
} else { | ||
url = TokenMacro.expandAll((AbstractBuild<?, ?>) run, listener, url); | ||
} | ||
|
||
} catch (IOException | InterruptedException | MacroEvaluationException ex) { | ||
PrintStream logger = listener.getLogger(); | ||
logger.println("Unable to expand Stash Server URL"); | ||
ex.printStackTrace(logger); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indentation |
||
return url; | ||
} | ||
|
||
/** | ||
* Returns the HTTP POST entity body with the JSON representation of the | ||
* run result to be sent to the Stash build API. | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indentation