Search in sources :

Example 16 with Result

use of de.tum.in.www1.artemis.domain.Result in project ArTEMiS by ls1intum.

the class BambooService method retrieveLatestBuildResult.

/**
 * Performs a request to the Bamboo REST API to retrive the latest result for the given plan.
 *
 * @param planKey the key of the plan for which to retrieve the latest result
 * @return a map containing the following data:
 * - successful:       if the build was successful
 * - buildTestSummary:      a string generated by Bamboo summarizing the build result
 * - buildCompletedDate:    the completion date of the build
 */
private Map<String, Object> retrieveLatestBuildResult(String planKey) {
    HttpHeaders headers = HeaderUtil.createAuthorization(BAMBOO_USER, BAMBOO_PASSWORD);
    HttpEntity<?> entity = new HttpEntity<>(headers);
    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<Map> response = null;
    try {
        response = restTemplate.exchange(BAMBOO_SERVER_URL + "/rest/api/latest/result/" + planKey.toUpperCase() + "/latest.json?expand=testResults,artifacts", HttpMethod.GET, entity, Map.class);
    } catch (Exception e) {
        log.error("HttpError while retrieving results", e);
    }
    if (response != null) {
        Map<String, Object> result = new HashMap<>();
        boolean successful = (boolean) response.getBody().get("successful");
        result.put("successful", successful);
        String buildTestSummary = (String) response.getBody().get("buildTestSummary");
        result.put("buildTestSummary", buildTestSummary);
        String dateString = (String) response.getBody().get("buildCompletedDate");
        ZonedDateTime buildCompletedDate = ZonedDateTime.parse(dateString);
        result.put("buildCompletedDate", buildCompletedDate);
        if (response.getBody().containsKey("artifacts")) {
            Map<String, Object> artifacts = (Map<String, Object>) response.getBody().get("artifacts");
            if ((int) artifacts.get("size") > 0 && artifacts.containsKey("artifact")) {
                Map<String, Object> firstArtifact = (Map<String, Object>) ((ArrayList<Map>) artifacts.get("artifact")).get(0);
                String artifact = (String) ((Map<String, Object>) firstArtifact.get("link")).get("href");
                result.put("artifact", artifact);
            }
        }
        return result;
    }
    return null;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) GitException(de.tum.in.www1.artemis.exception.GitException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) BambooException(de.tum.in.www1.artemis.exception.BambooException) ZonedDateTime(java.time.ZonedDateTime) RestTemplate(org.springframework.web.client.RestTemplate)

Example 17 with Result

use of de.tum.in.www1.artemis.domain.Result in project ArTEMiS by ls1intum.

the class LtiService method onNewBuildResult.

/**
 * This method is pinged on new build results.
 * It sends an message to the LTI consumer with the new score.
 *
 * @param participation
 */
public void onNewBuildResult(Participation participation) {
    // Get the LTI outcome URL
    Optional<LtiOutcomeUrl> ltiOutcomeUrl = ltiOutcomeUrlRepository.findByUserAndExercise(participation.getStudent(), participation.getExercise());
    ltiOutcomeUrl.ifPresent(ltiOutcomeUrl1 -> {
        String score = "0.00";
        // Get the latest result
        Optional<Result> latestResult = resultRepository.findFirstByParticipationIdOrderByCompletionDateDesc(participation.getId());
        if (latestResult.isPresent() && latestResult.get().getScore() != null) {
            // LTI scores needs to be formatted as String between "0.00" and "1.00"
            score = String.format(Locale.ROOT, "%.2f", latestResult.get().getScore().floatValue() / 100);
        }
        log.debug("Reporting to LTI consumer: Score {} for Participation {}", score, participation);
        try {
            // Using PatchedIMSPOXRequest until they fixed the problem: https://github.com/IMSGlobal/basiclti-util-java/issues/27
            HttpPost request = PatchedIMSPOXRequest.buildReplaceResult(ltiOutcomeUrl1.getUrl(), OAUTH_KEY, OAUTH_SECRET, ltiOutcomeUrl1.getSourcedId(), score, null, false);
            HttpClient client = HttpClientBuilder.create().build();
            HttpResponse response = client.execute(request);
            String responseString = new BasicResponseHandler().handleResponse(response);
            log.debug("Response from LTI consumer: {}", responseString);
            if (response.getStatusLine().getStatusCode() >= 400) {
                throw new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase());
            }
        } catch (Exception e) {
            log.error("Reporting to LTI consumer failed: {}", e);
        }
    });
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpClient(org.apache.http.client.HttpClient) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) HttpResponse(org.apache.http.HttpResponse) HttpResponseException(org.apache.http.client.HttpResponseException) LtiVerificationException(org.imsglobal.lti.launch.LtiVerificationException) InternalAuthenticationServiceException(org.springframework.security.authentication.InternalAuthenticationServiceException) AuthenticationException(org.springframework.security.core.AuthenticationException) HttpResponseException(org.apache.http.client.HttpResponseException) ArtemisAuthenticationException(de.tum.in.www1.artemis.exception.ArtemisAuthenticationException) LtiVerificationResult(org.imsglobal.lti.launch.LtiVerificationResult)

