jenkins.scm.api.SCMNavigatorOwner Java Examples

The following examples show how to use jenkins.scm.api.SCMNavigatorOwner. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GitHubSCMNavigator.java    From github-branch-source-plugin with MIT License 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void afterSave(@NonNull SCMNavigatorOwner owner) {
    GitHubWebHook.get().registerHookFor(owner);
    try {
        // FIXME MINOR HACK ALERT
        StandardCredentials credentials = Connector.lookupScanCredentials((Item)owner, getApiUri(), credentialsId);
        GitHub hub = Connector.connect(getApiUri(), credentials);
        try {
            GitHubOrgWebHook.register(hub, repoOwner);
        } finally {
            Connector.release(hub);
        }
    } catch (IOException e) {
        DescriptorImpl.LOGGER.log(Level.WARNING, e.getMessage(), e);
    }
}
 
Example #2
Source File: GitLabSCMNavigator.java    From gitlab-branch-source-plugin with MIT License 5 votes vote down vote up
@Override
public void afterSave(@NonNull SCMNavigatorOwner owner) {
    GitLabSCMNavigatorContext navigatorContext = new GitLabSCMNavigatorContext()
        .withTraits(traits);
    GitLabSCMSourceContext ctx = new GitLabSCMSourceContext(null, SCMHeadObserver.none())
        .withTraits(navigatorContext.traits());
    GitLabHookRegistration systemhookMode = ctx.systemhookRegistration();
    GitLabHookCreator.register(owner, this, systemhookMode);
}
 
Example #3
Source File: GitHubSCMNavigatorTest.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
@Test
public void fetchActions() throws Exception {
    assertThat(navigator.fetchActions(Mockito.mock(SCMNavigatorOwner.class), null, null), Matchers.containsInAnyOrder(
            Matchers.is(
                    new ObjectMetadataAction("CloudBeers, Inc.", null, "https://github.com/cloudbeers")
            ),
            Matchers.is(new GitHubOrgMetadataAction("https://avatars.githubusercontent.com/u/4181899?v=3")),
            Matchers.is(new GitHubLink("icon-github-logo", "https://github.com/cloudbeers"))));
}
 
Example #4
Source File: GitHubRepositoryEventSubscriber.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
@Override
protected boolean isApplicable(@Nullable Item item) {
    if (item instanceof SCMNavigatorOwner) {
        for (SCMNavigator navigator : ((SCMNavigatorOwner) item).getSCMNavigators()) {
            if (navigator instanceof GitHubSCMNavigator) {
                return true; // TODO allow navigators to opt-out
            }
        }
    }
    return false;
}
 
Example #5
Source File: GitHubSCMNavigator.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@NonNull
@Override
public List<Action> retrieveActions(@NonNull SCMNavigatorOwner owner,
                                    @CheckForNull SCMNavigatorEvent event,
                                    @NonNull TaskListener listener) throws IOException, InterruptedException {
    // TODO when we have support for trusted events, use the details from event if event was from trusted source
    listener.getLogger().printf("Looking up details of %s...%n", getRepoOwner());
    List<Action> result = new ArrayList<>();
    StandardCredentials credentials = Connector.lookupScanCredentials((Item)owner, getApiUri(), credentialsId);
    GitHub hub = Connector.connect(getApiUri(), credentials);
    try {
        Connector.checkApiRateLimit(listener, hub);
        GHUser u = hub.getUser(getRepoOwner());
        String objectUrl = u.getHtmlUrl() == null ? null : u.getHtmlUrl().toExternalForm();
        result.add(new ObjectMetadataAction(
                Util.fixEmpty(u.getName()),
                null,
                objectUrl)
        );
        result.add(new GitHubOrgMetadataAction(u));
        result.add(new GitHubLink("icon-github-logo", u.getHtmlUrl()));
        if (objectUrl == null) {
            listener.getLogger().println("Organization URL: unspecified");
        } else {
            listener.getLogger().printf("Organization URL: %s%n",
                    HyperlinkNote.encodeTo(objectUrl, StringUtils.defaultIfBlank(u.getName(), objectUrl)));
        }
        return result;
    } finally {
        Connector.release(hub);
    }
}
 
