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

PR for IntegrationTests #163

Open
wants to merge 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
import java.util.Optional;
import java.util.concurrent.TimeUnit;

import org.kohsuke.github.GHApp;
import org.kohsuke.github.GHAppCreateTokenBuilder;
import org.kohsuke.github.GHAppInstallation;
import org.kohsuke.github.GHCheckRun;
import org.kohsuke.github.GHCheckRun.Status;
import org.kohsuke.github.GHCheckRunBuilder;
import org.kohsuke.github.GHCommitPointer;
import org.kohsuke.github.GHFileNotFoundException;
import org.kohsuke.github.GHIssueState;
import org.kohsuke.github.GHPermissionType;
import org.kohsuke.github.GHPullRequest;
import org.kohsuke.github.GHRateLimit;
Expand Down Expand Up @@ -51,24 +51,24 @@ public class GithubWebhookHandler implements IGithubWebhookHandler {

private static final Logger LOGGER = LoggerFactory.getLogger(GithubWebhookHandler.class);

final GitHub github;
final GHApp githubApp;

final List<ObjectMapper> objectMappers;

public GithubWebhookHandler(GitHub github, List<ObjectMapper> objectMappers) {
this.github = github;
public GithubWebhookHandler(GHApp githubApp, List<ObjectMapper> objectMappers) {
this.githubApp = githubApp;
this.objectMappers = objectMappers;
}

@Override
public GitHub getGithubAsApp() {
return github;
public GHApp getGithubAsApp() {
return githubApp;
}

@Override
public GithubAndToken makeInstallationGithub(long installationId) {
try {
GHAppInstallation installationById = github.getApp().getInstallationById(installationId);
GHAppInstallation installationById = getGithubAsApp().getInstallationById(installationId);
LOGGER.info("Permissions: {}", installationById.getPermissions());
LOGGER.info("RepositorySelection: {}", installationById.getRepositorySelection());
// https://github.com/hub4j/github-api/issues/570
Expand Down Expand Up @@ -141,7 +141,7 @@ public GithubWebhookRelevancyResult filterWebhookEventRelevant(I3rdPartyWebhookE
if (optAction.isEmpty()) {
throw new IllegalStateException("We miss an action for a webhook holding a pull_request");
} else if ("opened".equals(optAction.get()) || "reopened".equals(optAction.get())) {
String headRef = PepperMapHelper.getRequiredString(input, "head", "ref");
String headRef = PepperMapHelper.getRequiredString(optPullRequest.get(), "head", "ref");
if (headRef.startsWith(GithubRefCleaner.PREFIX_REF_CLEANTHAT)) {
// Do not process CleanThat own PR open events
LOGGER.info("We discard as headRef is: {}", headRef);
Expand All @@ -155,14 +155,16 @@ public GithubWebhookRelevancyResult filterWebhookEventRelevant(I3rdPartyWebhookE
// Some dirty commits may have been pushed while the PR was closed
prOpen = true;
refHasOpenReviewRequest = true;
String baseRepoName = PepperMapHelper.getRequiredString(input, "base", "repo", "full_name");
String baseRef = PepperMapHelper.getRequiredString(input, "base", "ref");
String prId = PepperMapHelper.getRequiredString(optPullRequest.get(), "id");
String baseRepoName =
PepperMapHelper.getRequiredString(optPullRequest.get(), "base", "repo", "full_name");
String baseRef = PepperMapHelper.getRequiredString(optPullRequest.get(), "base", "ref");
long prId = PepperMapHelper.getRequiredNumber(optPullRequest.get(), "id").longValue();
optOpenPr = Optional.of(new GitPrHeadRef(baseRepoName, prId));
String headRepoName = PepperMapHelper.getRequiredString(input, "head", "repo", "full_name");
String baseSha = PepperMapHelper.getRequiredString(input, "base", "sha");
String headRepoName =
PepperMapHelper.getRequiredString(optPullRequest.get(), "head", "repo", "full_name");
String baseSha = PepperMapHelper.getRequiredString(optPullRequest.get(), "base", "sha");
optBaseRef = Optional.of(new GitRepoBranchSha1(baseRepoName, baseRef, baseSha));
String headSha = PepperMapHelper.getRequiredString(input, "head", "sha");
String headSha = PepperMapHelper.getRequiredString(optPullRequest.get(), "head", "sha");
optHeadRef = Optional.of(new GitRepoBranchSha1(headRepoName, headRef, headSha));
} else {
prOpen = false;
Expand Down Expand Up @@ -204,23 +206,28 @@ public GithubWebhookRelevancyResult filterWebhookEventRelevant(I3rdPartyWebhookE
}
pushBranch = true;
String ref = optFullRefName.get();
GitRepoBranchSha1 value =
new GitRepoBranchSha1(PepperMapHelper.getRequiredAs(input, "repository", "full_name"),
ref,
optSha.get());
String repoName = PepperMapHelper.getRequiredAs(input, "repository", "full_name");
GitRepoBranchSha1 value = new GitRepoBranchSha1(repoName, ref, optSha.get());
optHeadRef = Optional.of(value);
try {

Optional<GHPullRequest> prMatchingHead =
new GithubFacade(github, value.getRepoName()).findFirstPrHeadMatchingRef(ref);
optBaseRef = prMatchingHead.map(pr -> {
GHCommitPointer base = pr.getBase();
return new GitRepoBranchSha1(base.getRepository().getName(), base.getRef(), base.getSha());
});
refHasOpenReviewRequest = prMatchingHead.isPresent();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
// We need a Github installation instance to check for this, while current call has to be offline
// (i.e. just analyzing the event)
// try {
// Optional<GHPullRequest> prMatchingHead =
// new GithubFacade(github, value.getRepoName()).findFirstPrHeadMatchingRef(ref);
// optBaseRef = prMatchingHead.map(pr -> {
// GHCommitPointer base = pr.getBase();
// return new GitRepoBranchSha1(base.getRepository().getName(), base.getRef(), base.getSha());
// });
// refHasOpenReviewRequest = prMatchingHead.isPresent();
// } catch (IOException e) {
// throw new UncheckedIOException(e);
// }
optBaseRef = Optional.empty();

// TODO We could set a 'maybe' instead of 'false'
refHasOpenReviewRequest = false;

} else {
// TODO Unclear which case this can be (no pull_request and no action)
LOGGER.warn("WTF We miss at least one of sha1 and refName");
Expand Down Expand Up @@ -285,13 +292,38 @@ public WebhookRelevancyResult filterWebhookEventTargetRelevantBranch(ICodeCleane
} catch (IOException e) {
throw new UncheckedIOException(e);
}

Optional<GitPrHeadRef> optOpenPr = offlineResult.optOpenPr();
if (offlineResult.isPushBranch() && !offlineResult.refHasOpenReviewRequest()) {
assert optOpenPr.isEmpty();

LOGGER.info("Search for a PR merging the commited branch");
try {
String repoName = offlineResult.optPushedRef().get().getRepoName();
Optional<GHPullRequest> prMatchingHead = new GithubFacade(githubAsInst, repoName)
.findFirstPrHeadMatchingRef(offlineResult.optPushedRef().get().getRef());
// Optional<GitRepoBranchSha1> optBaseRef = prMatchingHead.map(pr -> {
// GHCommitPointer base = pr.getBase();
// return new GitRepoBranchSha1(base.getRepository().getName(), base.getRef(), base.getSha());
// });

if (prMatchingHead.isPresent()) {
optOpenPr = prMatchingHead.map(b -> new GitPrHeadRef(repoName, prMatchingHead.get().getId()));
}

// refHasOpenReviewRequest = prMatchingHead.isPresent();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

// String defaultBranch = GitHelper.getDefaultBranch(Optional.ofNullable(repo.getDefaultBranch()));
// final boolean isMainBranchCommit;
GitRepoBranchSha1 theRef;
if (offlineResult.optOpenPr().isPresent()) {
if (optOpenPr.isPresent()) {
Optional<GHPullRequest> optPr;
try {
String rawPrId = String.valueOf(offlineResult.optOpenPr().get().getId());
String rawPrId = String.valueOf(optOpenPr.get().getId());
int prIdAsInteger = Integer.parseInt(rawPrId);
optPr = Optional.of(baseRepo.getPullRequest(prIdAsInteger));
} catch (IOException e) {
Expand Down Expand Up @@ -352,6 +384,7 @@ public void doExecuteWebhookEvent(ICodeCleanerFactory cleanerFactory, IWebhookEv
}
WebhookRelevancyResult relevancyResult =
filterWebhookEventTargetRelevantBranch(cleanerFactory, githubAndBranchAcceptedEvent);

if (relevancyResult.getOptBranchToClean().isEmpty()) {
// TODO May happen if the PR is closed in the meantime
throw new IllegalArgumentException("We should have rejected this earlier");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public IGithubWebhookHandler makeWithFreshJwt() throws IOException, JOSEExceptio
// This leads to 401. Why?
// .withRateLimitChecker(new NoWaitRateLimitChecker())
.build();
return new GithubWebhookHandler(github, objectMappers);
return new GithubWebhookHandler(github.getApp(), objectMappers);
}

// https://connect2id.com/products/nimbus-jose-jwt/examples/jwt-with-rsa-signature
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eu.solven.cleanthat.github.event;

import org.kohsuke.github.GHApp;
import org.kohsuke.github.GitHub;

import eu.solven.cleanthat.github.event.pojo.GithubWebhookRelevancyResult;
Expand All @@ -18,7 +19,7 @@ public interface IGithubWebhookHandler {
*
* @return a {@link GitHub} instance authenticated as the Github Application.
*/
GitHub getGithubAsApp();
GHApp getGithubAsApp();

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
public class RunProcessWebhookEvent {

// Relevant to rely on GitHub.offline()?
final GitHub github = Mockito.mock(GitHub.class);
final GHApp ghApp = Mockito.mock(GHApp.class);

final GitHub installGithub = Mockito.mock(GitHub.class);

Expand All @@ -40,7 +40,7 @@ public class RunProcessWebhookEvent {
final IGithubRefCleaner prCleaner = Mockito.mock(IGithubRefCleaner.class);

final GithubWebhookHandler handler =
new GithubWebhookHandler(github, Arrays.asList(ConfigHelpers.makeJsonObjectMapper())) {
new GithubWebhookHandler(ghApp, Arrays.asList(ConfigHelpers.makeJsonObjectMapper())) {

@Override
protected GitHub makeInstallationGithub(String token) throws IOException {
Expand All @@ -52,8 +52,8 @@ protected GitHub makeInstallationGithub(String token) throws IOException {

@Before
public void initMocks() throws IOException {
GHApp ghApp = Mockito.mock(GHApp.class);
Mockito.when(github.getApp()).thenReturn(ghApp);
// GHApp ghApp = Mockito.mock(GHApp.class);
// Mockito.when(github.getApp()).thenReturn(ghApp);
GHAppInstallation appInstall = Mockito.mock(GHAppInstallation.class);
Mockito.when(ghApp.getInstallationById(Mockito.anyLong())).thenReturn(appInstall);
GHAppCreateTokenBuilder tokenBuilder = Mockito.mock(GHAppCreateTokenBuilder.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
public class TestGithubWebhookHandler {

// Relevant to rely on GitHub.offline()?
final GitHub github = Mockito.mock(GitHub.class);
final GHApp ghApp = Mockito.mock(GHApp.class);

final GitHub installGithub = Mockito.mock(GitHub.class);

Expand All @@ -40,7 +40,7 @@ public class TestGithubWebhookHandler {
final IGithubRefCleaner prCleaner = Mockito.mock(IGithubRefCleaner.class);

final GithubWebhookHandler handler =
new GithubWebhookHandler(github, Arrays.asList(ConfigHelpers.makeJsonObjectMapper())) {
new GithubWebhookHandler(ghApp, Arrays.asList(ConfigHelpers.makeJsonObjectMapper())) {

@Override
protected GitHub makeInstallationGithub(String token) throws IOException {
Expand All @@ -52,8 +52,8 @@ protected GitHub makeInstallationGithub(String token) throws IOException {

@Before
public void initMocks() throws IOException {
GHApp ghApp = Mockito.mock(GHApp.class);
Mockito.when(github.getApp()).thenReturn(ghApp);
// GHApp ghApp = Mockito.mock(GHApp.class);
// Mockito.when(github.getApp()).thenReturn(ghApp);
GHAppInstallation appInstall = Mockito.mock(GHAppInstallation.class);
Mockito.when(ghApp.getInstallationById(Mockito.anyLong())).thenReturn(appInstall);
GHAppCreateTokenBuilder tokenBuilder = Mockito.mock(GHAppCreateTokenBuilder.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ public void doSomethingAfterStartup(ContextRefreshedEvent event) throws IOExcept
GithubWebhookHandlerFactory factory = appContext.getBean(GithubWebhookHandlerFactory.class);
IGithubWebhookHandler handler = factory.makeWithFreshJwt();

GitHub github = handler.getGithubAsApp();
GHApp app = github.getApp();
GHApp app = handler.getGithubAsApp();
LOGGER.info("CleanThat has been installed {} times", app.getInstallationsCount());
app.listInstallations().forEach(installation -> {
long appId = installation.getAppId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Arrays;
import java.util.Map;

import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.kohsuke.github.GHApp;
import org.kohsuke.github.GitHub;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mock.env.MockEnvironment;

import com.google.common.io.Files;
Expand All @@ -23,8 +24,11 @@
import eu.solven.cleanthat.config.ConfigHelpers;
import eu.solven.cleanthat.git_abstraction.GithubFacade;
import eu.solven.cleanthat.github.event.GithubAndToken;
import eu.solven.cleanthat.github.event.GithubWebhookHandler;
import eu.solven.cleanthat.github.event.GithubWebhookHandlerFactory;
import eu.solven.cleanthat.github.event.IGithubWebhookHandler;
import eu.solven.cleanthat.github.event.pojo.GithubWebhookEvent;
import eu.solven.cleanthat.github.event.pojo.GithubWebhookRelevancyResult;

//https://github-api.kohsuke.org/githubappjwtauth.html
public class ITGithubWebhookHandlerFactory {
Expand Down Expand Up @@ -60,8 +64,7 @@ public void testMakeJwt() throws JOSEException, IOException {
GithubWebhookHandlerFactory factory =
new GithubWebhookHandlerFactory(env, Arrays.asList(ConfigHelpers.makeJsonObjectMapper()));
IGithubWebhookHandler fresh = factory.makeWithFreshJwt();
GitHub gitHubApp = fresh.getGithubAsApp();
GHApp app = gitHubApp.getApp();
GHApp app = fresh.getGithubAsApp();
app.listInstallations().forEach(install -> {
LOGGER.info("appId={} url={}", install.getId(), install.getHtmlUrl());
});
Expand All @@ -75,5 +78,39 @@ public void testMakeJwt() throws JOSEException, IOException {
// Private repo in same organisation
Assertions.assertThat(new GithubFacade(gitHubInstallation.getGithub(), SOLVEN_EU_MITRUST_DATASHARING)
.findFirstPrBaseMatchingRef("refs/heads/master")).isPresent();

{
Map<String, ?> body = ConfigHelpers.makeJsonObjectMapper()
.readValue(new ClassPathResource("/github/webhook/pr_open-open_event.json").getInputStream(),
Map.class);
GithubWebhookRelevancyResult result =
new GithubWebhookHandler(app, Arrays.asList(ConfigHelpers.makeJsonObjectMapper()))
.filterWebhookEventRelevant(new GithubWebhookEvent(body));

Assertions.assertThat(result.isPrOpen()).isTrue();
Assertions.assertThat(result.isPushBranch()).isFalse();
}
{
Map<String, ?> body = ConfigHelpers.makeJsonObjectMapper()
.readValue(new ClassPathResource("/github/webhook/pr_open-push_event-1.json").getInputStream(),
Map.class);
GithubWebhookRelevancyResult result =
new GithubWebhookHandler(app, Arrays.asList(ConfigHelpers.makeJsonObjectMapper()))
.filterWebhookEventRelevant(new GithubWebhookEvent(body));

Assertions.assertThat(result.isPrOpen()).isFalse();
Assertions.assertThat(result.isPushBranch()).isTrue();
}
{
Map<String, ?> body = ConfigHelpers.makeJsonObjectMapper()
.readValue(new ClassPathResource("/github/webhook/pr_open-push_event-1.json").getInputStream(),
Map.class);
GithubWebhookRelevancyResult result =
new GithubWebhookHandler(app, Arrays.asList(ConfigHelpers.makeJsonObjectMapper()))
.filterWebhookEventRelevant(new GithubWebhookEvent(body));

Assertions.assertThat(result.isPrOpen()).isFalse();
Assertions.assertThat(result.isPushBranch()).isTrue();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public class ITGithubPullRequestCleaner {
public void testInitWithDefaultConfiguration() throws IOException, JOSEException {
IGithubWebhookHandler handler = factory.makeWithFreshJwt();

GitHub github = handler.getGithubAsApp();
GHApp app = github.getApp();
GHApp app = handler.getGithubAsApp();

String repoName = "cleanthat";
GHAppInstallation installation = app.getInstallationByRepository("solven-eu", repoName);
Expand Down
Loading