Skip to content

Commit 8feabba

Browse files
authored
Initialise repository even without remote connection. (#180)
* Initialise repository even without remote connection. + Wrap all logging to catch exception of trigger run. Signed-off-by: Kanstantsin Shautsou <kanstantsin.sha@gmail.com> * fixup Signed-off-by: Kanstantsin Shautsou <kanstantsin.sha@gmail.com> * cleanup Signed-off-by: Kanstantsin Shautsou <kanstantsin.sha@gmail.com> * Be friendly for non persistent tests. Signed-off-by: Kanstantsin Shautsou <kanstantsin.sha@gmail.com> * kick triggering because there is no hooks. Signed-off-by: Kanstantsin Shautsou <kanstantsin.sha@gmail.com> * Debug travis. Signed-off-by: Kanstantsin Shautsou <kanstantsin.sha@gmail.com> * path Signed-off-by: Kanstantsin Shautsou <kanstantsin.sha@gmail.com> * Fix? Signed-off-by: Kanstantsin Shautsou <kanstantsin.sha@gmail.com> * Maven wrapper Signed-off-by: Kanstantsin Shautsou <kanstantsin.sha@gmail.com>
1 parent 424f155 commit 8feabba

17 files changed

Lines changed: 635 additions & 43 deletions

File tree

.mvn/wrapper/maven-wrapper.jar

48.4 KB
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.3.9/apache-maven-3.3.9-bin.zip

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@ before_install:
55
- pip install --user codecov
66
after_success:
77
- codecov
8+
after_failure:
9+
- pwd
10+
- cat /home/travis/build/KostyaSha/github-integration-plugin/github-pullrequest-plugin/target/surefire-reports/*.txt
811
install: true
912
script: ./travis.sh

github-pullrequest-plugin/src/main/java/com/github/kostyasha/github/integration/branch/GitHubBranchRepository.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import javax.annotation.Nonnull;
1919
import java.io.IOException;
20+
import java.net.URL;
2021
import java.util.ArrayList;
2122
import java.util.HashMap;
2223
import java.util.List;
@@ -50,6 +51,10 @@ public GitHubBranchRepository(GHRepository remoteRepository) {
5051
super(remoteRepository);
5152
}
5253

54+
public GitHubBranchRepository(String repoFullName, URL url) {
55+
super(repoFullName, url);
56+
}
57+
5358
@Nonnull
5459
public Map<String, GitHubBranch> getBranches() {
5560
if (isNull(branches)) {

github-pullrequest-plugin/src/main/java/com/github/kostyasha/github/integration/branch/GitHubBranchRepositoryFactory.java

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
package com.github.kostyasha.github.integration.branch;
22

3+
import com.cloudbees.jenkins.GitHubRepositoryName;
4+
import com.coravy.hudson.plugins.github.GithubProjectProperty;
35
import com.github.kostyasha.github.integration.generic.GitHubRepositoryFactory;
46
import hudson.Extension;
57
import hudson.XmlFile;
68
import hudson.model.Action;
79
import hudson.model.Job;
8-
import org.kohsuke.github.GHRepository;
910
import org.slf4j.Logger;
1011
import org.slf4j.LoggerFactory;
1112

1213
import javax.annotation.Nonnull;
1314
import java.io.File;
1415
import java.io.IOException;
16+
import java.net.URL;
1517
import java.util.Collection;
1618
import java.util.Collections;
1719

1820
import static com.github.kostyasha.github.integration.branch.utils.JobHelper.ghBranchTriggerFromJob;
19-
import static org.jenkinsci.plugins.github.pullrequest.utils.ObjectsUtil.isNull;
21+
import static java.util.Objects.requireNonNull;
2022
import static org.jenkinsci.plugins.github.pullrequest.utils.ObjectsUtil.nonNull;
2123

2224
/**
25+
* Create GitHubBranchRepository.
26+
* Don't depend on remote connection because updateTransientActions() called only once and connection may appear later.
27+
*
2328
* @author Kanstantsin Shautsou
2429
*/
2530
@Extension
@@ -46,31 +51,34 @@ private static GitHubBranchRepository forProject(Job<?, ?> job) throws IOExcepti
4651
XmlFile configFile = new XmlFile(new File(job.getRootDir(), GitHubBranchRepository.FILE));
4752

4853
GitHubBranchTrigger trigger = ghBranchTriggerFromJob(job);
54+
requireNonNull(trigger, "Can't extract Branch trigger from " + job.getFullName());
55+
56+
final GitHubRepositoryName repoFullName = trigger.getRepoFullName(job); // ask with job because trigger may not yet be started
57+
GithubProjectProperty property = job.getProperty(GithubProjectProperty.class);
58+
String githubUrl = property.getProjectUrl().toString();
4959

5060
GitHubBranchRepository localRepository;
5161
if (configFile.exists()) {
5262
try {
5363
localRepository = (GitHubBranchRepository) configFile.read();
5464
} catch (IOException e) {
55-
LOGGER.info("Can't read saved repository, creating new one", e);
56-
final GHRepository remoteRepository = trigger.getRemoteRepository();
57-
localRepository = new GitHubBranchRepository(remoteRepository);
65+
LOGGER.info("Can't read saved repository, re-creating new one", e);
66+
localRepository = new GitHubBranchRepository(repoFullName.toString(), new URL(githubUrl));
5867
}
5968
} else {
60-
final GHRepository remoteRepository = trigger.getRemoteRepository();
61-
localRepository = new GitHubBranchRepository(remoteRepository);
69+
LOGGER.info("Creating new Branch Repository for '{}'", job.getFullName());
70+
localRepository = new GitHubBranchRepository(repoFullName.toString(), new URL(githubUrl));
6271
}
6372

6473
// set transient cached fields
6574
localRepository.setJob(job);
6675
localRepository.setConfigFile(configFile);
6776

68-
if (isNull(localRepository.getGitUrl()) ||
69-
isNull(localRepository.getSshUrl())) {
70-
final GHRepository remoteRepository = trigger.getRemoteRepository();
71-
localRepository.withGitUrl(remoteRepository.getGitTransportUrl())
72-
.withSshUrl(remoteRepository.getSshUrl());
77+
try {
78+
localRepository.actualise(trigger.getRemoteRepository());
7379
localRepository.save();
80+
} catch (Throwable ignore) {
81+
//silently try actualise
7482
}
7583

7684
return localRepository;

github-pullrequest-plugin/src/main/java/com/github/kostyasha/github/integration/branch/GitHubBranchTrigger.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,19 +184,25 @@ public void doRun(String branch) {
184184
listener.debug("Running GitHub Branch trigger check for {} on {}",
185185
getDateTimeInstance().format(new Date(startTime)), localRepository.getFullName());
186186

187-
causes = readyToBuildCauses(localRepository, listener, branch);
187+
try {
188+
localRepository.actualise(getRemoteRepository());
188189

189-
localRepository.saveQuietly();
190+
causes = readyToBuildCauses(localRepository, listener, branch);
191+
192+
localRepository.saveQuietly();
193+
194+
// TODO print triggering to listener?
195+
from(causes).filter(new JobRunnerForBranchCause(job, this)).toSet();
196+
} catch (Throwable t) {
197+
listener.error("Can't end trigger check", t);
198+
}
190199

191200
long duration = System.currentTimeMillis() - startTime;
192201
listener.info(FINISH_MSG + " for {} at {}. Duration: {}ms",
193202
localRepository.getFullName(), getDateTimeInstance().format(new Date()), duration);
194203
} catch (Exception e) {
195204
LOG.error("Can't process check: {}", e);
196-
return;
197205
}
198-
199-
from(causes).filter(new JobRunnerForBranchCause(job, this)).toSet();
200206
}
201207

202208
/**

github-pullrequest-plugin/src/main/java/com/github/kostyasha/github/integration/generic/GitHubRepository.java

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212
import org.slf4j.Logger;
1313
import org.slf4j.LoggerFactory;
1414

15+
import javax.annotation.CheckForNull;
1516
import javax.annotation.Nonnull;
1617
import java.io.IOException;
1718
import java.net.URL;
1819

20+
import static java.util.Objects.isNull;
21+
1922
/**
2023
* @author Kanstantsin Shautsou
2124
*/
@@ -25,26 +28,62 @@ public abstract class GitHubRepository<T extends GitHubRepository> implements Ac
2528
protected transient XmlFile configFile; // for save()
2629
protected transient Job<?, ?> job; // for UI
2730

28-
private final String fullName;
29-
private final URL githubUrl;
31+
@CheckForNull
32+
private String fullName;
33+
@CheckForNull
34+
private URL githubUrl;
35+
36+
@CheckForNull
3037
private String gitUrl;
38+
@CheckForNull
3139
private String sshUrl;
3240

3341
public GitHubRepository(@Nonnull GHRepository ghRepository) {
34-
fullName = ghRepository.getFullName();
35-
githubUrl = ghRepository.getHtmlUrl();
36-
gitUrl = ghRepository.getGitTransportUrl();
37-
sshUrl = ghRepository.getSshUrl();
42+
actualise(ghRepository);
43+
}
44+
45+
public GitHubRepository(String repoFullName, URL githubUrl) {
46+
this.fullName = repoFullName;
47+
this.githubUrl = githubUrl;
48+
}
49+
50+
/**
51+
* Repository may be created without gh connection, but trigger logic expects this fields.
52+
* Should be called before trigger logic starts checks.
53+
*/
54+
public void actualise(@Nonnull GHRepository ghRepository) {
55+
if (isNull(fullName)) {
56+
fullName = ghRepository.getFullName();
57+
}
58+
if (isNull(githubUrl)) {
59+
githubUrl = ghRepository.getHtmlUrl();
60+
}
61+
if (isNull(gitUrl)) {
62+
gitUrl = ghRepository.getGitTransportUrl();
63+
}
64+
if (isNull(sshUrl)) {
65+
sshUrl = ghRepository.getSshUrl();
66+
}
3867
}
3968

4069
public String getFullName() {
4170
return fullName;
4271
}
4372

73+
public GitHubRepository<T> withFullName(String fullName) {
74+
this.fullName = fullName;
75+
return this;
76+
}
77+
4478
public URL getGithubUrl() {
4579
return githubUrl;
4680
}
4781

82+
public GitHubRepository<T> withGithubUrl(URL githubUrl) {
83+
this.githubUrl = githubUrl;
84+
return this;
85+
}
86+
4887
public String getGitUrl() {
4988
return gitUrl;
5089
}

github-pullrequest-plugin/src/main/java/org/jenkinsci/plugins/github/pullrequest/GitHubPRRepository.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import javax.annotation.Nonnull;
2222
import java.io.IOException;
23+
import java.net.URL;
2324
import java.util.ArrayList;
2425
import java.util.HashMap;
2526
import java.util.List;
@@ -53,6 +54,10 @@ public GitHubPRRepository(@Nonnull GHRepository ghRepository) {
5354
super(ghRepository);
5455
}
5556

57+
public GitHubPRRepository(String repoFullName, URL githubUrl) {
58+
super(repoFullName, githubUrl);
59+
}
60+
5661
@Nonnull
5762
public Map<Integer, GitHubPRPullRequest> getPulls() {
5863
if (isNull(pulls)) {

github-pullrequest-plugin/src/main/java/org/jenkinsci/plugins/github/pullrequest/GitHubPRRepositoryFactory.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
11
package org.jenkinsci.plugins.github.pullrequest;
22

3+
import com.cloudbees.jenkins.GitHubRepositoryName;
4+
import com.coravy.hudson.plugins.github.GithubProjectProperty;
35
import com.github.kostyasha.github.integration.generic.GitHubRepositoryFactory;
46
import hudson.Extension;
57
import hudson.XmlFile;
68
import hudson.model.Action;
79
import hudson.model.Job;
8-
import org.kohsuke.github.GHRepository;
910
import org.slf4j.Logger;
1011
import org.slf4j.LoggerFactory;
1112

1213
import javax.annotation.Nonnull;
1314
import java.io.File;
1415
import java.io.IOException;
16+
import java.net.URL;
1517
import java.util.Collection;
1618
import java.util.Collections;
1719

1820
import static java.util.Collections.singleton;
1921
import static java.util.Objects.requireNonNull;
2022
import static org.jenkinsci.plugins.github.pullrequest.utils.JobHelper.ghPRTriggerFromJob;
21-
import static org.jenkinsci.plugins.github.pullrequest.utils.ObjectsUtil.isNull;
2223
import static org.jenkinsci.plugins.github.pullrequest.utils.ObjectsUtil.nonNull;
2324

2425
/**
26+
* Create GitHubPRRepository.
27+
* Don't depend on remote connection because updateTransientActions() called only once and connection may appear later.
28+
*
2529
* @author Kanstantsin Shautsou
2630
*/
2731
@Extension
@@ -49,31 +53,30 @@ private static GitHubPRRepository forProject(Job<?, ?> job) throws IOException {
4953
GitHubPRTrigger trigger = ghPRTriggerFromJob(job);
5054
requireNonNull(trigger, "Can't extract PR trigger from " + job.getFullName());
5155

56+
final GitHubRepositoryName repoFullName = trigger.getRepoFullName(job); // ask with job because trigger may not yet be started
57+
GithubProjectProperty property = job.getProperty(GithubProjectProperty.class);
58+
String githubUrl = property.getProjectUrl().toString();
59+
5260
GitHubPRRepository localRepository;
5361
if (configFile.exists()) {
5462
try {
5563
localRepository = (GitHubPRRepository) configFile.read();
5664
} catch (IOException e) {
57-
LOGGER.info("Can't read saved repository, creating new one", e);
58-
final GHRepository ghRepository = trigger.getRemoteRepository();
59-
requireNonNull(ghRepository, "Can't get remote GH repository.");
60-
localRepository = new GitHubPRRepository(ghRepository);
65+
LOGGER.info("Can't read saved repository, re-creating new one", e);
66+
localRepository = new GitHubPRRepository(repoFullName.toString(), new URL(githubUrl));
6167
}
6268
} else {
63-
final GHRepository ghRepository = trigger.getRemoteRepository();
64-
requireNonNull(ghRepository, "Can't get remote GH repository.");
65-
localRepository = new GitHubPRRepository(ghRepository);
69+
localRepository = new GitHubPRRepository(repoFullName.toString(), new URL(githubUrl));
6670
}
6771

6872
localRepository.setJob(job);
6973
localRepository.setConfigFile(configFile);
7074

71-
if (isNull(localRepository.getGitUrl()) ||
72-
isNull(localRepository.getSshUrl())) {
73-
final GHRepository remoteRepository = trigger.getRemoteRepository();
74-
localRepository.withGitUrl(remoteRepository.getGitTransportUrl())
75-
.withSshUrl(remoteRepository.getSshUrl());
75+
try {
76+
localRepository.actualise(trigger.getRemoteRepository());
7677
localRepository.save();
78+
} catch (Throwable ignore) {
79+
//silently try actualise
7780
}
7881

7982
return localRepository;

github-pullrequest-plugin/src/main/java/org/jenkinsci/plugins/github/pullrequest/GitHubPRTrigger.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,20 +226,26 @@ public void doRun(Integer prNumber) {
226226
long startTime = System.currentTimeMillis();
227227
listener.debug("Running GitHub Pull Request trigger check for {} on {}",
228228
getDateTimeInstance().format(new Date(startTime)), localRepository.getFullName());
229+
try {
230+
localRepository.actualise(getRemoteRepository());
229231

230-
causes = readyToBuildCauses(localRepository, listener, prNumber);
232+
causes = readyToBuildCauses(localRepository, listener, prNumber);
231233

232-
localRepository.saveQuietly();
234+
localRepository.saveQuietly();
235+
236+
// TODO print triggering to listener?
237+
from(causes).filter(new JobRunnerForCause(job, this)).toSet();
238+
} catch (Throwable t) {
239+
listener.error("Can't end trigger check", t);
240+
}
233241

234242
long duration = System.currentTimeMillis() - startTime;
235243
listener.info(FINISH_MSG + " for {} at {}. Duration: {}ms",
236244
localRepository.getFullName(), getDateTimeInstance().format(new Date()), duration);
237245
} catch (Exception e) {
246+
// out of UI/user viewable error
238247
LOGGER.error("Can't process check ({})", e.getMessage(), e);
239-
return;
240248
}
241-
242-
from(causes).filter(new JobRunnerForCause(job, this)).toSet();
243249
}
244250

245251
/**

0 commit comments

Comments
 (0)