package com.github.terma.jenkins.githubprcoveragestatus;

import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

import hudson.Extension;
import hudson.model.UnprotectedRootAction;

@Extension
public class CoverageStatusIconAction implements UnprotectedRootAction {

    @Override
    public String getIconFileName() {
        return null;
    }

    @Override
    public String getDisplayName() {
        return null;
    }

    @Override
    public String getUrlName() {
        return "coverage-status-icon";
    }

    /**
     * Used by Jenkins Stapler service when get request on URL jenkins_host/getUrlName()
     *
     * @param request - request
     * @param response - response
     * @throws IOException
     */
    @SuppressWarnings("unused")
    public void doIndex(StaplerRequest request, StaplerResponse response) throws IOException {
        final float coverage = Float.parseFloat(request.getParameter("coverage"));
        final float masterCoverage = Float.parseFloat(request.getParameter("masterCoverage"));

        response.setContentType("image/svg+xml");

        String svg = IOUtils.toString(this.getClass().getResourceAsStream(
                "/com/github/terma/jenkins/githubprcoveragestatus/Icon/icon.svg"));

        final Message message = new Message(coverage, masterCoverage);
        svg = StringUtils.replace(svg, "{{ message }}", message.forIcon());

        final int coveragePercent = Percent.of(coverage);
        String color;
        if (coveragePercent < Configuration.getYellowThreshold()) color = "#b94947"; // red
        else if (coveragePercent < Configuration.getGreenThreshold()) color = "#F89406"; // yellow
        else color = "#97CA00"; // green
        svg = StringUtils.replace(svg, "{{ color }}", color);

        response.getWriter().write(svg);
    }

}

@@ -62,30 +62,69 @@
 ort 
-java.io.IOExcep
+hudson.Extension;%0Aimport hudson.model.UnprotectedRootAc
 tion;%0A
-%0A
 impo
@@ -300,63 +300,23 @@
 ort 
-hudson.Extension;%0Aimport hudson.model.UnprotectedRootAc
+java.io.IOExcep
 tion
@@ -403,16 +403,89 @@
 tion %7B%0A%0A
+    public static final String URL_NAME = %22stash-coverage-status-icon%22;%0A%0A
     @Ove
@@ -701,30 +701,16 @@
 urn 
-%22coverage-status-icon%22
+URL_NAME
 ;%0A  
@@ -1219,24 +1219,96 @@
 Coverage%22));
+%0A        final String color = toHexColor(request.getParameter(%22color%22));
 %0A%0A        re
@@ -1726,95 +1726,320 @@
     
-String color;%0A        if (coveragePercent %3C Configuration.getYellowThreshold()) color =
+svg = StringUtils.replace(svg, %22%7B%7B color %7D%7D%22, color);%0A%0A        response.getWriter().write(svg);%0A    %7D%0A%0A    private String toHexColor(String color) %7B%0A        if (StringUtils.startsWith(color, %22#%22)) %7B%0A            return color;%0A        %7D%0A%0A        if (StringUtils.equalsIgnoreCase(%22red%22, color)) %7B%0A            return
  %22#b
@@ -2056,24 +2056,26 @@
  red%0A       
+ %7D
  else if (co
@@ -2076,68 +2076,75 @@
 if (
-coveragePercent %3C Configuration.getGreenThreshold()) color =
+StringUtils.equalsIgnoreCase(%22yellow%22, color)) %7B%0A            return
  %22#F
@@ -2172,21 +2172,92 @@
     
+ %7D
  else 
-color =
+if (StringUtils.equalsIgnoreCase(%22brightgreen%22, color)) %7B%0A            return
  %22#9
@@ -2267,16 +2267,22 @@
 00%22; // 
+bright
 green%0A  
@@ -2279,38 +2279,43 @@
 htgreen%0A        
-svg = 
+%7D else if (
 StringUtils.repl
@@ -2314,33 +2314,31 @@
 ils.
-replace(svg, %22%7B%7B color %7D%7D
+equalsIgnoreCase(%22green
 %22, c
@@ -2334,35 +2334,40 @@
 (%22green%22, color)
-;%0A%0A
+) %7B%0A    
         response
@@ -2364,37 +2364,63 @@
   re
-sponse.getWriter().write(svg)
+turn %22#008000%22; // green%0A        %7D%0A        return color
 ;%0A  

Rename URL to prevent clash with github-pr-coverage-status-plugin