Skip to content

Commit

Permalink
stub out GitHub import prototype #5209
Browse files Browse the repository at this point in the history
  • Loading branch information
pdurbin committed Nov 5, 2018
1 parent 1316fea commit 00bcd29
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 1 deletion.
1 change: 1 addition & 0 deletions scripts/database/upgrades/upgrade_v4.9.4_to_v4.10.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ ALTER TABLE pendingworkflowinvocation ADD COLUMN datasetexternallyreleased BOOLE
INSERT INTO setting(
name, content)
VALUES (':UploadMethods', 'native/http');
ALTER TABLE dataset ADD COLUMN githuburl text;
23 changes: 23 additions & 0 deletions src/main/java/edu/harvard/iq/dataverse/Dataset.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import edu.harvard.iq.dataverse.util.BundleUtil;
import javax.persistence.Column;

/**
*
Expand Down Expand Up @@ -108,6 +109,20 @@ public class Dataset extends DvObjectContainer {
@OneToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST})
@JoinColumn(name = "guestbook_id", unique = false, nullable = true, insertable = true, updatable = true)
private Guestbook guestbook;

/**
* A single repo on GitHub (i.e. https://github.com/IQSS/dataverse)
* associated with the dataset for the "code deposit" or "GitHub import"
* feature
*
* TODO: Consider changing this to gitUrl to support GitLab and other git
* providers
*
* TODO: Consider changing this to vcsUrl to support even non-git version
* control systems like Subversion. VCS is "version control system".
*/
@Column(nullable = true)
private String githubUrl;

@OneToMany(mappedBy="dataset", cascade={CascadeType.REMOVE, CascadeType.MERGE, CascadeType.PERSIST})
private List<DatasetLinkingDataverse> datasetLinkingDataverses;
Expand Down Expand Up @@ -218,6 +233,14 @@ public void setGuestbook(Guestbook guestbook) {
this.guestbook = guestbook;
}

public String getGithubUrl() {
return githubUrl;
}

public void setGithubUrl(String githubUrl) {
this.githubUrl = githubUrl;
}

public boolean isFileAccessRequest() {
return fileAccessRequest;
}
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/edu/harvard/iq/dataverse/api/GithubApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package edu.harvard.iq.dataverse.api;

import edu.harvard.iq.dataverse.Dataset;
import edu.harvard.iq.dataverse.util.json.NullSafeJsonBuilder;
import static edu.harvard.iq.dataverse.util.json.NullSafeJsonBuilder.jsonObjectBuilder;
import java.io.StringReader;
import javax.json.Json;
import javax.json.JsonReader;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

@Path("datasets")
public class GithubApi extends AbstractApiBean {

@GET
@Path("{id}/github")
public Response getGithubUrl(@PathParam("id") String idSupplied) {
try {
Dataset dataset = findDatasetOrDie(idSupplied);
final NullSafeJsonBuilder jsonObject = jsonObjectBuilder()
.add("datasetId", dataset.getId())
.add("githubUrl", dataset.getGithubUrl());
return ok(jsonObject);
} catch (WrappedResponse wr) {
return wr.getResponse();
}
}

@POST
@Path("{id}/github/setUrl")
public Response setGithubUrl(String body, @PathParam("id") String idSupplied) {
try {
Dataset datasetBeforeSave = findDatasetOrDie(idSupplied);
JsonReader jsonReader = Json.createReader(new StringReader(body));
String githubUrl = jsonReader.readObject().getString("githubUrl");
datasetBeforeSave.setGithubUrl(githubUrl);
Dataset savedDataset = datasetSvc.merge(datasetBeforeSave);
final NullSafeJsonBuilder jsonObject = jsonObjectBuilder()
.add("datasetId", savedDataset.getId())
.add("githubUrl", savedDataset.getGithubUrl());
return ok(jsonObject);
} catch (WrappedResponse wr) {
return wr.getResponse();
}
}

@POST
@Path("{id}/github/import")
public Response importGithubRepo(@PathParam("id") String idSupplied) {
return ok("FIXME: download GitHub repo as zip and create a file in dataverse, putting metadata about the repo into the file description.");
}

}
70 changes: 70 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/api/GithubApiIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package edu.harvard.iq.dataverse.api;

import com.jayway.restassured.RestAssured;
import com.jayway.restassured.path.json.JsonPath;
import com.jayway.restassured.response.Response;
import java.util.logging.Logger;
import static javax.ws.rs.core.Response.Status.CREATED;
import static javax.ws.rs.core.Response.Status.OK;
import static org.hamcrest.CoreMatchers.equalTo;
import org.junit.BeforeClass;
import org.junit.Test;

