R/gitgantt.R

#' Create a list of issues in a GitHub repo
#'
#' @param owner The owner of the repo, e.g. `ropensci`
#' @param repo The name of the repo, e.g. `gh`
#' @param state One of `"all"` (the default), `"open"`, or `"closed"`
#' @param limit Maximum number of issues to return, `Inf` by default
#' @export
#' @examples
#' owner = "ATFutures"
#' repo = "who3"
#' gg_issue_list(owner, repo, limit = 2)
gg_issue_list = function(owner, repo, state = "all", limit = Inf) {
  issue_list = gh::gh("/repos/:owner/:repo/issues", owner = owner, repo = repo, state = "all", .limit = limit)
  message(length(issue_list), " issues returned for the repo ", repo)
  issue_list
}
#' Covert a list of issues into a data frame
#' @inheritParams gg_issue_list
#' @param issue_list A list of issues generated by `gg_issue_list`
#' @export
#' @examples
#' gg_issue_df(owner = "r-lib", repo = "bench")
#' owner = "ATFutures"
#' repo = "who3"
#' issue_list = gg_issue_list(owner, repo, limit = 2)
#' gg_issue_df(issue_list)
gg_issue_df = function(issue_list = NULL, owner, repo) {
  if(is.null(issue_list)) {
    issue_list = gg_issue_list(owner = owner, repo = repo)
  }
  tibble::tibble(
        number = purrr::map_int(issue_list, "number"),
        title = purrr::map_chr(issue_list, "title"),
        body = purrr::map_chr(issue_list, "body"),
        start_date = as.Date(gg_start_date(body)),
        due_date = as.Date(gg_due_date(body)),
        state = purrr::map_chr(issue_list, "state"),
        created_at = as.Date(purrr::map_chr(issue_list, "created_at"))
  )
}

#' Extract dates from gh issues
#'
#' @param issue_body The body of an issue that contains `GanttStart` and `GanttDue` lines
#' @param pattern The text string used to identify start and due dates
#' @export
#' @examples
#' issue_body = "GanttStart: 2019-09-01\r\nGanttDue: 2019-10-01"
#' gg_extract_dates(issue_body)
#' issue_body = c(issue_body, "No gantt", "GanttStart: 2019-09-01\r\nOther comment")
#' gg_start_date(issue_body)
#' gg_due_date(issue_body)
gg_extract_dates = function(issue_body, pattern = "GanttStart: |GanttDue: ") {
  body_lines = unlist(stringr::str_split(issue_body, "\r\n", n = Inf))
  body_date_strings = body_lines[grepl(pattern = pattern, body_lines)]
  if(length(body_date_strings) == 0) {
    return(NA)
  }
  gsub(pattern = pattern, replacement = "", body_date_strings)
}
#' @rdname gg_extract_dates
#' @export
gg_start_date = function(issue_body) {
  purrr::map_chr(issue_body, gg_extract_dates, pattern = "GanttStart: ")
}
#' @rdname gg_extract_dates
#' @export
gg_due_date = function(issue_body) {
  purrr::map_chr(issue_body, gg_extract_dates, pattern = "GanttDue: ")
}
#' Create interactive visualisation of GitHub issues
#' @inheritParams gg_issue_list
#' @param issue_df A data frame created by `gg_issue_df()`
#' @export
#' @examples
#' gg_timevis(owner = "robinlovelace", repo = "gitgantt")
#' \donttest{
#' issue_df = gg_issue_df(gg_issue_list("ATFutures", "who3"))
#' gg_timevis(issue_df)
#' }
gg_timevis = function(issue_df = NULL, owner, repo) {
  if(is.null(issue_df)) {
    issue_df = gg_issue_df(owner = owner, repo = repo)
  }
  n = names(issue_df)
  n_new = stringr::str_replace_all(n, c("start_date" = "start" , "due_date" = "end", "title" = "content"))
  timevis_df = issue_df
  names(timevis_df) = n_new
  timevis_df = timevis_df[!is.na(timevis_df$start), ]
  timevis_df = timevis_df[order(timevis_df$start, decreasing = FALSE), ]
  timevis_df$id = 1:nrow(timevis_df)
  timevis_df$order = nrow(timevis_df):1
  timevis_df$group = timevis_df$id
  g = tibble::tibble(
    id = 1:nrow(timevis_df),
    # content = timevis_df$content
    content = 1:nrow(timevis_df)
    )
  timevis::timevis(data = timevis_df, groups = g)
}
# extract_start_date = function(x) {
#
# }
Robinlovelace/gitgantt documentation built on June 6, 2019, 12:01 a.m.