-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathAbstractGitHubStatusAction.java
More file actions
157 lines (135 loc) · 7.04 KB
/
AbstractGitHubStatusAction.java
File metadata and controls
157 lines (135 loc) · 7.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package com.mhackner.bamboo;
import com.atlassian.bamboo.chains.ChainExecution;
import com.atlassian.bamboo.chains.StageExecution;
import com.atlassian.bamboo.configuration.AdministrationConfigurationAccessor;
import com.atlassian.bamboo.plan.PlanKey;
import com.atlassian.bamboo.plan.PlanManager;
import com.atlassian.bamboo.plan.PlanResultKey;
import com.atlassian.bamboo.plan.cache.ImmutableChain;
import com.atlassian.bamboo.plugins.git.GitHubRepository;
import com.atlassian.bamboo.plugins.git.GitRepository;
import com.atlassian.bamboo.repository.Repository;
import com.atlassian.bamboo.security.EncryptionService;
import com.atlassian.bamboo.utils.BambooUrl;
import com.atlassian.bamboo.utils.SystemProperty;
import com.atlassian.bamboo.vcs.configuration.PlanRepositoryDefinition;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Iterables;
import org.kohsuke.github.GHCommitState;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
public abstract class AbstractGitHubStatusAction {
private static final Logger log = LoggerFactory.getLogger(AbstractGitHubStatusAction.class);
private static final String gitHubEndpoint =
new SystemProperty(false, "atlassian.bamboo.github.api.base.url",
"ATLASSIAN_BAMBOO_GITHUB_API_BASE_URL").getValue("https://api.github.com");
private final EncryptionService encryptionService;
private final BambooUrl bambooUrl;
private final PlanManager planManager;
AbstractGitHubStatusAction(AdministrationConfigurationAccessor adminConfigAccessor,
EncryptionService encryptionService,
PlanManager planManager) {
this.encryptionService = encryptionService;
bambooUrl = new BambooUrl(adminConfigAccessor);
this.planManager = planManager;
}
void updateStatus(GHCommitState status, StageExecution stageExecution) {
ChainExecution chainExecution = stageExecution.getChainExecution();
PlanResultKey planResultKey = chainExecution.getPlanResultKey();
PlanKey planKey = planResultKey.getPlanKey();
ImmutableChain chain = (ImmutableChain) planManager.getPlanByKey(planKey);
for (PlanRepositoryDefinition repo : Configuration.getPlanRepositories(chain)) {
if (shouldUpdateRepo(chain, repo)) {
String sha = chainExecution.getBuildChanges().getVcsRevisionKey(repo.getId());
if (sha != null) {
setStatus(repo.asLegacyData().getRepository(), status, sha, planResultKey.getKey(), stageExecution.getName());
}
} else {
log.debug("Should not update repo: {}", repo.getName());
}
}
}
static boolean shouldUpdateRepo(ImmutableChain chain, final PlanRepositoryDefinition repo) {
PlanRepositoryDefinition repoToCheck = repo;
if (chain.hasMaster()) {
repoToCheck = FluentIterable.from(Configuration.getPlanRepositories(chain.getMaster()))
.firstMatch(new Predicate<PlanRepositoryDefinition>() {
@Override
public boolean apply(PlanRepositoryDefinition input) {
boolean result = input.getName().equals(repo.getName());
if (result) {
try {
result = isTargetGithubRepository(input);
}
catch (MalformedURLException ex) {
throw new RuntimeException("Failed checking repository definition hostname", ex);
}
}
return result;
}
})
.or(repo);
}
return Configuration.DEFAULT_REPO_PREDICATE.apply(repoToCheck) ||
Configuration.isRepositorySelected(chain.getBuildDefinition().getCustomConfiguration(), repoToCheck.getId());
}
private void setStatus(Repository repo, GHCommitState status, String sha,
String planResultKey, String context) {
String url = bambooUrl.withBaseUrlFromConfiguration("/browse/" + planResultKey);
try {
String password;
String username;
String repositoryUrl;
if (repo instanceof GitHubRepository) {
GitHubRepository gitHubRepository = (GitHubRepository) repo;
try {
password = gitHubRepository.getClass().getDeclaredMethod("getPassword").invoke(gitHubRepository).toString();
} catch (NoSuchMethodException ex) {
password = encryptionService.decrypt(
gitHubRepository.getClass().getDeclaredMethod("getEncryptedPassword").invoke(gitHubRepository).toString());
}
username = gitHubRepository.getUsername();
repositoryUrl = gitHubRepository.getRepository();
} else {
GitRepository gitRepository = (GitRepository) repo;
password = gitRepository.getAccessData().getPassword();
username = gitRepository.getAccessData().getUsername();
repositoryUrl = gitRepository.getAccessData().getRepositoryUrl();
repositoryUrl = getRelativePath(repositoryUrl);
}
log.info(String.format("Connecting to github ... username = %s, password = %s, repositoryUrl = %s",
username, password, repositoryUrl));
GitHub gitHub = GitHub.connectToEnterprise(gitHubEndpoint, username, password);
GHRepository repository = gitHub.getRepository(repositoryUrl);
sha = repository.getCommit(sha).getSHA1();
repository.createCommitStatus(sha, status, url, null, context);
log.info("GitHub status for commit {} ({}) set to {}.", sha, context, status);
} catch (Exception ex) {
log.error("Failed to update GitHub status", ex);
}
}
private static String getRelativePath(String url) throws MalformedURLException {
String path = new URL(url).getPath();
if (path.startsWith("/")) {
path = path.substring(1);
}
return path.replace(".git", "");
}
static boolean isTargetGithubRepository(PlanRepositoryDefinition repositoryDefinition) throws MalformedURLException {
Repository repository = repositoryDefinition.asLegacyData().getRepository();
if (repository instanceof GitRepository) {
GitRepository gitRepository = (GitRepository) repository;
URL repositoryUrl = new URL(gitRepository.getAccessData().getRepositoryUrl());
URL githubUrl = new URL(gitHubEndpoint);
return repositoryUrl.getHost().toLowerCase().equals(githubUrl.getHost().toLowerCase());
} else {
return true;
}
}
}