package com.flowdock.jenkins;

import hudson.model.AbstractBuild;
import hudson.model.Hudson;
import hudson.model.Result;
import java.io.UnsupportedEncodingException;

public class TeamInboxMessage extends FlowdockMessage {

    /*
        Default sender email addresses for displaying Gravatar icons in build
        notification based on the build status. You can also setup your own email
        addresses and configure custom Gravatar icons for them.
    */
    public static final String FLOWDOCK_BUILD_OK_EMAIL = "build+ok@flowdock.com";
    public static final String FLOWDOCK_BUILD_FAIL_EMAIL = "build+fail@flowdock.com";

    protected String source;
    protected String project;
    protected String subject;
    protected String link;
    protected String fromAddress;
    protected String fromName;

    public TeamInboxMessage() {
        this.source = "Jenkins";
        this.fromAddress = FLOWDOCK_BUILD_OK_EMAIL;
        this.fromName = "CI";
    }

    public void setSource(String source) {
        this.source = source;
    }

    public void setProject(String project) {
        this.project = project;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public void setFromAddress(String fromAddress) {
        this.fromAddress = fromAddress;
    }

    public void setFromName(String fromName) {
        this.fromName = fromName;
    }

    public String asPostData() throws UnsupportedEncodingException {
        StringBuffer postData = new StringBuffer();
        postData.append("subject=").append(urlEncode(subject));
        postData.append("&content=").append(urlEncode(content));
        postData.append("&from_address=").append(urlEncode(fromAddress));
        postData.append("&from_name=").append(urlEncode(fromName));
        postData.append("&source=").append(urlEncode(source));
        postData.append("&project=").append(urlEncode(project));
        postData.append("&link=").append(urlEncode(link));
        postData.append("&tags=").append(urlEncode(tags));
        return postData.toString();
    }

    public static TeamInboxMessage fromBuild(AbstractBuild build) {
        TeamInboxMessage msg = new TeamInboxMessage();
        msg.setProject(build.getProject().getName());
        msg.setSubject("Build: " + build.getProject().getName() + " " + build.getDisplayName() + " is " + build.getResult().toString());

        String rootUrl = Hudson.getInstance().getRootUrl();
        String buildLink = (rootUrl == null) ? null : rootUrl + build.getUrl();
        if(buildLink != null) msg.setLink(buildLink);

        if(build.getResult().isWorseThan(Result.SUCCESS))
            msg.setFromAddress(FLOWDOCK_BUILD_FAIL_EMAIL);

        StringBuffer content = new StringBuffer();
        content.append("Project: ").append(build.getProject().getName()).append("<br />");
        content.append("Build: ").append(build.getDisplayName()).append("<br />");
        content.append("Result: ").append(build.getResult().toString()).append("<br />");
        if(buildLink != null)
            content.append("URL: <a href=\"").append(buildLink).append("\">").append(buildLink).append("</a>").append("<br />");
        msg.setContent(content.toString());

        return msg;
    }
}
@@ -24,16 +24,67 @@
 nkins;%0A%0A
+import java.util.List;%0Aimport java.util.ArrayList;%0A
 import h
@@ -166,16 +166,86 @@
 Result;%0A
+import hudson.scm.ChangeLogSet;%0Aimport hudson.scm.ChangeLogSet.Entry;%0A
 import j
@@ -3377,71 +3377,745 @@
 %22);%0A
-        msg.setContent(content.toString());%0A%0A        return msg
+%0A        List%3CString%3E commits = parseCommits(build);%0A        if(commits != null) %7B%0A            content.append(%22%3Cbr /%3EChanges:%3Cbr/%3E%22);%0A            for(String commitId : commits) %7B%0A                content.append(commitId);%0A                content.append(%22%3Cbr /%3E%22);%0A            %7D%0A        %7D%0A%0A        msg.setContent(content.toString());%0A%0A        return msg;%0A    %7D%0A%0A    public static List%3CString%3E parseCommits(AbstractBuild build) %7B%0A        final ChangeLogSet%3C? extends Entry%3E cs = build.getChangeSet();%0A        if(cs == null %7C%7C cs.isEmptySet())%0A            return null;%0A%0A        List%3CString%3E revisions = new ArrayList();%0A        for (final Entry entry : cs) %7B%0A            revisions.add(entry.getCommitId());%0A        %7D%0A        return revisions
 ;%0A  

Read commit data from AbstractBuild