Search in sources :

Example 1 with ModifiedFile

use of com.thoughtworks.go.plugin.access.scm.revision.ModifiedFile in project gocd by gocd.

the class JsonMessageHandler1_0 method getScmRevisionFromMap.

SCMRevision getScmRevisionFromMap(Map map) {
    String revision;
    try {
        revision = (String) map.get("revision");
    } catch (Exception e) {
        throw new RuntimeException("SCM revision should be of type string");
    }
    if (isEmpty(revision)) {
        throw new RuntimeException("SCM revision's 'revision' is a required field");
    }
    Date timestamp;
    try {
        String timestampString = (String) map.get("timestamp");
        timestamp = new SimpleDateFormat(DATE_PATTERN).parse(timestampString);
    } catch (Exception e) {
        throw new RuntimeException("SCM revision timestamp should be of type string with format yyyy-MM-dd'T'HH:mm:ss.SSS'Z' and cannot be empty");
    }
    String revisionComment;
    try {
        revisionComment = (String) map.get("revisionComment");
    } catch (Exception e) {
        throw new RuntimeException("SCM revision comment should be of type string");
    }
    String user;
    try {
        user = (String) map.get("user");
    } catch (Exception e) {
        throw new RuntimeException("SCM revision user should be of type string");
    }
    Map data = (Map) map.get("data");
    List<ModifiedFile> modifiedFiles = new ArrayList<>();
    if (map.containsKey("modifiedFiles") && map.get("modifiedFiles") != null) {
        List modifiedFileMaps = null;
        try {
            modifiedFileMaps = (List) map.get("modifiedFiles");
        } catch (Exception e) {
            throw new RuntimeException("SCM revision 'modifiedFiles' should be of type list of map");
        }
        if (!modifiedFileMaps.isEmpty()) {
            for (Object message : modifiedFileMaps) {
                if (!(message instanceof Map)) {
                    throw new RuntimeException("SCM revision 'modified file' should be of type map");
                }
            }
            for (Object modifiedFileObj : modifiedFileMaps) {
                Map modifiedFileMap = (Map) modifiedFileObj;
                String fileName;
                try {
                    fileName = (String) modifiedFileMap.get("fileName");
                } catch (Exception e) {
                    throw new RuntimeException("modified file 'fileName' should be of type string");
                }
                if (isEmpty(fileName)) {
                    throw new RuntimeException("modified file 'fileName' is a required field");
                }
                String actionStr = null;
                ModifiedAction action;
                try {
                    actionStr = (String) modifiedFileMap.get("action");
                } catch (Exception e) {
                    throw new RuntimeException("modified file 'action' should be of type string");
                }
                try {
                    action = ModifiedAction.valueOf(actionStr);
                } catch (Exception e) {
                    throw new RuntimeException(String.format("modified file 'action' can only be %s, %s, %s", ModifiedAction.added, ModifiedAction.modified, ModifiedAction.deleted));
                }
                modifiedFiles.add(new ModifiedFile(fileName, action));
            }
        }
    }
    return new SCMRevision(revision, timestamp, user, revisionComment, data, modifiedFiles);
}
Also used : ModifiedFile(com.thoughtworks.go.plugin.access.scm.revision.ModifiedFile) ModifiedAction(com.thoughtworks.go.plugin.access.scm.revision.ModifiedAction) SCMRevision(com.thoughtworks.go.plugin.access.scm.revision.SCMRevision) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with ModifiedFile

use of com.thoughtworks.go.plugin.access.scm.revision.ModifiedFile in project gocd by gocd.

the class JsonMessageHandler1_0Test method shouldBuildSCMRevisionsFromLatestRevisionsSinceResponse.

