Skip to content

Commit

Permalink
#103: Handle pagination on repositories response
Browse files Browse the repository at this point in the history
When trying to find authentication tokens for accessing a repository, the list of repositories attached to a token are currently iterated over until one is found with a matching name, or the end of the retrieved list is reached. However, this search is not taking into account pagination in the response from Github, so any list of repositories that is greater than the default page size fails to find a matching authentication token if the repository is not present on the first page of the repository list. To resolve this, the response is checked for a `Link` header with a `rel="next"` element, with the link in this header being followed for each response until a matching repository is found, or the header is not present.

Includes a drive-by-change to allow correct reporting of test coverage to SonarCloud.
  • Loading branch information
mc1arke committed Apr 26, 2020
1 parent 0ccc0cd commit 7b02022
Show file tree
Hide file tree
Showing 8 changed files with 304 additions and 40 deletions.
12 changes: 11 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
plugins {
id('java')
id('jacoco')
id('org.sonarqube') version('2.7')
id('org.sonarqube') version('2.8')
id('info.solidsoft.pitest') version('1.4.0')
id('com.github.johnrengelman.shadow') version('5.1.0')
id('net.researchgate.release') version('2.6.0')
Expand Down Expand Up @@ -121,3 +121,13 @@ pitest {
timestampedReports = false
avoidCallsTo = ['org.sonar.api.utils.log.Logger']
}

jacocoTestReport {
reports {
xml.enabled true
}
}

plugins.withType(JacocoPlugin) {
tasks["test"].finalizedBy 'jacocoTestReport'
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@

import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.PostAnalysisIssueVisitor;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.PullRequestPostAnalysisTask;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.server.BitbucketServerPullRequestDecorator;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.github.GithubPullRequestDecorator;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.github.v3.DefaultLinkHeaderReader;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.github.v3.RestApplicationAuthenticationProvider;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.github.v4.GraphqlCheckRunProvider;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.gitlab.GitlabServerPullRequestDecorator;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.server.BitbucketServerPullRequestDecorator;
import org.sonar.ce.task.projectanalysis.container.ReportAnalysisComponentProvider;

import java.util.Arrays;
Expand All @@ -39,7 +40,7 @@ public class CommunityReportAnalysisComponentProvider implements ReportAnalysisC
public List<Object> getComponents() {
return Arrays.asList(CommunityBranchLoaderDelegate.class, PullRequestPostAnalysisTask.class,
PostAnalysisIssueVisitor.class, GithubPullRequestDecorator.class,
GraphqlCheckRunProvider.class, RestApplicationAuthenticationProvider.class,
GraphqlCheckRunProvider.class, DefaultLinkHeaderReader.class, RestApplicationAuthenticationProvider.class,
BitbucketServerPullRequestDecorator.class, GitlabServerPullRequestDecorator.class);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (C) 2020 Michael Clarke
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.github.v3;

import java.util.Arrays;
import java.util.Optional;

public class DefaultLinkHeaderReader implements LinkHeaderReader {

@Override
public Optional<String> findNextLink(String linkHeader) {
return Optional.ofNullable(linkHeader)
.flatMap(l -> Arrays.stream(l.split(","))
.map(i -> i.split(";"))
.filter(i -> i.length > 1)
.filter(i -> {
String[] relParts = i[1].trim().split("=");

if (relParts.length < 2) {
return false;
}

if (!"rel".equals(relParts[0])) {
return false;
}

return "next".equals(relParts[1]) || "\"next\"".equals(relParts[1]);
})
.map(i -> i[0])
.map(String::trim)
.filter(i -> i.startsWith("<") && i.endsWith(">"))
.map(i -> i.substring(1, i.length() - 1))
.findFirst());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (C) 2020 Michael Clarke
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.github.v3;

import java.util.Optional;

interface LinkHeaderReader {

Optional<String> findNextLink(String linkHeader);

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019 Michael Clarke
* Copyright (C) 2020 Michael Clarke
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -43,6 +43,7 @@
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Optional;

public class RestApplicationAuthenticationProvider implements GithubApplicationAuthenticationProvider {

Expand All @@ -53,16 +54,18 @@ public class RestApplicationAuthenticationProvider implements GithubApplicationA
private static final String APP_PREVIEW_ACCEPT_HEADER = "application/vnd.github.machine-man-preview+json";

private final Clock clock;
private final LinkHeaderReader linkHeaderReader;
private final UrlConnectionProvider urlProvider;

public RestApplicationAuthenticationProvider(Clock clock) {
this(clock, new DefaultUrlConnectionProvider());
public RestApplicationAuthenticationProvider(Clock clock, LinkHeaderReader linkHeaderReader) {
this(clock, linkHeaderReader, new DefaultUrlConnectionProvider());
}

RestApplicationAuthenticationProvider(Clock clock, UrlConnectionProvider urlProvider) {
RestApplicationAuthenticationProvider(Clock clock, LinkHeaderReader linkHeaderReader, UrlConnectionProvider urlProvider) {
super();
this.clock = clock;
this.urlProvider = urlProvider;
this.linkHeaderReader = linkHeaderReader;
}

@Override
Expand Down Expand Up @@ -97,32 +100,13 @@ public RepositoryAuthenticationToken getInstallationToken(String apiUrl, String
try (Reader reader = new InputStreamReader(accessTokenConnection.getInputStream())) {
AppToken appToken = objectMapper.readerFor(AppToken.class).readValue(reader);

URLConnection installationRepositoriesConnection =
urlProvider.createUrlConnection(installation.getRepositoriesUrl());
((HttpURLConnection) installationRepositoriesConnection).setRequestMethod("GET");
installationRepositoriesConnection.setRequestProperty(ACCEPT_HEADER, APP_PREVIEW_ACCEPT_HEADER);
installationRepositoriesConnection.setRequestProperty(AUTHORIZATION_HEADER,
BEARER_AUTHORIZATION_HEADER_PREFIX +
appToken.getToken());
String repositoryNodeId = null;
try (Reader installationRepositoriesReader = new InputStreamReader(
installationRepositoriesConnection.getInputStream())) {
InstallationRepositories installationRepositories =
objectMapper.readerFor(InstallationRepositories.class)
.readValue(installationRepositoriesReader);
for (Repository repository : installationRepositories.getRepositories()) {
if (projectPath.equals(repository.getFullName())) {
repositoryNodeId = repository.getNodeId();
break;
}
}
if (null == repositoryNodeId) {
continue;
}
String targetUrl = installation.getRepositoriesUrl();

}
Optional<RepositoryAuthenticationToken> potentialRepositoryAuthenticationToken = findRepositoryAuthenticationToken(appToken, targetUrl, projectPath, objectMapper);

return new RepositoryAuthenticationToken(repositoryNodeId, appToken.getToken());
if (potentialRepositoryAuthenticationToken.isPresent()) {
return potentialRepositoryAuthenticationToken.get();
}

}
}
Expand All @@ -131,6 +115,35 @@ public RepositoryAuthenticationToken getInstallationToken(String apiUrl, String
"No token could be found with access to the requested repository with the given application ID and key");
}

private Optional<RepositoryAuthenticationToken> findRepositoryAuthenticationToken(AppToken appToken, String targetUrl,
String projectPath, ObjectMapper objectMapper) throws IOException {
URLConnection installationRepositoriesConnection = urlProvider.createUrlConnection(targetUrl);
((HttpURLConnection) installationRepositoriesConnection).setRequestMethod("GET");
installationRepositoriesConnection.setRequestProperty(ACCEPT_HEADER, APP_PREVIEW_ACCEPT_HEADER);
installationRepositoriesConnection.setRequestProperty(AUTHORIZATION_HEADER,
BEARER_AUTHORIZATION_HEADER_PREFIX + appToken.getToken());

try (Reader installationRepositoriesReader = new InputStreamReader(
installationRepositoriesConnection.getInputStream())) {
InstallationRepositories installationRepositories =
objectMapper.readerFor(InstallationRepositories.class).readValue(installationRepositoriesReader);
for (Repository repository : installationRepositories.getRepositories()) {
if (projectPath.equals(repository.getFullName())) {
return Optional.of(new RepositoryAuthenticationToken(repository.getNodeId(), appToken.getToken()));
}
}

}

Optional<String> nextLink = linkHeaderReader.findNextLink(installationRepositoriesConnection.getHeaderField("Link"));

if (!nextLink.isPresent()) {
return Optional.empty();
}

return findRepositoryAuthenticationToken(appToken, nextLink.get(), projectPath, objectMapper);
}


private static PrivateKey createPrivateKey(String apiPrivateKey) throws IOException {
try (PEMParser pemParser = new PEMParser(new StringReader(apiPrivateKey))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class CommunityReportAnalysisComponentProviderTest {
@Test
public void testGetComponents() {
List<Object> result = new CommunityReportAnalysisComponentProvider().getComponents();
assertEquals(8, result.size());
assertEquals(9, result.size());
assertEquals(CommunityBranchLoaderDelegate.class, result.get(0));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright (C) 2020 Michael Clarke
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.github.v3;

import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class DefaultLinkHeaderReaderTest {

@Test
public void findNextLinkEmptyForNoHeader() {
DefaultLinkHeaderReader underTest = new DefaultLinkHeaderReader();
assertThat(underTest.findNextLink(null)).isEmpty();
}

@Test
public void findNextLinkEmptyForEmptyHeader() {
DefaultLinkHeaderReader underTest = new DefaultLinkHeaderReader();
assertThat(underTest.findNextLink("")).isEmpty();
}

@Test
public void findNextLinkEmptyForMissingRelContent() {
DefaultLinkHeaderReader underTest = new DefaultLinkHeaderReader();
assertThat(underTest.findNextLink("<http://url>; rel")).isEmpty();
}

@Test
public void findNextLinkEmptyForInvalidRelContent() {
DefaultLinkHeaderReader underTest = new DefaultLinkHeaderReader();
assertThat(underTest.findNextLink("<http://url>; abc=\"xyz\"")).isEmpty();
}

@Test
public void findNextLinkEmptyForIncorrectHeader() {
DefaultLinkHeaderReader underTest = new DefaultLinkHeaderReader();
assertThat(underTest.findNextLink("dummy")).isEmpty();
}

@Test
public void findNextLinkEmptyForMissingUrlPrefix() {
DefaultLinkHeaderReader underTest = new DefaultLinkHeaderReader();
assertThat(underTest.findNextLink("http://other>; rel=\"next\"")).isEmpty();
}

@Test
public void findNextLinkEmptyForMissingUrlPostfix() {
DefaultLinkHeaderReader underTest = new DefaultLinkHeaderReader();
assertThat(underTest.findNextLink("<http://other; rel=\"next\"")).isEmpty();
}

@Test
public void findNextLinkEmptyForMissingUrlWrapper() {
DefaultLinkHeaderReader underTest = new DefaultLinkHeaderReader();
assertThat(underTest.findNextLink("http://other; rel=\"next\"")).isEmpty();
}

@Test
public void findNextLinkReturnsCorrectUrlOnMatch() {
DefaultLinkHeaderReader underTest = new DefaultLinkHeaderReader();
assertThat(underTest.findNextLink("<http://other>; rel=\"next\"")).hasValue("http://other");
}

@Test
public void findNextLinkReturnsCorrectUrlOnMatchNoSpeechMarksAroundRel() {
DefaultLinkHeaderReader underTest = new DefaultLinkHeaderReader();
assertThat(underTest.findNextLink("<http://other>; rel=next")).hasValue("http://other");
}

@Test
public void findNextLinkReturnsCorrectUrlOnMatchWithOtherRelEntries() {
DefaultLinkHeaderReader underTest = new DefaultLinkHeaderReader();
assertThat(underTest.findNextLink("<http://other>; rel=\"last\", <http://other2>; rel=\"next\"")).hasValue("http://other2");
}
}
Loading

0 comments on commit 7b02022

Please sign in to comment.