Commits

Anonymous committed fa43f1a

Moving to atlassian_tutorials on BB

Comments (0)

Files changed (9)

+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+    <modelVersion>4.0.0</modelVersion>
+    <groupId>com.example.ampstutorial</groupId>
+    <artifactId>fecrutwitter</artifactId>
+    <version>1.0-SNAPSHOT</version>
+
+    <organization>
+        <name>Example Company</name>
+        <url>http://www.example.com/</url>
+    </organization>
+
+    <name>fecrutwitter</name>
+    <description>This is the com.example.ampstutorial:fecrutwitter plugin for Atlassian FishEye/Crucible.</description>
+    <packaging>atlassian-plugin</packaging>
+
+    <dependencies>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>3.8.1</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>javax.servlet</groupId>
+            <artifactId>servlet-api</artifactId>
+            <version>2.4</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>com.atlassian.fisheye</groupId>
+            <artifactId>atlassian-fisheye-api</artifactId>
+            <version>${fecru.version}</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>com.atlassian.crucible</groupId>
+            <artifactId>atlassian-crucible-scmutils</artifactId>
+            <version>${fecru.version}</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>log4j</groupId>
+            <artifactId>log4j</artifactId>
+            <version>1.2.15</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>net.homeip.yusuke</groupId>
+            <artifactId>twitter4j</artifactId>
+            <version>2.0.9</version>
+        </dependency>
+        <dependency>
+            <groupId>com.atlassian.templaterenderer</groupId>
+            <artifactId>atlassian-template-renderer-api</artifactId>
+            <version>1.1.0.beta1</version>
+            <scope>provided</scope>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>com.atlassian.maven.plugins</groupId>
+                <artifactId>maven-fecru-plugin</artifactId>
+                <version>3.2</version>
+                <extensions>true</extensions>
+                <configuration>
+                    <productVersion>${fecru.version}</productVersion>
+                    <productDataVersion>${fecru.data.version}</productDataVersion>
+                </configuration>
+            </plugin>
+            <plugin>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <configuration>
+                    <source>1.5</source>
+                    <target>1.5</target>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+
+    <properties>
+        <fecru.version>2.3.3-493</fecru.version>
+        <fecru.data.version>2.0.4.1-SNAPSHOT</fecru.data.version>
+    </properties>
+
+</project>

src/main/java/com/example/ampstutorial/CommitListener.java

+package com.example.ampstutorial;
+
+import com.atlassian.event.Event;
+import com.atlassian.event.EventListener;
+import com.atlassian.fisheye.event.CommitEvent;
+import com.atlassian.fisheye.spi.data.ChangesetDataFE;
+import com.atlassian.fisheye.spi.services.RevisionDataService;
+import twitter4j.Twitter;
+import twitter4j.TwitterException;
+
+public class CommitListener implements EventListener {
+    private final RevisionDataService revisionDataService;
+    private final TwitterLoginRecordStore twitterLoginRecordStore;
+
+    public CommitListener(RevisionDataService revisionDataService, TwitterLoginRecordStore twitterLoginRecordStore) {
+        this.revisionDataService = revisionDataService;
+        this.twitterLoginRecordStore = twitterLoginRecordStore;
+    }
+
+    public void handleEvent(Event event) {
+        if (event instanceof CommitEvent) {
+            CommitEvent ce = (CommitEvent) event;
+            ChangesetDataFE csData = revisionDataService.getChangeset(ce.getRepositoryName(), ce.getChangeSetId());
+            if (csData != null) {
+                TwitterLoginRecord twitterLogin = twitterLoginRecordStore.getByCommitterAndRepo(csData.getAuthor(), ce.getRepositoryName());
+                if (twitterLogin != null) {
+                    System.out.println("Found twitter login");
+                    Twitter twitter = new Twitter(twitterLogin.getUserName(),twitterLogin.getPassword());
+                    try {
+                        twitter.updateStatus(csData.getComment());
+                    } catch (TwitterException e) {
+                        throw new RuntimeException(e);
+                    }
+                }
+            }
+        }
+    }
+
+    public Class[] getHandledEventClasses() {
+        return new Class[] {CommitEvent.class};
+    }
+}

src/main/java/com/example/ampstutorial/ExampleServlet.java

