This repository has been archived by the owner on Jun 9, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Trigger Notification Button on Pull Request View #33
- Loading branch information
1 parent
da1c1dd
commit 98869f8
Showing
16 changed files
with
470 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package se.bjurr.prnfs; | ||
|
||
import static com.atlassian.stash.user.Permission.ADMIN; | ||
import static javax.ws.rs.core.MediaType.APPLICATION_JSON; | ||
import static javax.ws.rs.core.Response.status; | ||
import static javax.ws.rs.core.Response.Status.OK; | ||
import static javax.ws.rs.core.Response.Status.UNAUTHORIZED; | ||
import static se.bjurr.prnfs.listener.PrnfsPullRequestAction.MANUAL_TRIGGER; | ||
import static se.bjurr.prnfs.settings.SettingsStorage.getPrnfsSettings; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.PathParam; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.Context; | ||
import javax.ws.rs.core.Response; | ||
|
||
import se.bjurr.prnfs.listener.PrnfsPullRequestAction; | ||
import se.bjurr.prnfs.listener.PrnfsPullRequestEventListener; | ||
import se.bjurr.prnfs.listener.PrnfsRenderer; | ||
import se.bjurr.prnfs.settings.PrnfsNotification; | ||
import se.bjurr.prnfs.settings.PrnfsSettings; | ||
|
||
import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory; | ||
import com.atlassian.sal.api.user.UserManager; | ||
import com.atlassian.stash.event.pull.PullRequestEvent; | ||
import com.atlassian.stash.pull.PullRequest; | ||
import com.atlassian.stash.pull.PullRequestService; | ||
import com.atlassian.stash.repository.RepositoryService; | ||
import com.atlassian.stash.user.Permission; | ||
import com.atlassian.stash.user.SecurityService; | ||
import com.atlassian.stash.user.StashUser; | ||
import com.atlassian.stash.user.UserService; | ||
import com.atlassian.stash.util.Operation; | ||
|
||
@Path("/manual") | ||
public class ManualResource { | ||
private final UserManager userManager; | ||
private final UserService userService; | ||
private final PullRequestService pullRequestService; | ||
private final PrnfsPullRequestEventListener prnfsPullRequestEventListener; | ||
private final RepositoryService repositoryService; | ||
private final SecurityService securityService; | ||
private final PluginSettingsFactory pluginSettingsFactory; | ||
|
||
public ManualResource(UserManager userManager, UserService userService, PluginSettingsFactory pluginSettingsFactory, | ||
PullRequestService pullRequestService, PrnfsPullRequestEventListener prnfsPullRequestEventListener, | ||
RepositoryService repositoryService, SecurityService securityService) { | ||
this.userManager = userManager; | ||
this.userService = userService; | ||
this.pullRequestService = pullRequestService; | ||
this.prnfsPullRequestEventListener = prnfsPullRequestEventListener; | ||
this.repositoryService = repositoryService; | ||
this.securityService = securityService; | ||
this.pluginSettingsFactory = pluginSettingsFactory; | ||
} | ||
|
||
@GET | ||
@Path("{repositoryId}/{pullRequestId}") | ||
@Produces(APPLICATION_JSON) | ||
public Response get(@Context HttpServletRequest request, @PathParam("repositoryId") Integer repositoryId, | ||
@PathParam("pullRequestId") Long pullRequestId) throws Exception { | ||
if (userManager.getRemoteUser(request) == null) { | ||
return status(UNAUTHORIZED).build(); | ||
} | ||
|
||
final PullRequest pullRequest = pullRequestService.getById(repositoryId, pullRequestId); | ||
PullRequestEvent pullRequestEvent = null; | ||
final PrnfsSettings settings = securityService.withPermission(ADMIN, "Getting config").call( | ||
new Operation<PrnfsSettings, Exception>() { | ||
@Override | ||
public PrnfsSettings perform() throws Exception { | ||
return getPrnfsSettings(pluginSettingsFactory.createGlobalSettings()); | ||
} | ||
}); | ||
for (PrnfsNotification prnfsNotification : settings.getNotifications()) { | ||
PrnfsPullRequestAction pullRequestAction = PrnfsPullRequestAction.valueOf(MANUAL_TRIGGER); | ||
if (!prnfsNotification.getTriggers().contains(pullRequestAction)) { | ||
continue; | ||
} | ||
StashUser stashUser = userService.getUserBySlug(userManager.getRemoteUser(request).getUsername()); | ||
PrnfsRenderer renderer = new PrnfsRenderer(pullRequest, pullRequestAction, stashUser, repositoryService, | ||
prnfsNotification, pullRequestEvent); | ||
prnfsPullRequestEventListener.notify(prnfsNotification, renderer, pullRequest); | ||
} | ||
return status(OK).build(); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/main/java/se/bjurr/prnfs/ShouldShowTriggerButtonCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package se.bjurr.prnfs; | ||
|
||
import static com.atlassian.stash.user.Permission.ADMIN; | ||
import static java.lang.Boolean.TRUE; | ||
import static se.bjurr.prnfs.settings.SettingsStorage.getPrnfsSettings; | ||
|
||
import java.util.Map; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import se.bjurr.prnfs.settings.PrnfsSettings; | ||
|
||
import com.atlassian.plugin.PluginParseException; | ||
import com.atlassian.plugin.web.Condition; | ||
import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory; | ||
import com.atlassian.sal.api.user.UserManager; | ||
import com.atlassian.stash.user.SecurityService; | ||
import com.atlassian.stash.user.StashAuthenticationContext; | ||
import com.atlassian.stash.util.Operation; | ||
|
||
public class ShouldShowTriggerButtonCondition implements Condition { | ||
private static final Logger logger = LoggerFactory.getLogger(ShouldShowTriggerButtonCondition.class); | ||
|
||
public static final String REPOSITORY = "repository"; | ||
private final PluginSettingsFactory pluginSettingsFactory; | ||
private final SecurityService securityService; | ||
private final UserManager userManager; | ||
private final StashAuthenticationContext stashAuthenticationContext; | ||
|
||
public ShouldShowTriggerButtonCondition(PluginSettingsFactory pluginSettingsFactory, SecurityService securityService, | ||
StashAuthenticationContext stashAuthenticationContext, UserManager userManager) { | ||
this.pluginSettingsFactory = pluginSettingsFactory; | ||
this.securityService = securityService; | ||
this.userManager = userManager; | ||
this.stashAuthenticationContext = stashAuthenticationContext; | ||
} | ||
|
||
@Override | ||
public void init(Map<String, String> map) throws PluginParseException { | ||
|
||
} | ||
|
||
@Override | ||
public boolean shouldDisplay(Map<String, Object> context) { | ||
try { | ||
PrnfsSettings settings = securityService.withPermission(ADMIN, "Getting config").call( | ||
new Operation<PrnfsSettings, Exception>() { | ||
@Override | ||
public PrnfsSettings perform() throws Exception { | ||
return getPrnfsSettings(pluginSettingsFactory.createGlobalSettings()); | ||
} | ||
}); | ||
|
||
return shouldShowTriggerButton(settings); | ||
} catch (Exception e) { | ||
logger.error("Error when checking if PRNFS trigger button should be shown in PR view", e); | ||
return false; | ||
} | ||
} | ||
|
||
private boolean shouldShowTriggerButton(PrnfsSettings settings) { | ||
// TODO | ||
stashAuthenticationContext.getCurrentUser(); | ||
userManager.isAdmin(userManager.getRemoteUserKey()); | ||
return TRUE; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.