public PropertiesProcessor getParametersProcessor()

in commit-status-publisher-server/src/main/java/jetbrains/buildServer/commitPublisher/github/GitHubSettings.java [177:285]


  public PropertiesProcessor getParametersProcessor(@NotNull BuildTypeIdentity buildTypeOrTemplate) {
    final UpdateChangesConstants c = new UpdateChangesConstants();
    return new PropertiesProcessor() {
      private boolean checkNotEmpty(@NotNull final Map<String, String> properties,
                                    @NotNull final String key,
                                    @NotNull final String message,
                                    @NotNull final Collection<InvalidProperty> res) {
        if (isEmpty(properties, key)) {
          res.add(new InvalidProperty(key, message));
          return true;
        }
        return false;
      }

      private boolean isEmpty(@NotNull final Map<String, String> properties,
                              @NotNull final String key) {
        return StringUtil.isEmptyOrSpaces(properties.get(key));
      }

      @NotNull
      public Collection<InvalidProperty> process(@Nullable final Map<String, String> p) {
        final Collection<InvalidProperty> result = new ArrayList<InvalidProperty>();
        if (p == null) return result;

        GitHubApiAuthenticationType authenticationType = GitHubApiAuthenticationType.parse(p.get(c.getAuthenticationTypeKey()));
        if (authenticationType == GitHubApiAuthenticationType.PASSWORD_AUTH) {
          checkNotEmpty(p, c.getUserNameKey(), "Username must be specified", result);
          checkNotEmpty(p, c.getPasswordKey(), "Password must be specified", result);
          p.remove(c.getAccessTokenKey());
          p.remove(c.getTokenIdKey());
          p.remove(c.getOAuthUserKey());
          p.remove(c.getOAuthProviderIdKey());
        } else if (authenticationType == GitHubApiAuthenticationType.TOKEN_AUTH) {
          p.remove(c.getUserNameKey());
          p.remove(c.getPasswordKey());
          p.remove(c.getTokenIdKey());
          String oauthUsername = p.get(c.getOAuthUserKey());
          String oauthProviderId = p.get(c.getOAuthProviderIdKey());
          if (null != oauthUsername && null != oauthProviderId) {
            User currentUser = mySecurityContext.getAuthorityHolder().getAssociatedUser();
            if (null != currentUser && currentUser instanceof SUser) {
              for (OAuthToken token: myOAuthTokensStorage.getUserTokens(oauthProviderId, (SUser) currentUser, buildTypeOrTemplate.getProject(), false)) {
                if (token.getOauthLogin().equals(oauthUsername)) {
                  p.put(c.getAccessTokenKey(), token.getAccessToken());
                  p.remove(c.getOAuthProviderIdKey());
                  p.remove(c.getOAuthUserKey());
                  break;
                }
              }
            }
          }
          else {
            p.remove(c.getOAuthProviderIdKey());
            p.remove(c.getOAuthUserKey());
          }
          checkNotEmpty(p, c.getAccessTokenKey(), "Personal Access Token must be specified", result);
        } else if (authenticationType == GitHubApiAuthenticationType.STORED_TOKEN) {
          checkNotEmpty(p, c.getTokenIdKey(), "TokenId must be specified", result);
          checkNotEmpty(p, c.getVcsRootId(), "A VCS root must be selected to use this authentication type", result);
          p.remove(c.getAccessTokenKey());
          p.remove(c.getOAuthUserKey());
          p.remove(c.getUserNameKey());
          p.remove(c.getPasswordKey());
          p.remove(c.getOAuthProviderIdKey());
        } else if (authenticationType == GitHubApiAuthenticationType.VCS_ROOT) {
          List<SVcsRoot> roots = null;
          if (buildTypeOrTemplate instanceof BuildTypeSettings) {
            roots = ((BuildTypeSettings) buildTypeOrTemplate).getVcsRoots();
          }

          if (null == roots || roots.isEmpty()) {
            result.add(new InvalidProperty(c.getVcsRootId(), "No VCS Roots attached"));
          }
          else {
            if (p.containsKey(c.getVcsRootId())) {
              roots = roots.stream().filter(root -> root.getExternalId().equals(p.get(c.getVcsRootId()))).collect(Collectors.toList());
              if (roots.isEmpty()) {
                result.add(new InvalidProperty(c.getVcsRootId(), "Attached VCS Root (" + p.get(c.getVcsRootId()) + ") doesn't belong to the current build configuration"));
              }
            }

            for (VcsRoot vcsRoot : roots) {
              if (!SupportedVcsRootAuthentificationType.contains(vcsRoot.getProperty(c.getVcsAuthMethod()))) {
                result.add(new InvalidProperty(c.getAuthenticationTypeKey(), "Using " + vcsRoot.getProperty(c.getVcsAuthMethod()) + " authentication method in attached VCS Root (" + vcsRoot.getExternalId() +
                                                                             ") to extract statuses information is impossible. " +
                                                                             "Please provide an access token or GitHub App connection"));
              }
            }
          }

          p.remove(c.getAccessTokenKey());
          p.remove(c.getOAuthUserKey());
          p.remove(c.getUserNameKey());
          p.remove(c.getPasswordKey());
          p.remove(c.getOAuthProviderIdKey());
          p.remove(c.getTokenIdKey());
        }

        if (!checkNotEmpty(p, c.getServerKey(), "GitHub API URL must be specified", result)) {
          final String url = "" + p.get(c.getServerKey());
          if (!ReferencesResolverUtil.mayContainReference(url) && !(url.startsWith("http://") || url.startsWith("https://"))) {
            result.add(new InvalidProperty(c.getServerKey(), "GitHub API URL should start with http:// or https://"));
          }
        }

        return result;
      }
    };
  }