-
Notifications
You must be signed in to change notification settings - Fork 411
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
Fix folder credential usage #541
Changes from 2 commits
d35f206
8d05af3
1c8d88e
7d3c7e9
a01335b
94811cd
4e071dc
8217e7c
2edbd9d
c63a66c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,9 +34,11 @@ | |
import org.kohsuke.stapler.DataBoundConstructor; | ||
import org.kohsuke.stapler.DataBoundSetter; | ||
import org.kohsuke.stapler.QueryParameter; | ||
import java.util.logging.Level; | ||
|
||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.logging.Logger; | ||
import javax.annotation.Nonnull; | ||
|
||
import static com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials; | ||
|
@@ -46,6 +48,8 @@ | |
*/ | ||
public class SlackSendStep extends Step { | ||
|
||
private static final Logger logger = Logger.getLogger(SlackSendStep.class.getName()); | ||
|
||
private String message; | ||
private String color; | ||
private String token; | ||
|
@@ -232,7 +236,7 @@ public static class SlackSendStepExecution extends SynchronousNonBlockingStepExe | |
protected SlackResponse run() throws Exception { | ||
|
||
Jenkins jenkins = Jenkins.get(); | ||
|
||
Item item = getItemForCredentials(); | ||
SlackNotifier.DescriptorImpl slackDesc = jenkins.getDescriptorByType(SlackNotifier.DescriptorImpl.class); | ||
|
||
String baseUrl = step.baseUrl != null ? step.baseUrl : slackDesc.getBaseUrl(); | ||
|
@@ -253,7 +257,7 @@ protected SlackResponse run() throws Exception { | |
); | ||
|
||
SlackService slackService = getSlackService( | ||
baseUrl, teamDomain, token, tokenCredentialId, botUser, channel, step.replyBroadcast | ||
baseUrl, teamDomain, token, tokenCredentialId, botUser, channel, step.replyBroadcast, item | ||
); | ||
final boolean publishSuccess; | ||
if (step.attachments != null) { | ||
|
@@ -300,13 +304,12 @@ protected SlackResponse run() throws Exception { | |
|
||
JSONArray getAttachmentsAsJSONArray() throws Exception { | ||
final TaskListener listener = getContext().get(TaskListener.class); | ||
final String jsonString; | ||
if (step.attachments instanceof String) { | ||
jsonString = (String) step.attachments; | ||
} | ||
else { | ||
jsonString = JsonOutput.toJson(step.attachments); | ||
} | ||
final String jsonString; | ||
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. what changed in this block? tabs to spaces or something? can't tell from github 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. it seems something got merged into master that had a mix of tabs and spaces and my IDE "fixed" it... |
||
if (step.attachments instanceof String) { | ||
jsonString = (String) step.attachments; | ||
} else { | ||
jsonString = JsonOutput.toJson(step.attachments); | ||
} | ||
|
||
JsonSlurper jsonSlurper = new JsonSlurper(); | ||
JSON json = null; | ||
|
@@ -316,20 +319,46 @@ JSONArray getAttachmentsAsJSONArray() throws Exception { | |
listener.error(Messages.NotificationFailedWithException(e)); | ||
return null; | ||
} | ||
if(!(json instanceof JSONArray)){ | ||
if (!(json instanceof JSONArray)) { | ||
listener.error(Messages.NotificationFailedWithException(new IllegalArgumentException("Attachments must be JSONArray"))); | ||
return null; | ||
} | ||
return (JSONArray) json; | ||
} | ||
|
||
/** | ||
* Tries to obtain the proper Item object to provide to CredentialsProvider. | ||
* Project works for freestyle jobs, the parent of the Run works for pipelines. | ||
* In case the proper item cannot be found, null is returned, since when null is provided to CredentialsProvider, | ||
* it will internally use Jenkins.getInstance() which effectively only allows global credentials. | ||
* | ||
* @return the item to use for CredentialsProvider credential lookup | ||
*/ | ||
private Item getItemForCredentials() { | ||
Item item = null; | ||
try { | ||
item = getContext().get(Project.class); | ||
if (item == null) { | ||
Run run = getContext().get(Run.class); | ||
if (run != null) { | ||
item = run.getParent(); | ||
} else { | ||
item = null; | ||
} | ||
} | ||
} catch (Exception e) { | ||
logger.log(Level.INFO, "Exception obtaining item for credentials lookup. Only global credentials will be available", e); | ||
} | ||
return item; | ||
} | ||
|
||
private String defaultIfEmpty(String value) { | ||
return Util.fixEmpty(value) != null ? value : Messages.SlackSendStepValuesEmptyMessage(); | ||
} | ||
|
||
//streamline unit testing | ||
SlackService getSlackService(String baseUrl, String team, String token, String tokenCredentialId, boolean botUser, String channel, boolean replyBroadcast) { | ||
return new StandardSlackService(baseUrl, team, token, tokenCredentialId, botUser, channel, replyBroadcast); | ||
SlackService getSlackService(String baseUrl, String team, String token, String tokenCredentialId, boolean botUser, String channel, boolean replyBroadcast, Item item) { | ||
return new StandardSlackService(baseUrl, team, token, tokenCredentialId, botUser, channel, replyBroadcast, item); | ||
} | ||
} | ||
} |
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.
The legacy constructors will still need to be able to find a credential but that will be deprecated and removed at a later date
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.
Hopefully I addressed the 2 issues. I deprecated the other constructors, but maybe it's too early for that?