Skip to content
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

Merged
merged 53 commits into from
Jun 12, 2019

Conversation

EricBartusch
Copy link
Contributor

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.

@@ -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,
Copy link
Member

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?

Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Member

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) {
Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Member

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());
Copy link
Member

@jetersen jetersen Jun 1, 2019

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

Copy link
Member

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(":")) {
Copy link
Member

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

Suggested change
else if (!iconEmoji.startsWith(":")) {
iconEmoji = StringUtils.appendIfMissing(iconEmoji, ":")

else if (!iconEmoji.startsWith(":")) {
iconEmoji = ":" + iconEmoji;
}
if (!iconEmoji.endsWith(":")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also

Suggested change
if (!iconEmoji.endsWith(":")) {
iconEmoji = StringUtils.prependIfMissing(iconEmoji, ":")

pom.xml Outdated
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
Copy link
Member

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? 😢

@EricBartusch
Copy link
Contributor Author

I'm clueless on what SpotBugs wants from me. I'm guessing I've implemented the builder pattern incorrectly.

@timja
Copy link
Member

timja commented Jun 11, 2019

You need to update doTestConnection in SlackNotifier
So that the "Test connection" works on the global configuration page

@EricBartusch
Copy link
Contributor Author

Can I also change the name of the method and button? I didn't think to add it because doTestConnection makes our dinner like it's just making sure Jenkins can talk to Slack, not actually send a message. doTestMessage or sendTestMessage makes more sense to me.

@timja
Copy link
Member

timja commented Jun 11, 2019

it needs to start with do it makes sense to me how it is, it's testing the connection to slack...

String targetRoom = Util.fixEmpty(room) != null ? room : this.room;

SlackService testSlackService = getSlackService(targetUrl, targetDomain, targetTokenCredentialId, targetBotUser, targetRoom, project);
SlackService testSlackService = getSlackService(new StandardSlackServiceBuilder()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
SlackService testSlackService = getSlackService(new StandardSlackServiceBuilder()
SlackService testSlackService = getSlackService(StandardSlackServiceBuilder.builder()

.withRoomId(targetRoom)
.withIconEmoji(iconEmoji)
.withUsername(username)
,tokenCredentialId, project
Copy link
Member

@timja timja Jun 11, 2019

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?

@timja
Copy link
Member

timja commented Jun 11, 2019

Test connection is working in global config now:
image

@timja
Copy link
Member

timja commented Jun 11, 2019

The slack send pipeline log hasn't been updated:
image

but the functionality works

@@ -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 :
Copy link
Member

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

Copy link
Contributor Author

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()
Copy link
Member

@timja timja Jun 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
new StandardSlackServiceBuilder()
StandardSlackService.builder()

notifyNotBuilt, notifySuccess, notifyUnstable, notifyRegression, notifyBackToNormal, notifyRepeatedFailure,
includeTestSummary, includeFailedTests, matrixTriggerMode, commitInfoChoice, includeCustomMessage, customMessage,
customMessageSuccess, customMessageAborted, customMessageNotBuilt, customMessageUnstable, customMessageFailure);
super(new SlackNotifierBuilder()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
super(new SlackNotifierBuilder()
super(builder()

@timja
Copy link
Member

timja commented Jun 11, 2019

Can you update the tests to not call the deprecated constructor please?
i.e. in:
StandardSlackServiceTest
StandardSlackServiceStub
SlackNotifierStub (this one maybe needs a suppression, haven't looked closely)

Copy link
Member

@timja timja left a 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

@timja timja merged commit 1ece202 into jenkinsci:master Jun 12, 2019
@timja
Copy link
Member

timja commented Jun 12, 2019

Want me to wait for your other PR before doing a release?

@EricBartusch
Copy link
Contributor Author

Yeah, I should be able to do the other one pretty soon here

@EricBartusch EricBartusch deleted the feature/icon_emoji branch June 12, 2019 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants