R/jira_issues_api.R

Defines functions jira_issues_api

Documented in jira_issues_api

#' jira_issues_api
#'
#' Extract issue data from the JIRA API; returns a list of all issues
#' @param project_url The JIRA project ULR
#' @param project_id The JIRA project ID
#' @param changelog If you want to get a dataframe of all changes change this to TRUE
#' @param sample If you want to get only a sample of the most recent 200 issues, set this to TRUE
#' @param jql Pass a JQL string if you want to search for only specific issues
#' @param sprint Pass a specific sprint name if you want to search only for issues relating to it; defaults to NULL. Only works if formatted like 'Mobile Sprint 108', doesn't work with the full sprint name
#' @export

jira_issues_api <- function(project_url,project_id,changelog = FALSE,sample = FALSE,jql = NULL,sprint = NULL) {

require(httr)
require(jsonlite)
require(tidyverse)
require(rlist)
require(neugelbtools)

start_point <- 0
issues_list <- list()

#get the API credentials

if (is_empty(jql) == FALSE) {

  if (str_detect(jql,'project') == FALSE) {

  jql = paste(paste('project',project_id,sep='='),jql,sep=' & ')

  }

  jql_snippet <- str_replace_all(jql,' in ','%20in%20')
  jql_snippet <- str_replace_all(jql,' or ','%20or%20')
  jql_snippet <- str_replace_all(str_replace_all(jql_snippet,' ',''),'&|and','%20and%20')
  jql_snippet <- str_replace_all(jql_snippet,fixed('@'),'%40')
  jql_snippet <- paste('jql',jql_snippet,sep='=')

} else {

  jql_snippet <- paste('jql=project=',project_id,sep='')

}

if (is_empty(sprint) == FALSE) {

  sprint2 <-   str_replace_all(sprint,fixed('/'),'%20%2F%')
  sprint2 <-   paste("'",str_replace_all(sprint2,' ','%20'),"'",sep='')

  jql_snippet <- paste(jql_snippet,paste('sprint',sprint2,sep='='),sep='%20and%20')

}

jira_creds <- jira_auth_check()

if (sample == TRUE) {

  url <- paste(project_url,'/rest/api/2/search?',jql_snippet,'&startAt=',start_point,'&maxResults=',200,sep='')

  if (changelog == TRUE) {

url <- paste(url,'expand=changelog',sep='&')

  }

  x <- GET(url,authenticate(jira_creds$user,jira_creds$pass))

  print(paste('API call for results',start_point,'to',start_point+200,'successfully made'))

  if (x$status_code == 401) {
    stop('Sorry, 401 Authentication failed')

  } else if (x$status_code == 403) {

    stop('Sorry, 403 error')

  } else if (as.numeric(x$status_code > 403)) {

    stop('Sorry, error')

  } else {

    #extract the content

    y <- content(x,'parsed')

    issues_list <- y$issues

}

} else {

#make all the API calls

repeat {

  #generate the URL to use

  url <- paste(project_url,'/rest/api/2/search?',jql_snippet,'&startAt=',start_point,'&maxResults=',200,sep='')

  if (changelog == TRUE) {

url <- paste(url,'expand=changelog',sep='&')

  }

  #call the API

  x <- GET(url,authenticate(jira_creds$user,jira_creds$pass))

  print(paste('API call for results',start_point,'to',start_point+200,'successfully made'))

  if (x$status_code == 401) {
    stop('Sorry, 401 Authentication failed')

  } else if (x$status_code == 403) {

    stop('Sorry, 403 error')

  } else {

    #extract the content

    y <- content(x,'parsed')

    z <- y$issues

    if(length(z)<=1) {

      print('We are finished')
      break

    } else {

      #add the content to the main list

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

        i2 <- z[[i]]

        issues_list <- list.append(issues_list,i2)

      }

    }

    start_point <- start_point + length(z)

  }
}

}

print('All API calls made')

return(issues_list)

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