1   package com.atlassian.plugins.codegen.modules.stash.hook;
2   
3   import com.atlassian.plugins.codegen.PluginProjectChangeset;
4   import com.atlassian.plugins.codegen.annotations.StashPluginModuleCreator;
5   import com.atlassian.plugins.codegen.modules.AbstractPluginModuleCreator;
6   import com.google.common.collect.ImmutableList;
7   import com.google.common.collect.ImmutableMap;
8   import org.apache.commons.io.IOUtils;
9   
10  import java.io.InputStream;
11  import java.util.List;
12  import java.util.Map;
13  
14  import static com.atlassian.plugins.codegen.PluginProjectChangeset.changeset;
15  import static com.atlassian.plugins.codegen.ResourceFile.resourceFile;
16  import static com.atlassian.plugins.codegen.modules.Dependencies.MOCKITO_TEST;
17  
18  @StashPluginModuleCreator
19  public class RepositoryHookModuleCreator extends AbstractPluginModuleCreator<RepositoryHookProperties>
20  {
21  
22      public static final String MODULE_NAME = "Repository Hook";
23      private static final String TEMPLATE_PREFIX = "templates/stash/repository-hook/";
24      private static final String PRE_RECEIVE_TEMPLATE = TEMPLATE_PREFIX + "PreReceiveRepositoryHook.java.vtl";
25      private static final String ASYNC_POST_RECEIVE_TEMPLATE = TEMPLATE_PREFIX + "AsyncPostReceiveRepositoryHook.java.vtl";
26      private static final String MERGE_CHECK_TEMPLATE = TEMPLATE_PREFIX + "MergeCheckRepositoryHook.java.vtl";
27      private static final String PLUGIN_MODULE_TEMPLATE = TEMPLATE_PREFIX + "repository-hook-plugin.xml.vtl";
28      private static final String SOY_TEMPLATE = TEMPLATE_PREFIX + "repository-hook.soy.vtl";
29      public static final String TYPE_PRE = "pre";
30      public static final String TYPE_POST = "post";
31      public static final String TYPE_MERGE_CHECK = "merge";
32      public static final String TYPE_DEFAULT = TYPE_POST;
33      public static final Map<String, String> DEFAULT_CLASS_NAME_BY_TYPE = ImmutableMap.<String, String>builder()
34                                                                                       .put(TYPE_PRE, "MyPreReceiveRepositoryHook")
35                                                                                       .put(TYPE_POST, "MyPostReceiveRepositoryHook")
36                                                                                       .put(TYPE_MERGE_CHECK, "MyMergeCheckHook")
37                                                                                       .build();
38      public static final List<String> TYPES = ImmutableList.copyOf(DEFAULT_CLASS_NAME_BY_TYPE.keySet());
39  
40      @Override
41      public PluginProjectChangeset createModule(RepositoryHookProperties props) throws Exception
42      {
43          PluginProjectChangeset hookClass = createClass(props, props.getClassId(), getTemplate(props.getType()));
44          PluginProjectChangeset changeset = new PluginProjectChangeset()
45                  .with(MOCKITO_TEST)
46                  .with(createModule(props, PLUGIN_MODULE_TEMPLATE))
47                  .with(hookClass);
48          if (props.getIcon() != null)
49          {
50              InputStream in = getClass().getClassLoader().getResourceAsStream(TEMPLATE_PREFIX + props.getIcon());
51              changeset = changeset.with(changeset().with(resourceFile("", props.getIcon(), IOUtils.toByteArray(in))));
52          }
53          if (props.ifConfigured())
54          {
55              // Name of the file isn't referenced by atlassian-plugin.xml
56              changeset = changeset.with(createResource(props, "static", props.getSoyFile(), SOY_TEMPLATE));
57          }
58          return changeset;
59      }
60  
61      private String getTemplate(String type)
62      {
63          String template;
64          if (TYPE_PRE.equals(type))
65          {
66              template = PRE_RECEIVE_TEMPLATE;
67          }
68          else if (TYPE_POST.equals(type))
69          {
70              template = ASYNC_POST_RECEIVE_TEMPLATE;
71          }
72          else if (TYPE_MERGE_CHECK.equals(type))
73          {
74              template = MERGE_CHECK_TEMPLATE;
75          }
76          else
77          {
78              throw new RuntimeException("Unsupported type: " + type);
79          }
80          return template;
81      }
82  
83      @Override
84      public String getModuleName()
85      {
86          return MODULE_NAME;
87      }
88  
89  }