Example #1
0
  public void generateMergeRequestBuild(
      String json, Job project, StaplerRequest req, StaplerResponse rsp) {
    GitLabMergeRequest request = GitLabMergeRequest.create(json);
    if ("closed".equals(request.getObjectAttribute().getState())) {
      LOGGER.log(Level.INFO, "Closed Merge Request, no build started");
      return;
    }
    if ("merged".equals(request.getObjectAttribute().getState())) {
      LOGGER.log(Level.INFO, "Accepted Merge Request, no build started");
      return;
    }
    if ("update".equals(request.getObjectAttribute().getAction())) {
      LOGGER.log(
          Level.INFO,
          "Existing Merge Request, build will be trigged by buildOpenMergeRequests instead");
      return;
    }
    if (request.getObjectAttribute().getLastCommit() != null) {
      Run mergeBuild =
          getBuildBySHA1(project, request.getObjectAttribute().getLastCommit().getId(), true);
      if (mergeBuild != null) {
        LOGGER.log(
            Level.INFO,
            "Last commit in Merge Request has already been built in build #" + mergeBuild.getId());
        return;
      }
    }
    if (request.getObjectAttribute().getDescription().contains("[ci-skip]")) {
      LOGGER.log(
          Level.INFO,
          "Skipping MR " + request.getObjectAttribute().getTitle() + " due to ci-skip.");
      return;
    }

    Authentication old = SecurityContextHolder.getContext().getAuthentication();
    SecurityContextHolder.getContext().setAuthentication(ACL.SYSTEM);
    try {
      GitLabPushTrigger trigger = null;
      if (project instanceof ParameterizedJobMixIn.ParameterizedJob) {
        ParameterizedJobMixIn.ParameterizedJob p = (ParameterizedJobMixIn.ParameterizedJob) project;
        for (Trigger t : p.getTriggers().values()) {
          if (t instanceof GitLabPushTrigger) {
            trigger = (GitLabPushTrigger) t;
          }
        }
      }
      if (trigger == null) {
        return;
      }
      trigger.onPost(request);
    } finally {
      SecurityContextHolder.getContext().setAuthentication(old);
    }
  }
Example #2
0
  public void generatePushBuild(String json, Job project, StaplerRequest req, StaplerResponse rsp) {
    GitLabPushRequest request = GitLabPushRequest.create(json);
    String repositoryUrl = request.getRepository().getUrl();
    if (repositoryUrl == null) {

      LOGGER.log(Level.WARNING, "No repository url found.");
      return;
    }

    Authentication old = SecurityContextHolder.getContext().getAuthentication();
    SecurityContextHolder.getContext().setAuthentication(ACL.SYSTEM);
    try {

      GitLabPushTrigger trigger = null;
      if (project instanceof ParameterizedJobMixIn.ParameterizedJob) {
        ParameterizedJobMixIn.ParameterizedJob p = (ParameterizedJobMixIn.ParameterizedJob) project;
        for (Trigger t : p.getTriggers().values()) {

          if (t instanceof GitLabPushTrigger) {
            trigger = (GitLabPushTrigger) t;
          }
        }
      }

      if (trigger == null) {
        return;
      }

      if (trigger.getCiSkip() && request.getLastCommit() != null) {
        if (request.getLastCommit().getMessage().contains("[ci-skip]")) {
          LOGGER.log(Level.INFO, "Skipping due to ci-skip.");
          return;
        }
      }

      trigger.onPost(request);

      if (!trigger.getTriggerOpenMergeRequestOnPush().equals("never")) {
        // Fetch and build open merge requests with the same source branch
        buildOpenMergeRequests(trigger, request.getProject_id(), request.getRef());
      }
    } finally {
      SecurityContextHolder.getContext().setAuthentication(old);
    }
  }