Commits

Anonymous committed a18713f
  • Participants
  • Parent commits 4d05d91

Comments (0)

Files changed (5)

 This is simple example of how to create a plugin for Stash that will trigger a URL webhook in Jenkins.
 
-When running Stash add the following to your command line arguments:
-
-    -Dwebhook.url=http://localhost:8080/jenkins/git/notifyCommit?url=$URL&branches=$BRANCHES
+When running Stash you can enable the webhook for a specific repository via the
+[Repository Hook](https://confluence.atlassian.com/display/STASH/Using+repository+hooks) configuration.
 
 Where $URL will be replaced with the HTTP url of the repository, and $BRANCHES contains a comma separated list of branches.
 
 
     mvn install
 
-And upload `target/stash-webhook-plugin-1.0.0-SNAPSHOT.jar` to Stash.
+And upload `target/stash-webhook-plugin-2.0.0-SNAPSHOT.jar` to Stash.
 
     <groupId>com.atlassian.stash</groupId>
     <artifactId>stash-webhook-plugin</artifactId>
-    <version>1.0.0-SNAPSHOT</version>
+    <version>2.0.0-SNAPSHOT</version>
     <packaging>atlassian-plugin</packaging>
 
     <scm>
             <scope>provided</scope>
         </dependency>
         <dependency>
+            <groupId>com.atlassian.stash</groupId>
+            <artifactId>stash-spi</artifactId>
+            <version>${stash.version}</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
             <groupId>com.atlassian.event</groupId>
             <artifactId>atlassian-event</artifactId>
             <version>2.3.0</version>
     </build>
 
     <properties>
-        <stash.version>2.0.0</stash.version>
+        <stash.version>2.2.0</stash.version>
         <stash.data.version>1.0.0</stash.data.version>
         <amps.version>3.11</amps.version>
     </properties>

File src/main/java/com/atlassian/stash/plugin/webook/WebHook.java

 package com.atlassian.stash.plugin.webook;
 
-import com.atlassian.event.api.EventListener;
-import com.atlassian.stash.event.RepositoryPushEvent;
+import com.atlassian.stash.hook.repository.AsyncPostReceiveRepositoryHook;
+import com.atlassian.stash.hook.repository.RepositoryHookContext;
 import com.atlassian.stash.nav.NavBuilder;
 import com.atlassian.stash.repository.RefChange;
 import com.atlassian.stash.repository.RefChangeType;
+import com.atlassian.stash.repository.Repository;
+import com.atlassian.stash.setting.RepositorySettingsValidator;
+import com.atlassian.stash.setting.Settings;
+import com.atlassian.stash.setting.SettingsValidationErrors;
 import com.google.common.base.Function;
 import com.google.common.base.Joiner;
 import com.google.common.base.Predicate;
 import java.net.URL;
 import java.net.URLConnection;
 import java.net.URLEncoder;
+import java.util.Collection;
 
-public class WebHook {
+public class WebHook implements AsyncPostReceiveRepositoryHook, RepositorySettingsValidator {
 
-	private static final Logger log = LoggerFactory.getLogger(WebHook.class);
+    private static final Logger log = LoggerFactory.getLogger(WebHook.class);
 	private static final String REFS_HEADS = "refs/heads/";
+    private static final String CONFIG_URL = "url";
 
-	private final NavBuilder navBuilder;
-	private String URL = System.getProperty("webhook.url", "http://localhost:8080/jenkins/git/notifyCommit?url=$URL&branches=$BRANCHES");
+    private final NavBuilder navBuilder;
 
 	public WebHook(NavBuilder navBuilder) {
 		this.navBuilder = navBuilder;
 	}
 
-	@EventListener
-	public void onPushEvent(RepositoryPushEvent event) {
-		String url = getUrl(event);
+    @Override
+    public void postReceive(RepositoryHookContext context, Collection<RefChange> refChanges) {
+		String url = getUrl(context, getUpdateBranches(refChanges));
 		try {
 			URLConnection connection = new URL(url).openConnection();
 			connection.getInputStream().close();
 		}
 	}
 
-	private String getUrl(RepositoryPushEvent event) {
-		String cloneUrl = navBuilder.repo(event.getRepository()).clone("git").buildAbsolute();
-		String branches = urlEncode(Joiner.on(",").join(getUpdateBranches(event)));
-		return URL.replace("$URL", urlEncode(cloneUrl)).replace("$BRANCHES", branches);
+	private String getUrl(RepositoryHookContext context, Iterable<String> b) {
+		String cloneUrl = navBuilder.repo(context.getRepository()).clone(context.getRepository().getScmId()).buildAbsolute();
+		String branches = urlEncode(Joiner.on(",").join(b));
+		return context.getSettings().getString(CONFIG_URL, "").replace("$URL", urlEncode(cloneUrl)).replace("$BRANCHES", branches);
 	}
 
 	private static String urlEncode(String string) {
 		}
 	}
 
-	private Iterable<String> getUpdateBranches(RepositoryPushEvent event) {
-		return Iterables.transform(Iterables.filter(event.getRefChanges(), new Predicate<RefChange>() {
+	private Iterable<String> getUpdateBranches(Collection<RefChange> refChanges) {
+		return Iterables.transform(Iterables.filter(refChanges, new Predicate<RefChange>() {
 			@Override
 			public boolean apply(RefChange input) {
 				// We only care about non-deleted branches
 			}
 		});
 	}
+
+    @Override
+    public void validate(Settings settings, SettingsValidationErrors errors, Repository repository) {
+        String url = settings.getString(CONFIG_URL);
+        if (url == null || url.trim().isEmpty()) {
+            errors.addFieldError(CONFIG_URL, "URL field is blank, please supply one");
+        }
+    }
 }

File src/main/resources/atlassian-plugin.xml

 
     <component-import key="nav-builder" interface="com.atlassian.stash.nav.NavBuilder"/>
 
-    <component key="web-hook" class="com.atlassian.stash.plugin.webook.WebHook"/>
+    <repository-hook key="web-hook" name="Simple Webhook" class="com.atlassian.stash.plugin.webook.WebHook">
+        <description>Webhook for notifying a configured endpoint of changes to this repository.</description>
+        <config-form>
+            <view>stash.config.webhook.formContents</view>
+            <directory location="/static/"/>
+        </config-form>
+    </repository-hook>
 
 </atlassian-plugin>

File src/main/resources/static/config.soy

+{namespace stash.config.webhook}
+
+/**
+ * @param config
+ * @param? errors
+ */
+{template .formContents}
+    {call aui.form.textField}
+        {param id: 'url' /}
+        {param value: $config['url'] /}
+        {param labelContent}
+            {stash_i18n('stash.web.test.hook.config.label', 'URL')}
+        {/param}
+        {param descriptionText: stash_i18n('stash.web.test.hook.config.description', 'A URL to be notified') /}
+        {param extraClasses: 'long' /}
+        {param errorTexts: $errors ? $errors['url'] : null /}
+    {/call}
+{/template}