package com.isti.jira;

import hudson.model.AbstractBuild;

import java.io.IOException;
import java.util.Map;

import static java.lang.String.format;


/**
 * Encapsulate what we know about the git repository.
 */
public final class RepoDetails {

    /** The git repository URL. */
    private String URL;

    /** The git branch. */
    private String branch;

    /** The git commit. */
    private String commit;

    /**
     * @param URL The git repository URL.
     * @param branch The git branch.
     */
    public RepoDetails(final String URL, final String branch, final String commit) {
        this.URL = URL;
        this.branch = branch;
        this.commit = commit;
    }

    /**
     * @param build The current build.
     */
    public RepoDetails(final AbstractBuild build) {
        this(vars(build).get("GIT_URL"),
             vars(build).get("GIT_BRANCH"),
             vars(build).get("GIT_COMMIT"));
    }

    /**
     * @param build The current build.
     * @return A map of environment variables.
     */
    private static Map<String, String> vars(final AbstractBuild build) {
//        return build.getBuildVariables();
        try {
            return build.getEnvironment(null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @return The git repository URL.
     */
    public String getURL() {
        return URL;
    }

    /**
     * @return The git branch.
     */
    public String getBranch() {
        return branch;
    }

    /**
     * @return The git commit.
     */
    public String getCommit() {
        return commit;
    }

    @Override
    public String toString() {
        return format("%s [%s]", URL, branch);
    }

}

@@ -1702,16 +1702,19 @@
 %22%25s %5B%25s%5D
+ %25s
 %22, URL, 
@@ -1719,16 +1719,24 @@
 , branch
+, commit
 );%0A    %7D

add git commit