Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Merge pull request #1312 from zanata/manual-webhook
Browse files Browse the repository at this point in the history
ZNTA-1290 - manually triggered webhook
  • Loading branch information
Patrick Huang committed Oct 18, 2016
2 parents 72739f7 + b25e3cb commit f751e2f
Show file tree
Hide file tree
Showing 18 changed files with 279 additions and 38 deletions.
22 changes: 14 additions & 8 deletions zanata-model/src/main/java/org/zanata/model/WebHook.java
Expand Up @@ -23,7 +23,6 @@

import java.io.Serializable;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
Expand All @@ -41,15 +40,14 @@
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.Size;

import org.zanata.model.type.WebhookType;
import org.zanata.model.validator.Url;
import com.google.common.collect.Sets;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import org.zanata.model.type.WebhookType;
import org.zanata.model.validator.Url;

/**
* @author Alex Eng <a href="mailto:aeng@redhat.com">aeng@redhat.com</a>
*/
Expand Down Expand Up @@ -77,10 +75,16 @@ public class WebHook implements Serializable {
@Column(nullable = true)
private String secret;

public WebHook(HProject project, String url, Set<WebhookType> types,
String secret) {
@Size(max = 20)
private String name;


public WebHook(HProject project, String url, String name,
Set<WebhookType> types,
String secret) {
this.project = project;
this.url = url;
this.name = name;
this.types = types;
this.secret = secret;
}
Expand Down Expand Up @@ -109,14 +113,16 @@ public Set<WebhookType> getTypes() {

/**
* This will replace all properties with given ones.
*
* @param url - new url
* @param name - new name
* @param newTypes - new types
* @param secret - new secret key
*/
@Transient
public void update(String url, Set<WebhookType> newTypes, String secret) {
public void update(String url, String name, Set<WebhookType> newTypes,
String secret) {
this.url = url;
this.name = name;
this.secret = secret;

/**
Expand Down
Expand Up @@ -14,7 +14,9 @@ public enum WebhookType implements Serializable {
DocumentStatsEvent("Translation update"),
VersionChangedEvent("Project version"),
ProjectMaintainerChangedEvent("Project maintainer update"),
SourceDocumentChangedEvent("Document");
SourceDocumentChangedEvent("Document"),
ManuallyTriggeredEvent("Manual event")
;

@Getter
private String displayName;
Expand Down
14 changes: 7 additions & 7 deletions zanata-war/src/main/java/org/zanata/action/ProjectHome.java
Expand Up @@ -29,7 +29,6 @@
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.ResourceBundle;
import java.util.Set;

Expand Down Expand Up @@ -760,7 +759,8 @@ public boolean validateSlug(String slug, String componentId) {
String validationMessages =
ResourceBundle.getBundle("ValidationMessages").getString(
"javax.validation.constraints.Slug.message");
facesMessages.addToControl(componentId, validationMessages);
facesMessages.addToControl(componentId,
validationMessages);
return false;
}
return true;
Expand Down Expand Up @@ -1087,7 +1087,7 @@ public List<ValidationAction> getValidationList() {
}

@Transactional
public void addWebHook(String url, String secret, String strTypes) {
public void addWebHook(String url, String secret, String strTypes, String name) {
identity.checkPermission(getInstance(), "update");
Set<WebhookType> types = getTypesFromString(strTypes);
if(types.isEmpty()) {
Expand All @@ -1103,7 +1103,7 @@ public void addWebHook(String url, String secret, String strTypes) {
return;
}
boolean isAdded = projectServiceImpl.addWebhook(getInstance(), url,
secret, types);
secret, name, types);
if (isAdded) {
facesMessages.addGlobal(
msgs.format("jsf.project.AddNewWebhook", url));
Expand All @@ -1114,8 +1114,8 @@ public void addWebHook(String url, String secret, String strTypes) {
public void removeWebHook(String id) {
identity.checkPermission(getInstance(), "update");
WebHook webHook = webHookDAO.findById(new Long(id));
String url = webHook.getUrl();
if (webHook != null) {
String url = webHook.getUrl();
getInstance().getWebHooks().remove(webHook);
webHookDAO.makeTransient(webHook);
facesMessages.addGlobal(
Expand All @@ -1125,7 +1125,7 @@ public void removeWebHook(String id) {

@Transactional
public void updateWebhook(String id, String url, String secret,
String strTypes) {
String strTypes, String name) {
identity.checkPermission(getInstance(), "update");
Set<WebhookType> types = getTypesFromString(strTypes);
if(types.isEmpty()) {
Expand All @@ -1143,7 +1143,7 @@ public void updateWebhook(String id, String url, String secret,
return;
}
boolean updated = projectServiceImpl.updateWebhook(getInstance(),
webhookId, url, secret, types);
webhookId, url, secret, name, types);
if (updated) {
facesMessages.addGlobal(
msgs.format("jsf.project.UpdateWebhook", url));
Expand Down
39 changes: 39 additions & 0 deletions zanata-war/src/main/java/org/zanata/action/VersionHomeAction.java
Expand Up @@ -34,6 +34,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import javax.enterprise.inject.Model;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ViewScoped;
Expand All @@ -47,6 +48,7 @@
import org.apache.deltaspike.jpa.api.transaction.Transactional;
import org.richfaces.event.FileUploadEvent;
import org.richfaces.model.UploadedFile;
import org.zanata.dao.WebHookDAO;
import org.zanata.events.DocumentLocaleKey;
import org.zanata.exception.AuthorizationException;
import org.zanata.async.handle.CopyVersionTaskHandle;
Expand All @@ -68,7 +70,9 @@
import org.zanata.model.HLocale;
import org.zanata.model.HProjectIteration;
import org.zanata.model.HRawDocument;
import org.zanata.model.WebHook;
import org.zanata.model.type.TranslationSourceType;
import org.zanata.model.type.WebhookType;
import org.zanata.rest.StringSet;
import org.zanata.rest.dto.extensions.ExtensionType;
import org.zanata.rest.dto.extensions.comment.SimpleComment;
Expand All @@ -83,6 +87,7 @@
import org.zanata.service.TranslationService;
import org.zanata.service.TranslationStateCache;
import org.zanata.service.VersionStateCache;
import org.zanata.service.impl.WebhookServiceImpl;
import org.zanata.ui.AbstractListFilter;
import org.zanata.ui.AbstractSortAction;
import org.zanata.ui.CopyAction;
Expand All @@ -98,6 +103,7 @@
import org.zanata.webtrans.shared.model.DocumentStatus;
import org.zanata.webtrans.shared.util.TokenUtil;

import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
Expand Down Expand Up @@ -189,6 +195,13 @@ public class VersionHomeAction extends AbstractSortAction implements
@Inject
private UrlUtil urlUtil;

@Inject
private WebhookServiceImpl webhookService;

@Inject
private WebHookDAO webHookDAO;


private List<HLocale> supportedLocale;

private List<HDocument> documents;
Expand Down Expand Up @@ -309,6 +322,7 @@ protected boolean include(HLocale elem, String filter) {
elem.retrieveDisplayName(), filter);
}
};
private List<WebHook> manualWebhooks;

public void setVersionSlug(String versionSlug) {
this.versionSlug = versionSlug;
Expand All @@ -326,6 +340,30 @@ public void cancelCopyVersion() {
msgs.format("jsf.copyVersion.Cancelled", versionSlug));
}

private List<WebHook> getManualWebhooks() {
if (manualWebhooks == null) {
manualWebhooks = webHookDAO.getWebHooksForType(projectSlug,
WebhookType.ManuallyTriggeredEvent);
}
return manualWebhooks;
}

public boolean canTriggerManualWebhook() {
boolean hasLocalePermission =
isUserAllowedToTranslateOrReview(selectedLocale);
return hasLocalePermission && !getManualWebhooks().isEmpty();
}

public void triggerManualWebhookEvent() {
List<WebHook> manualWebhooks = getManualWebhooks();
if (selectedLocale != null && !manualWebhooks.isEmpty()) {
webhookService.processManualEvent(projectSlug, versionSlug,
selectedLocale.getLocaleId(), manualWebhooks);
conversationScopeMessages.setMessage(FacesMessage.SEVERITY_INFO,
msgs.format("jsf.iteration.manualWebhook.triggered"));
}
}

// TODO Serializable only because it's a dependent bean
@NoArgsConstructor
public static class CopyVersionHandler extends CopyAction implements Serializable {
Expand Down Expand Up @@ -435,6 +473,7 @@ public void resetPageData() {
documents = null;
version = null;
supportedLocale = null;
manualWebhooks = null;
loadStatistics();
}

Expand Down
41 changes: 41 additions & 0 deletions zanata-war/src/main/java/org/zanata/dao/WebHookDAO.java
@@ -1,10 +1,34 @@
/*
* Copyright 2016, Red Hat, Inc. and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.zanata.dao;

import java.util.List;

import org.hibernate.Session;

import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

import org.zanata.model.WebHook;
import org.zanata.model.type.WebhookType;

@Named("webHookDAO")
@RequestScoped
Expand All @@ -17,4 +41,21 @@ public WebHookDAO() {
public WebHookDAO(Session session) {
super(WebHook.class, session);
}

/**
* Get a list of webhook from a project which has the given type set up.
* @param projectSlug project slug
* @param type webhoot type
*/
public List<WebHook> getWebHooksForType(String projectSlug,
WebhookType type) {
// This generates a warning in log but as per https://hibernate.atlassian.net/browse/HHH-10621
// the warning message is a hibernate issue
List list = getSession().createQuery(
"from WebHook w where w.project.slug = :projectSlug and :webhookType in elements(w.types) ")
.setParameter("projectSlug", projectSlug)
.setParameter("webhookType", type)
.setCacheable(true).setComment("getWebHooksForType").list();
return list;
}
}
Expand Up @@ -28,7 +28,6 @@
import org.zanata.service.impl.ProjectServiceImpl;

import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
Expand All @@ -47,11 +46,11 @@ List<ProjectServiceImpl.UpdatedRole> updateProjectPermissions(HProject project,

@Transactional
boolean updateWebhook(HProject project, Long webhookId, String url,
String secret, Set<WebhookType> types);
String secret, String name, Set<WebhookType> types);

@Transactional
boolean addWebhook(HProject project, String url, String secret,
Set<WebhookType> types);
String name, Set<WebhookType> types);

/**
* Check if project contains duplicate webhook with matching url
Expand Down
Expand Up @@ -21,6 +21,7 @@

package org.zanata.service.impl;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Getter;
Expand Down Expand Up @@ -49,7 +50,6 @@
import java.util.Optional;
import java.util.Set;

import static javax.faces.application.FacesMessage.SEVERITY_ERROR;
import static org.zanata.model.LocaleRole.Coordinator;
import static org.zanata.model.LocaleRole.Reviewer;
import static org.zanata.model.LocaleRole.Translator;
Expand Down Expand Up @@ -109,7 +109,7 @@ public List<UpdatedRole> updateProjectPermissions(HProject project,
@Transactional
@Override
public boolean updateWebhook(HProject project, Long webhookId, String url,
String secret, Set<WebhookType> types) {
String secret, String name, Set<WebhookType> types) {
if (types.isEmpty()) {
return false;
}
Expand All @@ -121,15 +121,15 @@ public boolean updateWebhook(HProject project, Long webhookId, String url,
return false;
}
secret = StringUtils.isBlank(secret) ? null : secret;
webHook.update(url, types, secret);
webHook.update(url, Strings.emptyToNull(name), types, secret);
webHookDAO.makePersistent(webHook);
return true;
}

@Transactional
@Override
public boolean addWebhook(HProject project, String url, String secret,
Set<WebhookType> types) {
String name, Set<WebhookType> types) {
if (types.isEmpty()) {
return false;
}
Expand All @@ -138,7 +138,7 @@ public boolean addWebhook(HProject project, String url, String secret,
}
secret = StringUtils.isBlank(secret) ? null : secret;
WebHook webHook =
new WebHook(project, url, types, secret);
new WebHook(project, url, Strings.emptyToNull(name), types, secret);
project.getWebHooks().add(webHook);
projectDAO.makePersistent(project);
return true;
Expand Down

0 comments on commit f751e2f

Please sign in to comment.