tests/testthat/test_gitlab_api.R

single_list <- list(
  id = 301L, description = NULL, name = "theproject", name_with_namespace = "thegroup / theproject",
  path = "theproject", path_with_namespace = "thegroup/theproject",
  created_at = "2021-04-20T12:25:29.283Z", default_branch = NULL,
  tag_list = list(), ssh_url_to_repo = "ssh://git@gitlab.com/thegroup/theproject.git",
  http_url_to_repo = "https://gitlab.com/thegroup/theproject.git",
  web_url = "https://gitlab.com/thegroup/theproject", readme_url = NULL,
  avatar_url = NULL, forks_count = 0L, star_count = 0L, last_activity_at = "2021-04-20T12:25:29.283Z",
  namespace = list(
    id = 3L, name = "thegroup", path = "thegroup",
    kind = "group", full_path = "thegroup", parent_id = NULL,
    avatar_url = NULL, web_url = "https://gitlab.com/groups/thegroup"
  ),
  `_links` = list(
    self = "https://gitlab.com/api/v4/projects/301",
    issues = "https://gitlab.com/api/v4/projects/301/issues",
    merge_requests = "https://gitlab.com/api/v4/projects/301/merge_requests",
    repo_branches = "https://gitlab.com/api/v4/projects/301/repository/branches",
    labels = "https://gitlab.com/api/v4/projects/301/labels",
    events = "https://gitlab.com/api/v4/projects/301/events",
    members = "https://gitlab.com/api/v4/projects/301/members"
  ),
  packages_enabled = TRUE, empty_repo = TRUE, archived = FALSE,
  visibility = "private", resolve_outdated_diff_discussions = FALSE,
  container_registry_enabled = TRUE, container_expiration_policy = list(
    cadence = "1d", enabled = TRUE, keep_n = 10L, older_than = "90d",
    name_regex = NULL, name_regex_keep = NULL, next_run_at = "2021-04-21T12:25:29.298Z"
  ),
  issues_enabled = TRUE, merge_requests_enabled = TRUE, wiki_enabled = TRUE,
  jobs_enabled = TRUE, snippets_enabled = TRUE, service_desk_enabled = FALSE,
  service_desk_address = NULL, can_create_merge_request_in = TRUE,
  issues_access_level = "enabled", repository_access_level = "enabled",
  merge_requests_access_level = "enabled", forking_access_level = "enabled",
  wiki_access_level = "enabled", builds_access_level = "enabled",
  snippets_access_level = "enabled", pages_access_level = "private",
  emails_disabled = NULL, shared_runners_enabled = TRUE, lfs_enabled = TRUE,
  creator_id = 6L, import_status = "none", import_error = NULL,
  open_issues_count = 0L, runners_token = "gFZwfULZda5VgT7kJCEB",
  ci_default_git_depth = 50L, public_jobs = TRUE, build_git_strategy = "fetch",
  build_timeout = 3600L, auto_cancel_pending_pipelines = "enabled",
  build_coverage_regex = NULL, ci_config_path = NULL, shared_with_groups = list(),
  only_allow_merge_if_pipeline_succeeds = FALSE, allow_merge_on_skipped_pipeline = NULL,
  request_access_enabled = TRUE, only_allow_merge_if_all_discussions_are_resolved = FALSE,
  remove_source_branch_after_merge = TRUE, printing_merge_request_link_enabled = TRUE,
  merge_method = "merge", suggestion_commit_message = NULL,
  auto_devops_enabled = TRUE, auto_devops_deploy_strategy = "continuous",
  autoclose_referenced_issues = TRUE, repository_storage = "default",
  permissions = list(project_access = NULL, group_access = list(
    access_level = 50L, notification_level = 3L
  ))
)

