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);
}
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));
}
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)));
}
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);
}
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"));
}
Aggregations