Java源码示例:com.thoughtworks.go.plugin.api.request.DefaultGoApiRequest

示例1
public void appendToConsoleLog(JobIdentifier jobIdentifier, String text) throws ServerRequestFailedException {
    Map<String, String> requestMap = new HashMap<>();
    requestMap.put("pipelineName", jobIdentifier.getPipelineName());
    requestMap.put("pipelineCounter", String.valueOf(jobIdentifier.getPipelineCounter()));
    requestMap.put("stageName", jobIdentifier.getStageName());
    requestMap.put("stageCounter", jobIdentifier.getStageCounter());
    requestMap.put("jobName", jobIdentifier.getJobName());
    requestMap.put("text", text);

    DefaultGoApiRequest request = new DefaultGoApiRequest(Constants.REQUEST_SERVER_APPEND_TO_CONSOLE_LOG, CONSOLE_LOG_API_VERSION, PLUGIN_IDENTIFIER);
    request.setRequestBody(new GsonBuilder().create().toJson(requestMap));

    GoApiResponse response = accessor.submit(request);

    if (response.responseCode() != 200) {
        LOG.error("Failed to append to console log for " + jobIdentifier.getRepresentation() + " with text: " + text);
    }
}
 
示例2
public void appendToConsoleLog(JobIdentifier jobIdentifier, String text) throws ServerRequestFailedException {
    Map<String, String> requestMap = new HashMap<>();
    requestMap.put("pipelineName", jobIdentifier.getPipelineName());
    requestMap.put("pipelineCounter", String.valueOf(jobIdentifier.getPipelineCounter()));
    requestMap.put("stageName", jobIdentifier.getStageName());
    requestMap.put("stageCounter", jobIdentifier.getStageCounter());
    requestMap.put("jobName", jobIdentifier.getJobName());
    requestMap.put("text", text);

    DefaultGoApiRequest request = new DefaultGoApiRequest(Constants.REQUEST_SERVER_APPEND_TO_CONSOLE_LOG, CONSOLE_LOG_API_VERSION, PLUGIN_IDENTIFIER);
    request.setRequestBody(new GsonBuilder().create().toJson(requestMap));

    GoApiResponse response = accessor.submit(request);

    if (response.responseCode() != 200) {
        LOG.error("Failed to append to console log for " + jobIdentifier.represent() + " with text: " + text);
    }
}
 
示例3
@Test
void shouldRouteMessageToConsoleService() throws IOException, IllegalArtifactLocationException {
    Map<String, String> requestMap = new HashMap<>();
    requestMap.put("pipelineName", "p1");
    requestMap.put("pipelineCounter", "1");
    requestMap.put("stageName", "s1");
    requestMap.put("stageCounter", "2");
    requestMap.put("jobName", "j1");
    requestMap.put("text", "message1");

    DefaultGoApiRequest goApiRequest = new DefaultGoApiRequest(APPEND_TO_CONSOLE_LOG, VERSION_1, null);
    goApiRequest.setRequestBody(new GsonBuilder().create().toJson(requestMap));

    final ConsoleLogRequestProcessor processor = new ConsoleLogRequestProcessor(pluginRequestProcessorRegistry, consoleService);
    final GoApiResponse response = processor.process(pluginDescriptor, goApiRequest);

    assertThat(response.responseCode(), is(DefaultGoApiResponse.SUCCESS_RESPONSE_CODE));

    final JobIdentifier jobIdentifier = new JobIdentifier("p1", 1, null, "s1", "2", "j1");
    verify(consoleService).appendToConsoleLog(jobIdentifier, "message1");
}
 
