Skip to content

Commit 39890ae

Browse files
authored
Refresh urls, react on repository org/repo change (#271)
* Support repochanges... * WIP * WIP * undo * cleanup * WIP * For multibranch also * Fix test * Fix trigger cache + test * undo * fix test
1 parent e60e32d commit 39890ae

26 files changed

Lines changed: 509 additions & 47 deletions

File tree

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import hudson.model.Item;
66
import hudson.model.Result;
77
import hudson.model.Run;
8+
import hudson.model.TaskListener;
89
import hudson.model.queue.QueueTaskFuture;
910
import hudson.util.FormValidation;
1011
import hudson.util.RunList;
@@ -48,7 +49,7 @@ public class GitHubBranchRepository extends GitHubRepository<GitHubBranchReposit
4849
*
4950
* @param remoteRepository remote repository full name.
5051
*/
51-
public GitHubBranchRepository(GHRepository remoteRepository) {
52+
public GitHubBranchRepository(GHRepository remoteRepository) throws IOException {
5253
super(remoteRepository);
5354
}
5455

@@ -62,6 +63,14 @@ public Map<String, GitHubBranch> getBranches() {
6263
return branches;
6364
}
6465

66+
@Override
67+
public void actualiseOnChange(@Nonnull GHRepository ghRepository, @Nonnull TaskListener listener) {
68+
if (changed) {
69+
listener.getLogger().println("Local settings changed, removing branches in repository!");
70+
getBranches().clear();
71+
}
72+
}
73+
6574
@Override
6675
public String getIconFileName() {
6776
return GitHubBranch.getIconFileName();

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import hudson.XmlFile;
88
import hudson.model.Action;
99
import hudson.model.Job;
10+
import hudson.model.TaskListener;
11+
import org.jenkinsci.plugins.github.pullrequest.GitHubPRTrigger;
1012
import org.slf4j.Logger;
1113
import org.slf4j.LoggerFactory;
1214

@@ -27,6 +29,7 @@
2729
*
2830
* @author Kanstantsin Shautsou
2931
*/
32+
@SuppressWarnings("unused")
3033
@Extension
3134
public class GitHubBranchRepositoryFactory
3235
extends GitHubRepositoryFactory<GitHubBranchRepositoryFactory, GitHubBranchTrigger> {
@@ -58,29 +61,38 @@ private static GitHubBranchRepository forProject(Job<?, ?> job) throws IOExcepti
5861
String githubUrl = property.getProjectUrl().toString();
5962

6063
GitHubBranchRepository localRepository;
64+
boolean created = false;
6165
if (configFile.exists()) {
6266
try {
6367
localRepository = (GitHubBranchRepository) configFile.read();
6468
} catch (IOException e) {
6569
LOGGER.info("Can't read saved repository, re-creating new one", e);
6670
localRepository = new GitHubBranchRepository(repoFullName.toString(), new URL(githubUrl));
71+
created = true;
6772
}
6873
} else {
6974
LOGGER.info("Creating new Branch Repository for '{}'", job.getFullName());
7075
localRepository = new GitHubBranchRepository(repoFullName.toString(), new URL(githubUrl));
76+
created = true;
7177
}
7278

7379
// set transient cached fields
7480
localRepository.setJob(job);
7581
localRepository.setConfigFile(configFile);
7682

77-
try {
78-
localRepository.actualise(trigger.getRemoteRepository());
79-
localRepository.save();
80-
} catch (Throwable ignore) {
81-
//silently try actualise
83+
84+
GitHubPRTrigger.DescriptorImpl prTriggerDescriptor = GitHubPRTrigger.DescriptorImpl.get();
85+
if (prTriggerDescriptor.isActualiseOnFactory()) {
86+
try {
87+
localRepository.actualise(trigger.getRemoteRepository(), TaskListener.NULL);
88+
created = true;
89+
} catch (Throwable ignore) {
90+
//silently try actualise
91+
}
8292
}
8393

94+
if (created) localRepository.save();
95+
8496
return localRepository;
8597
}
8698

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public void doRun(String branch) {
191191
getDateTimeInstance().format(new Date(startTime)), localRepository.getFullName());
192192

193193
try {
194-
localRepository.actualise(getRemoteRepository());
194+
localRepository.actualise(getRemoteRepository(), listener);
195195

196196
causes = readyToBuildCauses(localRepository, listener, branch);
197197

@@ -200,7 +200,7 @@ public void doRun(String branch) {
200200
// TODO print triggering to listener?
201201
from(causes).filter(new JobRunnerForBranchCause(job, this)).toSet();
202202
} catch (Throwable t) {
203-
listener.error("Can't end trigger check: '{}'", t);
203+
listener.error("Can't end trigger check!", t);
204204
}
205205

206206
long duration = System.currentTimeMillis() - startTime;
@@ -219,6 +219,11 @@ private List<GitHubBranchCause> readyToBuildCauses(GitHubBranchRepository localR
219219
@Nullable String requestedBranch) {
220220
try {
221221
GitHub github = getRepoProvider().getGitHub(this);
222+
if (isNull(github)) {
223+
LOG.error("GitHub connection is null, check Repo Providers!");
224+
throw new IllegalStateException("GitHub connection is null, check Repo Providers!");
225+
}
226+
222227
GHRateLimit rateLimitBefore = github.getRateLimit();
223228
listener.debug("GitHub rate limit before check: {}", rateLimitBefore);
224229

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

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import hudson.model.Action;
66
import hudson.model.Job;
77
import hudson.model.Saveable;
8+
import hudson.model.TaskListener;
89
import hudson.model.listeners.SaveableListener;
910
import hudson.util.FormValidation;
1011
import org.kohsuke.github.GHRepository;
@@ -15,6 +16,7 @@
1516
import javax.annotation.CheckForNull;
1617
import javax.annotation.Nonnull;
1718
import java.io.IOException;
19+
import java.io.PrintStream;
1820
import java.net.URL;
1921

2022
import static java.util.Objects.isNull;
@@ -29,6 +31,7 @@ public abstract class GitHubRepository<T extends GitHubRepository> implements Ac
2931

3032
protected transient XmlFile configFile; // for save()
3133
protected transient Job<?, ?> job; // for UI
34+
protected transient boolean changed; // for actualisation
3235

3336
@CheckForNull
3437
private String fullName;
@@ -40,8 +43,8 @@ public abstract class GitHubRepository<T extends GitHubRepository> implements Ac
4043
@CheckForNull
4144
private String sshUrl;
4245

43-
public GitHubRepository(@Nonnull GHRepository ghRepository) {
44-
actualise(ghRepository);
46+
public GitHubRepository(@Nonnull GHRepository ghRepository) throws IOException {
47+
actualise(ghRepository, TaskListener.NULL);
4548
}
4649

4750
public GitHubRepository(String repoFullName, URL githubUrl) {
@@ -53,21 +56,38 @@ public GitHubRepository(String repoFullName, URL githubUrl) {
5356
* Repository may be created without gh connection, but trigger logic expects this fields.
5457
* Should be called before trigger logic starts checks.
5558
*/
56-
public void actualise(@Nonnull GHRepository ghRepository) {
57-
if (isNull(fullName)) {
59+
public void actualise(@Nonnull GHRepository ghRepository, @Nonnull TaskListener listener) throws IOException {
60+
changed = false;
61+
62+
PrintStream logger = listener.getLogger();
63+
// just in case your organisation decided to change domain
64+
// take into account only repo/name
65+
if (isNull(fullName) || !fullName.equals(ghRepository.getFullName())) {
66+
logger.printf("Repository full name changed from '%s' to '%s'.\n", fullName, ghRepository.getFullName());
5867
fullName = ghRepository.getFullName();
68+
changed = true;
5969
}
60-
if (isNull(githubUrl)) {
70+
71+
if (isNull(githubUrl) || !githubUrl.equals(ghRepository.getHtmlUrl())) {
72+
logger.printf("Changing GitHub url from '%s' to '%s'.\n", githubUrl, ghRepository.getHtmlUrl());
6173
githubUrl = ghRepository.getHtmlUrl();
6274
}
63-
if (isNull(gitUrl)) {
75+
76+
if (isNull(gitUrl) || !gitUrl.equals(ghRepository.getGitTransportUrl())) {
77+
logger.printf("Changing Git url from '%s' to '%s'.\n", gitUrl, ghRepository.getGitTransportUrl());
6478
gitUrl = ghRepository.getGitTransportUrl();
6579
}
66-
if (isNull(sshUrl)) {
80+
81+
if (isNull(sshUrl) || !sshUrl.equals(ghRepository.getSshUrl())) {
82+
logger.printf("Changing SSH url from '%s' to '%s'.\n", sshUrl, ghRepository.getSshUrl());
6783
sshUrl = ghRepository.getSshUrl();
6884
}
85+
86+
actualiseOnChange(ghRepository, listener);
6987
}
7088

89+
protected abstract void actualiseOnChange(@Nonnull GHRepository ghRepository, @Nonnull TaskListener listener);
90+
7191
public String getFullName() {
7292
return fullName;
7393
}

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ public void setRepoName(GitHubRepositoryName repoName) {
108108
this.repoName = repoName;
109109
}
110110

111+
@Override
112+
public void start(Job<?, ?> project, boolean newInstance) {
113+
repoName = null; // reset cache
114+
super.start(project, newInstance);
115+
}
116+
111117
@Beta
112118
@Nonnull
113119
public List<GitHubRepoProvider> getRepoProviders() {
@@ -152,11 +158,12 @@ public GitHubRepoProvider getRepoProvider() {
152158

153159
if (isNull(repoProvider)) {
154160
getErrorsAction().addOrReplaceError(new GitHubRepoProviderError(
155-
String.format("Can't find repo provider for %s.<br/> All providers failed: %s", job.getName(), throwables)
161+
String.format("Can't find repo provider for %s.<br/> All providers failed: %s",
162+
job.getFullName(), throwables)
156163
));
157164
}
158165

159-
checkState(nonNull(repoProvider), "Can't find repo provider for %s", job.getName());
166+
checkState(nonNull(repoProvider), "Can't find repo provider for %s", job.getFullName());
160167
getErrorsAction().removeErrors(GitHubRepoProviderError.class);
161168

162169
return repoProvider;
@@ -165,7 +172,7 @@ public GitHubRepoProvider getRepoProvider() {
165172
@Nonnull
166173
public GHRepository getRemoteRepository() throws IOException {
167174
GHRepository remoteRepository = getRepoProvider().getGHRepository(this);
168-
checkState(nonNull(remoteRepository), "Can't get remote GH repo for %s", job.getName());
175+
checkState(nonNull(remoteRepository), "Can't get remote GH repo for %s", job.getFullName());
169176
return remoteRepository;
170177
}
171178

@@ -179,6 +186,7 @@ public GitHubErrorsAction getErrorsAction() {
179186

180187
@Override
181188
public void stop() {
189+
repoName = null;
182190
//TODO clean hooks?
183191
if (nonNull(job)) {
184192
LOG.info("Stopping '{}' for project '{}'", getDescriptor().getDisplayName(), job.getFullName());

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public SequentialExecutionQueue getQueue() {
4141

4242
private String publishedURL;
4343

44+
private boolean actualiseOnFactory = false;
45+
4446
public String getPublishedURL() {
4547
return publishedURL;
4648
}
@@ -49,6 +51,14 @@ public void setPublishedURL(String publishedURL) {
4951
this.publishedURL = publishedURL;
5052
}
5153

54+
public boolean isActualiseOnFactory() {
55+
return actualiseOnFactory;
56+
}
57+
58+
public void setActualiseOnFactory(boolean actualiseOnFactory) {
59+
this.actualiseOnFactory = actualiseOnFactory;
60+
}
61+
5262
public String getJenkinsURL() {
5363
String url = getPublishedURL();
5464
if (isNotBlank(url)) {

github-pullrequest-plugin/src/main/java/com/github/kostyasha/github/integration/multibranch/action/GitHubRepo.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.github.kostyasha.github.integration.branch.GitHubBranchRepository;
44
import com.github.kostyasha.github.integration.tag.GitHubTagRepository;
55
import hudson.model.Action;
6+
import hudson.model.TaskListener;
67
import org.jenkinsci.plugins.github.pullrequest.GitHubPRRepository;
78
import org.kohsuke.github.GHRepository;
89
import org.slf4j.Logger;
@@ -36,7 +37,7 @@ public GitHubRepo(GitHubSCMSourcesLocalStorage owner) {
3637
/**
3738
* When remote side available.
3839
*/
39-
public GitHubRepo(GHRepository repository) {
40+
public GitHubRepo(GHRepository repository) throws IOException {
4041
this(new GitHubBranchRepository(repository), new GitHubTagRepository(repository), new GitHubPRRepository(repository));
4142
}
4243

@@ -86,12 +87,19 @@ public String getUrlName() {
8687
public void actualize(GHRepository remoteRepo) throws IOException {
8788
if (isNull(branchRepository)) {
8889
branchRepository = new GitHubBranchRepository(remoteRepo);
90+
} else {
91+
branchRepository.actualise(remoteRepo, TaskListener.NULL);
8992
}
93+
9094
if (isNull(tagRepository)) {
9195
tagRepository = new GitHubTagRepository(remoteRepo);
96+
} else {
97+
tagRepository.actualise(remoteRepo, TaskListener.NULL);
9298
}
9399
if (isNull(prRepository)) {
94100
prRepository = new GitHubPRRepository(remoteRepo);
101+
} else {
102+
prRepository.actualise(remoteRepo, TaskListener.NULL);
95103
}
96104

97105
if (owner != null) {

github-pullrequest-plugin/src/main/java/com/github/kostyasha/github/integration/tag/GitHubTagRepository.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.kostyasha.github.integration.tag;
22

33
import com.github.kostyasha.github.integration.generic.GitHubRepository;
4+
import hudson.model.TaskListener;
45
import hudson.util.FormValidation;
56
import org.kohsuke.github.GHRepository;
67
import org.kohsuke.stapler.StaplerRequest;
@@ -30,7 +31,7 @@ public class GitHubTagRepository extends GitHubRepository<GitHubTagRepository> {
3031
*
3132
* @param remoteRepository remote repository full name.
3233
*/
33-
public GitHubTagRepository(GHRepository remoteRepository) {
34+
public GitHubTagRepository(GHRepository remoteRepository) throws IOException {
3435
super(remoteRepository);
3536
}
3637

@@ -59,6 +60,14 @@ public String getUrlName() {
5960
return "github-tag";
6061
}
6162

63+
@Override
64+
public void actualiseOnChange(@Nonnull GHRepository ghRepository, @Nonnull TaskListener listener) {
65+
if (changed) {
66+
listener.getLogger().println("Local settings changed, removing tags in repository state!");
67+
getTags().clear();
68+
}
69+
}
70+
6271
@Override
6372
public FormValidation doClearRepo() throws IOException {
6473
return FormValidation.ok();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public GitHubPRPullRequest(GHPullRequest pr) {
106106

107107
// see https://github.com/kohsuke/github-api/issues/111
108108
try {
109-
mergeable = execute(() -> pr.getMergeable());
109+
mergeable = execute(pr::getMergeable);
110110
} catch (IOException e) {
111111
LOGGER.error("Can't get mergeable status.", e);
112112
mergeable = false;

0 commit comments

Comments
 (0)