Search in sources :

Example 31 with ConfigInvalidException

use of org.eclipse.jgit.errors.ConfigInvalidException in project gitblit by gitblit.

the class BugtraqConfig method read.

// Static =================================================================
@Nullable
public static BugtraqConfig read(@NotNull Repository repository) throws IOException, ConfigInvalidException {
    Config baseConfig = getBaseConfig(repository, DOT_GIT_BUGTRAQ);
    if (baseConfig == null) {
        baseConfig = getBaseConfig(repository, DOT_TGITCONFIG);
    }
    final Set<String> allNames = new HashSet<String>();
    final Config config;
    try {
        config = repository.getConfig();
    } catch (RuntimeException ex) {
        final Throwable cause = ex.getCause();
        if (cause instanceof IOException) {
            throw (IOException) cause;
        }
        throw ex;
    }
    if (getString(null, URL, config, baseConfig) != null) {
        allNames.add(null);
    } else {
        allNames.addAll(config.getSubsections(BUGTRAQ));
        if (baseConfig != null) {
            allNames.addAll(baseConfig.getSubsections(BUGTRAQ));
        }
    }
    final List<BugtraqEntry> entries = new ArrayList<BugtraqEntry>();
    for (String name : allNames) {
        final String url = getString(name, URL, config, baseConfig);
        if (url == null) {
            continue;
        }
        final String enabled = getString(name, ENABLED, config, baseConfig);
        if (enabled != null && !"true".equals(enabled)) {
            continue;
        }
        String idRegex = getString(name, LOG_REGEX, config, baseConfig);
        if (idRegex == null) {
            return null;
        }
        String filterRegex = getString(name, LOG_FILTERREGEX, config, baseConfig);
        final String linkRegex = getString(name, LOG_LINKREGEX, config, baseConfig);
        if (filterRegex == null && linkRegex == null) {
            final String[] split = idRegex.split("\n", Integer.MAX_VALUE);
            if (split.length == 2) {
                // Compatibility with TortoiseGit
                filterRegex = split[0];
                idRegex = split[1];
            } else {
                // Backwards compatibility with specification version < 0.3
                final List<String> logIdRegexs = new ArrayList<String>();
                for (int index = 1; index < Integer.MAX_VALUE; index++) {
                    final String logIdRegexN = getString(name, LOG_REGEX + index, config, baseConfig);
                    if (logIdRegexN == null) {
                        break;
                    }
                    logIdRegexs.add(logIdRegexN);
                }
                if (logIdRegexs.size() > 1) {
                    throw new ConfigInvalidException("More than three " + LOG_REGEX + " entries found. This is not supported anymore since bugtraq version 0.3, use " + LOG_FILTERREGEX + " and " + LOG_LINKREGEX + " instead.");
                } else if (logIdRegexs.size() == 1) {
                    filterRegex = idRegex;
                    idRegex = logIdRegexs.get(0);
                }
            }
        }
        final String linkText = getString(name, LOG_LINKTEXT, config, baseConfig);
        entries.add(new BugtraqEntry(url, idRegex, linkRegex, filterRegex, linkText));
    }
    if (entries.isEmpty()) {
        return null;
    }
    return new BugtraqConfig(entries);
}
Also used : ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) Config(org.eclipse.jgit.lib.Config) FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig) ArrayList(java.util.ArrayList) IOException(java.io.IOException) HashSet(java.util.HashSet) Nullable(org.jetbrains.annotations.Nullable)

Example 32 with ConfigInvalidException

use of org.eclipse.jgit.errors.ConfigInvalidException in project gitblit by gitblit.

the class BugtraqProcessor method processTextRegex.

/**
 * Apply globally or per-repository specified regex substitutions to the
 * text.
 *
 * @param repository
 * @param repositoryName
 * @param text
 * @return the processed text
 */