+package com.example.ampstutorial;
+
+import com.atlassian.crucible.spi.PluginId;
+import com.atlassian.crucible.spi.PluginIdAware;
+import com.atlassian.crucible.spi.data.UserData;
+import com.atlassian.crucible.spi.services.ImpersonationService;
+import com.atlassian.crucible.spi.services.ServerException;
+import com.atlassian.crucible.spi.services.UserService;
+import com.atlassian.fisheye.plugin.web.helpers.VelocityHelper;
+import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory;
+import com.atlassian.templaterenderer.TemplateRenderer;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+public class ExampleServlet extends HttpServlet implements PluginIdAware {
+    private final TemplateRenderer templateRenderer;
+    private final ImpersonationService impersonationService;
+    private final TwitterLoginRecordStore twitterLoginRecordStore;
+    private PluginId pluginId;
+
+    @Autowired
+    public ExampleServlet(TemplateRenderer templateRenderer, ImpersonationService impersonationService,TwitterLoginRecordStore twitterLoginRecordStore) {
+        this.templateRenderer = templateRenderer;
+        this.impersonationService = impersonationService;
+        this.twitterLoginRecordStore = twitterLoginRecordStore;
+    }
+
+    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
+        request.setAttribute("decorator", "fisheye.userprofile.tab");
+        response.setContentType("text/html");
+        Map<String,Object> params = new HashMap<String,Object>();
+        TwitterLoginRecord loginRecord = getLoginRecord();
+        if (loginRecord == null) {
+            loginRecord = new TwitterLoginRecord("","");
+        }
+        params.put("loginRecord", loginRecord);
+        templateRenderer.render("config.vm", params, response.getWriter());
+    }
+
+    @Override
+    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+        String newUserName = request.getParameter("userName");
+        String newPassword = request.getParameter("password");
+        if (newUserName != null && newPassword != null) {
+            putLoginRecord(newUserName, newPassword);
+        }
+        response.sendRedirect("./example-servlet");
+    }
+
+    private void putLoginRecord(String newUserName, String newPassword) {
+        twitterLoginRecordStore.storeByUsername(getCurrentUser().getUserName(), new TwitterLoginRecord(newUserName, newPassword));
+    }
+
+    public void setPluginId(PluginId pluginId) {
+        this.pluginId = pluginId;
+    }
+
+    private UserData getCurrentUser() {
+        try {
+            return impersonationService.getCurrentUser(pluginId);
+        } catch (ServerException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    private TwitterLoginRecord getLoginRecord() {
+        return twitterLoginRecordStore.getByUsername(getCurrentUser().getUserName());
+    }
+}

src/main/java/com/example/ampstutorial/TwitterLoginRecord.java

+package com.example.ampstutorial;
+
+public class TwitterLoginRecord {
+    private String userName;
+    private String password;
+
+    public TwitterLoginRecord(String userName, String password) {
+        this.userName = userName;
+        this.password = password;
+    }
+
+    public String getUserName() {
+        return userName;
+    }
+
+    public String getPassword() {
+        return password;
+    }
+}

src/main/java/com/example/ampstutorial/TwitterLoginRecordStore.java

+package com.example.ampstutorial;
+
+public interface TwitterLoginRecordStore {
+    void storeByUsername(String userName, TwitterLoginRecord record);
+    TwitterLoginRecord getByUsername(String userName);
+    TwitterLoginRecord getByCommitterAndRepo(String committer, String repoName);
+}

src/main/java/com/example/ampstutorial/TwitterLoginRecordStoreImpl.java

+package com.example.ampstutorial;
+
+import com.atlassian.crucible.spi.data.UserProfileData;
+import com.atlassian.crucible.spi.services.ServerException;
+import com.atlassian.crucible.spi.services.UserService;
+import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class TwitterLoginRecordStoreImpl implements TwitterLoginRecordStore {
+    private final Map<CommitterAndRepoKey,TwitterLoginRecord> byCommitterAndRepository = new HashMap<CommitterAndRepoKey,TwitterLoginRecord>();
+    private final Map<String,TwitterLoginRecord> byUser = new HashMap<String,TwitterLoginRecord>();
+    private UserService userService;
+    private PluginSettingsFactory pluginSettingsFactory;
+
+    public TwitterLoginRecordStoreImpl() {
+    }
+
+    public void storeByUsername(String userName, TwitterLoginRecord record) {
+        TwitterLoginRecord oldValue = byUser.put(userName, record);
+        if (oldValue != null) {
+            for (Map.Entry<CommitterAndRepoKey,TwitterLoginRecord> e : byCommitterAndRepository.entrySet()) {
+                if (e.getValue() == oldValue) {
+                    byCommitterAndRepository.remove(e.getKey());
+                }
+            }
+        }
+        try {
+            UserProfileData userProfile = userService.getUserProfile(userName);
+            for (Map.Entry<String, List<String>> repoCommitters : userProfile.getMappedCommitters().entrySet()) {
+                for (String committer : repoCommitters.getValue()) {
+                    byCommitterAndRepository.put(new CommitterAndRepoKey(committer, repoCommitters.getKey()), record);
+                }
+            }
+        } catch (ServerException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public TwitterLoginRecord getByUsername(String userName) {
+        return byUser.get(userName);
+    }
+
+    public TwitterLoginRecord getByCommitterAndRepo(String committer, String repoName) {
+        return byCommitterAndRepository.get(new CommitterAndRepoKey(committer, repoName));
+    }
+
+    @Resource
+    public void setPluginSettingsFactory(PluginSettingsFactory pluginSettingsFactory) {
+        System.out.println("Injected " + pluginSettingsFactory);
+        this.pluginSettingsFactory = pluginSettingsFactory;
+    }
+    @Resource
+    public void setUserService(UserService userService) {
+        System.out.println("Injected " + userService);
+
+        this.userService = userService;
+    }
+
+    private static class CommitterAndRepoKey {
+        private final String committer;
+        private final String repoName;
+
+        public CommitterAndRepoKey(String committer, String repoName) {
+            this.committer = committer;
+            this.repoName = repoName;
+        }
+
+        @Override
+        public boolean equals(Object o) {
+            if (this == o) return true;
+            if (o == null || getClass() != o.getClass()) return false;
+
+            CommitterAndRepoKey that = (CommitterAndRepoKey) o;
+
+            if (committer != null ? !committer.equals(that.committer) : that.committer != null) return false;
+            if (repoName != null ? !repoName.equals(that.repoName) : that.repoName != null) return false;
+
+            return true;
+        }
+
+        @Override
+        public int hashCode() {
+            int result = committer != null ? committer.hashCode() : 0;
+            result = 31 * result + (repoName != null ? repoName.hashCode() : 0);
+            return result;
+        }
+    }
+}

src/main/resources/atlassian-plugin.xml

+<atlassian-plugin key="${project.groupId}.${project.artifactId}" name="${project.artifactId}" plugins-version="2">
+    <plugin-info>
+        <description>${project.description}</description>
+        <version>${project.version}</version>
+        <vendor name="${project.organization.name}" url="${project.organization.url}" />
+    </plugin-info>
+
+    <servlet name="${project.artifactId}" class="com.example.ampstutorial.ExampleServlet" key="example-servlet">
+        <description>A Sample Servlet Module</description>
+        <url-pattern>/example-servlet</url-pattern>
+    </servlet>
+
+    <component-import key="templateRenderer" interface="com.atlassian.templaterenderer.TemplateRenderer" />
+
+    <web-item key="config-link" section="system.userprofile.tab">
+        <link>/plugins/servlet/example-servlet</link>
+        <label key="Twitter Configuration"/>
+    </web-item>
+
+    <component key="twitterLoginRecordStore" class="com.example.ampstutorial.TwitterLoginRecordStoreImpl">
+        <interface>com.example.ampstutorial.TwitterLoginRecordStore</interface>
+    </component>
+
+    <listener key="commit-listener" class="com.example.ampstutorial.CommitListener"/>
+</atlassian-plugin>

src/main/resources/config.vm

+#* @vtlvariable name="loginRecord" type="com.example.ampstutorial.TwitterLoginRecord" *#
+<html>
+<head>
+    <meta name='profile.tab.key' content='com.example.ampstutorial.fecrutwitter:config-link'/>
+</head>
+<body>
+<div>
+    <form action="./example-servlet" method="post">
+        <table class="dialog-prefs" cellspacing="0">
+            <thead>
+            <tr>
+                <th colspan="2"><h3>Twitter Settings</h3></th>
+            </tr>
+            </thead>
+            <tbody>
+            <tr>
+                <td class="tdLabel"><label for="userName" class="label">User Name:</label></td>
+                <td><input type="text" name="userName" value="${loginRecord.userName}" id="userName"/></td>
+            </tr>
+            <tr>
+                <td class="tdLabel"><label for="password" class="label">Password:</label></td>
+                <td><input type="password" name="password" value="${loginRecord.password}" id="password"/></td>
+            </tr>
+            <tr>
+                <td></td>
+                <td class="action"><input type="submit" value="Save"/></td>
+            </tr>
+            </tbody>
+        </table>
+    </form>
+</div>
+</body>
+</html>

src/test/resources/TEST_RESOURCES_README.txt

+Create any of the test resources you might need in this directory.
+
+Please remove this file before releasing your plugin.
+