示例4
@Test
void shouldRouteMessageToConsoleService() throws IOException, IllegalArtifactLocationException {
    Map<String, String> requestMap = new HashMap<>();
    requestMap.put("pipeline_name", "p1");
    requestMap.put("pipeline_counter", "1");
    requestMap.put("stage_name", "s1");
    requestMap.put("stage_counter", "2");
    requestMap.put("job_name", "j1");
    requestMap.put("text", "message1");

    DefaultGoApiRequest goApiRequest = new DefaultGoApiRequest(APPEND_TO_CONSOLE_LOG, VERSION_2, null);
    goApiRequest.setRequestBody(new GsonBuilder().create().toJson(requestMap));

    final ConsoleLogRequestProcessor processor = new ConsoleLogRequestProcessor(pluginRequestProcessorRegistry, consoleService);
    final GoApiResponse response = processor.process(pluginDescriptor, goApiRequest);

    assertThat(response.responseCode(), is(DefaultGoApiResponse.SUCCESS_RESPONSE_CODE));

    final JobIdentifier jobIdentifier = new JobIdentifier("p1", 1, null, "s1", "2", "j1");
    verify(consoleService).appendToConsoleLog(jobIdentifier, "message1");
}
 
示例5
private void sendLog(ConsoleLogMessage consoleLogMessage) {
    DefaultGoApiRequest request = new DefaultGoApiRequest(Constants.SEND_CONSOLE_LOG, CONSOLE_LOG_PROCESSOR_API_VERSION, PLUGIN_IDENTIFIER);
    request.setRequestBody(consoleLogMessage.toJSON());

    GoApiResponse response = accessor.submit(request);
    if (response.responseCode() != DefaultGoApiResponse.SUCCESS_RESPONSE_CODE) {
        LOG.error(String.format("Failed to submit console log: %s", response.responseBody()));
    }
}
 
示例6
public ServerInfo getSeverInfo() throws ServerRequestFailedException {
    DefaultGoApiRequest request = new DefaultGoApiRequest(Constants.REQUEST_SERVER_INFO, SERVER_INFO_API_VERSION, PLUGIN_IDENTIFIER);
    GoApiResponse response = accessor.submit(request);

    if (response.responseCode() != 200) {
        throw ServerRequestFailedException.serverInfo(response);
    }

    return ServerInfo.fromJSON(response.responseBody());
}
 
示例7
public Agents listAgents() throws ServerRequestFailedException {
    DefaultGoApiRequest request = new DefaultGoApiRequest(Constants.REQUEST_SERVER_LIST_AGENTS, PROCESSOR_API_VERSION, PLUGIN_IDENTIFIER);
    GoApiResponse response = accessor.submit(request);

    if (response.responseCode() != 200) {
        throw ServerRequestFailedException.listAgents(response);
    }

    return new Agents(Agent.fromJSONArray(response.responseBody()));
}
 
示例8
public void disableAgents(Collection<Agent> toBeDisabled) throws ServerRequestFailedException {
    LOG.debug(format("[Server Ping] Disabling Agents: {0}", toBeDisabled.toString()));
    if (toBeDisabled.isEmpty()) {
        return;
    }

    DefaultGoApiRequest request = new DefaultGoApiRequest(Constants.REQUEST_SERVER_DISABLE_AGENT, PROCESSOR_API_VERSION, PLUGIN_IDENTIFIER);
    request.setRequestBody(Agent.toJSONArray(toBeDisabled));

    GoApiResponse response = accessor.submit(request);

    if (response.responseCode() != 200) {
        throw ServerRequestFailedException.disableAgents(response);
    }
}
 
示例9
public void deleteAgents(Collection<Agent> toBeDeleted) throws ServerRequestFailedException {
    LOG.debug(format("[Server Ping] Deleting Agents: {0}", toBeDeleted.toString()));
    if (toBeDeleted.isEmpty()) {
        return;
    }

    DefaultGoApiRequest request = new DefaultGoApiRequest(Constants.REQUEST_SERVER_DELETE_AGENT, PROCESSOR_API_VERSION, PLUGIN_IDENTIFIER);
    request.setRequestBody(Agent.toJSONArray(toBeDeleted));
    GoApiResponse response = accessor.submit(request);

    if (response.responseCode() != 200) {
        throw ServerRequestFailedException.deleteAgents(response);
    }
}
 
