Skip to content
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

Support bamboo 6.4.1 and enterprise github #29

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
target
.idea/
*.iml
14 changes: 11 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,20 @@
</scm>

<properties>
<bamboo.version>5.15.3</bamboo.version>
<bamboo.data.version>5.15.3</bamboo.data.version>
<amps.version>6.2.11</amps.version>
<bamboo.version>6.4.1</bamboo.version>
<bamboo.data.version>6.4.1</bamboo.data.version>
<amps.version>6.3.15</amps.version>
<plugin.testrunner.version>1.2.3</plugin.testrunner.version>
</properties>

<repositories>
<repository>
<id>atlassian_maven</id>
<name>atlassian maven</name>
<url>https://maven.atlassian.com/repository/public</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>org.kohsuke</groupId>
Expand Down
116 changes: 85 additions & 31 deletions src/main/java/com/mhackner/bamboo/AbstractGitHubStatusAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,25 @@
import com.atlassian.bamboo.plan.PlanResultKey;
import com.atlassian.bamboo.plan.cache.ImmutableChain;
import com.atlassian.bamboo.plugins.git.GitHubRepository;
import com.atlassian.bamboo.repository.RepositoryDefinition;
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);
Expand All @@ -47,51 +53,78 @@ void updateStatus(GHCommitState status, StageExecution stageExecution) {
PlanKey planKey = planResultKey.getPlanKey();
ImmutableChain chain = (ImmutableChain) planManager.getPlanByKey(planKey);

for (RepositoryDefinition repo : Configuration.ghReposFrom(chain)) {
for (PlanRepositoryDefinition repo : Configuration.getPlanRepositories(chain)) {

if (shouldUpdateRepo(chain, repo)) {
String sha = chainExecution.getBuildChanges().getVcsRevisionKey(repo.getId());
if (sha != null) {
GitHubRepository ghRepo = (GitHubRepository) repo.getRepository();
setStatus(ghRepo, status, sha, planResultKey.getKey(), stageExecution.getName());
setStatus(repo.asLegacyData().getRepository(), status, sha, planResultKey.getKey(), stageExecution.getName());
}
} else {
log.debug("Should not update repo: {}", repo.getName());
}
}
}

private static boolean shouldUpdateRepo(ImmutableChain chain, final RepositoryDefinition repo) {
String config = chain.getBuildDefinition().getCustomConfiguration()
.get(Configuration.CONFIG_KEY);
if (config == null) {
return Configuration.DEFAULT_REPO_PREDICATE.apply(repo);
} else {
RepositoryDefinition repoToCheck = chain.hasMaster()
? Iterables.find(
Configuration.ghReposFrom(chain.getMaster()),
new Predicate<RepositoryDefinition>() {
@Override
public boolean apply(RepositoryDefinition input) {
return input.getName().equals(repo.getName());
}
})
: repo;

return Configuration.toList(config).contains(repoToCheck.getId());
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(GitHubRepository repo, GHCommitState status, String sha,
private void setStatus(Repository repo, GHCommitState status, String sha,
String planResultKey, String context) {
String url = bambooUrl.withBaseUrlFromConfiguration("/browse/" + planResultKey);
try {
String password;
try {
password = repo.getClass().getDeclaredMethod("getPassword").invoke(repo).toString();
} catch (NoSuchMethodException ex) {
password = encryptionService.decrypt(
repo.getClass().getDeclaredMethod("getEncryptedPassword").invoke(repo).toString());
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);
}
GitHub gitHub = GitHub.connectToEnterprise(gitHubEndpoint, repo.getUsername(), password);
GHRepository repository = gitHub.getRepository(repo.getRepository());

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);
Expand All @@ -100,4 +133,25 @@ private void setStatus(GitHubRepository repo, GHCommitState status, String sha,
}
}

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;
}
}
}
Loading