double_list <- list(
  list(
    id = 26976100L, description = "Progreso continuo del curso de React",
    name = "Curso React", name_with_namespace = "Andres Vega / Curso React",
    path = "curso-react", path_with_namespace = "Sulfur0/curso-react",
    created_at = "2021-05-27T19:35:08.305Z", default_branch = "main",
    tag_list = list(), ssh_url_to_repo = "git@gitlab.com:Sulfur0/curso-react.git",
    http_url_to_repo = "https://gitlab.com/Sulfur0/curso-react.git",
    web_url = "https://gitlab.com/Sulfur0/curso-react", readme_url = NULL,
    avatar_url = NULL, forks_count = 0L, star_count = 0L, last_activity_at = "2021-05-27T19:35:08.305Z",
    namespace = list(
      id = 5906058L, name = "Andres Vega", path = "Sulfur0",
      kind = "user", full_path = "Sulfur0", parent_id = NULL,
      avatar_url = "https://secure.gravatar.com/avatar/2e703fbc0fad375b351817201b04a423?s=80&d=identicon",
      web_url = "https://gitlab.com/Sulfur0"
    ), container_registry_image_prefix = "registry.gitlab.com/sulfur0/curso-react",
    `_links` = list(
      self = "https://gitlab.com/api/v4/projects/26976100",
      issues = "https://gitlab.com/api/v4/projects/26976100/issues",
      merge_requests = "https://gitlab.com/api/v4/projects/26976100/merge_requests",
      repo_branches = "https://gitlab.com/api/v4/projects/26976100/repository/branches",
      labels = "https://gitlab.com/api/v4/projects/26976100/labels",
      events = "https://gitlab.com/api/v4/projects/26976100/events",
      members = "https://gitlab.com/api/v4/projects/26976100/members"
    ),
    packages_enabled = TRUE, empty_repo = TRUE, archived = FALSE,
    visibility = "public", owner = list(
      id = 4499552L, name = "Andres Vega",
      username = "Sulfur0", state = "active", avatar_url = "https://secure.gravatar.com/avatar/2e703fbc0fad375b351817201b04a423?s=80&d=identicon",
      web_url = "https://gitlab.com/Sulfur0"
    ), resolve_outdated_diff_discussions = FALSE,
    container_registry_enabled = TRUE, container_expiration_policy = list(
      cadence = "1d", enabled = FALSE, keep_n = 10L, older_than = "90d",
      name_regex = ".*", name_regex_keep = NULL, next_run_at = "2021-05-28T19:35:08.345Z"
    ),
    issues_enabled = TRUE, merge_requests_enabled = TRUE, wiki_enabled = TRUE,
    jobs_enabled = TRUE, snippets_enabled = TRUE, service_desk_enabled = TRUE,
    service_desk_address = "incoming+sulfur0-curso-react-26976100-issue-@incoming.gitlab.com",
    can_create_merge_request_in = TRUE, issues_access_level = "enabled",
    repository_access_level = "enabled", merge_requests_access_level = "enabled",
    forking_access_level = "enabled", wiki_access_level = "enabled",
    builds_access_level = "enabled", snippets_access_level = "enabled",
    pages_access_level = "enabled", operations_access_level = "enabled",
    analytics_access_level = "enabled", emails_disabled = NULL,
    shared_runners_enabled = TRUE, lfs_enabled = TRUE, creator_id = 4499552L,
    import_status = "none", open_issues_count = 0L, ci_default_git_depth = 50L,
    ci_forward_deployment_enabled = TRUE, public_jobs = TRUE,
    build_timeout = 3600L, auto_cancel_pending_pipelines = "enabled",
    build_coverage_regex = NULL, ci_config_path = "", shared_with_groups = list(),
    only_allow_merge_if_pipeline_succeeds = FALSE, allow_merge_on_skipped_pipeline = NULL,
    restrict_user_defined_variables = FALSE, request_access_enabled = TRUE,
    only_allow_merge_if_all_discussions_are_resolved = FALSE,
    remove_source_branch_after_merge = TRUE, printing_merge_request_link_enabled = TRUE,
    merge_method = "merge", suggestion_commit_message = NULL,
    auto_devops_enabled = FALSE, auto_devops_deploy_strategy = "continuous",
    autoclose_referenced_issues = TRUE, approvals_before_merge = 0L,
    mirror = FALSE, external_authorization_classification_label = "",
    marked_for_deletion_at = NULL, marked_for_deletion_on = NULL,
    requirements_enabled = TRUE, security_and_compliance_enabled = FALSE,
    compliance_frameworks = list(), issues_template = NULL, merge_requests_template = NULL,
    permissions = list(project_access = NULL, group_access = NULL)
  ),
  list(
    id = 26976099L, description = "Learning GitLab xavki",
    name = "DanprasGitLabDemo", name_with_namespace = "daniel prasanth / DanprasGitLabDemo",
    path = "danprasgitlabdemo", path_with_namespace = "danprasfr/danprasgitlabdemo",
    created_at = "2021-05-27T19:35:07.189Z", default_branch = "main",
    tag_list = list(), ssh_url_to_repo = "git@gitlab.com:danprasfr/danprasgitlabdemo.git",
    http_url_to_repo = "https://gitlab.com/danprasfr/danprasgitlabdemo.git",
    web_url = "https://gitlab.com/danprasfr/danprasgitlabdemo",
    readme_url = NULL, avatar_url = NULL, forks_count = 0L,
    star_count = 0L, last_activity_at = "2021-05-27T19:35:07.189Z",
    namespace = list(
      id = 12211549L, name = "daniel prasanth",
      path = "danprasfr", kind = "user", full_path = "danprasfr",
      parent_id = NULL, avatar_url = "https://secure.gravatar.com/avatar/56922758357b6f9ff1ac6fc763590a2b?s=80&d=identicon",
      web_url = "https://gitlab.com/danprasfr"
    ), container_registry_image_prefix = "registry.gitlab.com/danprasfr/danprasgitlabdemo",
    `_links` = list(
      self = "https://gitlab.com/api/v4/projects/26976099",
      issues = "https://gitlab.com/api/v4/projects/26976099/issues",
      merge_requests = "https://gitlab.com/api/v4/projects/26976099/merge_requests",
      repo_branches = "https://gitlab.com/api/v4/projects/26976099/repository/branches",
      labels = "https://gitlab.com/api/v4/projects/26976099/labels",
      events = "https://gitlab.com/api/v4/projects/26976099/events",
      members = "https://gitlab.com/api/v4/projects/26976099/members"
    ),
    packages_enabled = TRUE, empty_repo = TRUE, archived = FALSE,
    visibility = "public", owner = list(
      id = 8980005L, name = "daniel prasanth",
      username = "danprasfr", state = "active", avatar_url = "https://secure.gravatar.com/avatar/56922758357b6f9ff1ac6fc763590a2b?s=80&d=identicon",
      web_url = "https://gitlab.com/danprasfr"
    ), resolve_outdated_diff_discussions = FALSE,
    container_registry_enabled = TRUE, container_expiration_policy = list(
      cadence = "1d", enabled = FALSE, keep_n = 10L, older_than = "90d",
      name_regex = ".*", name_regex_keep = NULL, next_run_at = "2021-05-28T19:35:07.218Z"
    ),
    issues_enabled = TRUE, merge_requests_enabled = TRUE,
    wiki_enabled = TRUE, jobs_enabled = TRUE, snippets_enabled = TRUE,
    service_desk_enabled = TRUE, service_desk_address = "incoming+danprasfr-danprasgitlabdemo-26976099-issue-@incoming.gitlab.com",
    can_create_merge_request_in = TRUE, issues_access_level = "enabled",
    repository_access_level = "enabled", merge_requests_access_level = "enabled",
    forking_access_level = "enabled", wiki_access_level = "enabled",
    builds_access_level = "enabled", snippets_access_level = "enabled",
    pages_access_level = "enabled", operations_access_level = "enabled",
    analytics_access_level = "enabled", emails_disabled = NULL,
    shared_runners_enabled = TRUE, lfs_enabled = TRUE, creator_id = 8980005L,
    import_status = "none", open_issues_count = 0L, ci_default_git_depth = 50L,
    ci_forward_deployment_enabled = TRUE, public_jobs = TRUE,
    build_timeout = 3600L, auto_cancel_pending_pipelines = "enabled",
    build_coverage_regex = NULL, ci_config_path = "", shared_with_groups = list(),
    only_allow_merge_if_pipeline_succeeds = FALSE, allow_merge_on_skipped_pipeline = NULL,
    restrict_user_defined_variables = FALSE, request_access_enabled = TRUE,
    only_allow_merge_if_all_discussions_are_resolved = FALSE,
    remove_source_branch_after_merge = TRUE, printing_merge_request_link_enabled = TRUE,
    merge_method = "merge", suggestion_commit_message = NULL,
    auto_devops_enabled = FALSE, auto_devops_deploy_strategy = "continuous",
    autoclose_referenced_issues = TRUE, approvals_before_merge = 0L,
    mirror = FALSE, external_authorization_classification_label = "",
    marked_for_deletion_at = NULL, marked_for_deletion_on = NULL,
    requirements_enabled = TRUE, security_and_compliance_enabled = FALSE,
    compliance_frameworks = list(), issues_template = NULL,
    merge_requests_template = NULL, permissions = list(
      project_access = NULL,
      group_access = NULL
    )
  )
)