示例10
public Agents listAgents() throws ServerRequestFailedException {
    DefaultGoApiRequest request = new DefaultGoApiRequest(Constants.REQUEST_SERVER_LIST_AGENTS, PROCESSOR_API_VERSION, PLUGIN_IDENTIFIER);
    GoApiResponse response = accessor.submit(request);

    if (response.responseCode() != 200) {
        throw ServerRequestFailedException.listAgents(response);
    }

    return new Agents(Agent.fromJSONArray(response.responseBody()));
}
 
示例11
public void disableAgents(Collection<Agent> toBeDisabled) throws ServerRequestFailedException {
    if (toBeDisabled.isEmpty()) {
        return;
    }

    DefaultGoApiRequest request = new DefaultGoApiRequest(Constants.REQUEST_SERVER_DISABLE_AGENT, PROCESSOR_API_VERSION, PLUGIN_IDENTIFIER);
    request.setRequestBody(Agent.toJSONArray(toBeDisabled));

    GoApiResponse response = accessor.submit(request);

    if (response.responseCode() != 200) {
        throw ServerRequestFailedException.disableAgents(response);
    }
}
 
示例12
public void deleteAgents(Collection<Agent> toBeDeleted) throws ServerRequestFailedException {
    if (toBeDeleted.isEmpty()) {
        return;
    }

    DefaultGoApiRequest request = new DefaultGoApiRequest(Constants.REQUEST_SERVER_DELETE_AGENT, PROCESSOR_API_VERSION, PLUGIN_IDENTIFIER);
    request.setRequestBody(Agent.toJSONArray(toBeDeleted));
    GoApiResponse response = accessor.submit(request);

    if (response.responseCode() != 200) {
        throw ServerRequestFailedException.deleteAgents(response);
    }
}
 
示例13
public Agents listAgents() throws ServerRequestFailedException {
    DefaultGoApiRequest request = new DefaultGoApiRequest(Constants.REQUEST_SERVER_LIST_AGENTS, PROCESSOR_API_VERSION, PLUGIN_IDENTIFIER);
    GoApiResponse response = accessor.submit(request);

    if (response.responseCode() != 200) {
        throw ServerRequestFailedException.listAgents(response);
    }

    return new Agents(Agent.fromJSONArray(response.responseBody()));
}
 
示例14
public void disableAgents(Collection<Agent> toBeDisabled) throws ServerRequestFailedException {
    if (toBeDisabled.isEmpty()) {
        return;
    }

    DefaultGoApiRequest request = new DefaultGoApiRequest(Constants.REQUEST_SERVER_DISABLE_AGENT, PROCESSOR_API_VERSION, PLUGIN_IDENTIFIER);
    request.setRequestBody(Agent.toJSONArray(toBeDisabled));

    GoApiResponse response = accessor.submit(request);

    if (response.responseCode() != 200) {
        throw ServerRequestFailedException.disableAgents(response);
    }
}
 
示例15
public void deleteAgents(Collection<Agent> toBeDeleted) throws ServerRequestFailedException {
    if (toBeDeleted.isEmpty()) {
        return;
    }

    DefaultGoApiRequest request = new DefaultGoApiRequest(Constants.REQUEST_SERVER_DELETE_AGENT, PROCESSOR_API_VERSION, PLUGIN_IDENTIFIER);
    request.setRequestBody(Agent.toJSONArray(toBeDeleted));
    GoApiResponse response = accessor.submit(request);

    if (response.responseCode() != 200) {
        throw ServerRequestFailedException.deleteAgents(response);
    }
}
 
示例16
@Test
public void shouldReturnAServerIdInJSONForm() {
    DefaultGoApiRequest request = new DefaultGoApiRequest(GET_SERVER_INFO, "1.0", new GoPluginIdentifier("extension1", Arrays.asList("1.0")));


    GoApiResponse response = processor.process(pluginDescriptor, request);

    assertThat(response.responseCode(), is(200));
    assertThat(response.responseBody(),
            is(format("{\"server_id\":\"%s\",\"site_url\":\"%s\",\"secure_site_url\":\"%s\"}",
                    serverConfig.getServerId(), serverConfig.getSiteUrl().getUrl(), serverConfig.getSecureSiteUrl().getUrl())));
}
 