Example #6
Source File: GitLabSCMWebHookItemListener.java    From gitlab-branch-source-plugin with GNU General Public License v2.0 5 votes vote down vote up
private void onUpdated(GitLabSCMNavigator navigator, SCMNavigatorOwner owner) {
    if (navigator.saved()) {
        if (navigator.getListenToWebHooks()) {
            register(navigator, owner);
        } else {
            unregister(navigator, owner);
        }
    }
}
 
Example #7
Source File: GitLabSCMWebHookItemListener.java    From gitlab-branch-source-plugin with GNU General Public License v2.0 5 votes vote down vote up
private void onUpdated(SCMNavigatorOwner owner) {
    for (SCMNavigator navigator : owner.getSCMNavigators()) {
        if (navigator instanceof GitLabSCMNavigator) {
            onUpdated((GitLabSCMNavigator) navigator, owner);
        }
    }
}
 
Example #8
Source File: GitLabSCMWebHookItemListener.java    From gitlab-branch-source-plugin with GNU General Public License v2.0 5 votes vote down vote up
private void onDeleted(SCMNavigatorOwner owner) {
    for (SCMNavigator navigator : owner.getSCMNavigators()) {
        if (navigator instanceof GitLabSCMNavigator) {
            unregister((GitLabSCMNavigator) navigator, owner);
        }
    }
}
 
Example #9
Source File: GitLabSCMWebHookItemListener.java    From gitlab-branch-source-plugin with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onDeleted(Item item) {
    if (item instanceof SCMNavigatorOwner) {
        onDeleted((SCMNavigatorOwner) item);
    }

    if (item instanceof SCMSourceOwner) {
        onDeleted((SCMSourceOwner) item);
    }
}
 
Example #10
Source File: GitLabSCMNavigator.java    From gitlab-branch-source-plugin with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void afterSave(@Nonnull SCMNavigatorOwner owner) {
    if (getListenToWebHooks()) {
        GitLabSCMWebHook.get().addListener(this, owner);
        saved = true;
    } else {
        GitLabSCMWebHook.get().removeListener(this, owner);
    }
}
 
Example #11
Source File: GiteaSCMNavigator.java    From gitea-plugin with MIT License 5 votes vote down vote up
@Override
public void afterSave(@NonNull SCMNavigatorOwner owner) {
    WebhookRegistration mode = new GiteaSCMSourceContext(null, SCMHeadObserver.none())
            .withTraits(new GiteaSCMNavigatorContext().withTraits(traits).traits())
            .webhookRegistration();
    GiteaWebhookListener.register(owner, this, mode, credentialsId);
}
 
Example #12
Source File: GiteaSCMNavigator.java    From gitea-plugin with MIT License 5 votes vote down vote up
@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 website: unspecified");
        } else {
            listener.getLogger().printf("Organization website: %s%n",
                    HyperlinkNote.encodeTo(website, StringUtils.defaultIfBlank(giteaOwner.getFullName(), website)));
        }
    }
    return result;
}
 
Example #13
Source File: GitLabHookCreator.java    From gitlab-branch-source-plugin with MIT License 5 votes vote down vote up
public static void register(SCMNavigatorOwner owner, GitLabSCMNavigator navigator,
    GitLabHookRegistration systemhookMode) {
    PersonalAccessToken credentials;
    GitLabServer server = GitLabServers.get().findServer(navigator.getServerName());
    if (server == null) {
        return;
    }
    switch (systemhookMode) {
        case DISABLE:
            return;
        case SYSTEM:
            if (!server.isManageSystemHooks()) {
                return;
            }
            credentials = server.getCredentials();
            if (credentials == null) {
                LOGGER.log(Level.WARNING, "No System credentials added, cannot create system hook");
            }
            break;
        case ITEM:
            credentials = navigator.credentials(owner);
            if (credentials == null) {
                LOGGER.log(Level.WARNING, "No Item credentials added, cannot create system hook");
            }
            break;
        default:
            return;
    }
    // add system hooks
    if (credentials != null) {
        createSystemHookWhenMissing(server, credentials);
    }
}
 
