Skip to content

Conversation

aliok
Copy link
Contributor

@aliok aliok commented Dec 9, 2016

  • Implemented public long build(String jobName, long timeout) in DiggerClient
  • Introduced SLF4J-LOG4J bridge as some dependency was using SLF4J and logs were gone to NoOpLogger
  • Added .editorconfig file matching current style. Will change it later to match Java conventions

Checked in as 2 separate commits: 1 for styling/formatting. 1 for the actual work.

@aliok
Copy link
Contributor Author

aliok commented Dec 9, 2016

I tested it like this:

public static void main(String[] args) throws URISyntaxException, DiggerClientException {
    DiggerClient client = new DiggerClient(new JenkinsAuth("JENKINS_URL","username","password"));
    final String jobName = "aliok-dec09-" + UUID.randomUUID().toString();
    client.createJob(jobName, "https://github.com/wtrocki/helloworld-android-gradle.git", "master");
    final long buildNumber = client.build(jobName, 10*1000);
    System.out.println(buildNumber);
  }

Please note that we use some features in Jenkins-Java-client 3.7-SNAPSHOT.
So, you need to clone https://github.com/jenkinsci/java-client-api.git and build it locally.

git clone https://github.com/jenkinsci/java-client-api.git
cd jenkins-client
mvn install

@aliok
Copy link
Contributor Author

aliok commented Dec 9, 2016

@matzew @wtrocki @ziccardi ping

<groupId>com.offbytwo.jenkins</groupId>
<artifactId>jenkins-client</artifactId>
<version>0.3.6</version>
<version>0.3.7-SNAPSHOT</version>
Copy link
Contributor

Choose a reason for hiding this comment

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

Do they have a CI running somewhere, where we can suck down the dependency ?
Not a blocker - just asking

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@matzew good point!
I looked now, but couldn't find anything. I checked the pom.xml files in the source code to see some repository reference. There is one, but that's not the snapshot repository.
No mention of CI.

@khmarbaise Where can we suck Jenkins-Java-client SNAPSHOT? Unfortunately, I haven't still got the approval to post to mailing list (https://groups.google.com/d/forum/java-client-api).
As a quick introduction: we are building a mobile application build farm on top of Jenkins 😎

Copy link
Contributor Author

Choose a reason for hiding this comment

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

