Search in sources :

Example 1 with PluginInfo

use of com.google.gerrit.extensions.common.PluginInfo in project gerrit by GerritCodeReview.

the class InstallPlugin method apply.

@Override
public Response<PluginInfo> apply(TopLevelResource resource, InstallPluginInput input) throws RestApiException, IOException {
    loader.checkRemoteAdminEnabled();
    try {
        try (InputStream in = openStream(input)) {
            String pluginName = loader.installPluginFromStream(name, in);
            PluginInfo info = ListPlugins.toPluginInfo(loader.get(pluginName));
            return created ? Response.created(info) : Response.ok(info);
        }
    } catch (PluginInstallException e) {
        StringWriter buf = new StringWriter();
        buf.write(String.format("cannot install %s", name));
        if (e.getCause() instanceof ZipException) {
            buf.write(": ");
            buf.write(e.getCause().getMessage());
        } else {
            buf.write(":\n");
            PrintWriter pw = new PrintWriter(buf);
            e.printStackTrace(pw);
            pw.flush();
        }
        throw new BadRequestException(buf.toString());
    }
}
Also used : StringWriter(java.io.StringWriter) InputStream(java.io.InputStream) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) PluginInfo(com.google.gerrit.extensions.common.PluginInfo) ZipException(java.util.zip.ZipException) IdString(com.google.gerrit.extensions.restapi.IdString) PrintWriter(java.io.PrintWriter)

Example 2 with PluginInfo

use of com.google.gerrit.extensions.common.PluginInfo in project gerrit by GerritCodeReview.

the class PluginLsCommand method run.

@Override
public void run() throws Exception {
    enableGracefulStop();
    Map<String, PluginInfo> output = list.apply(TopLevelResource.INSTANCE).value();
    if (format.isJson()) {
        format.newGson().toJson(output, new TypeToken<Map<String, PluginInfo>>() {
        }.getType(), stdout);
        stdout.print('\n');
    } else {
        String template = "%-30s %-10s %-16s %-8s %s\n";
        stdout.format(template, "Name", "Version", "Api-Version", "Status", "File");
        stdout.print("-------------------------------------------------------------------------------\n");
        for (Map.Entry<String, PluginInfo> p : output.entrySet()) {
            PluginInfo info = p.getValue();
            stdout.format(template, p.getKey(), Strings.nullToEmpty(info.version), Strings.nullToEmpty(info.apiVersion), status(info.disabled), Strings.nullToEmpty(info.filename));
        }
    }
    stdout.flush();
}
Also used : TypeToken(com.google.gson.reflect.TypeToken) PluginInfo(com.google.gerrit.extensions.common.PluginInfo) Map(java.util.Map)

Example 3 with PluginInfo

use of com.google.gerrit.extensions.common.PluginInfo in project gerrit by GerritCodeReview.

the class ListPlugins method apply.

@Override
public Response<SortedMap<String, PluginInfo>> apply(TopLevelResource resource) throws BadRequestException {
    Stream<Plugin> s = Streams.stream(pluginLoader.getPlugins(all));
    if (matchPrefix != null) {
        checkMatchOptions(matchSubstring == null && matchRegex == null);
        s = s.filter(p -> p.getName().startsWith(matchPrefix));
    } else if (matchSubstring != null) {
        checkMatchOptions(matchPrefix == null && matchRegex == null);
        String substring = matchSubstring.toLowerCase(Locale.US);
        s = s.filter(p -> p.getName().toLowerCase(Locale.US).contains(substring));
    } else if (matchRegex != null) {
        checkMatchOptions(matchPrefix == null && matchSubstring == null);
        Pattern pattern = Pattern.compile(matchRegex);
        s = s.filter(p -> pattern.matcher(p.getName()).matches());
    }
    s = s.sorted(comparing(Plugin::getName));
    if (start > 0) {
        s = s.skip(start);
    }
    if (limit > 0) {
        s = s.limit(limit);
    }
    return Response.ok(new TreeMap<>(s.collect(toMap(Plugin::getName, ListPlugins::toPluginInfo))));
}
Also used : RestReadView(com.google.gerrit.extensions.restapi.RestReadView) GlobalCapability(com.google.gerrit.common.data.GlobalCapability) RequiresCapability(com.google.gerrit.extensions.annotations.RequiresCapability) Inject(com.google.inject.Inject) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) Option(org.kohsuke.args4j.Option) Streams(com.google.common.collect.Streams) Response(com.google.gerrit.extensions.restapi.Response) TopLevelResource(com.google.gerrit.extensions.restapi.TopLevelResource) Stream(java.util.stream.Stream) Collectors.toMap(java.util.stream.Collectors.toMap) TreeMap(java.util.TreeMap) Locale(java.util.Locale) Plugins(com.google.gerrit.extensions.api.plugins.Plugins) Url(com.google.gerrit.extensions.restapi.Url) PluginInfo(com.google.gerrit.extensions.common.PluginInfo) Comparator.comparing(java.util.Comparator.comparing) Pattern(java.util.regex.Pattern) SortedMap(java.util.SortedMap) Pattern(java.util.regex.Pattern)