Example #14
Source File: GitLabSCMNavigator.java    From gitlab-branch-source-plugin with MIT License 5 votes vote down vote up
@NonNull
@Override
protected List<Action> retrieveActions(@NonNull SCMNavigatorOwner owner,
    SCMNavigatorEvent event,
    @NonNull TaskListener listener) throws IOException, InterruptedException {
    getGitlabOwner();
    String fullName = gitlabOwner.getFullName();
    String webUrl = gitlabOwner.getWebUrl();
    String avatarUrl = gitlabOwner.getAvatarUrl();
    String description = null;
    if (gitlabOwner instanceof GitLabGroup) {
        description = ((GitLabGroup) gitlabOwner).getDescription();
    }
    List<Action> result = new ArrayList<>();
    result.add(new ObjectMetadataAction(
        Util.fixEmpty(fullName),
        description,
        webUrl)
    );
    if (StringUtils.isNotBlank(avatarUrl)) {
        result.add(new GitLabAvatar(avatarUrl));
    }
    result.add(GitLabLink.toGroup(webUrl));
    if (StringUtils.isBlank(webUrl)) {
        listener.getLogger().println("Web URL unspecified");
    } else {
        listener.getLogger().printf("%s URL: %s%n", gitlabOwner.getWord(),
            HyperlinkNote
                .encodeTo(webUrl, StringUtils.defaultIfBlank(fullName, webUrl)));
    }
    return result;
}
 
Example #15
Source File: GitLabSCMWebHookItemListener.java    From gitlab-branch-source-plugin with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void onUpdated(Item item) {
    if (item instanceof SCMNavigatorOwner) {
        onUpdated((SCMNavigatorOwner) item);
    }
}
 
Example #16
Source File: GitLabSCMNavigator.java    From gitlab-branch-source-plugin with GNU General Public License v2.0 4 votes vote down vote up
@Nonnull
@Override
protected List<Action> retrieveActions(@Nonnull SCMNavigatorOwner owner, @CheckForNull SCMNavigatorEvent event, @Nonnull TaskListener listener) throws IOException, InterruptedException {
    return Collections.<Action>singletonList(GitLabLinkAction.create(getPronoun(), ICON_GITLAB, gitLabConnection(getSourceSettings().getConnectionName()).getUrl()));
}
 
Example #17
Source File: GiteaWebhookListener.java    From gitea-plugin with MIT License 4 votes vote down vote up
public static void register(SCMNavigatorOwner owner, GiteaSCMNavigator navigator,
                            WebhookRegistration mode, String credentialsId) {
    StandardCredentials credentials;
    String serverUrl = navigator.getServerUrl();
    switch (mode) {
        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;
        case DISABLE:
        default:
            return;
    }
    if (credentials == null) {
        return;
    }
    JenkinsLocationConfiguration locationConfiguration = JenkinsLocationConfiguration.get();
    String rootUrl = locationConfiguration.getUrl();
    if (StringUtils.isBlank(rootUrl) || rootUrl.startsWith("http://localhost:")) {
        // Jenkins URL not configured, can't register hooks
        LOGGER.log(Level.FINE, "JENKINS_URL is not defined. Cannot register a WebHook.");
        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);
    }
}
 
Example #18
Source File: GitLabSCMWebHookItemListener.java    From gitlab-branch-source-plugin with GNU General Public License v2.0 4 votes vote down vote up
private void register(GitLabSCMNavigator navigator, SCMNavigatorOwner owner) {
    GitLabSCMWebHook.get().addListener(navigator, owner);
}
 
Example #19
Source File: GitLabSCMWebHookItemListener.java    From gitlab-branch-source-plugin with GNU General Public License v2.0 4 votes vote down vote up
private void unregister(GitLabSCMNavigator navigator, SCMNavigatorOwner owner) {
    if (navigator.getListenToWebHooks() && !StringUtils.isEmpty(navigator.getSourceSettings().getConnectionName())) {
        GitLabSCMWebHook.get().removeListener(navigator, owner);
    }
}
 
Example #20
Source File: GitLabSCMWebHook.java    From gitlab-branch-source-plugin with GNU General Public License v2.0 4 votes vote down vote up
private void handle(GitLabSCMNavigator navigator, SCMNavigatorOwner owner) {
    if (navigator.getListenToWebHooks() && !StringUtils.isEmpty(navigator.getSourceSettings().getConnectionName())) {
        hook.addListener(navigator, owner);
    }
}