示例#1
0
 @Override
 public String getGerritFrontEndUrlFor(GerritTriggeredEvent event) {
   if (event instanceof ChangeBasedEvent) {
     String changeUrl = ((ChangeBasedEvent) event).getChange().getUrl();
     if (changeUrl != null && !changeUrl.isEmpty()) {
       return changeUrl;
     }
   }
   String url = getGerritFrontEndUrl();
   Provider provider = event.getProvider();
   if (provider != null) {
     String providerUrl = provider.getUrl();
     if (providerUrl != null && !providerUrl.isEmpty()) {
       url = providerUrl;
     }
   }
   StringBuilder str = new StringBuilder(url);
   if (event instanceof ChangeBasedEvent) {
     str.append(((ChangeBasedEvent) event).getChange().getNumber());
   }
   return str.toString();
 }
  /**
   * Adds or sets all the Gerrit-parameter values to the provided list.
   *
   * @param gerritEvent the event.
   * @param project the project for which the parameters are being set
   * @param parameters the default parameters
   * @see #setOrCreateStringParameterValue(java.util.List, String, boolean)
   */
  public static void setOrCreateParameters(
      GerritTriggeredEvent gerritEvent, AbstractProject project, List<ParameterValue> parameters) {

    boolean noNameAndEmailParameters = false;
    boolean escapeQuotes = false;
    boolean readableMessage = false;
    if (project != null) {
      GerritTrigger trigger = GerritTrigger.getTrigger(project);
      if (trigger != null) {
        noNameAndEmailParameters = trigger.isNoNameAndEmailParameters();
        escapeQuotes = trigger.isEscapeQuotes();
        readableMessage = trigger.isReadableMessage();
      }
    }

    GERRIT_EVENT_TYPE.setOrCreateStringParameterValue(
        parameters, gerritEvent.getEventType().getTypeValue(), escapeQuotes);
    GERRIT_EVENT_HASH.setOrCreateStringParameterValue(
        parameters, String.valueOf(((java.lang.Object) gerritEvent).hashCode()), escapeQuotes);
    if (gerritEvent instanceof ChangeBasedEvent) {
      ChangeBasedEvent event = (ChangeBasedEvent) gerritEvent;
      GERRIT_BRANCH.setOrCreateStringParameterValue(
          parameters, event.getChange().getBranch(), escapeQuotes);
      GERRIT_TOPIC.setOrCreateStringParameterValue(
          parameters, event.getChange().getTopic(), escapeQuotes);
      GERRIT_CHANGE_NUMBER.setOrCreateStringParameterValue(
          parameters, event.getChange().getNumber(), escapeQuotes);
      GERRIT_CHANGE_ID.setOrCreateStringParameterValue(
          parameters, event.getChange().getId(), escapeQuotes);
      String pNumber = null;
      if (null != event.getPatchSet()) {
        pNumber = event.getPatchSet().getNumber();
        GERRIT_PATCHSET_NUMBER.setOrCreateStringParameterValue(parameters, pNumber, escapeQuotes);
        GERRIT_PATCHSET_REVISION.setOrCreateStringParameterValue(
            parameters, event.getPatchSet().getRevision(), escapeQuotes);
        GERRIT_REFSPEC.setOrCreateStringParameterValue(
            parameters, StringUtil.makeRefSpec(event), escapeQuotes);
      }
      GERRIT_PROJECT.setOrCreateStringParameterValue(
          parameters, event.getChange().getProject(), escapeQuotes);
      if (event instanceof ChangeRestored) {
        if (!noNameAndEmailParameters) {
          GERRIT_CHANGE_RESTORER.setOrCreateStringParameterValue(
              parameters, getNameAndEmail(((ChangeRestored) event).getRestorer()), escapeQuotes);
        }
        GERRIT_CHANGE_RESTORER_NAME.setOrCreateStringParameterValue(
            parameters, getName(((ChangeRestored) event).getRestorer()), escapeQuotes);
        GERRIT_CHANGE_RESTORER_EMAIL.setOrCreateStringParameterValue(
            parameters, getEmail(((ChangeRestored) event).getRestorer()), escapeQuotes);
      }
      GERRIT_CHANGE_SUBJECT.setOrCreateStringParameterValue(
          parameters, event.getChange().getSubject(), escapeQuotes);

      String url = getURL(event, project);

      String commitMessage = event.getChange().getCommitMessage();
      if (commitMessage != null) {
        if (readableMessage) {
          GERRIT_CHANGE_COMMIT_MESSAGE.setOrCreateTextParameterValue(
              parameters, commitMessage, escapeQuotes);
        } else {
          try {
            byte[] encodedBytes = Base64.encodeBase64(commitMessage.getBytes("UTF-8"));
            GERRIT_CHANGE_COMMIT_MESSAGE.setOrCreateStringParameterValue(
                parameters, new String(encodedBytes), escapeQuotes);
          } catch (UnsupportedEncodingException uee) {
            logger.error("Failed to encode commit message as Base64: ", uee);
          }
        }
      }
      GERRIT_CHANGE_URL.setOrCreateStringParameterValue(parameters, url, escapeQuotes);
      if (event instanceof ChangeAbandoned) {
        if (!noNameAndEmailParameters) {
          GERRIT_CHANGE_ABANDONER.setOrCreateStringParameterValue(
              parameters, getNameAndEmail(((ChangeAbandoned) event).getAbandoner()), escapeQuotes);
        }
        GERRIT_CHANGE_ABANDONER_NAME.setOrCreateStringParameterValue(
            parameters, getName(((ChangeAbandoned) event).getAbandoner()), escapeQuotes);
        GERRIT_CHANGE_ABANDONER_EMAIL.setOrCreateStringParameterValue(
            parameters, getEmail(((ChangeAbandoned) event).getAbandoner()), escapeQuotes);
      }
      if (!noNameAndEmailParameters) {
        GERRIT_CHANGE_OWNER.setOrCreateStringParameterValue(
            parameters, getNameAndEmail(event.getChange().getOwner()), escapeQuotes);
      }
      GERRIT_CHANGE_OWNER_NAME.setOrCreateStringParameterValue(
          parameters, getName(event.getChange().getOwner()), escapeQuotes);
      GERRIT_CHANGE_OWNER_EMAIL.setOrCreateStringParameterValue(
          parameters, getEmail(event.getChange().getOwner()), escapeQuotes);
      Account uploader = findUploader(event);
      if (!noNameAndEmailParameters) {
        GERRIT_PATCHSET_UPLOADER.setOrCreateStringParameterValue(
            parameters, getNameAndEmail(uploader), escapeQuotes);
      }
      GERRIT_PATCHSET_UPLOADER_NAME.setOrCreateStringParameterValue(
          parameters, getName(uploader), escapeQuotes);
      GERRIT_PATCHSET_UPLOADER_EMAIL.setOrCreateStringParameterValue(
          parameters, getEmail(uploader), escapeQuotes);
    } else if (gerritEvent instanceof RefUpdated) {
      RefUpdated event = (RefUpdated) gerritEvent;
      GERRIT_REFNAME.setOrCreateStringParameterValue(
          parameters, event.getRefUpdate().getRefName(), escapeQuotes);
      GERRIT_PROJECT.setOrCreateStringParameterValue(
          parameters, event.getRefUpdate().getProject(), escapeQuotes);
      GERRIT_OLDREV.setOrCreateStringParameterValue(
          parameters, event.getRefUpdate().getOldRev(), escapeQuotes);
      GERRIT_NEWREV.setOrCreateStringParameterValue(
          parameters, event.getRefUpdate().getNewRev(), escapeQuotes);
    }
    Account account = gerritEvent.getAccount();
    if (account != null) {
      if (!noNameAndEmailParameters) {
        GERRIT_EVENT_ACCOUNT.setOrCreateStringParameterValue(
            parameters, getNameAndEmail(account), escapeQuotes);
      }
      GERRIT_EVENT_ACCOUNT_NAME.setOrCreateStringParameterValue(
          parameters, getName(account), escapeQuotes);
      GERRIT_EVENT_ACCOUNT_EMAIL.setOrCreateStringParameterValue(
          parameters, getEmail(account), escapeQuotes);
    }
    Provider provider = gerritEvent.getProvider();
    if (provider != null) {
      GERRIT_NAME.setOrCreateStringParameterValue(parameters, provider.getName(), escapeQuotes);
      GERRIT_HOST.setOrCreateStringParameterValue(parameters, provider.getHost(), escapeQuotes);
      GERRIT_PORT.setOrCreateStringParameterValue(parameters, provider.getPort(), escapeQuotes);
      GERRIT_SCHEME.setOrCreateStringParameterValue(parameters, provider.getScheme(), escapeQuotes);
      GERRIT_VERSION.setOrCreateStringParameterValue(
          parameters, provider.getVersion(), escapeQuotes);
    }
  }