-
Notifications
You must be signed in to change notification settings - Fork 414
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
Adding ability to set icon_emoji and username on bot messages #578
Conversation
Fixing issue where previous build could also be current build if multiple builds were running concurrently
@@ -390,7 +410,7 @@ public SlackNotifier(final String baseUrl, final String teamDomain, final String | |||
} | |||
|
|||
public SlackNotifier(final String baseUrl, final String teamDomain, final String authToken, final boolean botUser, final String room, final String tokenCredentialId, | |||
final String sendAs, final boolean startNotification, final boolean notifyAborted, final boolean notifyFailure, | |||
final String sendAs, final String iconEmoji, final String username, final boolean startNotification, final boolean notifyAborted, final boolean notifyFailure, |
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.
This is unwieldy, and you would need "another" deprecated SlackNotifier to avoid binary incompatibility.
Perhaps we ought to switch to a builder pattern?
Or is the only used for testing? Perhaps mark it as NoExternalAccess
@timja WDYT?
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.
I think we need a Pojo with a builder pattern, we can't keep adding arguments here.
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.
Help me understand a little bit: I get the builder pattern bit, but I don't know where it will actually be used. Wouldn't I need to make another constructor anyway to avoid the binary incompatibility?
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.
There will be one new constructor, and a conversion in the old one to the builder.
Never more will there be a new constructor, there's about 6 in this file
public String getIconEmoji() { return iconEmoji; } | ||
|
||
@DataBoundSetter | ||
public void setIconEmoji(String iconEmoji) { |
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.
Something is fishy when you need to add the setter/getter twice.
Surely we could use the same object/class.
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.
Aren't these needed to get/set values on configuration pages?
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.
This could be done as a separate improvement, but there is definitely some need to reduce the repetition 😓
} | ||
else { | ||
if (StringUtils.isNotEmpty(iconEmoji)) { | ||
url += "&icon_emoji=" + URLEncoder.encode(iconEmoji, StandardCharsets.UTF_8.name()); |
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.
May I suggest we start using handy URI?
No, you may not! Use JSON post request instead of building URL
You should use the Jenkins plugin version of it: https://github.com/jenkinsci/handy-uri-templates-2-api-plugin
However is the docs: https://github.com/damnhandy/Handy-URI-Templates
See usage here: https://github.com/jenkinsci/bitbucket-branch-source-plugin/pull/80/files
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.
See this comment: #577 (comment)
if(StringUtils.isEmpty(iconEmoji)) { | ||
return iconEmoji; | ||
} | ||
else if (!iconEmoji.startsWith(":")) { |
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.
You could shorten this to
else if (!iconEmoji.startsWith(":")) { | |
iconEmoji = StringUtils.appendIfMissing(iconEmoji, ":") |
else if (!iconEmoji.startsWith(":")) { | ||
iconEmoji = ":" + iconEmoji; | ||
} | ||
if (!iconEmoji.endsWith(":")) { |
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.
Also
if (!iconEmoji.endsWith(":")) { | |
iconEmoji = StringUtils.prependIfMissing(iconEmoji, ":") |
pom.xml
Outdated
<dependency> | ||
<groupId>org.apache.commons</groupId> | ||
<artifactId>commons-lang3</artifactId> | ||
<version>3.7</version> |
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.
Are we really using such an old commons-lang version? 😢
I'm clueless on what SpotBugs wants from me. I'm guessing I've implemented the builder pattern incorrectly. |
You need to update |
Can I also change the name of the method and button? I didn't think to add it because |
it needs to start with |
… needing to be saved before applying to the test connection
String targetRoom = Util.fixEmpty(room) != null ? room : this.room; | ||
|
||
SlackService testSlackService = getSlackService(targetUrl, targetDomain, targetTokenCredentialId, targetBotUser, targetRoom, project); | ||
SlackService testSlackService = getSlackService(new StandardSlackServiceBuilder() |
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.
SlackService testSlackService = getSlackService(new StandardSlackServiceBuilder() | |
SlackService testSlackService = getSlackService(StandardSlackServiceBuilder.builder() |
.withRoomId(targetRoom) | ||
.withIconEmoji(iconEmoji) | ||
.withUsername(username) | ||
,tokenCredentialId, project |
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.
hmm, formatting looks weird here, maybe put the comma one line up?
@@ -712,12 +815,18 @@ public FormValidation doTestConnection(@QueryParameter("baseUrl") final String b | |||
targetUrl = this.baseUrl; | |||
} | |||
String targetDomain = Util.fixEmpty(teamDomain) != null ? teamDomain : this.teamDomain; | |||
boolean targetBotUser = botUser || this.botUser; | |||
String targetTokenCredentialId = Util.fixEmpty(tokenCredentialId) != null ? tokenCredentialId : |
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.
removing these two lines break relying on global config when testing in a freestyle job
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.
You're right, the test needs to be consistent so the behavior is the same between the test and how it actually works.
@@ -471,7 +532,16 @@ public SlackService newSlackService(AbstractBuild abstractBuild, BuildListener l | |||
authTokenCredentialId = env.expand(authTokenCredentialId); | |||
room = env.expand(room); | |||
final String populatedToken = CredentialsObtainer.getTokenToUse(authTokenCredentialId, abstractBuild.getParent(), authToken); | |||
return new StandardSlackService(baseUrl, teamDomain, botUser, room, false, populatedToken); | |||
return new StandardSlackService( | |||
new StandardSlackServiceBuilder() |
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.
new StandardSlackServiceBuilder() | |
StandardSlackService.builder() |
return new StandardSlackService(baseUrl, team, botUser, channel, replyBroadcast, populatedToken); | ||
SlackService getSlackService(String baseUrl, String team, boolean botUser, String channel, boolean replyBroadcast, String iconEmoji, String username, String populatedToken) { | ||
return new StandardSlackService( | ||
new StandardSlackServiceBuilder() |
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.
new StandardSlackServiceBuilder() | |
StandardSlackService.builder() |
notifyNotBuilt, notifySuccess, notifyUnstable, notifyRegression, notifyBackToNormal, notifyRepeatedFailure, | ||
includeTestSummary, includeFailedTests, matrixTriggerMode, commitInfoChoice, includeCustomMessage, customMessage, | ||
customMessageSuccess, customMessageAborted, customMessageNotBuilt, customMessageUnstable, customMessageFailure); | ||
super(new SlackNotifierBuilder() |
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.
super(new SlackNotifierBuilder() | |
super(builder() |
Can you update the tests to not call the deprecated constructor please? |
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.
Thanks for this! merging when the build is green
Want me to wait for your other PR before doing a release? |
Yeah, I should be able to do the other one pretty soon here |
When using a bot, the iconEmoji and username fields will overwrite the emoji and name used by the bot in Slack. Either value being set will override the
as_user
field and turn the flag off. The parameters are optional and not including both of them results in the original behavior. It's possible to just set one or the other as well.This works in pipeline and freestyle jobs.