@Test
public void shouldBuildSCMRevisionsFromLatestRevisionsSinceResponse() throws Exception {
    String r1 = "{\"revision\":\"r1\",\"timestamp\":\"2011-07-14T19:43:37.100Z\",\"user\":\"some-user\",\"revisionComment\":\"comment\",\"data\":{\"dataKeyTwo\":\"data-value-two\",\"dataKeyOne\":\"data-value-one\"}," + "\"modifiedFiles\":[{\"fileName\":\"f1\",\"action\":\"added\"},{\"fileName\":\"f2\",\"action\":\"modified\"},{\"fileName\":\"f3\",\"action\":\"deleted\"}]}";
    String r2 = "{\"revision\":\"r2\",\"timestamp\":\"2011-07-14T19:43:37.101Z\",\"user\":\"new-user\",\"revisionComment\":\"comment\",\"data\":{\"dataKeyTwo\":\"data-value-two\",\"dataKeyOne\":\"data-value-one\"}," + "\"modifiedFiles\":[{\"fileName\":\"f1\",\"action\":\"added\"}]}";
    String responseBody = "{\"revisions\":[" + r1 + "," + r2 + "]}";
    MaterialPollResult pollResult = messageHandler.responseMessageForLatestRevisionsSince(responseBody);
    assertThat(pollResult.getMaterialData(), is(nullValue()));
    List<SCMRevision> scmRevisions = pollResult.getRevisions();
    assertThat(scmRevisions.size(), is(2));
    assertSCMRevision(scmRevisions.get(0), "r1", "some-user", "2011-07-14T19:43:37.100Z", "comment", asList(new ModifiedFile("f1", ModifiedAction.added), new ModifiedFile("f2", ModifiedAction.modified), new ModifiedFile("f3", ModifiedAction.deleted)));
    assertSCMRevision(scmRevisions.get(1), "r2", "new-user", "2011-07-14T19:43:37.101Z", "comment", asList(new ModifiedFile("f1", ModifiedAction.added)));
}
Also used : MaterialPollResult(com.thoughtworks.go.plugin.access.scm.material.MaterialPollResult) ModifiedFile(com.thoughtworks.go.plugin.access.scm.revision.ModifiedFile) SCMRevision(com.thoughtworks.go.plugin.access.scm.revision.SCMRevision) Test(org.junit.Test)

Example 3 with ModifiedFile

use of com.thoughtworks.go.plugin.access.scm.revision.ModifiedFile in project gocd by gocd.

the class JsonMessageHandler1_0Test method shouldBuildSCMRevisionFromLatestRevisionResponse.

@Test
public void shouldBuildSCMRevisionFromLatestRevisionResponse() throws Exception {
    String revisionJSON = "{\"revision\":\"r1\",\"timestamp\":\"2011-07-14T19:43:37.100Z\",\"user\":\"some-user\",\"revisionComment\":\"comment\",\"data\":{\"dataKeyTwo\":\"data-value-two\",\"dataKeyOne\":\"data-value-one\"}," + "\"modifiedFiles\":[{\"fileName\":\"f1\",\"action\":\"added\"},{\"fileName\":\"f2\",\"action\":\"modified\"},{\"fileName\":\"f3\",\"action\":\"deleted\"}]}";
    String responseBody = "{\"revision\": " + revisionJSON + "}";
    MaterialPollResult pollResult = messageHandler.responseMessageForLatestRevision(responseBody);
    assertThat(pollResult.getMaterialData(), is(nullValue()));
    assertSCMRevision(pollResult.getLatestRevision(), "r1", "some-user", "2011-07-14T19:43:37.100Z", "comment", asList(new ModifiedFile("f1", ModifiedAction.added), new ModifiedFile("f2", ModifiedAction.modified), new ModifiedFile("f3", ModifiedAction.deleted)));
}
Also used : MaterialPollResult(com.thoughtworks.go.plugin.access.scm.material.MaterialPollResult) ModifiedFile(com.thoughtworks.go.plugin.access.scm.revision.ModifiedFile) Test(org.junit.Test)

Example 4 with ModifiedFile

use of com.thoughtworks.go.plugin.access.scm.revision.ModifiedFile in project gocd by gocd.

the class PluggableSCMMaterialPoller method getModification.

private Modification getModification(SCMRevision scmRevision) {
    String additionalData = (scmRevision.getData() == null || scmRevision.getData().isEmpty()) ? null : JsonHelper.toJsonString(scmRevision.getData());
    Modification modification = new Modification(scmRevision.getUser(), scmRevision.getRevisionComment(), null, scmRevision.getTimestamp(), scmRevision.getRevision(), additionalData);
    if (scmRevision.getModifiedFiles() != null && !scmRevision.getModifiedFiles().isEmpty()) {
        for (ModifiedFile modifiedFile : scmRevision.getModifiedFiles()) {
            modification.createModifiedFile(modifiedFile.getFileName(), null, convertAction(modifiedFile.getAction()));
        }
    }
    return modification;
}
Also used : Modification(com.thoughtworks.go.domain.materials.Modification) ModifiedFile(com.thoughtworks.go.plugin.access.scm.revision.ModifiedFile)

