Search in sources :

Example 1 with TokenType

use of org.gitlab4j.api.Constants.TokenType in project violation-comments-to-gitlab-plugin by jenkinsci.

the class JvctglPerformer method doPerform.

@VisibleForTesting
public static void doPerform(final ViolationsToGitLabConfig config, final String apiToken, final File workspace, final TaskListener listener, final String proxyUser, final String proxyPassword) throws MalformedURLException {
    if (config.getMergeRequestIid() == null) {
        listener.getLogger().println("No merge request id defined, will not send violation comments. " + "\n\nPossible cause might be: mergeRequestId changed in version 2.0 and is now mergeRequestIid. " + "If you just updated the plugin, mergeRequestIid should have the same value that you previously used for mergeRequestId. " + "It is named wrong in the Java GitLab API and that wrong name has spread to this API." + "\n\n");
        return;
    }
    final ViolationsLogger violationsLogger = new ViolationsLogger() {

        @Override
        public void log(final Level level, final String string) {
            if (!config.getEnableLogging()) {
                return;
            }
            Logger.getLogger(JvctglPerformer.class.getName()).log(level, string);
            if (level != Level.FINE) {
                listener.getLogger().println(level + " " + string);
            }
        }

        @Override
        public void log(final Level level, final String string, final Throwable t) {
            if (!config.getEnableLogging()) {
                return;
            }
            Logger.getLogger(JvctglPerformer.class.getName()).log(level, string);
            final StringWriter sw = new StringWriter();
            t.printStackTrace(new PrintWriter(sw));
            listener.getLogger().println(level + " " + string + "\n" + sw.toString());
        }
    };
    final Set<Violation> allParsedViolations = new TreeSet<>();
    for (final ViolationConfig violationConfig : config.getViolationConfigs()) {
        if (!isNullOrEmpty(violationConfig.getPattern())) {
            Set<Violation> parsedViolations = violationsApi().withViolationsLogger(// 
            violationsLogger).findAll(// 
            violationConfig.getParser()).withReporter(// 
            violationConfig.getReporter()).inFolder(// 
            workspace.getAbsolutePath()).withPattern(// 
            violationConfig.getPattern()).violations();
            final SEVERITY minSeverity = config.getMinSeverity();
            if (minSeverity != null) {
                parsedViolations = Filtering.withAtLEastSeverity(parsedViolations, minSeverity);
            }
            allParsedViolations.addAll(parsedViolations);
            listener.getLogger().println("Found " + parsedViolations.size() + " violations from " + violationConfig + ".");
        }
    }
    final String hostUrl = config.getGitLabUrl();
    final String projectId = config.getProjectId();
    final String mergeRequestIid = config.getMergeRequestIid();
    listener.getLogger().println("Will comment PR " + hostUrl + " " + projectId + " " + mergeRequestIid);
    try {
        final TokenType tokenType = config.getApiTokenPrivate() ? TokenType.PRIVATE : TokenType.ACCESS;
        final Integer mergeRequestIidInteger = Integer.parseInt(mergeRequestIid);
        final boolean shouldKeepOldComments = config.getKeepOldComments();
        final boolean shouldSetWIP = config.getShouldSetWip();
        final String commentTemplate = config.getCommentTemplate();
        violationCommentsToGitLabApi().setProxyServer(config.getProxyUri()).setProxyUser(proxyUser).setProxyPassword(proxyPassword).setHostUrl(hostUrl).setProjectId(projectId).setMergeRequestIid(mergeRequestIidInteger).setApiToken(apiToken).setTokenType(tokenType).setCommentOnlyChangedContent(config.getCommentOnlyChangedContent()).setCommentOnlyChangedContentContext(config.getCommentOnlyChangedContentContext()).withShouldCommentOnlyChangedFiles(config.getCommentOnlyChangedFiles()).setCreateCommentWithAllSingleFileComments(config.getCreateCommentWithAllSingleFileComments()).setCreateSingleFileComments(config.getCreateSingleFileComments()).setIgnoreCertificateErrors(// 
        config.getIgnoreCertificateErrors()).setViolations(// 
        allParsedViolations).setShouldKeepOldComments(// 
        shouldKeepOldComments).setShouldSetWIP(// 
        shouldSetWIP).setCommentTemplate(// 
        commentTemplate).setViolationsLogger(// 
        violationsLogger).setMaxNumberOfViolations(// 
        config.getMaxNumberOfViolations()).toPullRequest();
    } catch (final Exception e) {
        Logger.getLogger(JvctglPerformer.class.getName()).log(SEVERE, "", e);
        final StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
        listener.getLogger().println(sw.toString());
    }
}
Also used : Violation(se.bjurr.violations.lib.model.Violation) ViolationConfig(org.jenkinsci.plugins.jvctgl.config.ViolationConfig) ViolationsLogger(se.bjurr.violations.lib.ViolationsLogger) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) TokenType(org.gitlab4j.api.Constants.TokenType) StringWriter(java.io.StringWriter) TreeSet(java.util.TreeSet) Level(java.util.logging.Level) SEVERITY(se.bjurr.violations.lib.model.SEVERITY) PrintWriter(java.io.PrintWriter) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with TokenType

use of org.gitlab4j.api.Constants.TokenType in project legend-sdlc by finos.

the class GitLabTokenManager method putAllFromToken.

void putAllFromToken(Token.TokenReader reader) {
    synchronized (this.tokens) {
        for (int size = reader.getInt(); size > 0; size--) {
            // Read values
            String modeName = reader.getString();
            String appId = reader.getString();
            String typeName = reader.getString();
            String token = reader.getString();
            if ((modeName != null) && (appId != null) && (typeName != null) && (token != null)) {
                // Get mode and token type
                GitLabMode mode;
                TokenType type;
                try {
                    mode = GitLabMode.valueOf(modeName);
                    type = TokenType.valueOf(typeName);
                } catch (IllegalArgumentException e) {
                    // unknown mode or token type - token will be ignored
                    continue;
                }
                // Check the mode info
                GitLabModeInfo modeInfo = this.modeInfos.getModeInfo(mode);
                if ((modeInfo != null) && appId.equals(modeInfo.getAppInfo().getAppId())) {
                    this.tokens.put(mode, GitLabToken.newGitLabToken(type, token));
                }
            }
        }
    }
}
Also used : TokenType(org.gitlab4j.api.Constants.TokenType) GitLabModeInfo(org.finos.legend.sdlc.server.gitlab.mode.GitLabModeInfo) GitLabMode(org.finos.legend.sdlc.server.gitlab.mode.GitLabMode)

Aggregations

TokenType (org.gitlab4j.api.Constants.TokenType)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 MalformedURLException (java.net.MalformedURLException)1 TreeSet (java.util.TreeSet)1 Level (java.util.logging.Level)1 GitLabMode (org.finos.legend.sdlc.server.gitlab.mode.GitLabMode)1 GitLabModeInfo (org.finos.legend.sdlc.server.gitlab.mode.GitLabModeInfo)1 ViolationConfig (org.jenkinsci.plugins.jvctgl.config.ViolationConfig)1 ViolationsLogger (se.bjurr.violations.lib.ViolationsLogger)1 SEVERITY (se.bjurr.violations.lib.model.SEVERITY)1 Violation (se.bjurr.violations.lib.model.Violation)1