Skip to content

Commit

Permalink
Merge pull request #150 from cronik/feature/do-toggle-favorite-refresh
Browse files Browse the repository at this point in the history
Simplify doToggleFavorite
  • Loading branch information
mawinter69 authored Dec 11, 2023
2 parents 0a4dda9 + ec3fd7b commit ef3c368
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 46 deletions.
50 changes: 15 additions & 35 deletions src/main/java/hudson/plugins/favorite/FavoritePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import jenkins.model.Jenkins;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.HttpResponses;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
Expand All @@ -19,47 +20,26 @@ public class FavoritePlugin extends Plugin {

@RequirePOST
public void doToggleFavorite(StaplerRequest req, StaplerResponse resp, @QueryParameter String job, @QueryParameter Boolean redirect) throws IOException {
Jenkins jenkins = Jenkins.get();
User user = User.current();
if (user != null) {
try {
Favorites.toggleFavorite(user, getItem(job));
} catch (FavoriteException e) {
throw new IOException(e);
Jenkins jenkins = Jenkins.get();
Item item = jenkins.getItemByFullName(job);
if (item == null) {
throw HttpResponses.notFound();
}
}
if(redirect) {
if (!job.contains("/"))
{
// Works for default URL pattern: rootUrl/job/jobName
resp.sendRedirect(resp.encodeRedirectURL(jenkins.getRootUrl() + "job/" + job));

try {
Favorites.toggleFavorite(user, item);
} catch (FavoriteException fe) {
throw HttpResponses.error(fe);

Check warning on line 34 in src/main/java/hudson/plugins/favorite/FavoritePlugin.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 33-34 are not covered by tests
}
else
{
// Works for folder URL pattern:
// rootUrl/job/folder/job/jobName
// rootUrl/job/folder/job/subfolder/job/jobName etc.
StringBuilder urlPostfix = new StringBuilder("job/");
String[] itemNames = job.split("/");
for (int i = 0; i < itemNames.length; i++)
{
urlPostfix.append(itemNames[i]);
if (i < itemNames.length - 1)
{
urlPostfix.append("/job/");
}
}
resp.sendRedirect(resp.encodeRedirectURL(jenkins.getRootUrl() + urlPostfix.toString()));

if (redirect) {
resp.sendRedirect(jenkins.getRootUrl() + item.getUrl());
}
} else {
throw HttpResponses.forbidden();
}
}

public static Item getItem(String fullName) {
Jenkins jenkins = Jenkins.get();
Item item = jenkins.getItemByFullName(fullName);
if (item == null) {
throw new IllegalArgumentException("Item <" + fullName + "> does not exist");
}
return item;
}
}
15 changes: 4 additions & 11 deletions src/test/java/hudson/plugins/favorite/FavoritesPluginTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

public class FavoritesPluginTest {

private static List<String> events = new CopyOnWriteArrayList<>();
private static final List<String> events = new CopyOnWriteArrayList<>();
@Rule
public JenkinsRule rule = new JenkinsRule();

Expand All @@ -47,9 +47,7 @@ public void testToggleFavoriteAnonymous() throws Exception {
rule.createFreeStyleProject("cats");
try (JenkinsRule.WebClient client = rule.createWebClient()) {
WebResponse response = requestToggleFavorite(client, "cats", false);
// TODO should return 403 (pr to follow)
//assertEquals(403, response.getStatusCode());
assertEquals(200, response.getStatusCode());
assertEquals(403, response.getStatusCode());

// verify favorite column not visible
HtmlPage root = client.goTo("");
Expand Down Expand Up @@ -91,20 +89,15 @@ public void testToggleFavorite() throws Exception {
assertFalse(Favorites.isFavorite(bob, cats));
}

// TODO fix favorite listener issue (pr to follow)
//assertEquals(Arrays.asList("add:bob:cats", "remove:bob:cats"), events);
assertEquals(Arrays.asList("add:bob:cats", "remove:bob:cats"), events);
}

@Test
public void testToggleFavoriteJobDoesNotExist() throws Exception {
try (JenkinsRule.WebClient client = rule.createWebClient()) {
client.login("bob", "bob");
WebResponse res = requestToggleFavorite(client, "foo/bar", false);

// TODO should return 404 (pr to follow)
//assertEquals(404, res.getStatusCode());
assertEquals(500, res.getStatusCode());

assertEquals(404, res.getStatusCode());
}

assertEquals(0, events.size());
Expand Down

0 comments on commit ef3c368

Please sign in to comment.