Example 5 with ModifiedFile

use of com.thoughtworks.go.plugin.access.scm.revision.ModifiedFile in project gocd by gocd.

the class PluggableSCMMaterialPollerTest method shouldGetLatestModificationAlongWithAdditionalDataFromTheSCMRevision.

@Test
public void shouldGetLatestModificationAlongWithAdditionalDataFromTheSCMRevision() {
    Date timestamp = new Date();
    Map<String, String> data = new HashMap<>();
    String dataKey = "revision_data";
    String dataValue = "revision_value";
    data.put(dataKey, dataValue);
    List<ModifiedFile> modifiedFiles = new ArrayList<>(asList(new ModifiedFile("f1", ModifiedAction.added), new ModifiedFile("f2", ModifiedAction.modified), new ModifiedFile("f3", ModifiedAction.deleted)));
    SCMRevision scmRevision = new SCMRevision("revision-123", timestamp, "user", "comment", data, modifiedFiles);
    MaterialPollResult materialPollResult = new MaterialPollResult(null, scmRevision);
    when(scmExtension.getLatestRevision(eq(material.getPluginId()), scmConfiguration.capture(), materialData.capture(), eq(flyweightFolderPath))).thenReturn(materialPollResult);
    List<Modification> modifications = poller.latestModification(material, new File(flyweightFolderPath), null);
    assertThat(modifications.get(0).getRevision(), is("revision-123"));
    assertThat(modifications.get(0).getModifiedTime(), is(timestamp));
    assertThat(modifications.get(0).getUserName(), is("user"));
    assertThat(modifications.get(0).getComment(), is("comment"));
    assertThat(modifications.get(0).getAdditionalData(), is(JsonHelper.toJsonString(data)));
    assertThat(modifications.get(0).getModifiedFiles().size(), is(3));
    com.thoughtworks.go.domain.materials.ModifiedFile f1 = new com.thoughtworks.go.domain.materials.ModifiedFile("f1", null, com.thoughtworks.go.domain.materials.ModifiedAction.added);
    com.thoughtworks.go.domain.materials.ModifiedFile f2 = new com.thoughtworks.go.domain.materials.ModifiedFile("f2", null, com.thoughtworks.go.domain.materials.ModifiedAction.modified);
    com.thoughtworks.go.domain.materials.ModifiedFile f3 = new com.thoughtworks.go.domain.materials.ModifiedFile("f3", null, com.thoughtworks.go.domain.materials.ModifiedAction.deleted);
    assertThat(new HashSet(modifications.get(0).getModifiedFiles()), is(new HashSet(asList(f1, f2, f3))));
    assertConfiguration(scmConfiguration.getValue(), material.getScmConfig().getConfiguration());
    assertThat(materialData.getValue().size(), is(1));
    assertThat(materialData.getValue().get("mk-1"), is("mv-1"));
}
Also used : Modification(com.thoughtworks.go.domain.materials.Modification) MaterialPollResult(com.thoughtworks.go.plugin.access.scm.material.MaterialPollResult) ModifiedFile(com.thoughtworks.go.plugin.access.scm.revision.ModifiedFile) SCMRevision(com.thoughtworks.go.plugin.access.scm.revision.SCMRevision) ModifiedFile(com.thoughtworks.go.plugin.access.scm.revision.ModifiedFile) File(java.io.File) Test(org.junit.Test)

Aggregations

ModifiedFile (com.thoughtworks.go.plugin.access.scm.revision.ModifiedFile)5 MaterialPollResult (com.thoughtworks.go.plugin.access.scm.material.MaterialPollResult)3 SCMRevision (com.thoughtworks.go.plugin.access.scm.revision.SCMRevision)3 Test (org.junit.Test)3 Modification (com.thoughtworks.go.domain.materials.Modification)2 ModifiedAction (com.thoughtworks.go.plugin.access.scm.revision.ModifiedAction)1 File (java.io.File)1 SimpleDateFormat (java.text.SimpleDateFormat)1