Example 18 with Result

use of de.tum.in.www1.artemis.domain.Result in project ArTEMiS by ls1intum.

the class LtiUserIdResource method createLtiUserId.

/**
 * POST  /lti-user-ids : Create a new ltiUserId.
 *
 * @param ltiUserId the ltiUserId to create
 * @return the ResponseEntity with status 201 (Created) and with body the new ltiUserId, or with status 400 (Bad Request) if the ltiUserId has already an ID
 * @throws URISyntaxException if the Location URI syntax is incorrect
 */
@PostMapping("/lti-user-ids")
@Timed
public ResponseEntity<LtiUserId> createLtiUserId(@RequestBody LtiUserId ltiUserId) throws URISyntaxException {
    log.debug("REST request to save LtiUserId : {}", ltiUserId);
    if (ltiUserId.getId() != null) {
        return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new ltiUserId cannot already have an ID")).body(null);
    }
    LtiUserId result = ltiUserIdRepository.save(ltiUserId);
    return ResponseEntity.created(new URI("/api/lti-user-ids/" + result.getId())).headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString())).body(result);
}
Also used : LtiUserId(de.tum.in.www1.artemis.domain.LtiUserId) URI(java.net.URI) Timed(com.codahale.metrics.annotation.Timed)

Example 19 with Result

use of de.tum.in.www1.artemis.domain.Result in project ArTEMiS by ls1intum.

the class LtiUserIdResource method updateLtiUserId.

/**
 * PUT  /lti-user-ids : Updates an existing ltiUserId.
 *
 * @param ltiUserId the ltiUserId to update
 * @return the ResponseEntity with status 200 (OK) and with body the updated ltiUserId,
 * or with status 400 (Bad Request) if the ltiUserId is not valid,
 * or with status 500 (Internal Server Error) if the ltiUserId couldn't be updated
 * @throws URISyntaxException if the Location URI syntax is incorrect
 */
@PutMapping("/lti-user-ids")
@Timed
public ResponseEntity<LtiUserId> updateLtiUserId(@RequestBody LtiUserId ltiUserId) throws URISyntaxException {
    log.debug("REST request to update LtiUserId : {}", ltiUserId);
    if (ltiUserId.getId() == null) {
        return createLtiUserId(ltiUserId);
    }
    LtiUserId result = ltiUserIdRepository.save(ltiUserId);
    return ResponseEntity.ok().headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, ltiUserId.getId().toString())).body(result);
}
Also used : LtiUserId(de.tum.in.www1.artemis.domain.LtiUserId) Timed(com.codahale.metrics.annotation.Timed)

Example 20 with Result

use of de.tum.in.www1.artemis.domain.Result in project ArTEMiS by ls1intum.

the class ModelingExerciseResource method createModelingExercise.

/**
 * POST  /modeling-exercises : Create a new modelingExercise.
 *
 * @param modelingExercise the modelingExercise to create
 * @return the ResponseEntity with status 201 (Created) and with body the new modelingExercise, or with status 400 (Bad Request) if the modelingExercise has already an ID
 * @throws URISyntaxException if the Location URI syntax is incorrect
 */
@PostMapping("/modeling-exercises")
@Timed
public ResponseEntity<ModelingExercise> createModelingExercise(@RequestBody ModelingExercise modelingExercise) throws URISyntaxException {
    log.debug("REST request to save ModelingExercise : {}", modelingExercise);
    if (modelingExercise.getId() != null) {
        return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new modelingExercise cannot already have an ID")).body(null);
    }
    ModelingExercise result = modelingExerciseRepository.save(modelingExercise);
    return ResponseEntity.created(new URI("/api/modeling-exercises/" + result.getId())).headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString())).body(result);
}
Also used : ModelingExercise(de.tum.in.www1.artemis.domain.ModelingExercise) URI(java.net.URI) Timed(com.codahale.metrics.annotation.Timed)

Aggregations

Timed (com.codahale.metrics.annotation.Timed)15 URI (java.net.URI)10 Result (de.tum.in.www1.artemis.domain.Result)8 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)8 Test (org.junit.Test)7 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)7 BadRequestAlertException (de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException)6 RestTemplate (org.springframework.web.client.RestTemplate)6 MalformedURLException (java.net.MalformedURLException)5 Transactional (org.springframework.transaction.annotation.Transactional)5 Participation (de.tum.in.www1.artemis.domain.Participation)4 BambooException (de.tum.in.www1.artemis.exception.BambooException)4 GitException (de.tum.in.www1.artemis.exception.GitException)4 HeaderUtil (de.tum.in.www1.artemis.web.rest.util.HeaderUtil)4 IOException (java.io.IOException)4 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)4 HttpEntity (org.springframework.http.HttpEntity)4 HttpHeaders (org.springframework.http.HttpHeaders)4 ResponseEntity (org.springframework.http.ResponseEntity)4 de.tum.in.www1.artemis.domain (de.tum.in.www1.artemis.domain)3