Search in sources :

Example 1 with SCMRevision

use of com.thoughtworks.go.plugin.access.scm.revision.SCMRevision 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 SCMRevision

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

the class PluggableSCMMaterialUpdaterIntegrationTest method shouldUpdateMaterialInstanceWhenAdditionalDataIsUpdatedDuringLatestModificationsSince.

@Test
public void shouldUpdateMaterialInstanceWhenAdditionalDataIsUpdatedDuringLatestModificationsSince() throws Exception {
    final PluggableSCMMaterial material = MaterialsMother.pluggableSCMMaterial();
    final MaterialInstance materialInstance = material.createMaterialInstance();
    Map<String, String> oldData = new HashMap<>();
    oldData.put("k1", "v1");
    materialInstance.setAdditionalData(new GsonBuilder().create().toJson(oldData));
    materialRepository.saveOrUpdate(materialInstance);
    Map<String, String> newData = new HashMap<>(oldData);
    newData.put("k2", "v2");
    when(scmExtension.latestModificationSince(any(String.class), any(SCMPropertyConfiguration.class), any(Map.class), any(String.class), any(SCMRevision.class))).thenReturn(new MaterialPollResult(newData, new SCMRevision()));
    mockSCMExtensionInPoller();
    scmMaterialUpdater = new ScmMaterialUpdater(materialRepository, materialChecker, subprocessExecutionContext, materialService);
    pluggableSCMMaterialUpdater = new PluggableSCMMaterialUpdater(materialRepository, scmMaterialUpdater, transactionTemplate);
    transactionTemplate.execute(new TransactionCallback() {

        @Override
        public Object doInTransaction(TransactionStatus transactionStatus) {
            pluggableSCMMaterialUpdater.insertLatestOrNewModifications(material, materialInstance, new File(""), new Modifications(new Modification()));
            return null;
        }
    });
    MaterialInstance actualInstance = materialRepository.findMaterialInstance(material);
    assertThat(actualInstance.getAdditionalDataMap(), is(newData));
}
Also used : Modification(com.thoughtworks.go.domain.materials.Modification) PluggableSCMMaterial(com.thoughtworks.go.config.materials.PluggableSCMMaterial) HashMap(java.util.HashMap) GsonBuilder(com.google.gson.GsonBuilder) MaterialPollResult(com.thoughtworks.go.plugin.access.scm.material.MaterialPollResult) TransactionStatus(org.springframework.transaction.TransactionStatus) Modifications(com.thoughtworks.go.domain.materials.Modifications) TransactionCallback(org.springframework.transaction.support.TransactionCallback) SCMRevision(com.thoughtworks.go.plugin.access.scm.revision.SCMRevision) MaterialInstance(com.thoughtworks.go.domain.MaterialInstance) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File) Test(org.junit.Test)

Example 3 with SCMRevision

use of com.thoughtworks.go.plugin.access.scm.revision.SCMRevision 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 4 with SCMRevision

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

the class SCMExtensionTest method shouldTalkToPluginToGetLatestModificationSinceLastRevision.

@Test
public void shouldTalkToPluginToGetLatestModificationSinceLastRevision() throws Exception {
    String flyweight = "flyweight";
    SCMRevision previouslyKnownRevision = new SCMRevision();
    when(jsonMessageHandler.requestMessageForLatestRevisionsSince(scmPropertyConfiguration, materialData, flyweight, previouslyKnownRevision)).thenReturn(requestBody);
    MaterialPollResult deserializedResponse = new MaterialPollResult();
    when(jsonMessageHandler.responseMessageForLatestRevisionsSince(responseBody)).thenReturn(deserializedResponse);
    MaterialPollResult response = scmExtension.latestModificationSince(PLUGIN_ID, scmPropertyConfiguration, materialData, flyweight, previouslyKnownRevision);
    assertRequest(requestArgumentCaptor.getValue(), SCMExtension.EXTENSION_NAME, "1.0", SCMExtension.REQUEST_LATEST_REVISIONS_SINCE, requestBody);
    verify(jsonMessageHandler).requestMessageForLatestRevisionsSince(scmPropertyConfiguration, materialData, flyweight, previouslyKnownRevision);
    verify(jsonMessageHandler).responseMessageForLatestRevisionsSince(responseBody);
    assertSame(response, deserializedResponse);
}
Also used : MaterialPollResult(com.thoughtworks.go.plugin.access.scm.material.MaterialPollResult) SCMRevision(com.thoughtworks.go.plugin.access.scm.revision.SCMRevision) Test(org.junit.Test)

Example 5 with SCMRevision

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

the class SCMRevisionTest method shouldAcceptDataKeyMadeUpOfAlphaNumericAndUnderScoreCharacters.

@Test
public void shouldAcceptDataKeyMadeUpOfAlphaNumericAndUnderScoreCharacters() throws Exception {
    SCMRevision scmRevision = new SCMRevision("rev123", new Date(), "loser", null, new HashMap<>(), null);
    scmRevision.addData("HELLO_WORLD123", "value");
    assertThat(scmRevision.getDataFor("HELLO_WORLD123"), is("value"));
}
Also used : SCMRevision(com.thoughtworks.go.plugin.access.scm.revision.SCMRevision) Date(java.util.Date) Test(org.junit.Test)

Aggregations

SCMRevision (com.thoughtworks.go.plugin.access.scm.revision.SCMRevision)22 Test (org.junit.Test)16 MaterialPollResult (com.thoughtworks.go.plugin.access.scm.material.MaterialPollResult)15 File (java.io.File)10 MaterialInstance (com.thoughtworks.go.domain.MaterialInstance)7 Modification (com.thoughtworks.go.domain.materials.Modification)7 ModifiedFile (com.thoughtworks.go.plugin.access.scm.revision.ModifiedFile)7 PluggableSCMMaterialRevision (com.thoughtworks.go.domain.materials.scm.PluggableSCMMaterialRevision)6 PluggableSCMMaterial (com.thoughtworks.go.config.materials.PluggableSCMMaterial)5 SCMPropertyConfiguration (com.thoughtworks.go.plugin.access.scm.SCMPropertyConfiguration)5 Date (java.util.Date)4 Map (java.util.Map)4 Modifications (com.thoughtworks.go.domain.materials.Modifications)3 Result (com.thoughtworks.go.plugin.api.response.Result)3 SimpleDateFormat (java.text.SimpleDateFormat)3 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)2 PackageMaterial (com.thoughtworks.go.config.materials.PackageMaterial)2 DependencyMaterial (com.thoughtworks.go.config.materials.dependency.DependencyMaterial)2 GitMaterial (com.thoughtworks.go.config.materials.git.GitMaterial)2 HgMaterial (com.thoughtworks.go.config.materials.mercurial.HgMaterial)2