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