Skip to content
Open
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 @@ -44,10 +44,11 @@ public class ActionResolver {
private static final Pattern COMMIT_STATUS_PATTERN =
Pattern.compile("^(refs/[^/]+/)?(commits|builds)/(?<sha1>[0-9a-fA-F]+)(?<statusJson>/status.json)?$");

public WebHookAction resolve(final String projectName, StaplerRequest2 request) {
public WebHookAction resolve(StaplerRequest2 request) {
Iterator<String> restOfPathParts = Arrays.stream(request.getRestOfPath().split("/"))
.filter(s -> !s.isEmpty())
.iterator();
String projectName = restOfPathParts.hasNext() ? restOfPathParts.next() : "";
Item project = resolveProject(projectName, restOfPathParts);
if (project == null) {
throw HttpResponses.notFound();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public String getUrlName() {
return WEBHOOK_URL;
}

public void getDynamic(final String projectName, final StaplerRequest2 request, StaplerResponse2 response) {
LOGGER.log(Level.INFO, "WebHook called with url: {0}", request.getRequestURIWithQueryString());
actionResolver.resolve(projectName, request).execute(response);
public void doDynamic(final StaplerRequest2 request, StaplerResponse2 response) {
LOGGER.log(Level.FINE, "WebHook called with url: {0}", request.getRequestURIWithQueryString());
actionResolver.resolve(request).execute(response);
}

@Extension
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ static void setUp(JenkinsRule rule) {
void getBranchBuildPageRedirect() throws Exception {
String projectName = "getBranchBuildPageRedirect";
jenkins.createFreeStyleProject(projectName);
when(request.getRestOfPath()).thenReturn("");
when(request.getRestOfPath()).thenReturn(projectName);
when(request.hasParameter("ref")).thenReturn(true);
when(request.getMethod()).thenReturn("GET");

WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request);
WebHookAction resolvedAction = new ActionResolver().resolve(request);

assertThat(resolvedAction, instanceOf(BranchBuildPageRedirectAction.class));
}
Expand All @@ -63,10 +63,10 @@ void getBranchBuildPageRedirect() throws Exception {
void getCommitStatus() throws Exception {
String projectName = "getCommitStatus";
jenkins.createFreeStyleProject(projectName);
when(request.getRestOfPath()).thenReturn("builds/1234abcd/status.json");
when(request.getRestOfPath()).thenReturn(projectName + "/builds/1234abcd/status.json");
when(request.getMethod()).thenReturn("GET");

WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request);
WebHookAction resolvedAction = new ActionResolver().resolve(request);

assertThat(resolvedAction, instanceOf(StatusJsonAction.class));
}
Expand All @@ -75,10 +75,10 @@ void getCommitStatus() throws Exception {
void getCommitBuildPageRedirect_builds() throws Exception {
String projectName = "getCommitBuildPageRedirect_builds";
jenkins.createFreeStyleProject(projectName);
when(request.getRestOfPath()).thenReturn("builds/1234abcd");
when(request.getRestOfPath()).thenReturn(projectName + "/builds/1234abcd");
when(request.getMethod()).thenReturn("GET");

WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request);
WebHookAction resolvedAction = new ActionResolver().resolve(request);

assertThat(resolvedAction, instanceOf(CommitBuildPageRedirectAction.class));
}
Expand All @@ -87,10 +87,10 @@ void getCommitBuildPageRedirect_builds() throws Exception {
void getCommitBuildPageRedirect_commits() throws Exception {
String projectName = "getCommitBuildPageRedirect_commits";
jenkins.createFreeStyleProject(projectName);
when(request.getRestOfPath()).thenReturn("commits/7890efab");
when(request.getRestOfPath()).thenReturn(projectName + "/commits/7890efab");
when(request.getMethod()).thenReturn("GET");

WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request);
WebHookAction resolvedAction = new ActionResolver().resolve(request);

assertThat(resolvedAction, instanceOf(CommitBuildPageRedirectAction.class));
}
Expand All @@ -99,11 +99,11 @@ void getCommitBuildPageRedirect_commits() throws Exception {
void getBranchStatusPng() throws Exception {
String projectName = "getBranchStatusPng";
jenkins.createFreeStyleProject(projectName);
when(request.getRestOfPath()).thenReturn("builds/status.png");
when(request.getRestOfPath()).thenReturn(projectName + "/builds/status.png");
when(request.hasParameter("ref")).thenReturn(true);
when(request.getMethod()).thenReturn("GET");

WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request);
WebHookAction resolvedAction = new ActionResolver().resolve(request);

assertThat(resolvedAction, instanceOf(BranchStatusPngAction.class));
}
Expand All @@ -112,11 +112,11 @@ void getBranchStatusPng() throws Exception {
void getCommitStatusPng() throws Exception {
String projectName = "getCommitStatusPng";
jenkins.createFreeStyleProject(projectName);
when(request.getRestOfPath()).thenReturn("builds/status.png");
when(request.getRestOfPath()).thenReturn(projectName + "/builds/status.png");
when(request.hasParameter("ref")).thenReturn(false);
when(request.getMethod()).thenReturn("GET");

WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request);
WebHookAction resolvedAction = new ActionResolver().resolve(request);

assertThat(resolvedAction, instanceOf(CommitStatusPngAction.class));
}
Expand All @@ -125,13 +125,13 @@ void getCommitStatusPng() throws Exception {
void postMergeRequest() throws Exception {
String projectName = "postMergeRequest";
jenkins.createFreeStyleProject(projectName);
when(request.getRestOfPath()).thenReturn("");
when(request.getRestOfPath()).thenReturn(projectName);
when(request.getMethod()).thenReturn("POST");
when(request.getHeader("X-Gitlab-Event")).thenReturn("Merge Request Hook");
when(request.getInputStream())
.thenReturn(new ResourceServletInputStream("ActionResolverTest_postMergeRequest.json"));

WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request);
WebHookAction resolvedAction = new ActionResolver().resolve(request);

assertThat(resolvedAction, instanceOf(MergeRequestBuildAction.class));
}
Expand All @@ -140,13 +140,13 @@ void postMergeRequest() throws Exception {
void postSystemHookMergeRequest() throws Exception {
String projectName = "postSystemHookMergeRequest";
jenkins.createFreeStyleProject(projectName);
when(request.getRestOfPath()).thenReturn("");
when(request.getRestOfPath()).thenReturn(projectName);
when(request.getMethod()).thenReturn("POST");
when(request.getHeader("X-Gitlab-Event")).thenReturn("System Hook");
when(request.getInputStream())
.thenReturn(new ResourceServletInputStream("ActionResolverTest_postSystemHook_MergeRequest.json"));

WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request);
WebHookAction resolvedAction = new ActionResolver().resolve(request);

assertThat(resolvedAction, instanceOf(MergeRequestBuildAction.class));
}
Expand All @@ -155,13 +155,13 @@ void postSystemHookMergeRequest() throws Exception {
void postSystemHookPush() throws Exception {
String projectName = "postSystemHookPush";
jenkins.createFreeStyleProject(projectName);
when(request.getRestOfPath()).thenReturn("");
when(request.getRestOfPath()).thenReturn(projectName);
when(request.getMethod()).thenReturn("POST");
when(request.getHeader("X-Gitlab-Event")).thenReturn("System Hook");
when(request.getInputStream())
.thenReturn(new ResourceServletInputStream("ActionResolverTest_postSystemHook_Push.json"));

WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request);
WebHookAction resolvedAction = new ActionResolver().resolve(request);

assertThat(resolvedAction, instanceOf(PushBuildAction.class));
}
Expand All @@ -170,13 +170,13 @@ void postSystemHookPush() throws Exception {
void postSystemHookPushTag() throws Exception {
String projectName = "postSystemHookPushTag";
jenkins.createFreeStyleProject(projectName);
when(request.getRestOfPath()).thenReturn("");
when(request.getRestOfPath()).thenReturn(projectName);
when(request.getMethod()).thenReturn("POST");
when(request.getHeader("X-Gitlab-Event")).thenReturn("System Hook");
when(request.getInputStream())
.thenReturn(new ResourceServletInputStream("ActionResolverTest_postSystemHook_PushTag.json"));

WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request);
WebHookAction resolvedAction = new ActionResolver().resolve(request);

assertThat(resolvedAction, instanceOf(PushBuildAction.class));
}
Expand All @@ -185,12 +185,12 @@ void postSystemHookPushTag() throws Exception {
void postNote() throws Exception {
String projectName = "postNote";
jenkins.createFreeStyleProject(projectName);
when(request.getRestOfPath()).thenReturn("");
when(request.getRestOfPath()).thenReturn(projectName);
when(request.getMethod()).thenReturn("POST");
when(request.getHeader("X-Gitlab-Event")).thenReturn("Note Hook");
when(request.getInputStream()).thenReturn(new ResourceServletInputStream("ActionResolverTest_postNote.json"));

WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request);
WebHookAction resolvedAction = new ActionResolver().resolve(request);

assertThat(resolvedAction, instanceOf(NoteBuildAction.class));
}
Expand All @@ -199,12 +199,12 @@ void postNote() throws Exception {
void postPush() throws Exception {
String projectName = "postPush";
jenkins.createFreeStyleProject(projectName);
when(request.getRestOfPath()).thenReturn("");
when(request.getRestOfPath()).thenReturn(projectName);
when(request.getMethod()).thenReturn("POST");
when(request.getHeader("X-Gitlab-Event")).thenReturn("Push Hook");
when(request.getInputStream()).thenReturn(new ResourceServletInputStream("ActionResolverTest_postPush.json"));

WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request);
WebHookAction resolvedAction = new ActionResolver().resolve(request);

assertThat(resolvedAction, instanceOf(PushBuildAction.class));
}
Expand All @@ -213,13 +213,13 @@ void postPush() throws Exception {
void postPushTag() throws Exception {
String projectName = "postPushTag";
jenkins.createFreeStyleProject(projectName);
when(request.getRestOfPath()).thenReturn("");
when(request.getRestOfPath()).thenReturn(projectName);
when(request.getMethod()).thenReturn("POST");
when(request.getHeader("X-Gitlab-Event")).thenReturn("Tag Push Hook");
when(request.getInputStream())
.thenReturn(new ResourceServletInputStream("ActionResolverTest_postPushTag.json"));

WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request);
WebHookAction resolvedAction = new ActionResolver().resolve(request);

assertThat(resolvedAction, instanceOf(PushBuildAction.class));
}
Expand All @@ -228,12 +228,12 @@ void postPushTag() throws Exception {
void postPushMissingEventHeader() throws Exception {
String projectName = "postPushMissingEventHeader";
jenkins.createFreeStyleProject(projectName);
when(request.getRestOfPath()).thenReturn("");
when(request.getRestOfPath()).thenReturn(projectName);
when(request.getMethod()).thenReturn("POST");
when(request.getHeader("X-Gitlab-Event")).thenReturn(null);
when(request.getInputStream()).thenReturn(new ResourceServletInputStream("ActionResolverTest_postPush.json"));

WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request);
WebHookAction resolvedAction = new ActionResolver().resolve(request);

assertThat(resolvedAction, instanceOf(NoopAction.class));
}
Expand All @@ -242,12 +242,12 @@ void postPushMissingEventHeader() throws Exception {
void postPushUnsupportedEventHeader() throws Exception {
String projectName = "postPushUnsupportedEventHeader";
jenkins.createFreeStyleProject(projectName);
when(request.getRestOfPath()).thenReturn("");
when(request.getRestOfPath()).thenReturn(projectName);
when(request.getMethod()).thenReturn("POST");
when(request.getHeader("X-Gitlab-Event")).thenReturn("__Not Supported Header__");
when(request.getInputStream()).thenReturn(new ResourceServletInputStream("ActionResolverTest_postPush.json"));

WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request);
WebHookAction resolvedAction = new ActionResolver().resolve(request);

assertThat(resolvedAction, instanceOf(NoopAction.class));
}
Expand Down
Loading