Skip to content
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

Resolve incorrect badge examples when opened directly from URL #290

Merged
merged 2 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@
/* Needed for the jelly syntax hints page */
String url = "";
StaplerRequest req = Stapler.getCurrentRequest();
if (req != null) {
url = req.getReferer();
if (url == null) {
url = "null-referer";
if (req != null && req.getRequestURL() != null) {

Check warning on line 56 in src/main/java/org/jenkinsci/plugins/badge/actions/JobBadgeAction.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 56 is only partially covered, 3 branches are missing
url = req.getRequestURL().toString();
int badgeIndex = url.lastIndexOf("badge/");

if (badgeIndex != -1) {
url = url.substring(0, badgeIndex);

Check warning on line 61 in src/main/java/org/jenkinsci/plugins/badge/actions/JobBadgeAction.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 57-61 are not covered by tests
}
}
return url;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.WebMethod;

public class RunBadgeAction implements Action, IconSpec {
Expand Down Expand Up @@ -46,10 +47,19 @@
}

public String getUrl() {
/* TODO: Is a permission check needed here? */

Check warning on line 50 in src/main/java/org/jenkinsci/plugins/badge/actions/RunBadgeAction.java

View check run for this annotation

ci.jenkins.io / Open Tasks Scanner

TODO

NORMAL: Is a permission check needed here? */
/* Needed for the jelly syntax hints page */
String url = Stapler.getCurrentRequest().getReferer();
return url == null ? "null-referer" : url;
String url = "";
StaplerRequest req = Stapler.getCurrentRequest();
if (req != null && req.getRequestURL() != null) {

Check warning on line 54 in src/main/java/org/jenkinsci/plugins/badge/actions/RunBadgeAction.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 54 is only partially covered, 2 branches are missing
url = req.getRequestURL().toString();
int badgeIndex = url.lastIndexOf("badge/");

if (badgeIndex != -1) {
url = url.substring(0, badgeIndex);
}
}
return url;
}

public String getUrlEncodedFullName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,24 @@ void getUrlEncodedFullNameWithProjectNull() {
}

@Test
void getUrl() {
void getUrlWithoutBadge() {
try (MockedStatic<Stapler> mockedStatic = Mockito.mockStatic(Stapler.class)) {
StaplerRequest staplerRequest = Mockito.mock(StaplerRequest.class);
Mockito.when(staplerRequest.getReferer()).thenReturn("referer");
Mockito.when(staplerRequest.getRequestURL()).thenReturn(new StringBuffer("http://jenkins.io/"));
mockedStatic.when(() -> Stapler.getCurrentRequest()).thenReturn(staplerRequest);

assertThat(runBadgeAction.getUrl(), is("referer"));
assertThat(runBadgeAction.getUrl(), is("http://jenkins.io/"));
}
}

@Test
void getUrlWithBadge() {
try (MockedStatic<Stapler> mockedStatic = Mockito.mockStatic(Stapler.class)) {
StaplerRequest staplerRequest = Mockito.mock(StaplerRequest.class);
Mockito.when(staplerRequest.getRequestURL()).thenReturn(new StringBuffer("http://jenkins.io/badge/"));
mockedStatic.when(() -> Stapler.getCurrentRequest()).thenReturn(staplerRequest);

assertThat(runBadgeAction.getUrl(), is("http://jenkins.io/"));
}
}

Expand Down