diff --git a/R/get_org_attributes.R b/R/get_org_attributes.R index 7b55a79..554a2ba 100644 --- a/R/get_org_attributes.R +++ b/R/get_org_attributes.R @@ -35,13 +35,20 @@ filter_for_non_empty_milestones <- function(milestones) { return(non_empty_milestones) } +filter_for_ghqc_milestones <- function(owner, repo, milestones) { + labels <- sapply(milestones, function(x) gh::gh("GET /repos/:owner/:repo/milestones/:milestone_number/labels", owner = owner, repo = repo, milestone_number = x$number)) + milestones[sapply(labels, function(x) "ghqc" %in% sapply(x, function(y) y$name))] +} + #' @importFrom log4r warn error info debug get_open_milestone_objects <- function(owner, repo) { debug(.le$logger, glue::glue("Retrieving open Milestone(s) in organization {owner}, repo {repo}...")) milestones <- gh::gh("GET /repos/:owner/:repo/milestones", .api_url = .le$github_api_url, owner = owner, repo = repo, state = "open", .limit = Inf) - info(.le$logger, glue::glue("Retrieved {length(milestones)} open Milestone(s) in repo {repo}")) - non_empty_milestones <- filter_for_non_empty_milestones(milestones) + debug(.le$logger, glue::glue("Retrieved {length(milestones)} open Milestone(s) in repo {repo}")) + ghqc_milestones <- filter_for_ghqc_milestones(owner, repo, milestones) + info(.le$logger, glue::glue("Retrieved {length(ghqc_milestones)} open ghqc Milestone(s) in repo {repo}")) + non_empty_milestones <- filter_for_non_empty_milestones(ghqc_milestones) } #' @importFrom log4r warn error info debug @@ -49,8 +56,10 @@ get_closed_milestone_objects <- function(owner, repo) { debug(.le$logger, glue::glue("Retrieving closed Milestone(s) in organization {owner}, repo {repo}...")) milestones <- gh::gh("GET /repos/:owner/:repo/milestones", .api_url = .le$github_api_url, owner = owner, repo = repo, state = "closed", .limit = Inf) - info(.le$logger, glue::glue("Retrieved {length(milestones)} closed Milestone(s) in repo {repo}")) - non_empty_milestones <- filter_for_non_empty_milestones(milestones) + debug(.le$logger, glue::glue("Retrieved {length(milestones)} closed Milestone(s) in repo {repo}")) + ghqc_milestones <- filter_for_ghqc_milestones(owner, repo, milestones) + info(.le$logger, glue::glue("Retrieved {length(ghqc_milestones)} closed ghqc Milestone(s) in repo {repo}")) + non_empty_milestones <- filter_for_non_empty_milestones(ghqc_milestones) } #' @importFrom log4r warn error info debug @@ -86,8 +95,10 @@ list_milestones <- function(org, repo) { debug(.le$logger, glue::glue("Retrieving Milestone(s) in organization {org}, repo {repo}...")) milestones <- get_all_milestone_objects(org, repo) info(.le$logger, glue::glue("Retrieved {length(milestones)} total Milestone(s) in repo {repo}")) - non_empty_milestones <- filter_for_non_empty_milestones(milestones) - info(.le$logger, glue::glue("Retrieved {length(non_empty_milestones)} non-empty Milestone(s) in repo {repo}")) + ghqc_milestones <- filter_for_ghqc_milestones(org, repo, milestones) + info(.le$logger, glue::glue("Retrieved {length(ghqc_milestones)} ghqc Milestone(s) in repo {repo}")) + non_empty_milestones <- filter_for_non_empty_milestones(ghqc_milestones) + info(.le$logger, glue::glue("Retrieved {length(non_empty_milestones)} non-empty ghqc Milestone(s) in repo {repo}")) res <- purrr::map_chr(non_empty_milestones, "title") return(res) } @@ -314,13 +325,18 @@ get_all_issues_in_repo <- function(owner, repo) { page <- page + 1 } - issues <- c(open_issues, closed_issues) + issues <- get_only_ghqc_issues(c(open_issues, closed_issues)) num_issues <- length(issues) info(.le$logger, glue::glue("Retrieved {num_issues} Issue(s) from repo: {repo}")) return(issues) } +get_only_ghqc_issues <- function(issues) { + labels <- sapply(issues, function(x) x$labels) + issues[sapply(labels, function(x) "ghqc" %in% sapply(x, function(y) y$name))] +} + # sort by open/closed #' @importFrom log4r warn error info debug get_all_issues_in_milestone <- function(owner, repo, milestone_name) { @@ -379,8 +395,8 @@ get_all_issues_in_milestone <- function(owner, repo, milestone_name) { page <- page + 1 } - issues <- c(open_issues, closed_issues) - info(.le$logger, glue::glue("Retrieved {length(issues)} Issue(s) from Milestone: {milestone_name}")) + issues <- get_only_ghqc_issues(c(open_issues, closed_issues)) + info(.le$logger, glue::glue("Retrieved {length(issues)} ghqc Issue(s) from Milestone: {milestone_name}")) return(issues) } diff --git a/R/multi_issue.R b/R/multi_issue.R index 4b21a0c..2c09e04 100644 --- a/R/multi_issue.R +++ b/R/multi_issue.R @@ -19,6 +19,14 @@ create_issue <- function(file, issue_params) { issue <- do.call(gh::gh, c("POST /repos/{owner}/{repo}/issues", issue_params)) debug(.le$logger, glue::glue("Created Issue {issue_params$title}")) + debug(.le$logger, glue::glue("Adding 'ghqc' label to {issue_params$title}")) + label_params <- list(owner = issue_params$owner, + repo = issue_params$repo, + issue_number = issue$number, + labels = array("ghqc")) + label <- do.call(gh::gh, c("POST /repos/{owner}/{repo}/issues/{issue_number}/labels", label_params)) + debug(.le$logger, glue::glue("Label 'ghqc' added to {issue_params$title}")) + # return the issue number list(number = issue$number, assignees = issue_params$assignees) } # create_issue @@ -71,11 +79,31 @@ create_issues <- function(data) { info(.le$logger, glue::glue("Created checklist(s) for file(s): {file_names}")) } # create_issues +ghqc_label_exists <- function(data) { + labels <- do.call(gh::gh, c("GET /repos/{owner}/{repo}/labels", + list(owner = data$owner, + repo = data$repo + ))) + "ghqc" %in% sapply(labels, function(x) x$name) +} + +create_ghqc_label <- function(data) { + issue_params <- list( + owner = data$owner, + repo = data$repo, + name = "ghqc", + color = "FFCB05", + description = "Issue created by the ghqc package" + ) + do.call(gh::gh, c("POST /repos/{owner}/{repo}/labels", issue_params)) +} + # test with "test_yamls/checklist.yaml" #' @importFrom log4r warn error info debug create_checklists <- function(yaml_path) { data <- read_and_validate_yaml(yaml_path) + if (!ghqc_label_exists(data)) labels <- create_ghqc_label(data) create_issues(data) }