-
Notifications
You must be signed in to change notification settings - Fork 9
AGDIGGER-59 trigger jenkins job - 2 #3
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,30 +22,71 @@ public class DiggerClient { | |
|
|
||
| public static final long DEFAULT_BUILD_TIMEOUT = 60 * 1000; | ||
|
|
||
| private final JenkinsServer jenkins; | ||
| private JenkinsServer jenkinsServer; | ||
|
|
||
| public DiggerClient(JenkinsAuth auth) throws URISyntaxException { | ||
| this.jenkins = new JenkinsServer(new URI(auth.getUrl()), auth.getUser(), auth.getPassword()); | ||
| private CreateJobService createJobService; | ||
| private TriggerBuildService triggerBuildService; | ||
|
|
||
| private DiggerClient() { | ||
| } | ||
|
|
||
| /** | ||
| * Create client using provided url and credentials | ||
| * Create a client with defaults using provided url and credentials. | ||
| * <p> | ||
| * This client will use the defaults for the services. This is perfectly fine for majorith of the cases. | ||
| * | ||
| * @param url Jenkins url | ||
| * @param user Jenkins user | ||
| * @param password Jenkins password | ||
| * @return client instance | ||
| * @throws DiggerClientException if something goes wrong | ||
| */ | ||
| public static DiggerClient from(String url, String user, String password) throws DiggerClientException { | ||
| try { | ||
| JenkinsAuth jenkinsAuth = new JenkinsAuth(url, user, password); | ||
| return new DiggerClient(jenkinsAuth); | ||
| } catch (URISyntaxException e) { | ||
| throw new DiggerClientException("Invalid jenkins url format."); | ||
| public static DiggerClient createDefaultWithAuth(String url, String user, String password) throws DiggerClientException { | ||
| return DiggerClient.builder() | ||
| .createJobService(new CreateJobService()) | ||
| .triggerBuildService(new TriggerBuildService(TriggerBuildService.DEFAULT_FIRST_CHECK_DELAY, TriggerBuildService.DEFAULT_POLL_PERIOD)) | ||
| .withAuth(url, user, password) | ||
| .build(); | ||
| } | ||
|
|
||
| public static DiggerClientBuilder builder() { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO there's no point on having this method since DiggerClientBuilder is already public.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, right.. I personally think that I prefer keeping it for now, unless more objections arise. |
||
| return new DiggerClientBuilder(); | ||
| } | ||
|
|
||
| public static class DiggerClientBuilder { | ||
| private JenkinsAuth auth; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Like it. That would be easy to test. |
||
| private CreateJobService createJobService; | ||
| private TriggerBuildService triggerBuildService; | ||
|
|
||
| public DiggerClientBuilder withAuth(String url, String user, String password) { | ||
| this.auth = new JenkinsAuth(url, user, password); | ||
| return this; | ||
| } | ||
|
|
||
| public DiggerClientBuilder createJobService(CreateJobService createJobService) { | ||
| this.createJobService = createJobService; | ||
| return this; | ||
| } | ||
|
|
||
| public DiggerClientBuilder triggerBuildService(TriggerBuildService triggerBuildService) { | ||
| this.triggerBuildService = triggerBuildService; | ||
| return this; | ||
| } | ||
|
|
||
| public DiggerClient build() throws DiggerClientException { | ||
| final DiggerClient client = new DiggerClient(); | ||
| try { | ||
| client.jenkinsServer = new JenkinsServer(new URI(auth.getUrl()), auth.getUser(), auth.getPassword()); | ||
| client.createJobService = this.createJobService; | ||
| client.triggerBuildService = this.triggerBuildService; | ||
| return client; | ||
| } catch (URISyntaxException e) { | ||
| throw new DiggerClientException("Invalid jenkins url format."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Create new Digger job on Jenkins platform | ||
| * | ||
|
|
@@ -55,9 +96,8 @@ public static DiggerClient from(String url, String user, String password) throws | |
| * @throws DiggerClientException if something goes wrong | ||
| */ | ||
| public void createJob(String name, String gitRepo, String gitBranch) throws DiggerClientException { | ||
| CreateJobService service = new CreateJobService(this.jenkins); | ||
| try { | ||
| service.create(name, gitRepo, gitBranch); | ||
| createJobService.create(this.jenkinsServer, name, gitRepo, gitBranch); | ||
| } catch (Throwable e) { | ||
| throw new DiggerClientException(e); | ||
| } | ||
|
|
@@ -72,22 +112,21 @@ public void createJob(String name, String gitRepo, String gitBranch) throws Digg | |
| * This method will block until there is a build number, or the given timeout period is passed. If the build is still in the queue | ||
| * after the given timeout period, a {@code BuildStatus} is returned with state {@link BuildStatus.State#TIMED_OUT}. | ||
| * <p> | ||
| * Please note that timeout period is never meant to be very precise. It has the resolution of {@link TriggerBuildService#POLL_PERIOD} because | ||
| * Please note that timeout period is never meant to be very precise. It has the resolution of {@link TriggerBuildService#DEFAULT_POLL_PERIOD} because | ||
| * timeout is checked before every pull. | ||
| * <p> | ||
| * Similarly, {@link BuildStatus.State#CANCELLED_IN_QUEUE} is returned if the build is cancelled on Jenkins side and | ||
| * {@link BuildStatus.State#STUCK_IN_QUEUE} is returned if the build is stuck. | ||
| * | ||
| * @param jobName name of the job to trigger the build | ||
| * @param timeout how many milliseconds should this call block before returning {@link BuildStatus.State#TIMED_OUT}. | ||
| * Should be larger than {@link TriggerBuildService#FIRST_CHECK_DELAY} | ||
| * Should be larger than {@link TriggerBuildService#DEFAULT_FIRST_CHECK_DELAY} | ||
| * @return the build status | ||
| * @throws DiggerClientException if connection problems occur during connecting to Jenkins | ||
| */ | ||
| public BuildStatus build(String jobName, long timeout) throws DiggerClientException { | ||
| final TriggerBuildService triggerBuildService = new TriggerBuildService(jenkins); | ||
| try { | ||
| return triggerBuildService.build(jobName, timeout); | ||
| return triggerBuildService.build(this.jenkinsServer, jobName, timeout); | ||
| } catch (IOException e) { | ||
| LOG.debug("Exception while connecting to Jenkins", e); | ||
| throw new DiggerClientException(e); | ||
|
|
||
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.
Awesome idea!