Skip to content

Commit

Permalink
Resolved incorrect badge examples for some jobs when opened directly …
Browse files Browse the repository at this point in the history
…from URL
  • Loading branch information
sridamul committed Feb 15, 2024
1 parent 3496301 commit fe1801e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ public String getUrl() {
/* 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 @@ -48,8 +49,17 @@ public String getUrlName() {
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

0 comments on commit fe1801e

Please sign in to comment.