Skip to content

Commit

Permalink
Use scm API adapted for Jakarta EE 9
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkEWaite committed Oct 9, 2024
1 parent 8071fe8 commit 91db9e2
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 70 deletions.
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>scm-api</artifactId>
<!-- TODO: Replace with release once it is available -->
<version>698.v29a_7e2c2a_6cf</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down
71 changes: 1 addition & 70 deletions src/main/java/hudson/plugins/git/GitStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,75 +115,6 @@ public String toString() {
return s.toString();
}

/**
* Copied from scm API plugin.
* Helper method to get the origin of an event from a {@link HttpServletRequest}. The current format is the
* list of hostname / ip addresses from the request (parsing {@code X-Forwarded-For} headers) separated by
* {@code} followed by a {@code} and finally the requested URL (omitting the query portion of the URL).
*
* @param req the {@link HttpServletRequest} or {@code null} (this is to allow passing
* {@link Stapler#getCurrentRequest()} without having to check for {@code null})
* @return the origin of the event or {@code null} if the {@link HttpServletRequest} is null.
* @since 2.0.3
*/
@CheckForNull
private String originOf(@CheckForNull HttpServletRequest req) {
if (req == null) {
return null;
}
String last = null;
StringBuilder result = new StringBuilder();
// TODO RFC 7239 support once standard is approved
String header = req.getHeader("X-Forwarded-For");
if (StringUtils.isNotBlank(header)) {
for (String remote : header.split("(,\\s*)")) {
if (StringUtils.isBlank(remote)) {
continue;
}
if (last != null) {
result.append(" → ");
}
last = StringUtils.trim(remote);
result.append(last);
}
}
String remoteHost = req.getRemoteHost();
String remoteAddr = req.getRemoteAddr();
if (last == null || (!(StringUtils.equals(last, remoteHost) || StringUtils.equals(last, remoteAddr)))) {
if (last != null) {
result.append(" → ");
}
if (!StringUtils.isBlank(remoteHost) && !remoteHost.equals(remoteAddr)) {
result.append(remoteHost);
result.append('/');
}
result.append(remoteAddr);
}
result.append(" ⇒ ");
String scheme = StringUtils.defaultIfBlank(req.getHeader("X-Forwarded-Proto"), req.getScheme());
result.append(scheme);
result.append("://");
result.append(req.getServerName());
String portStr = req.getHeader("X-Forwarded-Port");
int port;
if (portStr != null) {
try {
port = Integer.parseInt(portStr);
} catch (NumberFormatException e) {
port = req.getLocalPort();
}
} else {
port = req.getLocalPort();
}
if (!("http".equals(scheme) && port == 80 || "https".equals(scheme) && port == 443)) {
result.append(':');
result.append(port);
}
result.append(req.getRequestURI());
// omit query as may contain "secrets"
return result.toString();
}

public HttpResponse doNotifyCommit(HttpServletRequest request, @QueryParameter(required=true) String url,
@QueryParameter() String branches, @QueryParameter() String sha1,
@QueryParameter() String token) {
Expand Down Expand Up @@ -242,7 +173,7 @@ public HttpResponse doNotifyCommit(HttpServletRequest request, @QueryParameter(r

final List<ResponseContributor> contributors = new ArrayList<>();
Jenkins jenkins = Jenkins.get();
String origin = originOf(request);
String origin = SCMEvent.originOf(request);
for (Listener listener : jenkins.getExtensionList(Listener.class)) {
contributors.addAll(listener.onNotifyCommit(origin, uri, sha1, buildParameters, branchesArray));
}
Expand Down

0 comments on commit 91db9e2

Please sign in to comment.