R/jira_changelog.R

Defines functions jira_changelog

Documented in jira_changelog

#' jira_changelog
#'
#' Generate a dataframe of changes data from a Jira API call
#' @param issues_list A list of the Jira issues
#' @param changes_list A list of the changelogs
#' @param fields_list A list of all of the issue fields
#' @export

jira_changelog <- function(issues_list,changes_list,fields_list) {

  require(tidyverse)
  require(purrr)
  require(lubridate)

  all_changes <- tibble()

  if (is_empty(changes_list) == TRUE & is_empty(fields_list) == TRUE) {

    changes_list <- map(issues_list, ~.x[['changelog']])

    fields_list <- map(issues_list,~.x[['fields']])

    issues_list <- map(issues_list, ~ discard(.x, names(.x) %in% c('fields','changelog')))

    fields_list <- map(fields_list,compact)

    fields_list <- map(fields_list, ~ discard(.x, names(.x) == 'self'))

  }

  #get the data on the creation of the ticket

  creation_ts <- do.call('bind_rows',map(map(fields_list,~.x[['created']]) ,as_tibble)) %>%
    mutate(value = as.POSIXct(str_replace_all(value,'T',' ')),
           date = as_date(value))

  #create a table covering all the status changes for each ticket

  for (i in 1:length(issues_list)) {

    x2 <- pluck(issues_list,i)

    xchanges <- pluck(changes_list,i)

    sd = pull(slice(creation_ts,i),date)

    status_changes <- tibble()

    index = i

    #first row is the ticket creation

    status_start <- tibble(issue = x2$key,
                           start_date = sd,
                           change_author = fields_list[[i]]$reporter$displayName,
                           change_type = 'status',
                           change_time =  pull(slice(creation_ts,i),value),
                           change_from = NA,
                           change_to = 'Backlog')

    if (length(xchanges$histories) == 0) {

      #if no changes return only the set up row

      all_changes <- bind_rows(all_changes,status_start)

    } else {

      #loop through all the changes and check for status changes

      for (i in 1:length(xchanges$histories)) {

        field <- xchanges$histories[[i]]

        field_type <- field$items[[1]]$field

        #only create another row when it's a status change, include the relevant info

        if (field_type == 'status') {

          values <- tibble(issue = x2$key,
                           start_date = sd,
                           change_author = ifelse(is_empty(field$author$displayName)==TRUE,NA,field$author$displayName),
                           change_type = field_type,
                           change_time = as.POSIXct(str_replace_all(field$created,'T',' ')),
                           change_from = field$items[[1]]$fromString,
                           change_to = field$items[[1]]$toString
          )


          status_changes <- bind_rows(status_changes,values)

        } else {

          next()

        }

      }

      status_changes <- bind_rows(status_start,status_changes)

      all_changes <- bind_rows(all_changes,status_changes)

    }

  }

  print('change history processed')

  #tidy the data frame

  all_changes <- all_changes %>%
    arrange(desc(start_date),issue,change_time) %>%
    group_by(issue) %>%
    mutate(days_difference = round(ifelse(min(change_time)==change_time,0,as.numeric(difftime(change_time,lag(change_time),units='days'))),4)) %>%
    ungroup() %>%
    arrange(desc(start_date),desc(issue),desc(change_time)) %>%
    mutate_if(is.character, trimws)

  return(all_changes)

}
neugelb/njira documentation built on Sept. 2, 2020, 7:02 p.m.