public class GithubApiIT {

private static final Logger logger = Logger.getLogger(GithubApiIT.class.getCanonicalName());

@BeforeClass
public static void setUpClass() {
RestAssured.baseURI = UtilIT.getRestAssuredBaseUri();
}

@Test
public void testGithubDeposit() {

Response createUser = UtilIT.createRandomUser();
createUser.prettyPrint();
createUser.then().assertThat()
.statusCode(OK.getStatusCode());
String username = UtilIT.getUsernameFromResponse(createUser);
String apitoken = UtilIT.getApiTokenFromResponse(createUser);

Response createDataverseResponse = UtilIT.createRandomDataverse(apitoken);
createDataverseResponse.prettyPrint();
createDataverseResponse.then().assertThat()
.statusCode(CREATED.getStatusCode());

String dataverseAlias = UtilIT.getAliasFromResponse(createDataverseResponse);

Response createDataset = UtilIT.createRandomDatasetViaNativeApi(dataverseAlias, apitoken);
createDataset.prettyPrint();
createDataset.then().assertThat()
.statusCode(CREATED.getStatusCode());

Integer datasetId = UtilIT.getDatasetIdFromResponse(createDataset);
String datasetPersistentId = JsonPath.from(createDataset.getBody().asString()).getString("data.persistentId");

Response githubUrlShouldBeAbsent = UtilIT.getGithubUrl(datasetPersistentId, apitoken);
githubUrlShouldBeAbsent.prettyPrint();
githubUrlShouldBeAbsent.then().assertThat()
.statusCode(OK.getStatusCode())
.body("data.githubUrl", equalTo(null));

String githubUrl = "https://github.com/IQSS/metrics.dataverse.org";
Response setGithubUrl = UtilIT.setGithubUrl(datasetPersistentId, githubUrl, apitoken);
setGithubUrl.then().assertThat()
.statusCode(OK.getStatusCode());
setGithubUrl.prettyPrint();

Response getGithubUrl = UtilIT.getGithubUrl(datasetPersistentId, apitoken);
getGithubUrl.prettyPrint();
getGithubUrl.then().assertThat()
.statusCode(OK.getStatusCode())
.body("data.githubUrl", equalTo(githubUrl));

Response importGithubRepo = UtilIT.importGithubRepo(datasetPersistentId, apitoken);
importGithubRepo.prettyPrint();

}

}
57 changes: 56 additions & 1 deletion src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,62 @@ static Response removeDatasetThumbnail(String datasetPersistentId, String apiTok
.header(API_TOKEN_HTTP_HEADER, apiToken)
.delete("/api/datasets/:persistentId/thumbnail" + "?persistentId=" + datasetPersistentId);
}


static Response getGithubUrl(String idOrPersistentId, String apiToken) {
logger.info("Getting GitHub URL");
String idInPath = idOrPersistentId; // Assume it's a number.
String optionalQueryParam = ""; // If idOrPersistentId is a number we'll just put it in the path.
if (!NumberUtils.isNumber(idOrPersistentId)) {
idInPath = ":persistentId";
optionalQueryParam = "?persistentId=" + idOrPersistentId;
}
RequestSpecification requestSpecification = given();
if (apiToken != null) {
requestSpecification = given()
.header(UtilIT.API_TOKEN_HTTP_HEADER, apiToken);
}
return requestSpecification.get("/api/datasets/" + idInPath + "/github" + optionalQueryParam);
}

static Response setGithubUrl(String idOrPersistentId, String githubUrl, String apiToken) {
logger.info("Setting GitHub URL");
JsonObjectBuilder roleBuilder = Json.createObjectBuilder();
roleBuilder.add("githubUrl", githubUrl);
JsonObject roleObject = roleBuilder.build();
String idInPath = idOrPersistentId; // Assume it's a number.
String optionalQueryParam = ""; // If idOrPersistentId is a number we'll just put it in the path.
if (!NumberUtils.isNumber(idOrPersistentId)) {
idInPath = ":persistentId";
optionalQueryParam = "?persistentId=" + idOrPersistentId;
}
RequestSpecification requestSpecification = given();
if (apiToken != null) {
requestSpecification = given()
.header(UtilIT.API_TOKEN_HTTP_HEADER, apiToken);
}
return requestSpecification
.body(roleObject.toString())
.contentType(ContentType.JSON)
.post("/api/datasets/" + idInPath + "/github/setUrl" + optionalQueryParam);
}

static Response importGithubRepo(String idOrPersistentId, String apiToken) {
logger.info("Importing GitHub repo into dataset " + idOrPersistentId);
String idInPath = idOrPersistentId; // Assume it's a number.
String optionalQueryParam = ""; // If idOrPersistentId is a number we'll just put it in the path.
if (!NumberUtils.isNumber(idOrPersistentId)) {
idInPath = ":persistentId";
optionalQueryParam = "?persistentId=" + idOrPersistentId;
}
RequestSpecification requestSpecification = given();
if (apiToken != null) {
requestSpecification = given()
.header(UtilIT.API_TOKEN_HTTP_HEADER, apiToken);
}
return requestSpecification
.post("/api/datasets/" + idInPath + "/github/import" + optionalQueryParam);
}

static Response getDatasetVersions(String idOrPersistentId, String apiToken) {
logger.info("Getting Dataset Versions");
String idInPath = idOrPersistentId; // Assume it's a number.
Expand Down

0 comments on commit 00bcd29

Please sign in to comment.