Skip to content
Snippets Groups Projects
Commit c9f6f691 authored by Zachary Sabourin's avatar Zachary Sabourin
Browse files

feat: Create starting point for hook processing

Resolves #78
parent eb077790
No related branches found
No related tags found
1 merge request!103feat: Create initial endpoint and add all supporting elements
/*********************************************************************
* Copyright (c) 2022 Eclipse Foundation.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* Author: Zachary Sabourin <zachary.sabourin@eclipse-foundation.org>
*
* SPDX-License-Identifier: EPL-2.0
**********************************************************************/
package org.eclipsefoundation.git.eca.model;
import java.time.ZonedDateTime;
import java.util.List;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import com.google.auto.value.AutoValue;
@AutoValue
@JsonDeserialize(builder = AutoValue_SystemHook.Builder.class)
public abstract class SystemHook {
public abstract ZonedDateTime getCreatedAt();
public abstract ZonedDateTime getUpdatedAt();
public abstract String getEventName();
public abstract String getName();
public abstract String getOwnerEmail();
public abstract String getOwnerName();
public abstract List<Owner> getOwners();
public abstract String getPath();
public abstract String getPathWithNamespace();
public abstract Integer getProjectId();
public abstract String getProjectVisibility();
public static Builder builder() {
return new AutoValue_SystemHook.Builder();
}
@AutoValue.Builder
@JsonPOJOBuilder(withPrefix = "set")
public abstract static class Builder {
public abstract Builder setCreatedAt(ZonedDateTime created);
public abstract Builder setUpdatedAt(ZonedDateTime updated);
public abstract Builder setEventName(String eventName);
public abstract Builder setName(String name);
public abstract Builder setOwnerEmail(String ownerEmail);
public abstract Builder setOwnerName(String ownerName);
public abstract Builder setOwners(List<Owner> owners);
public abstract Builder setPath(String path);
public abstract Builder setPathWithNamespace(String path);
public abstract Builder setProjectId(Integer projectId);
public abstract Builder setProjectVisibility(String visibility);
public abstract SystemHook build();
}
@AutoValue
@JsonDeserialize(builder = AutoValue_SystemHook_Owner.Builder.class)
public abstract static class Owner {
public abstract String getName();
public abstract String getEmail();
public static Builder builder() {
return new AutoValue_SystemHook_Owner.Builder();
}
@AutoValue.Builder
@JsonPOJOBuilder(withPrefix = "set")
public abstract static class Builder {
public abstract Builder setName(String name);
public abstract Builder setEmail(String email);
public abstract Owner build();
}
}
}
/*********************************************************************
* Copyright (c) 2022 Eclipse Foundation.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* Author: Zachary Sabourin <zachary.sabourin@eclipse-foundation.org>
*
* SPDX-License-Identifier: EPL-2.0
**********************************************************************/
package org.eclipsefoundation.git.eca.resource;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.inject.Inject;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import org.eclipsefoundation.git.eca.model.SystemHook;
import org.eclipsefoundation.git.eca.service.SystemHookService;
import org.jboss.resteasy.annotations.jaxrs.HeaderParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
@Path("/webhooks")
public class WebhooksResource {
private static final Logger LOGGER = LoggerFactory.getLogger(WebhooksResource.class);
private static final ExecutorService EXECUTOR = Executors.newSingleThreadExecutor();
@Inject
ObjectMapper om;
@Inject
SystemHookService hookService;
@POST
@Path("/gitlab/system")
public Response processGitlabHook(@HeaderParam("X-Gitlab-Event") String eventHeader, String body) {
LOGGER.info("EVENT RECEIVED: {}", eventHeader);
if (!isValidHook(eventHeader, body, "project_create")) {
return Response.status(400).build();
}
EXECUTOR.submit(new Runnable() {
@Override
public void run() {
hookService.processProjectCreateHook(convertRequestBody(body));
}
});
return Response.ok().build();
}
private SystemHook convertRequestBody(String body) {
return null;
}
/**
* Validates that the hook header and body contain the required hook type.
*
* @param eventHeader
* @param body
* @return
*/
private boolean isValidHook(String eventHeader, String body, String hookType) {
try {
JsonNode eventName = om.readTree(body).get("event_name");
return eventHeader.equalsIgnoreCase(hookType)
&& eventName.textValue().equalsIgnoreCase(hookType);
} catch (JsonProcessingException e) {
throw new RuntimeException("Error processing request body", e);
}
}
}
/*********************************************************************
* Copyright (c) 2022 Eclipse Foundation.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* Author: Zachary Sabourin <zachary.sabourin@eclipse-foundation.org>
*
* SPDX-License-Identifier: EPL-2.0
**********************************************************************/
package org.eclipsefoundation.git.eca.service;
import org.eclipsefoundation.git.eca.model.SystemHook;
public interface SystemHookService {
void processProjectCreateHook(SystemHook hook);
}
/*********************************************************************
* Copyright (c) 2022 Eclipse Foundation.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* Author: Zachary Sabourin <zachary.sabourin@eclipse-foundation.org>
*
* SPDX-License-Identifier: EPL-2.0
**********************************************************************/
package org.eclipsefoundation.git.eca.service.impl;
import org.eclipsefoundation.git.eca.model.SystemHook;
import org.eclipsefoundation.git.eca.service.SystemHookService;
public class DefaultSystemHookService implements SystemHookService {
@Override
public void processProjectCreateHook(SystemHook hook) {
// TODO Auto-generated method stub
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment