Skip to content

Instantly share code, notes, and snippets.

@adamrobinson361
Last active July 18, 2019 19:04
Show Gist options
  • Save adamrobinson361/1ac092b8aa77c1610887ce62fe5ae0e5 to your computer and use it in GitHub Desktop.
Save adamrobinson361/1ac092b8aa77c1610887ce62fe5ae0e5 to your computer and use it in GitHub Desktop.
Interacting with azure devops api from R
library(httr)
library(RCurl)
library(dplyr)
devops_auth <- function(username, password = rstudioapi::askForPassword()){
paste("Basic", RCurl::base64(paste(username, password, sep = ":")))
}
devops_get_projects <- function(domain, auth){
url <- paste0("https://dev.azure.com/",domain,"/_apis/projects?api-version=5.0")
response <- httr::GET(url, httr::add_headers(Authorization = auth))
content(response,as = 'text', encoding = 'UTF-8') %>%
jsonlite::fromJSON(., flatten = TRUE) %>%
.$value
}
devops_get_repos <- function(domain, project, auth){
url <- paste0("https://dev.azure.com/",domain,"/", project, "/_apis/git/repositories?api-version=5.0")
response <- httr::GET(url, httr::add_headers(Authorization = auth))
content(response,as = 'text', encoding = 'UTF-8') %>%
jsonlite::fromJSON(., flatten = TRUE) %>%
.$value
}
devops_get_builds <- function(domain, project, auth){
url <- paste0("https://dev.azure.com/",domain,"/", project, "/_apis/build/builds?api-version=5.0")
response <- httr::GET(url, httr::add_headers(Authorization = auth))
content(response,as = 'text', encoding = 'UTF-8') %>%
jsonlite::fromJSON(., flatten = TRUE) %>%
.$value
}
devops_get_build_defs <- function(domain, project, auth){
url <- paste0("https://dev.azure.com/",domain,"/", project, "/_apis/build/definitions?api-version=5.0")
response <- httr::GET(url, httr::add_headers(Authorization = auth))
content(response,as = 'text', encoding = 'UTF-8') %>%
jsonlite::fromJSON(., flatten = TRUE) %>%
.$value
}
devops_queue_build <- function(domain, project, buildid, auth){
uri <- paste0("https://dev.azure.com/", domain,"/", project, "/_apis/build/builds?api-version=5.0")
content_body <- jsonlite::toJSON(list(definition = list(id = buildid)), auto_unbox = TRUE)
httr::POST(uri, httr::add_headers(Authorization = auth), httr::content_type("application/json"), body = content_body)
}
# Set up authentication
auth <- devops_auth("adamrobinson361@gmail.com")
# Specify Domain
domain = "arobinson361"
# Get table of all projects
projects <- devops_get_projects(domain, auth)
# Get repos for all projects
repos <- lapply(projects$name, function(project){devops_get_repos(domain, project, auth)}) %>%
bind_rows()
# Get build defs for all projects
build_defs <- lapply(projects$name, function(project){devops_get_build_defs(domain, project, auth)}) %>%
bind_rows()
# Get builds for all projects
builds <- lapply(projects$name, function(project){devops_get_builds(domain, project, auth)}) %>%
bind_rows()
# Dummy project
project <- "docker-deployment"
# Dummy build id
buildid <- 3
# Queue build
devops_queue_build(domain, project, buildid, auth)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment