فهرست منبع

Merge pull request #50 from overleaf/sk-fix-imported-unchanged-projects

Handle imported, and unchanged, projects
Shane Kilkelly 7 سال پیش
والد
کامیت
d86d257b05

+ 4 - 1
services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/snapshot/SnapshotApiFacade.java

@@ -97,7 +97,10 @@ public class SnapshotApiFacade {
                 = api.getSavedVers(oauth2, projectName);
         GetDocResult latestDoc = SnapshotApi.getResult(getDoc);
         int latest = latestDoc.getVersionID();
-        if (latest > version) {
+        // Handle edge-case for projects with no changes, that were imported
+        // to v2. In which case both `latest` and `version` will be zero.
+        // See: https://github.com/overleaf/writelatex-git-bridge/pull/50
+        if (latest > version || (latest == 0 && version == 0)) {
             for (
                     SnapshotInfo snapshotInfo :
                     SnapshotApi.getResult(savedVers).getSavedVers()

+ 8 - 1
services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/snapshot/getdoc/GetDocResult.java

@@ -98,7 +98,14 @@ public class GetDocResult extends Result {
             }
         } else {
             versionID = jsonObject.get("latestVerId").getAsInt();
-            createdAt = jsonObject.get("latestVerAt").getAsString();
+            // Handle edge-case for projects with no changes, that were imported
+            // to v2. In which case `latestVerAt` will not be present.
+            // See: https://github.com/overleaf/writelatex-git-bridge/pull/50
+            if (jsonObject.has("latestVerAt")) {
+                createdAt = jsonObject.get("latestVerAt").getAsString();
+            } else {
+                createdAt = null;
+            }
             if (jsonObject.has("migratedFromId")) {
                 migratedFromID = jsonObject.get("migratedFromId").getAsString();
             } else {

+ 17 - 4
services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/snapshot/servermock/state/SnapshotAPIStateBuilder.java

@@ -83,7 +83,20 @@ public class SnapshotAPIStateBuilder {
             String projectName,
             JsonObject jsonGetDoc
     ) {
+        int versionID = jsonGetDoc.get("versionID").getAsInt();
+        String createdAt = null;
+        String email = null;
+        String name = null;
         String migratedFromId = null;
+        if (jsonGetDoc.has("createdAt")) {
+            createdAt = jsonGetDoc.get("createdAt").getAsString();
+        }
+        if (jsonGetDoc.has("email")) {
+            email = jsonGetDoc.get("email").getAsString();
+        }
+        if (jsonGetDoc.has("name")) {
+            name = jsonGetDoc.get("name").getAsString();
+        }
         if (jsonGetDoc.has("migratedFromId")) {
             migratedFromId = jsonGetDoc.get("migratedFromId").getAsString();
         }
@@ -91,10 +104,10 @@ public class SnapshotAPIStateBuilder {
                 projectName,
                 new GetDocResult(
                         jsonGetDoc.get("error"),
-                        jsonGetDoc.get("versionID").getAsInt(),
-                        jsonGetDoc.get("createdAt").getAsString(),
-                        jsonGetDoc.get("email").getAsString(),
-                        jsonGetDoc.get("name").getAsString(),
+                        versionID,
+                        createdAt,
+                        email,
+                        name,
                         migratedFromId
                 )
         );

+ 19 - 0
services/git-bridge/src/test/java/uk/ac/ic/wlgitbridge/application/WLGitBridgeIntegrationTest.java

@@ -123,6 +123,9 @@ public class WLGitBridgeIntegrationTest {
         put("skipMigrationWhenMigratedFromMissing", new HashMap<String, SnapshotAPIState>() {{
             put("state", new SnapshotAPIStateBuilder(getResourceAsStream("/skipMigrationWhenMigratedFromMissing/state/state.json")).build());
         }});
+        put("canCloneAMigratedRepositoryWithoutChanges", new HashMap<String, SnapshotAPIState>() {{
+            put("state", new SnapshotAPIStateBuilder(getResourceAsStream("/canCloneAMigratedRepositoryWithoutChanges/state/state.json")).build());
+        }});
     }};
 
     @Rule
@@ -830,6 +833,22 @@ public class WLGitBridgeIntegrationTest {
         assertTrue(FileUtil.gitDirectoriesAreEqual(getResource("/skipMigrationWhenMigratedFromMissing/state/testproj2"), testprojDir2.toPath()));
     }
 
+    @Test
+    public void canCloneAMigratedRepositoryWithoutChanges() throws IOException, GitAPIException, InterruptedException {
+        int gitBridgePort = 33883;
+        int mockServerPort = 3883;
+        MockSnapshotServer server = new MockSnapshotServer(mockServerPort, getResource("/canCloneAMigratedRepositoryWithoutChanges").toFile());
+        server.start();
+        server.setState(states.get("canCloneAMigratedRepositoryWithoutChanges").get("state"));
+        GitBridgeApp wlgb = new GitBridgeApp(new String[] {
+                makeConfigFile(gitBridgePort, mockServerPort)
+        });
+        wlgb.run();
+        File testprojDir = gitClone("testproj_no_change", gitBridgePort, dir);
+        wlgb.stop();
+        assertTrue(FileUtil.gitDirectoriesAreEqual(getResource("/canCloneAMigratedRepositoryWithoutChanges/state/testproj_no_change"), testprojDir.toPath()));
+    }
+
     private String makeConfigFile(
             int port,
             int apiPort

+ 29 - 0
services/git-bridge/src/test/resources/uk/ac/ic/wlgitbridge/WLGitBridgeIntegrationTest/canCloneAMigratedRepositoryWithoutChanges/state/state.json

@@ -0,0 +1,29 @@
+[
+	{
+		"project": "testproj_no_change",
+		"getDoc": {
+			"versionID": 0,
+			"createdAt": "2014-11-30T18:40:58.123Z",
+			"email": "jdleesmiller+1@gmail.com",
+			"name": "John+1"
+		},
+		"getSavedVers": [],
+		"getForVers": [
+			{
+				"versionID": 0,
+				"srcs": [
+                    {
+                    	"content": "test content\n",
+                    	"path": "main.tex"
+                    }
+	            ],
+	            "atts": []
+	        }
+		],
+		"push": "success",
+		"postback": {
+			"type": "success",
+			"versionID": 1
+		}
+	}
+]

+ 1 - 0
services/git-bridge/src/test/resources/uk/ac/ic/wlgitbridge/WLGitBridgeIntegrationTest/canCloneAMigratedRepositoryWithoutChanges/state/testproj_no_change/main.tex

@@ -0,0 +1 @@
+test content