protected String processTextRegex(Repository repository, String repositoryName, String text) {
    Map<String, String> map = new HashMap<String, String>();
    // global regex keys
    if (settings.getBoolean(Keys.regex.global, false)) {
        for (String key : settings.getAllKeys(Keys.regex.global)) {
            if (!key.equals(Keys.regex.global)) {
                String subKey = key.substring(key.lastIndexOf('.') + 1);
                map.put(subKey, settings.getString(key, ""));
            }
        }
    }
    // repository-specific regex keys
    List<String> keys = settings.getAllKeys(Keys.regex._ROOT + "." + repositoryName.toLowerCase());
    for (String key : keys) {
        String subKey = key.substring(key.lastIndexOf('.') + 1);
        map.put(subKey, settings.getString(key, ""));
    }
    for (Entry<String, String> entry : map.entrySet()) {
        String definition = entry.getValue().trim();
        String[] chunks = definition.split("!!!");
        if (chunks.length == 2) {
            text = text.replaceAll(chunks[0], chunks[1]);
        } else {
            logger.warn(entry.getKey() + " improperly formatted.  Use !!! to separate match from replacement: " + definition);
        }
    }
    try {
        // parse bugtraq repo config
        BugtraqConfig config = BugtraqConfig.read(repository);
        if (config != null) {
            BugtraqFormatter formatter = new BugtraqFormatter(config);
            StringBuilder sb = new StringBuilder();
            formatter.formatLogMessage(text, new BugtraqOutputHandler(sb));
            text = sb.toString();
        }
    } catch (IOException e) {
        logger.error(MessageFormat.format("Bugtraq config for {0} is invalid!", repositoryName), e);
    } catch (ConfigInvalidException e) {
        logger.error(MessageFormat.format("Bugtraq config for {0} is invalid!", repositoryName), e);
    }
    return text;
}
Also used : BugtraqFormatter(com.syntevo.bugtraq.BugtraqFormatter) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) HashMap(java.util.HashMap) BugtraqConfig(com.syntevo.bugtraq.BugtraqConfig) IOException(java.io.IOException)

Example 33 with ConfigInvalidException

use of org.eclipse.jgit.errors.ConfigInvalidException in project gerrit by GerritCodeReview.

the class ExternalIdCacheImpl method get.

/**
 * Returns the cached value or a freshly loaded value that will be cached with this call in case
 * the value was absent from the cache.
 *
 * <p>This method will load the value using {@link ExternalIdCacheLoader} in case it is not
 * already cached. {@link ExternalIdCacheLoader} requires loading older versions of the cached
 * value and Caffeine does not support recursive calls to the cache from loaders. Hence, we use a
 * Cache instead of a LoadingCache and perform the loading ourselves here similar to what a
 * loading cache would do.
 */
private AllExternalIds get(ObjectId rev) throws IOException {
    AllExternalIds cachedValue = extIdsByAccount.getIfPresent(rev);
    if (cachedValue != null) {
        return cachedValue;
    }
    // Load the value and put it in the cache.
    lock.lock();
    try {
        // Check if value was already loaded while waiting for the lock.
        cachedValue = extIdsByAccount.getIfPresent(rev);
        if (cachedValue != null) {
            return cachedValue;
        }
        AllExternalIds newlyLoadedValue;
        try {
            newlyLoadedValue = externalIdCacheLoader.load(rev);
        } catch (ConfigInvalidException e) {
            throw new IOException("Cannot load external ids", e);
        }
        extIdsByAccount.put(rev, newlyLoadedValue);
        return newlyLoadedValue;
    } finally {
        lock.unlock();
    }
}
Also used : ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) IOException(java.io.IOException)

Example 34 with ConfigInvalidException

use of org.eclipse.jgit.errors.ConfigInvalidException in project gerrit by GerritCodeReview.

the class ExternalIdsConsistencyChecker method check.

private List<ConsistencyProblemInfo> check(ExternalIdNotes extIdNotes) throws IOException {
    List<ConsistencyProblemInfo> problems = new ArrayList<>();
    ListMultimap<String, ExternalId> emails = MultimapBuilder.hashKeys().arrayListValues().build();
    try (RevWalk rw = new RevWalk(extIdNotes.getRepository())) {
        NoteMap noteMap = extIdNotes.getNoteMap();
        for (Note note : noteMap) {
            byte[] raw = ExternalIdNotes.readNoteData(rw, note.getData());
            try {
                ExternalId extId = externalIdFactory.parse(note.getName(), raw, note.getData());
                problems.addAll(validateExternalId(extId));
                if (extId.email() != null) {
                    String email = extId.email();
                    if (emails.get(email).stream().noneMatch(e -> e.accountId().get() == extId.accountId().get())) {
                        emails.put(email, extId);
                    }
                }
            } catch (ConfigInvalidException e) {
                addError(String.format(e.getMessage()), problems);
            }
        }
    }
    emails.asMap().entrySet().stream().filter(e -> e.getValue().size() > 1).forEach(e -> addError(String.format("Email '%s' is not unique, it's used by the following external IDs: %s", e.getKey(), e.getValue().stream().map(k -> "'" + k.key().get() + "'").sorted().collect(joining(", "))), problems));
    return problems;
}
Also used : AllUsersName(com.google.gerrit.server.config.AllUsersName) AccountCache(com.google.gerrit.server.account.AccountCache) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) OutgoingEmailValidator(com.google.gerrit.server.mail.send.OutgoingEmailValidator) Note(org.eclipse.jgit.notes.Note) ListMultimap(com.google.common.collect.ListMultimap) MultimapBuilder(com.google.common.collect.MultimapBuilder) Inject(com.google.inject.Inject) IOException(java.io.IOException) Collectors.joining(java.util.stream.Collectors.joining) ArrayList(java.util.ArrayList) ObjectId(org.eclipse.jgit.lib.ObjectId) RevWalk(org.eclipse.jgit.revwalk.RevWalk) List(java.util.List) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) SCHEME_USERNAME(com.google.gerrit.server.account.externalids.ExternalId.SCHEME_USERNAME) ConsistencyProblemInfo(com.google.gerrit.extensions.api.config.ConsistencyCheckInfo.ConsistencyProblemInfo) HashedPassword(com.google.gerrit.server.account.HashedPassword) Repository(org.eclipse.jgit.lib.Repository) NoteMap(org.eclipse.jgit.notes.NoteMap) Singleton(com.google.inject.Singleton) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) Note(org.eclipse.jgit.notes.Note) ArrayList(java.util.ArrayList) ConsistencyProblemInfo(com.google.gerrit.extensions.api.config.ConsistencyCheckInfo.ConsistencyProblemInfo) NoteMap(org.eclipse.jgit.notes.NoteMap) RevWalk(org.eclipse.jgit.revwalk.RevWalk)

Example 35 with ConfigInvalidException

use of org.eclipse.jgit.errors.ConfigInvalidException in project gerrit by GerritCodeReview.

the class ExternalIdCaseSensitivityMigrator method migrate.

public void migrate(Collection<ExternalId> todo, Runnable monitor) throws RepositoryNotFoundException, IOException, ConfigInvalidException {
    try (Repository repo = repoManager.openRepository(allUsersName)) {
        ExternalIdNotes extIdNotes = externalIdNotesFactory.load(repo);
        for (ExternalId extId : todo) {
            recomputeExternalIdNoteId(extIdNotes, extId);
            monitor.run();
        }
        if (!dryRun) {
            try (MetaDataUpdate metaDataUpdate = metaDataUpdateServerFactory.get().create(allUsersName)) {
                metaDataUpdate.setMessage(String.format("Migration to case %ssensitive usernames", isUserNameCaseInsensitive ? "" : "in"));
                extIdNotes.commit(metaDataUpdate);
            } catch (Exception e) {
                logger.atSevere().withCause(e).log("%s", e.getMessage());
            }
        }
    } catch (DuplicateExternalIdKeyException e) {
        logger.atSevere().withCause(e).log("%s", e.getMessage());
        throw e;
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) IOException(java.io.IOException) DuplicateKeyException(com.google.gerrit.exceptions.DuplicateKeyException) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate)

Aggregations

ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)158 IOException (java.io.IOException)95 Inject (com.google.inject.Inject)38 Repository (org.eclipse.jgit.lib.Repository)37 Provider (com.google.inject.Provider)34 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)31 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)30 ArrayList (java.util.ArrayList)30 Account (com.google.gerrit.entities.Account)27 List (java.util.List)26 Set (java.util.Set)26 ObjectId (org.eclipse.jgit.lib.ObjectId)26 AuthException (com.google.gerrit.extensions.restapi.AuthException)25 Config (org.eclipse.jgit.lib.Config)24 Singleton (com.google.inject.Singleton)23 OrmException (com.google.gwtorm.server.OrmException)22 AccountGroup (com.google.gerrit.entities.AccountGroup)21 RevWalk (org.eclipse.jgit.revwalk.RevWalk)21 StorageException (com.google.gerrit.exceptions.StorageException)20 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)20