de.sjka.jenkins.view.branches.BranchOverviewAction.java Source code

Java tutorial

Introduction

Here is the source code for de.sjka.jenkins.view.branches.BranchOverviewAction.java

Source

/*******************************************************************************
 * Copyright (c) 2014 itemis AG (http://www.itemis.eu) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *      Simon Kaufmann - initial API and implementation
 *******************************************************************************/
package de.sjka.jenkins.view.branches;

import hudson.model.Action;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Actionable;
import hudson.plugins.git.GitSCM;
import hudson.plugins.git.util.Build;
import hudson.plugins.git.util.BuildData;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ListBranchCommand.ListMode;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Ref;

public class BranchOverviewAction implements Action {

    private final AbstractProject<?, ?> target;

    public BranchOverviewAction(AbstractProject<?, ?> target) {
        this.target = target;
    }

    public String getIconFileName() {
        return "/plugin/branch-overview/images/Git-Icon-1788C.png";
    }

    public String getDisplayName() {
        return "Branch Overview";
    }

    public String getUrlName() {
        return "branchOverview";
    }

    @SuppressWarnings("rawtypes")
    public List<BranchState> getBranches() {
        // read remote branches from the repository
        Map<String, String> branches = new HashMap<String, String>();
        Map<String, String> messages = new HashMap<String, String>();
        try {
            if (target.getScm() instanceof GitSCM) {
                GitSCM gitSCM = (GitSCM) target.getScm();
                String location = target.getLastBuild().getWorkspace().getRemote() + gitSCM.getRelativeTargetDir();
                Git git = Git.open(new File(location));
                for (Ref ref : git.branchList().setListMode(ListMode.REMOTE).call()) {
                    String name = ref.getName();
                    if (name.endsWith("/HEAD")) {
                        continue;
                    }
                    if (name.startsWith("refs/remotes/")) {
                        name = name.replace("refs/remotes/", "");
                    }
                    branches.put(name, ref.getObjectId().getName());
                    System.out.println(name);
                    messages.put(name, git.log().add(ref.getObjectId()).call().iterator().next().getShortMessage());
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (GitAPIException e) {
            throw new RuntimeException(e);
        }

        // read commits from the build history
        Map<String, AbstractBuild> builds = new HashMap<String, AbstractBuild>();
        for (AbstractBuild build : target.getBuilds()) {
            for (Entry<String, Build> entry : ((Actionable) build).getAction(BuildData.class)
                    .getBuildsByBranchName().entrySet()) {
                String sha1 = entry.getValue().getRevision().getSha1String();
                int number = entry.getValue().getBuildNumber();
                if (number == build.getNumber() && builds.get(sha1) == null) {
                    builds.put(sha1, build);
                    break;
                }
            }
        }

        // create model
        Map<String, BranchState> ret = new HashMap<String, BranchState>();
        for (Entry<String, String> branch : branches.entrySet()) {
            AbstractBuild build = builds.get(branch.getValue());
            BranchState state = new BranchState(branch.getKey(), branch.getValue(), messages.get(branch.getKey()),
                    build);
            ret.put(branch.getKey(), state);
        }

        return new ArrayList<BranchState>(ret.values());
    }
}