Example 4 with PluginInfo

use of com.google.gerrit.extensions.common.PluginInfo in project gerrit by GerritCodeReview.

the class PluginIT method pluginManagement.

@Test
@GerritConfig(name = "plugins.allowRemoteAdmin", value = "true")
public void pluginManagement() throws Exception {
    // No plugins are loaded
    assertThat(list().get()).isEmpty();
    assertThat(list().all().get()).isEmpty();
    PluginApi api;
    // Install all the plugins
    InstallPluginInput input = new InstallPluginInput();
    for (String plugin : PLUGINS) {
        input.raw = pluginContent(plugin);
        api = gApi.plugins().install(plugin, input);
        assertThat(api).isNotNull();
        PluginInfo info = api.get();
        String name = pluginName(plugin);
        assertThat(info.id).isEqualTo(name);
        assertThat(info.version).isEqualTo(pluginVersion(plugin));
        assertThat(info.apiVersion).isEqualTo(pluginApiVersion(plugin));
        assertThat(info.indexUrl).isEqualTo(String.format("plugins/%s/", name));
        assertThat(info.filename).isEqualTo(plugin);
        assertThat(info.disabled).isNull();
    }
    assertPlugins(list().get(), PLUGINS);
    // With pagination
    assertPlugins(list().start(1).limit(2).get(), PLUGINS.subList(1, 3));
    // With prefix
    assertPlugins(list().prefix("plugin-b").get(), ImmutableList.of("plugin-b.js"));
    assertPlugins(list().prefix("PLUGIN-").get(), ImmutableList.of());
    // With substring
    assertPlugins(list().substring("lugin-").get(), PLUGINS.subList(0, PLUGINS.size() - 1));
    assertPlugins(list().substring("lugin-").start(1).limit(2).get(), PLUGINS.subList(1, 3));
    // With regex
    assertPlugins(list().regex(".*in-b").get(), ImmutableList.of("plugin-b.js"));
    assertPlugins(list().regex("plugin-.*").get(), PLUGINS.subList(0, PLUGINS.size() - 1));
    assertPlugins(list().regex("plugin-.*").start(1).limit(2).get(), PLUGINS.subList(1, 3));
    // Invalid match combinations
    assertBadRequest(list().regex(".*in-b").substring("a"));
    assertBadRequest(list().regex(".*in-b").prefix("a"));
    assertBadRequest(list().substring(".*in-b").prefix("a"));
    // Disable mandatory
    mandatoryPluginsCollection.add("plugin_e");
    assertThrows(MethodNotAllowedException.class, () -> gApi.plugins().name("plugin_e").disable());
    api = gApi.plugins().name("plugin_e");
    assertThat(api.get().disabled).isNull();
    // Disable non-mandatory
    api = gApi.plugins().name("plugin-a");
    api.disable();
    api = gApi.plugins().name("plugin-a");
    assertThat(api.get().disabled).isTrue();
    assertPlugins(list().get(), PLUGINS.subList(1, PLUGINS.size()));
    assertPlugins(list().all().get(), PLUGINS);
    // Enable
    api.enable();
    api = gApi.plugins().name("plugin-a");
    assertThat(api.get().disabled).isNull();
    assertPlugins(list().get(), PLUGINS);
    // Using deprecated input
    deprecatedInput();
    // Non-admin cannot disable
    requestScopeOperations.setApiUser(user.id());
    assertThrows(AuthException.class, () -> gApi.plugins().name("plugin-a").disable());
}
Also used : InstallPluginInput(com.google.gerrit.extensions.api.plugins.InstallPluginInput) PluginInfo(com.google.gerrit.extensions.common.PluginInfo) PluginApi(com.google.gerrit.extensions.api.plugins.PluginApi) GerritConfig(com.google.gerrit.acceptance.config.GerritConfig) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Aggregations

PluginInfo (com.google.gerrit.extensions.common.PluginInfo)4 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)2 Streams (com.google.common.collect.Streams)1 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)1 GerritConfig (com.google.gerrit.acceptance.config.GerritConfig)1 GlobalCapability (com.google.gerrit.common.data.GlobalCapability)1 RequiresCapability (com.google.gerrit.extensions.annotations.RequiresCapability)1 InstallPluginInput (com.google.gerrit.extensions.api.plugins.InstallPluginInput)1 PluginApi (com.google.gerrit.extensions.api.plugins.PluginApi)1 Plugins (com.google.gerrit.extensions.api.plugins.Plugins)1 IdString (com.google.gerrit.extensions.restapi.IdString)1 Response (com.google.gerrit.extensions.restapi.Response)1 RestReadView (com.google.gerrit.extensions.restapi.RestReadView)1 TopLevelResource (com.google.gerrit.extensions.restapi.TopLevelResource)1 Url (com.google.gerrit.extensions.restapi.Url)1 TypeToken (com.google.gson.reflect.TypeToken)1 Inject (com.google.inject.Inject)1 InputStream (java.io.InputStream)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1