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 JenkinsEventNotifier to send events back to TFS/VSTS #167

Merged
merged 4 commits into from
Aug 15, 2017

Conversation

jpricket
Copy link
Contributor

Adding "team-events-connect" endpoint which allows TFS/VSTS to "connect" to the Jenkins server and start receiving events.

* - Job Completion event
*/
public class JenkinsEventNotifier {
protected final static Logger log = Logger.getLogger(JenkinsEventNotifier.class.getName());

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

Copy link
Contributor Author

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.

Copy link
Contributor Author

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,

Choose a reason for hiding this comment

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

Split this line ?

Copy link
Contributor Author

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) != '/') {
Copy link

@lkillgore lkillgore Aug 14, 2017

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.)

Copy link
Contributor Author

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());

Choose a reason for hiding this comment

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

+final

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep

Copy link
Contributor Author

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) != '/') {

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 ?

Copy link
Contributor Author

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;

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'.

Copy link
Contributor Author

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()) {

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.

Copy link
Contributor Author

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",
Copy link

@AlexRukhlin AlexRukhlin Aug 14, 2017

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"
    },

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?

Copy link
Contributor Author

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() : "";

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.

Copy link
Contributor Author

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

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?

Copy link
Contributor Author

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 :(

@jpricket jpricket merged commit 39bb8d2 into master Aug 15, 2017
@jpricket jpricket deleted the users/jpricket/backup branch August 17, 2017 18:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants