-
-
Notifications
You must be signed in to change notification settings - Fork 530
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#103: Handle pagination on repositories response
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
Showing
8 changed files
with
304 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...com/github/mc1arke/sonarqube/plugin/ce/pullrequest/github/v3/DefaultLinkHeaderReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...n/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/github/v3/LinkHeaderReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
...github/mc1arke/sonarqube/plugin/ce/pullrequest/github/v3/DefaultLinkHeaderReaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
Oops, something went wrong.