use of com.facebook.buck.doctor.config.DoctorEndpointRequest in project buck by facebook.
the class DoctorCommand method runWithoutHelp.
@Override
public int runWithoutHelp(CommandRunnerParams params) throws IOException, InterruptedException {
ProjectFilesystem filesystem = params.getCell().getFilesystem();
BuildLogHelper buildLogHelper = new BuildLogHelper(filesystem, params.getObjectMapper());
UserInput userInput = new UserInput(params.getConsole().getStdOut(), new BufferedReader(new InputStreamReader(params.getStdIn())));
DoctorReportHelper helper = new DoctorReportHelper(params.getCell().getFilesystem(), userInput, params.getConsole(), params.getObjectMapper(), params.getBuckConfig().getView(DoctorConfig.class));
Optional<BuildLogEntry> entry = helper.promptForBuild(new ArrayList<>(buildLogHelper.getBuildLogs()));
if (!entry.isPresent()) {
params.getConsole().getStdOut().println("No interesting commands found in buck-out/log.");
return 0;
}
Optional<DefectSubmitResult> rageResult = generateRageReport(params, userInput, entry.get());
if (!rageResult.isPresent()) {
params.getConsole().printErrorText("Failed to generate report to send.");
return 1;
}
DoctorEndpointRequest request = helper.generateEndpointRequest(entry.get(), rageResult.get());
DoctorEndpointResponse response = helper.uploadRequest(request);
helper.presentResponse(response);
helper.presentRageResult(rageResult);
return 0;
}
use of com.facebook.buck.doctor.config.DoctorEndpointRequest in project buck by facebook.
the class DoctorCommandIntegrationTest method testEndpointUrl.
@Test
public void testEndpointUrl() throws Exception {
DoctorReportHelper helper = createDoctorHelper((new UserInputFixture("0")).getUserInput(), DoctorConfig.of(FakeBuckConfig.builder().build()));
BuildLogHelper buildLogHelper = new BuildLogHelper(workspace.asCell().getFilesystem(), objectMapper);
Optional<BuildLogEntry> entry = helper.promptForBuild(new ArrayList<>(buildLogHelper.getBuildLogs()));
DoctorEndpointRequest request = helper.generateEndpointRequest(entry.get(), rageResult);
DoctorEndpointResponse response = helper.uploadRequest(request);
assertEquals("Please define URL", response.getErrorMessage().get(), "Doctor endpoint URL is not set. Please set [doctor] endpoint_url on your .buckconfig");
}
use of com.facebook.buck.doctor.config.DoctorEndpointRequest in project buck by facebook.
the class DoctorCommandIntegrationTest method testPromptWithoutRageReport.
@Test
public void testPromptWithoutRageReport() throws Exception {
assumeThat(Platform.detect(), Matchers.not(Matchers.is(Platform.WINDOWS)));
DoctorReportHelper helper = createDoctorHelper((new UserInputFixture("0")).getUserInput(), createDoctorConfig(httpd));
BuildLogHelper buildLogHelper = new BuildLogHelper(workspace.asCell().getFilesystem(), objectMapper);
Optional<BuildLogEntry> entry = helper.promptForBuild(new ArrayList<>(buildLogHelper.getBuildLogs()));
DoctorEndpointRequest request = helper.generateEndpointRequest(entry.get(), rageResult);
DoctorEndpointResponse response = helper.uploadRequest(request);
helper.presentResponse(response);
assertEquals(response, doctorResponse);
assertEquals("\n:: Suggestions\n- [Error] Suggestion no1\n- [Warning][Area] Suggestion no2\n\n", ((TestConsole) helper.getConsole()).getTextWrittenToStdOut());
}
use of com.facebook.buck.doctor.config.DoctorEndpointRequest in project buck by facebook.
the class DoctorReportHelper method uploadRequest.
public DoctorEndpointResponse uploadRequest(DoctorEndpointRequest request) {
if (!doctorConfig.getEndpointUrl().isPresent()) {
String errorMsg = String.format("Doctor endpoint URL is not set. Please set [%s] %s on your .buckconfig", DoctorConfig.DOCTOR_SECTION, DoctorConfig.URL_FIELD);
return createErrorDoctorEndpointResponse(errorMsg);
}
byte[] requestJson;
try {
requestJson = objectMapper.writeValueAsBytes(request);
} catch (JsonProcessingException e) {
return createErrorDoctorEndpointResponse("Failed to encode request to JSON. " + "Reason: " + e.getMessage());
}
OkHttpClient httpClient = new OkHttpClient.Builder().connectTimeout(doctorConfig.getHttpTimeoutMs(), TimeUnit.MILLISECONDS).readTimeout(doctorConfig.getHttpTimeoutMs(), TimeUnit.MILLISECONDS).writeTimeout(doctorConfig.getHttpTimeoutMs(), TimeUnit.MILLISECONDS).build();
Response httpResponse;
try {
RequestBody requestBody;
ImmutableMap<String, String> extraArgs = doctorConfig.getExtraRequestArgs();
if (extraArgs.isEmpty()) {
requestBody = RequestBody.create(JSON, requestJson);
} else {
FormBody.Builder formBody = new FormBody.Builder();
formBody.add("data", new String(requestJson));
for (Map.Entry<String, String> entry : extraArgs.entrySet()) {
formBody.add(entry.getKey(), entry.getValue());
}
requestBody = formBody.build();
}
Request httpRequest = new Request.Builder().url(doctorConfig.getEndpointUrl().get()).post(requestBody).build();
httpResponse = httpClient.newCall(httpRequest).execute();
} catch (IOException e) {
return createErrorDoctorEndpointResponse("Failed to perform the request to " + doctorConfig.getEndpointUrl().get() + ". Reason: " + e.getMessage());
}
try {
if (httpResponse.isSuccessful()) {
String body = new String(httpResponse.body().bytes(), Charsets.UTF_8);
return objectMapper.readValue(body, DoctorEndpointResponse.class);
}
return createErrorDoctorEndpointResponse("Request was not successful.");
} catch (JsonProcessingException e) {
return createErrorDoctorEndpointResponse(String.format(DECODE_FAIL_TEMPLATE, e.getMessage()));
} catch (IOException e) {
return createErrorDoctorEndpointResponse(String.format(DECODE_FAIL_TEMPLATE, e.getMessage()));
}
}
Aggregations