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;
}
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;
}
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);
}
}
Aggregations