Skip to content

Commit

Permalink
feat: different message if repo is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
cmeessen authored and dmijatovic committed Oct 14, 2022
1 parent b8b90db commit a8a9102
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 25 deletions.
3 changes: 3 additions & 0 deletions frontend/pages/software/[slug]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ export default function SoftwareIndexPage(props:SoftwareIndexData) {
} = props

let noCommitGraphMessage : string | undefined
if (repositoryInfo?.commit_history_scraped_at && repositoryInfo?.commit_history && Object.keys( repositoryInfo?.commit_history ).length == 0 ) {
noCommitGraphMessage = 'We cannot display this graph because the repository is empty.'
}
if (repositoryInfo?.commit_history_scraped_at && repositoryInfo?.commit_history === null) {
noCommitGraphMessage = 'We cannot display this graph because we cannot read the commit history.'
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-FileCopyrightText: 2022 Christian Meeßen (GFZ) <christian.meessen@gfz-potsdam.de>
// SPDX-FileCopyrightText: 2022 Helmholtz Centre Potsdam - GFZ German Research Centre for Geosciences
//
// SPDX-License-Identifier: Apache-2.0

package nl.esciencecenter.rsd.scraper;

public class ResponseException extends RuntimeException {
private Integer statusCode;

public ResponseException(Integer statusCode) {
this.statusCode = statusCode;
}

public ResponseException(Integer statusCode, String message) {
super(message);
this.statusCode = statusCode;
}

public Integer getStatusCode() {
return this.statusCode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static String get(String uri, String... headers) {
throw new RuntimeException(e);
}
if (response.statusCode() >= 300) {
throw new RuntimeException("Error fetching data from endpoint " + uri + " with response: " + response.body());
throw new ResponseException(response.statusCode(), "Error fetching data from endpoint " + uri + " with response: " + response.body());
}
return response.body();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,29 +74,31 @@ static String contributionsGitHub(String data) {
* @return A String representing JSON
*/
static String contributionsGitLab(String data) {
JsonArray allCommits = JsonParser.parseString(data).getAsJsonArray();
String oldestCommit = allCommits.get(allCommits.size() - 1).getAsJsonObject().get("committed_date").getAsString();
ZonedDateTime oldestCommitDate = ZonedDateTime.parse(oldestCommit).withZoneSameInstant(ZoneOffset.UTC);
ZonedDateTime firstAggregationWeek = collapseToWeekUTC(oldestCommitDate);
ZonedDateTime lastAggregationWeek = collapseToWeekUTC(ZonedDateTime.now(ZoneOffset.UTC));
long oneWeekInSeconds = 604800L; // 60*60*24*7
// create empty map
SortedMap<Long, Long> commitsPerWeek = new TreeMap<>();
long currentSeconds = firstAggregationWeek.toEpochSecond();
while (currentSeconds < lastAggregationWeek.toEpochSecond() + oneWeekInSeconds) {
commitsPerWeek.put(currentSeconds, 0L);
currentSeconds += oneWeekInSeconds;
}
// Fill map
for (int i = allCommits.size() - 1; i >= 0; i--) {
JsonObject currentCommit = allCommits.get(i).getAsJsonObject();
ZonedDateTime commitDateUTC = ZonedDateTime.parse(currentCommit.get("committed_date").getAsString()).withZoneSameInstant(ZoneOffset.UTC);
ZonedDateTime commitWeek = collapseToWeekUTC(commitDateUTC);
Long commitWeekSeconds = commitWeek.toEpochSecond();
Long newCommitsThisWeek;
newCommitsThisWeek = commitsPerWeek.get(commitWeekSeconds) + 1L;
commitsPerWeek.put(commitWeekSeconds, newCommitsThisWeek);
}
JsonArray allCommits = JsonParser.parseString(data).getAsJsonArray();
if (allCommits.size() > 0) {
String oldestCommit = allCommits.get(allCommits.size() - 1).getAsJsonObject().get("committed_date").getAsString();
ZonedDateTime oldestCommitDate = ZonedDateTime.parse(oldestCommit).withZoneSameInstant(ZoneOffset.UTC);
ZonedDateTime firstAggregationWeek = collapseToWeekUTC(oldestCommitDate);
ZonedDateTime lastAggregationWeek = collapseToWeekUTC(ZonedDateTime.now(ZoneOffset.UTC));
long oneWeekInSeconds = 604800L; // 60*60*24*7
long currentSeconds = firstAggregationWeek.toEpochSecond();
while (currentSeconds < lastAggregationWeek.toEpochSecond() + oneWeekInSeconds) {
commitsPerWeek.put(currentSeconds, 0L);
currentSeconds += oneWeekInSeconds;
}
// Fill map
for (int i = allCommits.size() - 1; i >= 0; i--) {
JsonObject currentCommit = allCommits.get(i).getAsJsonObject();
ZonedDateTime commitDateUTC = ZonedDateTime.parse(currentCommit.get("committed_date").getAsString()).withZoneSameInstant(ZoneOffset.UTC);
ZonedDateTime commitWeek = collapseToWeekUTC(commitDateUTC);
Long commitWeekSeconds = commitWeek.toEpochSecond();
Long newCommitsThisWeek;
newCommitsThisWeek = commitsPerWeek.get(commitWeekSeconds) + 1L;
commitsPerWeek.put(commitWeekSeconds, newCommitsThisWeek);
}
}
return new Gson().toJson(commitsPerWeek);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// SPDX-FileCopyrightText: 2022 Christian Meeßen (GFZ) <christian.meessen@gfz-potsdam.de>
// SPDX-FileCopyrightText: 2022 Ewan Cahen (Netherlands eScience Center) <e.cahen@esciencecenter.nl>
// SPDX-FileCopyrightText: 2022 Helmholtz Centre Potsdam - GFZ German Research Centre for Geosciences
// SPDX-FileCopyrightText: 2022 Netherlands eScience Center
//
// SPDX-License-Identifier: Apache-2.0
Expand All @@ -8,6 +10,7 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import nl.esciencecenter.rsd.scraper.Config;
import nl.esciencecenter.rsd.scraper.ResponseException;
import nl.esciencecenter.rsd.scraper.Utils;

import java.util.Objects;
Expand Down Expand Up @@ -67,8 +70,21 @@ public String license() {
*/
@Override
public String contributions() {
return Config.apiCredentialsGithub()
String contributions = "";
try {
contributions = Config.apiCredentialsGithub()
.map(apiCredentials -> Utils.get(baseApiUrl + "/repos/" + repo + "/stats/contributors", "Authorization", "Basic " + Utils.base64Encode(apiCredentials)))
.orElseGet(() -> Utils.get(baseApiUrl + "/repos/" + repo + "/stats/contributors"));
if ( contributions.equals("") ) {
// Repository exists, but no contributions yet, empty list is more appropriate
contributions = "[]";
}
} catch (ResponseException e) {
if (e.getStatusCode() == 404) {
// Repository does not exist, emtpy string will be parsed to null
contributions = "";
}
}
return contributions;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// SPDX-FileCopyrightText: 2022 Christian Meeßen (GFZ) <christian.meessen@gfz-potsdam.de>
// SPDX-FileCopyrightText: 2022 Ewan Cahen (Netherlands eScience Center) <e.cahen@esciencecenter.nl>
// SPDX-FileCopyrightText: 2022 Helmholtz Centre Potsdam - GFZ German Research Centre for Geosciences
// SPDX-FileCopyrightText: 2022 Netherlands eScience Center
//
// SPDX-License-Identifier: Apache-2.0
Expand All @@ -11,6 +13,17 @@
import org.junit.jupiter.api.Test;

public class AggregateContributionsPerWeekSIDecoratorTest {
private final String apiUrl = "https://api.github.com";
private final String projectPath = "cmeessen/empty";

@Test
void emptyRepository() {
// String scrapedCommits = new AggregateContributionsPerWeekSIDecorator(new GithubSI(apiUrl, projectPath), CodePlatformProvider.GITHUB).contributions();
String scrapedCommits = new AggregateContributionsPerWeekSIDecorator(
new GitLabSI("https://gitlab.hzdr.de/api", "christian.meessen/empty"),
CodePlatformProvider.GITHUB
).contributions();
}

@Test
void givenGithubCommitData_whenAggregating_thenAggregatedDataReturned() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// SPDX-FileCopyrightText: 2022 Christian Meeßen (GFZ) <christian.meessen@gfz-potsdam.de>
// SPDX-FileCopyrightText: 2022 Ewan Cahen (Netherlands eScience Center) <e.cahen@esciencecenter.nl>
// SPDX-FileCopyrightText: 2022 Helmholtz Centre Potsdam - GFZ German Research Centre for Geosciences
// SPDX-FileCopyrightText: 2022 Netherlands eScience Center
//
// SPDX-License-Identifier: Apache-2.0
Expand All @@ -12,7 +14,12 @@
public class GithubSITest {
private final String apiUrl = "https://api.github.com";
private final String repo = "research-software-directory/RSD-as-a-service";
private final String repoEmpty = "cmeessen/empty";
private final String repoNonEx = "research-software-directory/does-not-exist";

private final GithubSI githubScraper = new GithubSI(apiUrl, repo);
private final GithubSI githubScraperEmpty = new GithubSI(apiUrl, repoEmpty);
private final GithubSI githubScraperNonEx = new GithubSI(apiUrl, repoNonEx);

@Disabled
@Test
Expand All @@ -21,7 +28,6 @@ void languages() {
Assertions.assertTrue(languages.startsWith("{"));
Assertions.assertTrue(languages.endsWith("}"));
Assertions.assertTrue(languages.contains("Java"));

}

@Disabled
Expand All @@ -30,10 +36,24 @@ void license() {
Assertions.assertEquals("Apache-2.0", githubScraper.license());
}

@Disabled
// @Disabled
@Test
void contributions() {
final String contributions = githubScraper.contributions();
Assertions.assertTrue(contributions.startsWith("[{\"total"));
}

// @Disabled
@Test
void contributionsEmpty() {
final String contributionsEmpty = githubScraperEmpty.contributions();
Assertions.assertTrue(contributionsEmpty.equals("[]"));
}

// @Disabled
@Test
void contributionsNonEx() {
final String contributionsNonEx = githubScraperNonEx.contributions();
Assertions.assertTrue(contributionsNonEx.equals(""));
}
}

0 comments on commit a8a9102

Please sign in to comment.