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

Java tutorial

Introduction

Here is the source code for de.sjka.jenkins.view.branches.BranchBadgeAction.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.BuildBadgeAction;
import hudson.model.AbstractBuild;
import hudson.model.Actionable;
import hudson.plugins.git.GitTagAction;

import java.util.LinkedList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

public class BranchBadgeAction implements BuildBadgeAction {

    @SuppressWarnings("rawtypes")
    private final AbstractBuild build;

    public BranchBadgeAction(@SuppressWarnings("rawtypes") AbstractBuild build) {
        this.build = build;
    }

    public String getIconFileName() {
        return null;
    }

    public String getDisplayName() {
        return null;
    }

    public String getUrlName() {
        return null;
    }

    public String getBranch() {
        List<String> branches = new LinkedList<String>();
        if (BranchOverviewActionFactory.isApplicable(build)) {
            for (String thatBranch : ((Actionable) build).getAction(GitTagAction.class).getTags().keySet()) {
                // skip "*/HEAD" since it's not a branch in our sense
                if (thatBranch.endsWith("/HEAD")) {
                    continue;
                }

                // cut off "origin/" since that's the default most of the time
                if (thatBranch.startsWith("origin/")) {
                    thatBranch = thatBranch.replace("origin/", "");
                }

                branches.add(thatBranch);
            }
        }
        return "(" + StringUtils.join(branches, ", ") + ")";
    }

}