Search in sources :

Example 1 with GiteaUser

use of org.jenkinsci.plugin.gitea.client.api.GiteaUser in project gitea-plugin by jenkinsci.

the class GiteaWebhookListener method register.

public static void register(SCMNavigatorOwner owner, GiteaSCMNavigator navigator, WebhookRegistration mode, String credentialsId) {
    StandardCredentials credentials;
    String serverUrl = navigator.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 = navigator.credentials(owner);
            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)) {
        GiteaUser user = c.fetchUser(navigator.getRepoOwner());
        if (StringUtils.isNotBlank(user.getEmail())) {
            // it's a user not an org
            return;
        }
        GiteaOrganization org = c.fetchOrganization(navigator.getRepoOwner());
        if (org == null) {
            return;
        }
        List<GiteaHook> hooks = c.fetchHooks(org);
        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(org, 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(org, hook);
        }
    } catch (IOException | InterruptedException e) {
        LOGGER.log(Level.WARNING, "Could not manage organization hooks for " + navigator.getRepoOwner() + " 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) GiteaOrganization(org.jenkinsci.plugin.gitea.client.api.GiteaOrganization) GiteaServer(org.jenkinsci.plugin.gitea.servers.GiteaServer) GiteaUser(org.jenkinsci.plugin.gitea.client.api.GiteaUser) GiteaEventType(org.jenkinsci.plugin.gitea.client.api.GiteaEventType) StandardCredentials(com.cloudbees.plugins.credentials.common.StandardCredentials)

Example 2 with GiteaUser

use of org.jenkinsci.plugin.gitea.client.api.GiteaUser in project gitea-plugin by jenkinsci.

the class MockGiteaConnection method withUser.

public MockGiteaConnection withUser(GiteaUser user) {
    GiteaUser clone = user.clone();
    clone.setId(nextId.incrementAndGet());
    this.users.put(user.getUsername(), clone);
    return this;
}
Also used : GiteaUser(org.jenkinsci.plugin.gitea.client.api.GiteaUser)

Aggregations

GiteaUser (org.jenkinsci.plugin.gitea.client.api.GiteaUser)2 StandardCredentials (com.cloudbees.plugins.credentials.common.StandardCredentials)1 IOException (java.io.IOException)1 JenkinsLocationConfiguration (jenkins.model.JenkinsLocationConfiguration)1 GiteaConnection (org.jenkinsci.plugin.gitea.client.api.GiteaConnection)1 GiteaEventType (org.jenkinsci.plugin.gitea.client.api.GiteaEventType)1 GiteaHook (org.jenkinsci.plugin.gitea.client.api.GiteaHook)1 GiteaOrganization (org.jenkinsci.plugin.gitea.client.api.GiteaOrganization)1 GiteaServer (org.jenkinsci.plugin.gitea.servers.GiteaServer)1