New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add rest api for settings #71

Open
wants to merge 1 commit into
base: master
from
Jump to file or symbol
Failed to load files and symbols.
+224 −0
Diff settings

Always

Just for now

Copy path View file
@@ -119,6 +119,13 @@
<artifactId>xalan</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
@@ -0,0 +1,172 @@
package com.pragbits.bitbucketserver.rest;
import com.atlassian.bitbucket.AuthorisationException;
import com.atlassian.bitbucket.permission.Permission;
import com.atlassian.bitbucket.permission.PermissionValidationService;
import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.bitbucket.repository.RepositoryService;
import com.pragbits.bitbucketserver.ImmutableSlackSettings;
import com.pragbits.bitbucketserver.SlackGlobalSettingsService;
import com.pragbits.bitbucketserver.SlackSettings;
import com.pragbits.bitbucketserver.SlackSettingsService;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/settings")
public class RestResource {
private final SlackSettingsService slackSettingsService;
private final SlackGlobalSettingsService globalSettingsService;
private final RepositoryService repositoryService;
private final PermissionValidationService validationService;
public RestResource(SlackSettingsService slackSettingsService,
SlackGlobalSettingsService globalSettingsService,
RepositoryService repositoryService,
PermissionValidationService validationService) {
this.slackSettingsService = slackSettingsService;
this.globalSettingsService = globalSettingsService;
this.repositoryService = repositoryService;
this.validationService = validationService;
}
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getGlobalSettings() {
validate(() -> validationService.validateForGlobal(Permission.ADMIN));
return Response.ok(getDTOFromGlobalSettings(globalSettingsService)).build();
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/{projectKey}/{repoSlug}")
public Response getProjectSettings(@PathParam("projectKey") String projectKey,
@PathParam("repoSlug") String repoSlug) {
Repository repository = repositoryService.getBySlug(projectKey, repoSlug);
if (repository == null) {
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
validate(() -> validationService.validateForRepository(repository, Permission.REPO_READ));
SlackSettings settings = slackSettingsService.getSlackSettings(repository);
return Response.ok(getDTOFromSlackSettings(settings)).build();
}
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response postGlobalSettings(final SettingsDTO settings) {
validate(() -> validationService.validateForGlobal(Permission.SYS_ADMIN));
setGlobalSettings(settings);
return Response.ok().build();
}
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/{projectKey}/{repoSlug}")
public Response postProjectSettings(@PathParam("projectKey") String projectKey,
@PathParam("repoSlug") String repoSlug,
final SettingsDTO settings) {
Repository repository = repositoryService.getBySlug(projectKey, repoSlug);
if (repository == null) {
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
validate(() -> validationService.validateForRepository(repository, Permission.REPO_ADMIN));
slackSettingsService.setSlackSettings(repository, getSlackSettingsFromDTO(settings));
return Response.ok().build();
}
private void validate(Runnable validator) {
try {
validator.run();
} catch (AuthorisationException exception) {
throw new WebApplicationException(Response.Status.UNAUTHORIZED);
}
}
private SlackSettings getSlackSettingsFromDTO(final SettingsDTO settings) {
return new ImmutableSlackSettings(settings.isSlackNotificationsOverrideEnabled(), settings.isSlackNotificationsEnabled(), settings.isSlackNotificationsOpenedEnabled(), settings.isSlackNotificationsReopenedEnabled(), settings.isSlackNotificationsUpdatedEnabled(), settings.isSlackNotificationsApprovedEnabled(), settings.isSlackNotificationsUnapprovedEnabled(), settings.isSlackNotificationsDeclinedEnabled(), settings.isSlackNotificationsMergedEnabled(), settings.isSlackNotificationsCommentedEnabled(), settings.isSlackNotificationsEnabledForPush(), settings.isSlackNotificationsEnabledForPersonal(), settings.isSlackNotificationsNeedsWorkEnabled(), settings.getNotificationLevel(), settings.getNotificationPrLevel(), settings.getSlackChannelName(), settings.getSlackWebHookUrl(), settings.getSlackUsername(), settings.getSlackIconUrl(), settings.getSlackIconEmoji());
}
private void setGlobalSettings(final SettingsDTO settings) {
this.globalSettingsService.setChannelName(settings.getSlackChannelName());
this.globalSettingsService.setWebHookUrl(settings.getSlackWebHookUrl());
this.globalSettingsService.setChannelName(settings.getSlackChannelName());
this.globalSettingsService.setSlackNotificationsEnabled(settings.isSlackNotificationsEnabled());
this.globalSettingsService.setSlackNotificationsOpenedEnabled(settings.isSlackNotificationsOpenedEnabled());
this.globalSettingsService.setSlackNotificationsReopenedEnabled(settings.isSlackNotificationsReopenedEnabled());
this.globalSettingsService.setSlackNotificationsUpdatedEnabled(settings.isSlackNotificationsUpdatedEnabled());
this.globalSettingsService.setSlackNotificationsApprovedEnabled(settings.isSlackNotificationsApprovedEnabled());
this.globalSettingsService.setSlackNotificationsUnapprovedEnabled(settings.isSlackNotificationsUnapprovedEnabled());
this.globalSettingsService.setSlackNotificationsDeclinedEnabled(settings.isSlackNotificationsDeclinedEnabled());
this.globalSettingsService.setSlackNotificationsMergedEnabled(settings.isSlackNotificationsMergedEnabled());
this.globalSettingsService.setSlackNotificationsCommentedEnabled(settings.isSlackNotificationsCommentedEnabled());
this.globalSettingsService.setSlackNotificationsEnabledForPush(settings.isSlackNotificationsEnabledForPush());
this.globalSettingsService.setNotificationLevel(settings.getNotificationLevel().value());
this.globalSettingsService.setNotificationPrLevel(settings.getNotificationPrLevel().value());
this.globalSettingsService.setSlackNotificationsEnabledForPersonal(settings.isSlackNotificationsEnabledForPersonal());
this.globalSettingsService.setSlackNotificationsNeedsWorkEnabled(settings.isSlackNotificationsNeedsWorkEnabled());
this.globalSettingsService.setUsername(settings.getSlackUsername());
this.globalSettingsService.setIconUrl(settings.getSlackIconUrl());
this.globalSettingsService.setIconEmoji(settings.getSlackIconEmoji());
}
private static SettingsDTO getDTOFromSlackSettings(SlackSettings settings) {
return new SettingsDTO(settings.isSlackNotificationsOverrideEnabled(),
settings.isSlackNotificationsEnabled(),
settings.isSlackNotificationsOpenedEnabled(),
settings.isSlackNotificationsReopenedEnabled(),
settings.isSlackNotificationsUpdatedEnabled(),
settings.isSlackNotificationsApprovedEnabled(),
settings.isSlackNotificationsUnapprovedEnabled(),
settings.isSlackNotificationsDeclinedEnabled(),
settings.isSlackNotificationsMergedEnabled(),
settings.isSlackNotificationsCommentedEnabled(),
settings.isSlackNotificationsEnabledForPush(),
settings.isSlackNotificationsEnabledForPersonal(),
settings.isSlackNotificationsNeedsWorkEnabled(),
settings.getNotificationLevel(),
settings.getNotificationPrLevel(),
settings.getSlackChannelName(),
settings.getSlackWebHookUrl(),
settings.getSlackUsername(),
settings.getSlackIconUrl(),
settings.getSlackIconEmoji());
}
private static SettingsDTO getDTOFromGlobalSettings(SlackGlobalSettingsService service) {
return new SettingsDTO(false,
service.getSlackNotificationsEnabled(),
service.getSlackNotificationsOpenedEnabled(),
service.getSlackNotificationsReopenedEnabled(),
service.getSlackNotificationsUpdatedEnabled(),
service.getSlackNotificationsApprovedEnabled(),
service.getSlackNotificationsUnapprovedEnabled(),
service.getSlackNotificationsDeclinedEnabled(),
service.getSlackNotificationsMergedEnabled(),
service.getSlackNotificationsCommentedEnabled(),
service.getSlackNotificationsEnabledForPush(),
service.getSlackNotificationsEnabledForPersonal(),
service.getSlackNotificationsNeedsWorkEnabled(),
service.getNotificationLevel(),
service.getNotificationPrLevel(),
service.getChannelName(),
service.getWebHookUrl(),
service.getUsername(),
service.getIconUrl(),
service.getIconEmoji());
}
}
@@ -0,0 +1,41 @@
package com.pragbits.bitbucketserver.rest;
import com.pragbits.bitbucketserver.NotificationLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import static javax.xml.bind.annotation.XmlAccessType.FIELD;
@XmlRootElement
@XmlAccessorType(FIELD)
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
class SettingsDTO {
private boolean slackNotificationsOverrideEnabled;
private boolean slackNotificationsEnabled;
private boolean slackNotificationsOpenedEnabled;
private boolean slackNotificationsReopenedEnabled;
private boolean slackNotificationsUpdatedEnabled;
private boolean slackNotificationsApprovedEnabled;
private boolean slackNotificationsUnapprovedEnabled;
private boolean slackNotificationsDeclinedEnabled;
private boolean slackNotificationsMergedEnabled;
private boolean slackNotificationsCommentedEnabled;
private boolean slackNotificationsEnabledForPush;
private boolean slackNotificationsEnabledForPersonal;
private boolean slackNotificationsNeedsWorkEnabled;
private NotificationLevel notificationLevel;
private NotificationLevel notificationPrLevel;
private String slackChannelName;
private String slackWebHookUrl;
private String slackUsername;
private String slackIconUrl;
private String slackIconEmoji;
}
@@ -56,4 +56,8 @@
<url-pattern>/slack-global-settings/admin</url-pattern>
</servlet>
<rest key="slack-rest" path="/stash2slack" version="1.0">
<description>Stash2Slack Rest API</description>
</rest>
</atlassian-plugin>
ProTip! Use n and p to navigate between commits in a pull request.