LOG.debug("Exception while connecting to Jenkins", e);
throw new DiggerClientException(e);
} catch (InterruptedException e) {
LOG.debug("Exception while sleeping between checks", e);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we rephrase: "Exception while sleeping between checks" - It's not clear what that means for me.


if (queueItem.isCancelled()) {
LOG.debug("Queue item is cancelled. Returning -1");
return -1;
Copy link
Contributor

Choose a reason for hiding this comment

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

I do not like this because we introducing magic number and missing actual problem here.
Best to introduce some object (model) that would contain build number + some info about this build. If something is wrong build number would be empty.
Alternative is to use Long and return null or provide specific client exception (Subclass our exception)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See my other comment: #2 (comment)

if (queueItem.isCancelled()) {
LOG.debug("Queue item is cancelled. Returning -1");
return -1;
} else if (queueItem.isStuck()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This probably would require us to return some exception to the client - job needs to be retrgered. I do not remember where item can be stuck, but it this is permanent we should let clients now.

Copy link
Contributor Author

@aliok aliok Dec 9, 2016

Choose a reason for hiding this comment

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

@wtrocki good point. This is a combined answer for the comment above #2 (comment) and this comment.

I don't like subclassing the client exception. That would mean some casting and instanceof calls.

Lets use this model then:

class BuildStatus{
   STATE state;
   int buildNumber;
}
enum STATE{
  BUILDING,
  QUEUE_ITEM_CANCELLED,
  QUEUE_ITEM_STUCK,
  TIMED_OUT
  ...
}
class DiggerClient{
...
  public BuildStatus build(String jobName, long timeout) throws DiggerClientException{...}
}

Method will only throw exception in case of

  • thread interruption during sleep calls
  • connection problem with Jenkins

@@ -1,24 +1,23 @@
package com.redhat.digkins.services;
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's use org.aerogear.*** as package for all of this

Copy link
Contributor

Choose a reason for hiding this comment

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

let's do the package change in a SEPARATE PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

+1

@matzew
Copy link
Contributor

matzew commented Dec 9, 2016

unit tests ?

@wtrocki
Copy link
Contributor

wtrocki commented Dec 9, 2016

@aliok Would be good to paste your example into README.md (create job is already there)

@wtrocki
Copy link
Contributor

wtrocki commented Dec 9, 2016

Could not review change because of missing artifact:
Failed to execute goal on project jenkins-client: Could not resolve dependencies for project groupId:jenkins-client:jar:1.0.0: Could not find artifact com.offbytwo.jenkins:jenkins-client:jar:0.3.7-SNAPSHOT in jcenter (https://jcenter.bintray.com/) -> [Help 1]

I needed to build it locally.
Do you have some specific profile configured?

@wtrocki
Copy link
Contributor

wtrocki commented Dec 11, 2016

PR extended in aliok#1

@aliok
Copy link
Contributor Author

aliok commented Dec 12, 2016

Do you have some specific profile configured?
@wtrocki You need to build it locally and install into your local repo.

git clone https://github.com/jenkinsci/java-client-api.git
cd jenkins-client
mvn install

@aliok
Copy link
Contributor Author

aliok commented Dec 12, 2016

@wtrocki @matzew @josemigallas
requesting a review...

updates:

  • returning an object instead of plain number when triggering a build
  • unit test for the service

notes:

  • couldn't write unit tests for the class DiggerClient as services are currently instantiated in the methods (and I don't want to introduce PowerMock for stubbing constructors).
  • the unit test takes a while because of delays (Thread.sleep).

follow ups that I am gonna do immediately

  • convert services to singletons and pass them to client
  • make the delays parametrizable in services
  • write unit test for DiggerClient

How to test:

mvn clean test

Note that you need to build Jenkins-Java-client and install it locally as described in previous comments.

private final static String GIT_REPO_BRANCH = "GIT_REPO_BRANCH";

private final static String GIT_REPO_URL = "GIT_REPO_URL", GIT_REPO_BRANCH = "GIT_REPO_BRANCH";
private JenkinsServer jenkins;

Choose a reason for hiding this comment

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

Should this be final as in DiggerClient?

* @param gitBranch git repository branch (default branch used to checkout source code)
*/
public void create(String name, String gitRepo, String gitBranch) throws IOException {
JtwigTemplate template = JtwigTemplate.classpathTemplate("templates/job.xml");

Choose a reason for hiding this comment

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

I strongly recommend to extract hardcoded strings into a constant field, this way it can be found, checked and modified easier when needed.

/**
* How long should we wait before we start checking the queue item status.
*/
public static final long FIRST_CHECK_DELAY = 5 * 1000L;

Choose a reason for hiding this comment

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

Just a suggestion: the time unit should be always explicit. In this case I believe they are millis so I would either name it FIRST_CHECK_DELAY_MILLIS or include it in comment /** [...] to queue item status, in milli seconds. */

public static final long POLL_PERIOD = 2 * 1000L;


private JenkinsServer jenkins;

Choose a reason for hiding this comment

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

Should it be final?

@aliok aliok merged commit 795eb04 into aerogear-attic:master Dec 12, 2016
@aliok
Copy link
Contributor Author

aliok commented Dec 12, 2016

Merged this one.

@josemigallas your concerns are done in a separate PR

@wtrocki
Copy link
Contributor

wtrocki commented Dec 12, 2016

Reviewed. Looks ok.

couldn't write unit tests for the class DiggerClient as services are currently instantiated in the methods (and I don't want to introduce PowerMock for stubbing constructors).

DiggerClient should not have any logic and just pass all execution parameters to services so I would not bother too much

@aliok
Copy link
Contributor Author

aliok commented Dec 12, 2016

@wtrocki on a second thought... you're right. I won't write tests for DiggerClient class

@aliok aliok deleted the AGDIGGER-59-trigger-jenkins-job branch May 30, 2017 08:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants