Search in sources :

Example 1 with GiteaOrganization

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

the class MockGiteaConnection method withOrg.

public MockGiteaConnection withOrg(GiteaOrganization org) {
    GiteaOrganization clone = org.clone();
    clone.setId(nextId.incrementAndGet());
    this.organizations.put(org.getUsername(), clone);
    this.orgHooks.put(org.getUsername(), new ArrayList<GiteaHook>());
    return this;
}
Also used : GiteaOrganization(org.jenkinsci.plugin.gitea.client.api.GiteaOrganization) GiteaHook(org.jenkinsci.plugin.gitea.client.api.GiteaHook)

Example 2 with GiteaOrganization

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

the class GiteaSCMNavigator method retrieveActions.

@NonNull
@Override
protected List<Action> retrieveActions(@NonNull SCMNavigatorOwner owner, SCMNavigatorEvent event, @NonNull TaskListener listener) throws IOException, InterruptedException {
    if (this.giteaOwner == null) {
        try (GiteaConnection c = gitea(owner).open()) {
            this.giteaOwner = c.fetchUser(repoOwner);
            if (StringUtils.isBlank(giteaOwner.getEmail())) {
                this.giteaOwner = c.fetchOrganization(repoOwner);
            }
        }
    }
    List<Action> result = new ArrayList<>();
    String objectUrl = UriTemplate.buildFromTemplate(serverUrl).path("owner").build().set("owner", repoOwner).expand();
    result.add(new ObjectMetadataAction(Util.fixEmpty(giteaOwner.getFullName()), null, objectUrl));
    if (StringUtils.isNotBlank(giteaOwner.getAvatarUrl())) {
        result.add(new GiteaAvatar(giteaOwner.getAvatarUrl()));
    }
    result.add(new GiteaLink("icon-gitea-org", objectUrl));
    if (giteaOwner instanceof GiteaOrganization) {
        String website = ((GiteaOrganization) giteaOwner).getWebsite();
        if (StringUtils.isBlank(website)) {
            listener.getLogger().println("Organization URL: unspecified");
            listener.getLogger().printf("Organization URL: %s%n", HyperlinkNote.encodeTo(website, StringUtils.defaultIfBlank(giteaOwner.getFullName(), website)));
        }
    }
    return result;
}
Also used : ObjectMetadataAction(jenkins.scm.api.metadata.ObjectMetadataAction) Action(hudson.model.Action) GiteaOrganization(org.jenkinsci.plugin.gitea.client.api.GiteaOrganization) ObjectMetadataAction(jenkins.scm.api.metadata.ObjectMetadataAction) NamedArrayList(jenkins.scm.impl.form.NamedArrayList) ArrayList(java.util.ArrayList) GiteaConnection(org.jenkinsci.plugin.gitea.client.api.GiteaConnection) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 3 with GiteaOrganization

use of org.jenkinsci.plugin.gitea.client.api.GiteaOrganization 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)

Aggregations

GiteaOrganization (org.jenkinsci.plugin.gitea.client.api.GiteaOrganization)3 GiteaConnection (org.jenkinsci.plugin.gitea.client.api.GiteaConnection)2 GiteaHook (org.jenkinsci.plugin.gitea.client.api.GiteaHook)2 StandardCredentials (com.cloudbees.plugins.credentials.common.StandardCredentials)1 NonNull (edu.umd.cs.findbugs.annotations.NonNull)1 Action (hudson.model.Action)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 JenkinsLocationConfiguration (jenkins.model.JenkinsLocationConfiguration)1 ObjectMetadataAction (jenkins.scm.api.metadata.ObjectMetadataAction)1 NamedArrayList (jenkins.scm.impl.form.NamedArrayList)1 GiteaEventType (org.jenkinsci.plugin.gitea.client.api.GiteaEventType)1 GiteaUser (org.jenkinsci.plugin.gitea.client.api.GiteaUser)1 GiteaServer (org.jenkinsci.plugin.gitea.servers.GiteaServer)1