# Function to correct - behaviour is not the expected one for now
test_that("is_single_row works in this case", {
  expect_true(gitlabr:::is_single_row(single_list))
  expect_false(gitlabr:::is_single_row(double_list))
})

# test that it works for a single project
project_info <- gitlab(
  req = paste0("projects/", test_project_id),
  verb = httr::GET
)

# Multiple lines
gl_list_projects_output_raw <- gl_list_projects(max_page = 1)

test_that("is_single_row works in this case", {
  expect_true(nrow(project_info) == 1)
  expect_true(nrow(gl_list_projects_output_raw) > 1)
})

# Pass an array of parameters
# For instance for issues, that should be:
# GET /issues?iids[]=42&iids[]=43
test_that("gitlab() works with an array of parameters", {
  expect_true(
    nrow(gitlab(c("projects", test_project_id, "issues"),
      `iids[]` = 1
    )) == 1
  )
  expect_true(
    nrow(gitlab(c("projects", test_project_id, "issues"),
      `iids[]` = 2
    )) == 0
  )
  expect_true(
    nrow(gitlab(c("projects", test_project_id, "issues"),
      `iids[]` = c(1, 2)
    )) == 1
  )

  # Create an issue
  new_issue_infos <- gl_new_issue(test_project_id, "test issue for array")
  new_issue_iid <- new_issue_infos$iid[1]

  expect_true(
    nrow(gitlab(c("projects", test_project_id, "issues"),
      `iids[]` = c(1, new_issue_iid)
    )) == 2
  )

  # delete issue
  gl_delete_issue(test_project_id, new_issue_iid)
  # clean state
  all_issues <- gl_list_issues(test_project_id, max_page = 1)
  expect_false(any(all_issues$iid == new_issue_iid))
  expect_equal(nrow(all_issues), 1)
})

test_that("flattenbody works", {
  expect_true(
    all.equal(
      flattenbody(list(iids = 1)),
      list("iids" = 1)
    )
  )
  expect_true(
    all.equal(
      flattenbody(list(iids = c(1, 2))),
      list("iids" = 1, iids = 2)
    )
  )
  expect_true(
    all.equal(
      flattenbody(list(iids = c(1, 2), toto = "a")),
      list("iids" = 1, iids = 2, toto = "a")
    )
  )
})

Try the gitlabr package in your browser

Any scripts or data that you put into this service are public.

gitlabr documentation built on June 22, 2024, 11:34 a.m.