@Override public void checkout(PluggableSCMMaterial material, File baseDir, Revision revision, SubprocessExecutionContext execCtx) { SCMPropertyConfiguration scmPropertyConfiguration = buildSCMPropertyConfigurations(material.getScmConfig()); MaterialInstance materialInstance = materialRepository.findMaterialInstance(material); PluggableSCMMaterialRevision pluggableSCMMaterialRevision = (PluggableSCMMaterialRevision) revision; SCMRevision scmRevision = new SCMRevision( pluggableSCMMaterialRevision.getRevision(), pluggableSCMMaterialRevision.getTimestamp(), null, null, pluggableSCMMaterialRevision.getData(), null); Result result = scmExtension.checkout(material.getPluginId(), scmPropertyConfiguration, baseDir.getAbsolutePath(), scmRevision); if(!result.isSuccessful()) throw new RuntimeException("Failed to perform checkout on pluggable SCM"); }
@Override public void pluginLoaded(GoPluginDescriptor pluginDescriptor) { if (scmExtension.canHandlePlugin(pluginDescriptor.id())) { fetchSCMMetaData(pluginDescriptor); } }
public Result checkConnection(final SCM scmConfig) { final String pluginId = scmConfig.getPluginConfiguration().getId(); final SCMPropertyConfiguration configuration = getScmPropertyConfiguration(scmConfig); return scmExtension.checkConnectionToSCM(pluginId, configuration); }
void fetchSCMMetaData(GoPluginDescriptor pluginDescriptor) { try { SCMPropertyConfiguration scmConfiguration = scmExtension.getSCMConfiguration(pluginDescriptor.id()); if (scmConfiguration == null) { throw new RuntimeException(format("Plugin[%s] returned null SCM configuration", pluginDescriptor.id())); } SCMView scmView = scmExtension.getSCMView(pluginDescriptor.id()); if (scmView == null) { throw new RuntimeException(format("Plugin[%s] returned null SCM view", pluginDescriptor.id())); } scmMetadataStore.addMetadataFor(pluginDescriptor.id(), new SCMConfigurations(scmConfiguration), scmView); } catch (GoPluginFrameworkException e) { LOGGER.error("Failed to fetch SCM metadata for plugin : {}", pluginDescriptor.id(), e); } }
@Before public void setUp() throws Exception { extension = mock(SCMExtension.class); SCMPropertyConfiguration value = new SCMPropertyConfiguration(); value.add(new SCMProperty("username", null).with(Property.REQUIRED, true).with(Property.SECURE, false).with(Property.PART_OF_IDENTITY, true).with(Property.DISPLAY_ORDER, 1)); value.add(new SCMProperty("password", null).with(Property.REQUIRED, true).with(Property.SECURE, true).with(Property.PART_OF_IDENTITY, false).with(Property.DISPLAY_ORDER, 2)); when(extension.getSCMConfiguration("plugin1")).thenReturn(value); when(extension.getSCMView("plugin1")).thenReturn(new SCMView() { @Override public String displayValue() { return "some scm plugin"; } @Override public String template() { return "some html"; } }); PluginSettingsConfiguration pluginSettingsConfiguration = new PluginSettingsConfiguration(); pluginSettingsConfiguration.add(new PluginSettingsProperty("k1", null).with(Property.REQUIRED, true).with(Property.SECURE, false).with(Property.DISPLAY_ORDER, 3)); when(extension.getPluginSettingsConfiguration("plugin1")).thenReturn(pluginSettingsConfiguration); when(extension.getPluginSettingsView("plugin1")).thenReturn("settings view"); }
@Test public void shouldNotFailWhenAPluginWithMultipleExtensionsHasMoreThanOneExtensionRespondingWithSettings_BUT_OneIsValidAndOtherThrowsException() throws Exception { PluginSettingsConfiguration configuration = new PluginSettingsConfiguration(); configuration.add(new PluginSettingsProperty("k1").with(Property.REQUIRED, true).with(Property.SECURE, false)); String pluginID = "plugin-id"; GoPluginDescriptor pluginDescriptor = new GoPluginDescriptor(pluginID, "1.0", null, null, null, true); setupSettingsResponses(notificationExtension, pluginID, configuration, "view"); when(packageRepositoryExtension.canHandlePlugin(pluginID)).thenReturn(false); when(scmExtension.canHandlePlugin(pluginID)).thenReturn(true); when(scmExtension.getPluginSettingsConfiguration(pluginID)).thenThrow(new RuntimeException("Ouch!")); when(scmExtension.getPluginSettingsView(pluginID)).thenReturn("view"); metadataLoader.fetchPluginSettingsMetaData(pluginDescriptor); assertThat(PluginSettingsMetadataStore.getInstance().hasPlugin(pluginID), is(true)); verify(packageRepositoryExtension, never()).getPluginSettingsConfiguration(pluginID); verify(packageRepositoryExtension, never()).getPluginSettingsView(pluginID); }
public boolean isValid(final SCM scmConfig) { if (!scmConfig.doesPluginExist()) { throw new RuntimeException(String.format("Plugin with id '%s' is not found.", scmConfig.getPluginConfiguration().getId())); } ValidationResult validationResult = scmExtension.isSCMConfigurationValid(scmConfig.getPluginConfiguration().getId(), getScmPropertyConfiguration(scmConfig)); addErrorsToConfiguration(validationResult, scmConfig); return validationResult.isSuccessful(); }
@Test public void shouldTalkToPluginToGetSCMConfiguration() throws Exception { SCMPropertyConfiguration deserializedResponse = new SCMPropertyConfiguration(); when(jsonMessageHandler.responseMessageForSCMConfiguration(responseBody)).thenReturn(deserializedResponse); SCMPropertyConfiguration response = scmExtension.getSCMConfiguration(PLUGIN_ID); assertRequest(requestArgumentCaptor.getValue(), SCM_EXTENSION, "1.0", SCMExtension.REQUEST_SCM_CONFIGURATION, null); verify(jsonMessageHandler).responseMessageForSCMConfiguration(responseBody); assertSame(response, deserializedResponse); }
@Test public void shouldTalkToPluginToGetLatestModification() throws Exception { String flyweight = "flyweight"; when(jsonMessageHandler.requestMessageForLatestRevision(scmPropertyConfiguration, materialData, flyweight)).thenReturn(requestBody); MaterialPollResult deserializedResponse = new MaterialPollResult(); when(jsonMessageHandler.responseMessageForLatestRevision(responseBody)).thenReturn(deserializedResponse); MaterialPollResult response = scmExtension.getLatestRevision(PLUGIN_ID, scmPropertyConfiguration, materialData, flyweight); assertRequest(requestArgumentCaptor.getValue(), SCM_EXTENSION, "1.0", SCMExtension.REQUEST_LATEST_REVISION, requestBody); verify(jsonMessageHandler).requestMessageForLatestRevision(scmPropertyConfiguration, materialData, flyweight); verify(jsonMessageHandler).responseMessageForLatestRevision(responseBody); assertSame(response, deserializedResponse); }
@Test public void shouldThrowAnExceptionIfScmViewReturnedByPluginIsNull() { GoPluginDescriptor descriptor = new GoPluginDescriptor("plugin1", null, null, null, null, false); when(extension.getSCMView("plugin1")).thenReturn(null); thrown.expectMessage("Plugin[plugin1] returned null scm view"); new SCMPluginInfoBuilder(extension).pluginInfoFor(descriptor); } }
@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(), SCM_EXTENSION, "1.0", SCMExtension.REQUEST_LATEST_REVISIONS_SINCE, requestBody); verify(jsonMessageHandler).requestMessageForLatestRevisionsSince(scmPropertyConfiguration, materialData, flyweight, previouslyKnownRevision); verify(jsonMessageHandler).responseMessageForLatestRevisionsSince(responseBody); assertSame(response, deserializedResponse); }
public SCMPluginInfo pluginInfoFor(GoPluginDescriptor descriptor) { SCMPropertyConfiguration scmConfiguration = extension.getSCMConfiguration(descriptor.id()); SCMView scmView = extension.getSCMView(descriptor.id()); PluggableInstanceSettings pluginSettingsAndView = getPluginSettingsAndView(descriptor, extension); if (scmConfiguration == null) { throw new RuntimeException(format("Plugin[%s] returned null scm configuration", descriptor.id())); } if (scmView == null) { throw new RuntimeException(format("Plugin[%s] returned null scm view", descriptor.id())); } PluggableInstanceSettings scmSettings = new PluggableInstanceSettings(scmPluginConfigurations(scmConfiguration), new PluginView(scmView.template())); return new SCMPluginInfo(descriptor, scmView.displayValue(), scmSettings, pluginSettingsAndView); }
@Test public void shouldTalkToPluginToCheckIfSCMConfigurationIsValid() throws Exception { when(jsonMessageHandler.requestMessageForIsSCMConfigurationValid(scmPropertyConfiguration)).thenReturn(requestBody); ValidationResult deserializedResponse = new ValidationResult(); when(jsonMessageHandler.responseMessageForIsSCMConfigurationValid(responseBody)).thenReturn(deserializedResponse); ValidationResult response = scmExtension.isSCMConfigurationValid(PLUGIN_ID, scmPropertyConfiguration); assertRequest(requestArgumentCaptor.getValue(), SCM_EXTENSION, "1.0", SCMExtension.REQUEST_VALIDATE_SCM_CONFIGURATION, requestBody); verify(jsonMessageHandler).requestMessageForIsSCMConfigurationValid(scmPropertyConfiguration); verify(jsonMessageHandler).responseMessageForIsSCMConfigurationValid(responseBody); assertSame(response, deserializedResponse); }
@Test public void shouldThrowAnExceptionIfScmConfigReturnedByPluginIsNull() { GoPluginDescriptor descriptor = new GoPluginDescriptor("plugin1", null, null, null, null, false); when(extension.getSCMConfiguration("plugin1")).thenReturn(null); thrown.expectMessage("Plugin[plugin1] returned null scm configuration"); new SCMPluginInfoBuilder(extension).pluginInfoFor(descriptor); }
@Override public List<Modification> latestModification(final PluggableSCMMaterial material, File baseDir, SubprocessExecutionContext execCtx) { SCMPropertyConfiguration scmPropertyConfiguration = buildSCMPropertyConfigurations(material.getScmConfig()); final MaterialInstance materialInstance = materialRepository.findMaterialInstance(material); MaterialPollResult pollResult = scmExtension.getLatestRevision(material.getPluginId(), scmPropertyConfiguration, materialInstance.getAdditionalDataMap(), baseDir.getAbsolutePath()); final Map<String, String> materialData = pollResult.getMaterialData(); if (materialInstance.requiresUpdate(materialData)) { updateAdditionalData(materialInstance.getId(), materialData); } SCMRevision scmRevision = pollResult.getLatestRevision(); return scmRevision == null ? new Modifications() : new Modifications(getModification(scmRevision)); }
@Test public void shouldTalkToPluginToGetSCMView() throws Exception { SCMView deserializedResponse = new SCMView() { @Override public String displayValue() { return null; } @Override public String template() { return null; } }; when(jsonMessageHandler.responseMessageForSCMView(responseBody)).thenReturn(deserializedResponse); SCMView response = scmExtension.getSCMView(PLUGIN_ID); assertRequest(requestArgumentCaptor.getValue(), SCM_EXTENSION, "1.0", SCMExtension.REQUEST_SCM_VIEW, null); verify(jsonMessageHandler).responseMessageForSCMView(responseBody); assertSame(response, deserializedResponse); }
@Override public List<Modification> modificationsSince(final PluggableSCMMaterial material, File baseDir, final Revision revision, SubprocessExecutionContext execCtx) { SCMPropertyConfiguration scmPropertyConfiguration = buildSCMPropertyConfigurations(material.getScmConfig()); MaterialInstance materialInstance = materialRepository.findMaterialInstance(material); PluggableSCMMaterialRevision pluggableSCMMaterialRevision = (PluggableSCMMaterialRevision) revision; SCMRevision previouslyKnownRevision = new SCMRevision(pluggableSCMMaterialRevision.getRevision(), pluggableSCMMaterialRevision.getTimestamp(), null, null, pluggableSCMMaterialRevision.getData(), null); MaterialPollResult pollResult = scmExtension.latestModificationSince(material.getPluginId(), scmPropertyConfiguration, materialInstance.getAdditionalDataMap(), baseDir.getAbsolutePath(), previouslyKnownRevision); final Map<String, String> materialData = pollResult.getMaterialData(); if (materialInstance.requiresUpdate(materialData)) { updateAdditionalData(materialInstance.getId(), materialData); } List<SCMRevision> scmRevisions = pollResult.getRevisions(); return getModifications(scmRevisions); }
@Test(expected = RuntimeException.class) public void shouldLogToErrorOutputWhenPluginSendsErrorResponse() { SCM scmConfig = SCMMother.create("scm-id", "scm-name", "pluginid", "version", new Configuration()); PluggableSCMMaterial pluggableSCMMaterial = MaterialsMother.pluggableSCMMaterial(); pluggableSCMMaterial.setFolder("destination-folder"); pluggableSCMMaterial.setSCMConfig(scmConfig); Modification modification = ModificationsMother.oneModifiedFile("r1"); Map<String, String> additionalData = new HashMap<>(); additionalData.put("a1", "v1"); additionalData.put("a2", "v2"); modification.setAdditionalData(new Gson().toJson(additionalData)); MaterialRevision revision = new MaterialRevision(pluggableSCMMaterial, modification); String pipelineFolder = new File(System.getProperty("java.io.tmpdir")).getAbsolutePath(); String destinationFolder = new File(pipelineFolder, "destination-folder").getAbsolutePath(); PluggableSCMMaterialAgent pluggableSCMMaterialAgent = new PluggableSCMMaterialAgent(scmExtension, revision, new File(pipelineFolder), consumer); when(scmExtension.checkout(eq("pluginid"), any(), eq(destinationFolder), any())) .thenThrow(new RuntimeException("some message from plugin")); pluggableSCMMaterialAgent.prepare(); verify(consumer, times(1)).errOutput("Material scm-name checkout failed: some message from plugin"); } }
@Test public void shouldThrowExceptionWhenNullSCMViewReturned() { when(scmExtension.getSCMConfiguration(pluginDescriptor.id())).thenReturn(new SCMPropertyConfiguration()); when(scmExtension.getSCMView(pluginDescriptor.id())).thenReturn(null); try { metadataLoader.fetchSCMMetaData(pluginDescriptor); } catch (Exception e) { assertThat(e.getMessage(), is("Plugin[plugin-id] returned null SCM view")); } assertThat(SCMMetadataStore.getInstance().getConfigurationMetadata(pluginDescriptor.id()), nullValue()); }
@Override public void pluginUnLoaded(GoPluginDescriptor pluginDescriptor) { if (scmExtension.canHandlePlugin(pluginDescriptor.id())) { scmMetadataStore.removeMetadata(pluginDescriptor.id()); } } }