Search in sources :

Example 6 with GiteaServer

use of org.jenkinsci.plugin.gitea.servers.GiteaServer in project gitea-plugin by jenkinsci.

the class GiteaWebhookListener method register.

public static void register(SCMSourceOwner owner, GiteaSCMSource source, WebhookRegistration mode, String credentialsId) {
    StandardCredentials credentials;
    String serverUrl = source.getServerUrl();
    switch(mode) {
        case DISABLE:
            return;
        case SYSTEM:
            GiteaServer server = GiteaServers.get().findServer(serverUrl);
            if (server == null || !server.isManageHooks()) {
                return;
            }
            credentials = server.credentials();
            break;
        case ITEM:
            credentials = source.credentials();
            break;
        default:
            return;
    }
    if (credentials == null) {
        return;
    }
    JenkinsLocationConfiguration locationConfiguration = JenkinsLocationConfiguration.get();
    if (locationConfiguration == null) {
        return;
    }
    String rootUrl = locationConfiguration.getUrl();
    if (StringUtils.isBlank(rootUrl) || rootUrl.startsWith("http://localhost:")) {
        return;
    }
    String hookUrl = UriTemplate.buildFromTemplate(rootUrl).literal("gitea-webhook").literal("/post").build().expand();
    try (GiteaConnection c = connect(serverUrl, credentials)) {
        GiteaRepository repo = c.fetchRepository(source.getRepoOwner(), source.getRepository());
        if (repo == null) {
            return;
        }
        List<GiteaHook> hooks = c.fetchHooks(repo);
        GiteaHook hook = null;
        for (GiteaHook h : hooks) {
            if (hookUrl.equals(h.getConfig().getUrl())) {
                if (hook == null && h.getType() == GiteaHookType.GITEA && h.getConfig().getContentType() == GiteaPayloadType.JSON && h.isActive() && EnumSet.allOf(GiteaEventType.class).equals(h.getEvents())) {
                    hook = h;
                } else {
                    c.deleteHook(repo, h);
                }
            }
        }
        if (hook == null) {
            hook = new GiteaHook();
            GiteaHook.Configuration configuration = new GiteaHook.Configuration();
            configuration.setContentType(GiteaPayloadType.JSON);
            configuration.setUrl(hookUrl);
            hook.setType(GiteaHookType.GITEA);
            hook.setConfig(configuration);
            hook.setEvents(EnumSet.allOf(GiteaEventType.class));
            hook.setActive(true);
            c.createHook(repo, hook);
        }
    } catch (IOException | InterruptedException e) {
        LOGGER.log(Level.WARNING, "Could not manage repository hooks for " + source.getRepoOwner() + "/" + source.getRepository() + " on " + serverUrl, e);
    }
}
Also used : JenkinsLocationConfiguration(jenkins.model.JenkinsLocationConfiguration) JenkinsLocationConfiguration(jenkins.model.JenkinsLocationConfiguration) GiteaConnection(org.jenkinsci.plugin.gitea.client.api.GiteaConnection) GiteaHook(org.jenkinsci.plugin.gitea.client.api.GiteaHook) IOException(java.io.IOException) GiteaServer(org.jenkinsci.plugin.gitea.servers.GiteaServer) GiteaEventType(org.jenkinsci.plugin.gitea.client.api.GiteaEventType) StandardCredentials(com.cloudbees.plugins.credentials.common.StandardCredentials) GiteaRepository(org.jenkinsci.plugin.gitea.client.api.GiteaRepository)

Aggregations

GiteaServer (org.jenkinsci.plugin.gitea.servers.GiteaServer)6 StandardCredentials (com.cloudbees.plugins.credentials.common.StandardCredentials)4 JenkinsLocationConfiguration (jenkins.model.JenkinsLocationConfiguration)3 GiteaConnection (org.jenkinsci.plugin.gitea.client.api.GiteaConnection)3 GiteaEventType (org.jenkinsci.plugin.gitea.client.api.GiteaEventType)3 GiteaHook (org.jenkinsci.plugin.gitea.client.api.GiteaHook)3 AbortException (hudson.AbortException)2 IOException (java.io.IOException)2 GiteaAuth (org.jenkinsci.plugin.gitea.client.api.GiteaAuth)2 GiteaRepository (org.jenkinsci.plugin.gitea.client.api.GiteaRepository)2 IgnoreNotifyCommit (hudson.plugins.git.extensions.impl.IgnoreNotifyCommit)1 ConfiguredWithReadme (io.jenkins.plugins.casc.misc.ConfiguredWithReadme)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 List (java.util.List)1 SCMSourceOwner (jenkins.scm.api.SCMSourceOwner)1 RemoteConfig (org.eclipse.jgit.transport.RemoteConfig)1 URIish (org.eclipse.jgit.transport.URIish)1