@Test
  public void search() throws Exception {
    Activity activity1 = new Activity();
    activity1.setType(Activity.Type.ANALYSIS_REPORT);
    activity1.setAction("LOG_ANALYSIS_REPORT");
    activity1.setData("projectKey", "P1");
    activity1.setData("projectName", "POne");
    activity1.setData("projectUuid", "U1");
    activity1.setData("status", AnalysisReportDto.Status.SUCCESS);
    activity1.setData("submittedAt", new Date());
    activityService.save(activity1);

    Activity activity2 = new Activity();
    activity2.setType(Activity.Type.ANALYSIS_REPORT);
    activity2.setAction("LOG_ANALYSIS_REPORT");
    activity2.setData("projectKey", "P2");
    activity2.setData("projectName", "PTwo");
    activity2.setData("projectUuid", "U2");
    activity2.setData("status", AnalysisReportDto.Status.FAILED);
    activity2.setData("submittedAt", new Date());
    activityService.save(activity2);

    userSessionRule.setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);

    WsTester.TestRequest request = tester.wsTester().newGetRequest("api/computation", "history");
    request.execute().assertJson(getClass(), "list_history_reports.json");
  }
예제 #2
0
  private List<ActiveRuleChange> cascadeDeactivation(
      ActiveRuleKey key, DbSession dbSession, boolean isCascade, boolean force) {
    List<ActiveRuleChange> changes = Lists.newArrayList();
    RuleActivatorContext context = contextFactory.create(key, dbSession);
    ActiveRuleChange change;
    if (context.activeRule() == null) {
      return changes;
    }
    if (!force && !isCascade && context.activeRule().getInheritance() != null) {
      throw new IllegalStateException("Cannot deactivate inherited rule '" + key.ruleKey() + "'");
    }
    change = ActiveRuleChange.createFor(ActiveRuleChange.Type.DEACTIVATED, key);
    changes.add(change);
    persist(change, context, dbSession);

    // get all inherited profiles
    List<QualityProfileDto> profiles =
        db.qualityProfileDao().findByParentKey(dbSession, key.qProfile());

    for (QualityProfileDto profile : profiles) {
      ActiveRuleKey activeRuleKey = ActiveRuleKey.of(profile.getKey(), key.ruleKey());
      changes.addAll(cascadeDeactivation(activeRuleKey, dbSession, true, force));
    }

    if (!changes.isEmpty()) {
      log.write(dbSession, Activity.Type.ACTIVE_RULE, changes);
      previewCache.reportGlobalModification();
    }

    return changes;
  }
예제 #3
0
  @Test
  public void insert_and_index() {
    when(system.now()).thenReturn(1_500_000_000_000L);

    Activity activity = new Activity();
    activity.setType(Activity.Type.ANALYSIS_REPORT);
    activity.setAction("THE_ACTION");
    activity.setMessage("THE_MSG");
    activity.setData("foo", "bar");
    activity.setProfileKey("PROFILE_KEY");
    service.save(activity);

    Map<String, Object> dbMap =
        db.selectFirst(
            "select "
                + "  log_type as \"type\", "
                + "  log_action as \"action\", "
                + "  log_message as \"msg\", "
                + "  data_field as \"data\", "
                + "  profile_key as \"profileKey\" "
                + "from activities");
    assertThat(dbMap).containsEntry("type", "ANALYSIS_REPORT");
    assertThat(dbMap).containsEntry("action", "THE_ACTION");
    assertThat(dbMap).containsEntry("msg", "THE_MSG");
    assertThat(dbMap).containsEntry("profileKey", "PROFILE_KEY");
    assertThat(dbMap.get("data")).isEqualTo("foo=bar");

    List<ActivityDoc> docs = es.getDocuments("activities", "activity", ActivityDoc.class);
    assertThat(docs).hasSize(1);
    assertThat(docs.get(0).getKey()).isNotEmpty();
    assertThat(docs.get(0).getAction()).isEqualTo("THE_ACTION");
    assertThat(docs.get(0).getMessage()).isEqualTo("THE_MSG");
    assertThat(docs.get(0).getDetails())
        .containsOnly(MapEntry.entry("foo", "bar"), MapEntry.entry("profileKey", "PROFILE_KEY"));
  }
예제 #4
0
  public List<ActiveRuleChange> activate(DbSession dbSession, RuleActivation activation) {
    RuleActivatorContext context = contextFactory.create(activation.getKey(), dbSession);
    context.verifyForActivation();
    List<ActiveRuleChange> changes = Lists.newArrayList();
    ActiveRuleChange change;
    boolean stopPropagation = false;

    if (context.activeRule() == null) {
      // new activation
      change = ActiveRuleChange.createFor(ActiveRuleChange.Type.ACTIVATED, activation.getKey());
      if (activation.isCascade() || context.isSameAsParent(activation)) {
        change.setInheritance(ActiveRule.Inheritance.INHERITED);
      }
      applySeverityAndParamToChange(activation, context, change);

    } else {
      // already activated

      if (activation.isCascade() && context.activeRule().doesOverride()) {
        // propagating to descendants, but child profile already overrides rule -> stop propagation
        return changes;
      }
      change = ActiveRuleChange.createFor(ActiveRuleChange.Type.UPDATED, activation.getKey());
      if (activation.isCascade() && context.activeRule().getInheritance() == null) {
        // activate on child, then on parent -> mark child as overriding parent
        change.setInheritance(ActiveRule.Inheritance.OVERRIDES);
        change.setSeverity(context.activeRule().getSeverityString());
        change.setParameters(context.activeRuleParamsAsStringMap());
        stopPropagation = true;
      } else {
        applySeverityAndParamToChange(activation, context, change);
        if (!activation.isCascade() && context.parentProfile() != null) {
          // override rule which is already declared on parents
          change.setInheritance(
              context.isSameAsParent(activation)
                  ? ActiveRule.Inheritance.INHERITED
                  : ActiveRule.Inheritance.OVERRIDES);
        }
      }
    }

    changes.add(change);
    persist(change, context, dbSession);

    if (!stopPropagation) {
      changes.addAll(cascadeActivation(dbSession, activation));
    }

    if (!changes.isEmpty()) {
      log.write(dbSession, Activity.Type.ACTIVE_RULE, changes);
      previewCache.reportGlobalModification();
    }
    return changes;
  }
  @Test
  public void search() throws Exception {
    Activity activity = new Activity();
    activity.setType(Activity.Type.ANALYSIS_REPORT);
    activity.setAction("THE_ACTION");
    activity.setMessage("THE_MSG");
    activity.setData("foo", "bar");
    service.save(activity);

    WsTester.TestRequest request = tester.wsTester().newGetRequest("api/activities", "search");
    WsTester.Result result = request.execute();
    assertThat(result.outputAsString()).contains("\"total\":1");
    assertThat(result.outputAsString()).contains("\"type\":\"ANALYSIS_REPORT\"");
    assertThat(result.outputAsString()).contains("\"details\":{\"foo\":\"bar\"}");
  }