-
Notifications
You must be signed in to change notification settings - Fork 171
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 JenkinsEventNotifier to send events back to TFS/VSTS #167
Conversation
Adding "team-events-connect" endpoint
* - Job Completion event | ||
*/ | ||
public class JenkinsEventNotifier { | ||
protected final static Logger log = Logger.getLogger(JenkinsEventNotifier.class.getName()); |
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 don't know if we care): I think Checkstyle will flag this line for the order of the keywords:
https://stackoverflow.com/questions/10299067/modifier-keyword-order-in-java
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.
Probably right. I will run check style and fix those issues.
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 added CheckStyle to the build but it will be in another PR since there are so many changes.
payload.put("server", connectionParameters.getConnectionKey()); | ||
final String jsonPayload = payload.toString(); | ||
final JobCompletionEventArgs args = new JobCompletionEventArgs( | ||
connectionParameters.getConnectionKey(), jsonPayload, |
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.
Split this line ?
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 assume you want me to put each parameter on a separate line? That makes sense.
sb.append(url); | ||
for(final String s : parts) { | ||
if (StringUtils.isNotBlank(s)) { | ||
if (sb.charAt(sb.length() - 1) != '/') { |
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.
Protect against an empty url?
(If url is blank, this will throw IndexOutOfBounds.)
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.
good catch.
@Extension | ||
public class JenkinsRunListener extends RunListener<Run> { | ||
|
||
protected static Logger log = Logger.getLogger(JenkinsRunListener.class.getName()); |
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.
+final
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.
yep
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 made this change in another PR (with CheckStyle)
sb.append(url); | ||
for(final String s : parts) { | ||
if (StringUtils.isNotBlank(s)) { | ||
if (sb.charAt(sb.length() - 1) != '/') { |
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.
Shouldn't we check that sb.Length() > 0 ?
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.
absolutely, good catch.
|
||
private String getStartedBy(final Run run) { | ||
final Cause.UserIdCause cause = (Cause.UserIdCause) run.getCause(Cause.UserIdCause.class); | ||
final String startedBy; |
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 this would be simpler without the 'final' and the 'else' clause. Just initialize to 'null'.
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.
It would be less lines, but I like the final. I will see how it looks.
try { | ||
// Check to see if there are any collections "connected" to this Jenkins server | ||
final ConnectionParameters connectionParameters = c.getConnectionParameters(); | ||
if (connectionParameters == null || !connectionParameters.isSendJobCompletionEvents()) { |
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.
It seems to me that the method TeamCollectionConfiguration.getConnectedCollections() already does this check.
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.
good point. I will remove the null check and perhaps annotate the method with NotNull
|
||
public void sendJobCompletionEvent(final JobCompletionEventArgs args) throws IOException { | ||
final QueryString qs = new QueryString( | ||
API_VERSION, "3.0-preview.1", |
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.
{
"id": "e0e0a1c9-beeb-4fb7-a8c8-b18e3161a50e",
"area": "hooks",
"resourceName": "externalEvents",
"routeTemplate": "_apis/public/{area}/{resource}",
"resourceVersion": 1,
"minVersion": "2.0",
"maxVersion": "4.0",
"releasedVersion": "3.2"
},
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.
Shouldn't we use a released API 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.
will do. Thanks for providing the version information.
final String message = serviceHookEvent.getMessage().getText(); | ||
final String detailedMessage = serviceHookEvent.getDetailedMessage().getText(); | ||
final String message = serviceHookEvent.getMessage() != null ? serviceHookEvent.getMessage().getText() : ""; | ||
final String detailedMessage = serviceHookEvent.getDetailedMessage() != null ? serviceHookEvent.getDetailedMessage().getText() : ""; |
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.
It looks like we do not use these messages.
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 don't see them being used either, but it would be bigger change to remove them. I only changed the code because I ran into an error here. I don't know why they were originally added.
getPayloadSignature(connectionParameters, jsonPayload)); | ||
client.sendJobCompletionEvent(args); | ||
} catch (final Exception e) { | ||
log.info("ERROR: sendJobCompletionEvent: (collection=" + c.getCollectionUrl() + ") " + e.getMessage()); |
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.
Maybe this log level should be "warn"? Is this error expected?
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.
Agreed. It is not expected. I looked for a log.error, but there wasn't one :(
Adding "team-events-connect" endpoint which allows TFS/VSTS to "connect" to the Jenkins server and start receiving events.