library(RCurl) library(curl) library(jsonlite) library(gsubfn) library(gplots) library(RColorBrewer) #clean any junk rm(list = ls()) #need hash to work for lists people_company_dictionary<- new.env(hash=TRUE) #functions companies <- function (names) { companys = list() for (name in names) { for (n in name) { company <- member_company(n) if (company != '') { companys[[length(companys) + 1]] <- member_company(n) } else { #cant find them in ldap, use the email string as a guess #This means that in many cases people no linger in LDAP get counted as Lianro folks as #the original member company can no longer be determined. warning(paste(n,"not found in LDAP, guessing from email string")) companys[[length(companys) + 1]] <- strapplyc(n, "@(.*)", simplify = TRUE) } } } company <- unique(companys) return (company) } member_company <- function (name) { company <- people_company_dictionary[[name]] if (is.null(company)) { #not in hash, need to look it up ldap_command <- paste ('python', '/home/mike/bin/linaro-ldap-lookup.py', 'mike.holmes', name) ldap_raw <- as.character(system(ldap_command, intern = TRUE)) if (length(ldap_raw) == 0) { #that name could not be found company <- "" } else { #Pass back just the company ldap <- fromJSON(ldap_raw, simplifyDataFrame = TRUE) company <- ldap$company } #fix any names that look like directories company <- gsub("/", "_", company) #save in dictionary people_company_dictionary[[name]] <- company } return (company) } display_barplot <- function(report, m, suffix) { report_name <- paste(report["name"], suffix, ".png",sep = "") png(file=report_name, title = report["name"]) #display raw data cat("Generating", report_name, "\n") print(m) # following code limits the lowest and highest color to 5%, and 95% of your range, respectively quantile.range <- quantile(m, probs = seq(0, 1, 0.01)) palette.breaks <- seq(quantile.range["5%"], quantile.range["95%"], 0.1) # use http://colorbrewer2.org/ to find optimal divergent color palette (or set own) color.palette <- colorRampPalette(c("#FC8D59", "#FFFFBF", "#91CF60"))(length(palette.breaks) - 1) watcher_barplot <- barplot2(m, ylab = "interest", xlab = "project",col = color.palette) dev.off() } watcher_barplot <- function(report) { baseurl <- "https://projects.linaro.org/rest/api/2/" fields <- "&fields=watches,key,project" type <- "search?" jql <- paste ("jql=project+in+(",report["team"],")+AND+issuetype+in+(Initiative,Epic)&maxResults=2000", sep = "") url <- paste(baseurl, type, jql, fields, sep = "") cat (url,"\n") webpage <- curl (url, handle = h) #get data it into R jira_raw <- fromJSON(webpage, simplifyDataFrame = TRUE) # extract the data node keys <- jira_raw$issues$key #x axis the number of projects projects <- unique(jira_raw$issues$fields$project$key) num_projects <- length(projects) #projects #from the website now get watchers names with another query for all keys found previously fields <- "/watchers" type <- "issue/" names = list() for (key in keys) { url <- paste(baseurl, type, key, fields, sep = "") webpage <- curl (url, handle = h) jira_raw_names <- fromJSON(webpage, simplifyDataFrame = TRUE) names[[length(names) + 1]] <- as.list(jira_raw_names$watchers$name) } #create empty matrix comp <- companies(names) num_comp <- length(comp) m <- matrix(NA, nrow = num_comp, ncol = num_projects) colnames(m) <- projects rownames(m) <- comp m[] <- 0L #from the website now get watchers names with another query for all keys found previously fields <- "/watchers" type <- "issue/" for (key in keys) { #y mike <- match (key, keys) team <- jira_raw$issues$fields$project$key[mike] y <- match (team, projects) #x names = list() url <- paste(baseurl, type, key, fields, sep = "") webpage <- curl (url, handle = h) jira_raw_names <- fromJSON(webpage, simplifyDataFrame = TRUE) names[[length(names) + 1]] <- as.list(jira_raw_names$watchers$name) interested = companies(names) for (c in interested) { x <- match (c, comp) m[x, y] <- m[x, y] + 1 } } print ( member_company ('mike.holmes@linaro.org')) #remove companies # rows.to.delete <- c('linaro.org', 'broadcom.com', 'character') # for (d in rows.to.delete) { # m <- as.matrix(m[!grepl(d, rownames(m)),]) # } for (c in comp) { v <- as.vector(m[c,]) names(v) <- projects report["name"] <- c display_barplot(report, v, "_wt") } setwd(home) } sponsor_barplot <- function (report) { baseurl <- "https://projects.linaro.org/rest/api/2/" fields <- "&fields=customfield_10101,key,project" type <- "search?" jql <- paste( "jql=project+in+(", report["team"], ")+AND+issuetype+in+(Initiative,Epic)&maxResults=1000", sep = "" ) url <- paste(baseurl, type, jql, fields, sep = "") cat (url,"\n") webpage <- curl (url, handle = h) #get data it into R jira_raw <- fromJSON(webpage, simplifyDataFrame = TRUE) #list of every issue keys <- jira_raw$issues$key #x axis the number of projects projects <- unique(jira_raw$issues$fields$project$key) num_projects <- length(projects) #y axis the companies or sponsors sponsor <- jira_raw$issues$fields$customfield_10101 names = list() for (key in keys) { s <- sponsor[[match (key, keys)]][[2]] for (n in s) { names[[length(names) + 1]] <- n } } #create empty matrix comp <- unique(names) num_comp <- length(comp) m <- matrix(NA, nrow = num_comp, ncol = num_projects) colnames(m) <- projects rownames(m) <- comp m[] <- 0L # count companies per project by looking at every key and update matrix for (key in keys) { #y mike <- match (key, keys) team <- jira_raw$issues$fields$project$key[mike] y <- match (team, projects) #x s <- sponsor[[match (key, keys)]][[2]] for (c in s) { x <- match (c, comp) m[x, y] <- m[x, y] + 1 } } #remove companies we don't like ;) # rows.to.delete <- c('Linaro') # for (d in rows.to.delete) { # m <- as.matrix(m[!grepl(d, rownames(m)),]) # } for (c in comp) { v <- as.vector(m[c,]) names(v) <- projects report["name"] <- c display_barplot(report, v, "_sp") } } #from the website get the initiatives list for the four teams id <- Sys.getenv(c("JIRA_USERNAME", "JIRA_PASSWORD","JIRA_OUTPUT")) if (id[1] == '') { print ("export JIRA_USERNAME='john.doe@linaro.org'") q() } if (id[2] == '') { print ("export JIRA_PASSWORD='my-super-secret-password'") q() } if (id[3] == '') { home <- paste(getwd(),"/company", sep="") } else { home <- paste(id[3],"/company", sep="") } print(paste("Outputdir = ", home)) dir.create(home) setwd(home) user_password <- paste(id[1], id[2], sep=":") h <- new_handle(failonerror = TRUE) handle_setopt(h, verbose = 0) handle_setopt(h, userpwd = user_password) handle_setopt(h, httpauth = 1) report_name <- c("ti") team_list <- c( "SWG,KWG,PMWG,VIRT,LSK,LHG,LEG,LNG,LITE,LMG,CTT,BB,LAVA,LAB,QA,SYS,TCWG" ) reports <- data.frame(name = report_name , team = team_list, stringsAsFactors = FALSE) apply(reports, 1, sponsor_barplot) apply(reports, 1, watcher_barplot) warning() setwd(home)