diff --git a/src/main/java/com/redhat/sso/ninja/Database2.java b/src/main/java/com/redhat/sso/ninja/Database2.java index aa49191..e7f7ed7 100644 --- a/src/main/java/com/redhat/sso/ninja/Database2.java +++ b/src/main/java/com/redhat/sso/ninja/Database2.java @@ -58,13 +58,13 @@ public void setVersion(String version){ public static String buildLinkMarkdown(Map params){ if (!params.containsKey("linkId")) return ""; if (params.get("id").startsWith("TR")){ - return "[Trello: "+params.get("linkId")+"/"+params.get("id")+"](https://trello.com/c/"+params.get("linkId"); + return "[Trello: "+params.get("linkId")+"/"+params.get("id")+"](https://trello.com/c/"+params.get("linkId")+")"; }else if (params.get("id").startsWith("GH")){ if (params.get("pool").toLowerCase().contains("pull")){ - return "[Github PR: "+params.get("linkId")+"](https://github.com/"+params.get("org")+"/"+params.get("board")+"/pull/"+params.get("linkId")+")"; - }else{ // assume "issues" - return "[Github Issue: "+params.get("linkId")+"](https://github.com/"+params.get("org")+"/"+params.get("board")+"/issues/"+params.get("linkId")+")"; - } + return ""; + }else{ // assume "issues" + return ""; + } } return ""; } @@ -109,11 +109,11 @@ public Database2 increment(String poolId, String userId, Integer increment, Map< if (params!=null && params.size()>1){ //because "id" is always added // addEvent("Points Increment", userId, increment+" point"+(increment<=1?"":"s")+" added to "+poolId+" "+buildLink(params)); - addEvent2("Points Increment", userId, increment, buildLinkMarkdown(params), poolId, ""); + addEvent2("Points Increment", userId, increment, buildLinkMarkdown(params), poolId); }else{ // no params & therefore no link // addEvent("Points Increment", userId, increment+" point"+(increment<=1?"":"s")+" added to "+poolId+""); - addEvent2("Points Increment", userId, increment, "", poolId, ""); + addEvent2("Points Increment", userId, increment, "", poolId); } }else{ @@ -172,12 +172,12 @@ public enum TASK_FIELDS{ } - public void addEvent2(String type, String user, Integer points, String source, String pool, String text){ + public void addEvent2(String type, String user, Integer points, String source, String pool){ Map event=new HashMap(); event.put(EVENT_FIELDS.TIMESTAMP.v, sdf2.format(new Date())); event.put(EVENT_FIELDS.TYPE.v, type); event.put(EVENT_FIELDS.USER.v, user); - if (text!=null && !"".equals(text)) event.put(EVENT_FIELDS.TEXT.v, text); +// if (text!=null && !"".equals(text)) event.put(EVENT_FIELDS.TEXT.v, text); event.put(EVENT_FIELDS.POINTS.v, String.valueOf(points)); event.put(EVENT_FIELDS.SOURCE.v, source); event.put(EVENT_FIELDS.POOL.v, pool); diff --git a/src/main/java/com/redhat/sso/ninja/Heartbeat2.java b/src/main/java/com/redhat/sso/ninja/Heartbeat2.java index 3227a92..1b62f40 100644 --- a/src/main/java/com/redhat/sso/ninja/Heartbeat2.java +++ b/src/main/java/com/redhat/sso/ninja/Heartbeat2.java @@ -35,8 +35,10 @@ import org.apache.poi.xssf.usermodel.XSSFSheet; import org.mortbay.log.Log; +import com.google.common.base.Joiner; import com.google.common.collect.Lists; import com.redhat.sso.ninja.ChatNotification.ChatEvent; +import com.redhat.sso.ninja.ScriptRunner.ProcessResult; import com.redhat.sso.ninja.controllers.EventsController; import com.redhat.sso.ninja.user.UserService; import com.redhat.sso.ninja.user.UserService.User; @@ -464,35 +466,18 @@ public void run() { command=convertLastRun(command, lastRun); } - log.info("Script downloaded ("+version+"): "+originalCommand); log.info("Script executing: "+command); - Process script_exec=Runtime.getRuntime().exec(command); - script_exec.waitFor(); - if(script_exec.exitValue() != 0){ - - BufferedReader stdInput=new BufferedReader(new InputStreamReader(script_exec.getInputStream())); - StringBuffer sb=new StringBuffer(); - String s; - while ((s=stdInput.readLine()) != null) sb.append(s).append("\n"); - log.error("Error while executing script (stdout): "+sb.toString()); - - BufferedReader stdErr=new BufferedReader(new InputStreamReader(script_exec.getErrorStream())); - sb.setLength(0); - while ((s=stdErr.readLine()) != null) sb.append(s).append("\n"); - log.error("Error while executing script (stderr): "+sb.toString()); - - db.addEvent("Script Execution FAILED", "", command+"\nERROR (stderr):\n"+sb.toString()); - - new ChatNotification().send(ChatEvent.onScriptError, name+" script failure occurred. Please investigate"); - - scriptFailure=true; - - }else{ - allocatePoints(db, script_exec.getInputStream(), script, scriptFolder, poolToUserIdMapper); - db.addEvent("Script Execution Succeeded", "", command +" (took "+(System.currentTimeMillis()-start)+"ms)"); - + PointsAllocation allocate=new PointsAllocation().database(db).mapper(poolToUserIdMapper).scriptName((String)script.get("name")); + + ProcessResult process=new ScriptRunner().run(scriptFolder, command); + if (0==process.exitValue()){ + for(String line:process.lines()) + allocate.allocatePoints2(line); + }else{ // error + db.addEvent("Script Execution FAILED", "", command+"\nERROR (stderr):\n"+ Joiner.on("\n").join(process.lines())); + new ChatNotification().send(ChatEvent.onScriptError, name+" script failure occurred. Please investigate"); } }catch (IOException e){ @@ -616,7 +601,6 @@ public void publishGraphsData(Database2 db, Config config){ try{ Map filters=new MapBuilder().put("daysOld", "180").put("events","User Promotion,Points Increment").put("asCSV", "true").build(); String events=(String)ec.getEventsV2(filters); - System.out.println("\n\n\n"+events); if (200!=Http.post(url+"/events180", events).responseCode) log.error("Error pushing 'events180' info to graphsProxy"); }catch (IOException e){ e.printStackTrace(); } @@ -652,7 +636,7 @@ public void levelUpChecks(Database2 db){ db.addEvent("User Promotion", userInfo.get("username"), "Promoted to "+nextLevel.getRight()); String displayName=userInfo.containsKey("displayName")?userInfo.get("displayName"):userInfo.get("username"); - String message=" promoted to "+nextLevel.getRight()+" belt"; + String message=displayName +" promoted to "+nextLevel.getRight(); db.addTask(message, userInfo.get("username")); // Notify everyone on the Ninja chat group of a new belt promotion diff --git a/src/main/java/com/redhat/sso/ninja/PointsAllocation.java b/src/main/java/com/redhat/sso/ninja/PointsAllocation.java new file mode 100644 index 0000000..6cf0b2a --- /dev/null +++ b/src/main/java/com/redhat/sso/ninja/PointsAllocation.java @@ -0,0 +1,99 @@ +package com.redhat.sso.ninja; + +import java.io.UnsupportedEncodingException; +import java.util.HashMap; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.log4j.Logger; + +import com.redhat.sso.ninja.utils.ParamParser; + +public class PointsAllocation{ + private static final Logger log=Logger.getLogger(PointsAllocation.class); + + private Database2 db; + private String scriptName; + private Map mapper; + + public PointsAllocation database(Database2 db){ + this.db=db; return this; + } + public PointsAllocation scriptName(String scriptName){ + this.scriptName=scriptName; return this; + } + public PointsAllocation mapper(Map mapper){ + this.mapper=mapper; return this; + } + + private Pattern paramsPattern=Pattern.compile(".*(\\[.*\\]).*"); + public void allocatePoints2(String line) throws UnsupportedEncodingException{ + + String s=line.trim(); +// scriptLog.append(s).append("\n"); + log.debug(s); + + Map params=new HashMap(); + // check for params here, extract them if present for use later on + if (s.matches(".* \\[.*\\]")){ + Matcher m=paramsPattern.matcher(s); + if (m.find()){ + String paramsExtract=m.group(1); + params.putAll(new ParamParser().splitParams(paramsExtract.replaceAll("\\[", "").replaceAll("\\]", "").trim())); + s=s.replaceAll("\\[.*\\]", "").trim(); + } + } + + if (s.startsWith("#")){ // Informational lines only, some may need to be added to event logging as reasons points were not awarded + + + + }else if (s.contains("/")){ // ignore the line if it doesn't contain a slash + String[] split=s.split("/"); + + // take the last section of the script name as the pool id. so "trello" stays as "trello", but "trello.thoughtleadership" becomes "thoughtleadership" where the "trello" part is the source type/context + String pool=(String)scriptName; + String[] splitPool=pool.split("\\."); + pool=splitPool[splitPool.length-1]; + + String actionId; + String poolUserId; + Integer inc; + + if (split.length==4){ //pool.sub + pool=pool+"."+split[0]; + actionId=split[1]; + poolUserId=split[2]; + inc=Integer.valueOf(split[3]); + + params.put("id", actionId); + params.put("pool", pool); + + if (!db.getPointsDuplicateChecker().contains(actionId+"."+poolUserId)){ + db.getPointsDuplicateChecker().add(actionId+"."+poolUserId); + + String userId=mapper.get(poolUserId); // convert the channel (trello, github etc..) to the kerberos username + + if (null!=userId){ + db.increment(pool, userId, inc, params);//.save(); + }else{ + log.info("Unable to find '"+poolUserId+"' "+scriptName+" user - not registered? "+Database2.buildLink(params)); + db.addEvent("Lost Points", poolUserId +"("+scriptName+")", scriptName+" user '"+poolUserId+"' was not found - not registered? "+Database2.buildLink(params)); + } + }else{ + // it's a duplicate increment for that actionId & user, so ignore it + log.warn(actionId+"."+poolUserId+" is a duplicate"); + } + + }else{ + // dont increment because we dont know the structure of the script data + } + + + } + + } + + +} diff --git a/src/main/java/com/redhat/sso/ninja/ScriptRunner.java b/src/main/java/com/redhat/sso/ninja/ScriptRunner.java new file mode 100644 index 0000000..78c068c --- /dev/null +++ b/src/main/java/com/redhat/sso/ninja/ScriptRunner.java @@ -0,0 +1,41 @@ +package com.redhat.sso.ninja; + +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; + +public class ScriptRunner{ + + public class ProcessResult{ + private List lines=new ArrayList(); + private int exitValue; + public int exitValue(){ + return this.exitValue; + } + public List lines(){ + return this.lines; + } + } + + public ProcessResult run(File workingFolder, String command) throws IOException, InterruptedException{ + boolean isWindows=false; + ProcessBuilder pBuilder=new ProcessBuilder(isWindows?"cmd.exe":"/bin/sh").directory(workingFolder).command(command.split(" ")); + pBuilder.redirectErrorStream(true); + Process process=pBuilder.start(); + String buf; + ProcessResult result=new ProcessResult(); + BufferedReader reader=new BufferedReader(new InputStreamReader(process.getInputStream())); + while ((buf=reader.readLine())!=null) { + result.lines.add(buf); + } + process.waitFor(10, TimeUnit.SECONDS); + result.exitValue=process.exitValue(); + return result; + } + + +} diff --git a/src/main/java/com/redhat/sso/ninja/controllers/EventsController.java b/src/main/java/com/redhat/sso/ninja/controllers/EventsController.java index bd770a1..5a2196f 100644 --- a/src/main/java/com/redhat/sso/ninja/controllers/EventsController.java +++ b/src/main/java/com/redhat/sso/ninja/controllers/EventsController.java @@ -137,8 +137,6 @@ private List> getFilteredEvents1(Map filters) for(Map v:result){ // add a generated "text" field if none exists - this is because on the events UI there is not enough space for all the separated fields if ("Points Increment".equals(v.get(EVENT_FIELDS.TYPE.v)) && !v.containsKey(EVENT_FIELDS.TEXT.v)){ -// System.out.println("adding a text field"); -// if (!v.containsKey(EVENT_FIELDS.TEXT.v)){// && v.containsKey(EVENT_FIELDS.POINTS.v)){ Integer points=Integer.parseInt(v.get(EVENT_FIELDS.POINTS.v)); v.put(EVENT_FIELDS.TEXT.v, points+" point"+(points<=1?"":"s")+" added to "+v.get(EVENT_FIELDS.POOL.v)+" "+convertSourceToShowdown(v.get(EVENT_FIELDS.SOURCE.v))); } @@ -218,7 +216,7 @@ private List> getFilteredEvents2(Map filters) } } - if (include) result.add(e); + if (include) result.add(new HashMap<>(e)); } } } diff --git a/src/main/webapp/mojo/igloo-ninjaWall.jsp b/src/main/webapp/mojo/igloo-ninjaWall.jsp index 7225046..65b34d8 100644 --- a/src/main/webapp/mojo/igloo-ninjaWall.jsp +++ b/src/main/webapp/mojo/igloo-ninjaWall.jsp @@ -149,7 +149,7 @@ users.push(username); var belt = json['custom1'][i].split("|")[1]; var geo = json['custom1'][i].split("|")[2]; - belt = (belt == "zero" ? "No" : "" + uCase(belt)) + " Star<\/span>"; + belt = (belt == "zero" ? "No" : "" + beltToStar(uCase(belt))) + " Star<\/span>"; var NL = "
"; var badges = ""; @@ -187,7 +187,14 @@ { return string.charAt(0).toUpperCase() + string.slice(1); } - + + function beltToStar(string) + { + if (string == "Black") return "Gold"; + if (string == "Red") return "Green"; + return string; + } + function getUserPics(users) { var jqxhr = jQuery.getJSON("/.api2/api/v1/communities/10/search/members?query=" + users.join("+OR+") + "&memberSearchType=email&limit=1000", function(data) diff --git a/src/main/webapp/mojo/mojo-dashboard-card.jsp b/src/main/webapp/mojo/mojo-dashboard-card.jsp index d09808c..0463e84 100644 --- a/src/main/webapp/mojo/mojo-dashboard-card.jsp +++ b/src/main/webapp/mojo/mojo-dashboard-card.jsp @@ -1,6 +1,7 @@ + -