示例17
@Test
public void shouldReturnSuccessForServerInfoV2() {
    DefaultGoApiRequest request = new DefaultGoApiRequest(GET_SERVER_INFO, "2.0", new GoPluginIdentifier("extension1", Arrays.asList("1.0")));

    GoApiResponse response = processor.process(pluginDescriptor, request);

    assertThat(response.responseCode(), is(200));
}
 
示例18
@Test
public void shouldReturnAErrorResponseIfExtensionDoesNotSupportServerInfo() {
    DefaultGoApiRequest request = new DefaultGoApiRequest(GET_SERVER_INFO, "bad-version", new GoPluginIdentifier("foo", Arrays.asList("1.0")));

    GoApiResponse response = processor.process(pluginDescriptor, request);

    assertThat(response.responseCode(), is(400));
}
 
示例19
@Test
void shouldRespondWithAMessageIfSomethingGoesWrong() {
    DefaultGoApiRequest goApiRequest = new DefaultGoApiRequest(APPEND_TO_CONSOLE_LOG, VERSION_1, null);
    goApiRequest.setRequestBody("this_is_invalid_JSON");

    final ConsoleLogRequestProcessor processor = new ConsoleLogRequestProcessor(pluginRequestProcessorRegistry, consoleService);
    final GoApiResponse response = processor.process(pluginDescriptor, goApiRequest);

    assertThat(response.responseCode(), is(DefaultGoApiResponse.INTERNAL_ERROR));

    final Map responseContents = new Gson().fromJson(response.responseBody(), Map.class);
    assertThat((String) responseContents.get("message"), containsString("Error:"));
}
 
示例20
@Test
void shouldRespondWithAMessageIfSomethingGoesWrong() {
    DefaultGoApiRequest goApiRequest = new DefaultGoApiRequest(APPEND_TO_CONSOLE_LOG, VERSION_2, null);
    goApiRequest.setRequestBody("this_is_invalid_JSON");

    final ConsoleLogRequestProcessor processor = new ConsoleLogRequestProcessor(pluginRequestProcessorRegistry, consoleService);
    final GoApiResponse response = processor.process(pluginDescriptor, goApiRequest);

    assertThat(response.responseCode(), is(DefaultGoApiResponse.INTERNAL_ERROR));

    final Map responseContents = new Gson().fromJson(response.responseBody(), Map.class);
    assertThat((String) responseContents.get("message"), containsString("Error:"));
}
 
示例21
@Test
public void shouldGetPluginSettingsForPluginThatExistsInDB() {
    String PLUGIN_ID = "plugin-foo-id";

    when(pluginDescriptor.id()).thenReturn(PLUGIN_ID);
    when(pluginSqlMapDao.findPlugin(PLUGIN_ID)).thenReturn(new Plugin(PLUGIN_ID, "{\"k1\": \"v1\",\"k2\": \"v2\"}"));

    DefaultGoApiRequest apiRequest = new DefaultGoApiRequest(PluginSettingsRequestProcessor.GET_PLUGIN_SETTINGS, "1.0", new GoPluginIdentifier("extension1", Collections.singletonList("1.0")));
    apiRequest.setRequestBody("expected-request");
    GoApiResponse response = processor.process(pluginDescriptor, apiRequest);

    assertThat(response.responseCode(), is(200));
    assertThat(response.responseBody(), is("{\"k1\":\"v1\",\"k2\":\"v2\"}"));
}
 
示例22
@Test
public void shouldNotGetPluginSettingsForPluginThatDoesNotExistInDB() {
    String PLUGIN_ID = "plugin-foo-id";
    String requestBody = "expected-request";

    when(pluginDescriptor.id()).thenReturn(PLUGIN_ID);
    when(pluginSqlMapDao.findPlugin(PLUGIN_ID)).thenReturn(new NullPlugin());

    DefaultGoApiRequest apiRequest = new DefaultGoApiRequest(PluginSettingsRequestProcessor.GET_PLUGIN_SETTINGS, "1.0", new GoPluginIdentifier("extension1", Collections.singletonList("1.0")));
    apiRequest.setRequestBody(requestBody);
    GoApiResponse response = processor.process(pluginDescriptor, apiRequest);

    assertThat(response.responseCode(), is(200));
    assertThat(response.responseBody(), is(nullValue()));
}
 
示例23
@Test
public void shouldRespondWith400InCaseOfErrors() {
    String PLUGIN_ID = "plugin-foo-id";

    when(pluginDescriptor.id()).thenReturn(PLUGIN_ID);
    when(pluginSqlMapDao.findPlugin(PLUGIN_ID)).thenThrow(new RuntimeException());

    DefaultGoApiRequest apiRequest = new DefaultGoApiRequest(PluginSettingsRequestProcessor.GET_PLUGIN_SETTINGS, "1.0", new GoPluginIdentifier("extension1", Collections.singletonList("1.0")));
    apiRequest.setRequestBody("expected-request");

    GoApiResponse response = processor.process(pluginDescriptor, apiRequest);

    assertThat(response.responseCode(), is(400));
}
 
示例24
@Test
public void shouldProcessInvalidateCacheRequest() throws Exception {
    PluginRoleService pluginRoleService = mock(PluginRoleService.class);
    when(authorizationExtension.getMessageConverter(AuthorizationMessageConverterV1.VERSION)).thenReturn(new AuthorizationMessageConverterV1());

    GoApiRequest request = new DefaultGoApiRequest(INVALIDATE_CACHE_REQUEST.requestName(), "1.0", null);
    AuthorizationRequestProcessor authorizationRequestProcessor = new AuthorizationRequestProcessor(registry, pluginRoleService);

    GoApiResponse response = authorizationRequestProcessor.process(pluginDescriptor, request);

    assertThat(response.responseCode(), is(200));
    verify(pluginRoleService).invalidateRolesFor("cd.go.authorization.github");
}
 
示例25
public static GoApiRequest requestWithCredentials(String username, String password) {
    final DefaultGoApiRequest defaultGoApiRequest = new DefaultGoApiRequest(null, null, null);
    defaultGoApiRequest.setRequestBody(requestJson(username, password));
    return defaultGoApiRequest;
}
 
示例26
public void addServerHealthMessage(List<Map<String, String>> messages) {
    Gson gson = new Gson();

    DefaultGoApiRequest request = new DefaultGoApiRequest(Constants.REQUEST_SERVER_SERVER_HEALTH_ADD_MESSAGES, PROCESSOR_API_VERSION, PLUGIN_IDENTIFIER);

    request.setRequestBody(gson.toJson(messages));

    // submit the request
    GoApiResponse response = accessor.submit(request);

    // check status
    if (response.responseCode() != 200) {
        LOG.error("The server sent an unexpected status code " + response.responseCode() + " with the response body " + response.responseBody());
    }

}
 
示例27
@Test
public void shouldRegisterAPIRequestWithProcessor() {
    DefaultGoApiRequest request = new DefaultGoApiRequest(GET_SERVER_INFO, "1.0", new GoPluginIdentifier("extension1", Collections.singletonList("1.0")));
    assertThat(processorRegistry.canProcess(request), is(true));
}
 
示例28
private DefaultGoApiRequest createRequest(String apiVersion, String requestBody) {
    DefaultGoApiRequest request = new DefaultGoApiRequest(ADD_SERVER_HEALTH_MESSAGES, apiVersion, null);
    request.setRequestBody(requestBody);
    return request;
}
 
示例29
public void addServerHealthMessage(List<Map<String, String>> messages) {
    Gson gson = new Gson();

    DefaultGoApiRequest request = new DefaultGoApiRequest(Constants.REQUEST_SERVER_SERVER_HEALTH_ADD_MESSAGES, PROCESSOR_API_VERSION, PLUGIN_IDENTIFIER);

    request.setRequestBody(gson.toJson(messages));

    GoApiResponse response = accessor.submit(request);

    if (response.responseCode() != 200) {
        LOG.error("The server sent an unexpected status code " + response.responseCode() + " with the response body " + response.responseBody());
    }
}