diff --git a/.build.yml b/.build.yml index 8f950a9..4a5ad07 100644 --- a/.build.yml +++ b/.build.yml @@ -79,61 +79,15 @@ sources: - https://git.sr.ht/~hrbrmstr/sourcehut tasks: - setup: | - echo "en_US.UTF-8 UTF-8" > loc - sudo cp loc /etc/locale.gen - sudo locale-gen --purge "en_US.UTF-8" - sudo /usr/sbin/update-locale LANG=en_US.UTF-8 - export LC_ALL=en_US.UTF-8 - export LANG=en_US.UTF-8 - export LANGUAGE=en_US.UTF-8 - echo 'options(repos = c(CRAN = "https://cloud.r-project.org"))' > ~/.Rprofile - echo 'R_LIBS_USER=/home/build/packages' > ~/.Renviron - mkdir -p ~/packages ~/.R - echo "CFLAGS=" > ~/.R/Makevars - echo "CPPFLAGS= " >> ~/.R/Makevars - echo "CXXFLAGS=" >> ~/.R/Makevars - echo "FCFLAGS=" >> ~/.R/Makevars - echo "FFLAGS=" >> ~/.R/Makevars - echo "GCJFLAGS=" >> ~/.R/Makevars - echo "LDFLAGS =" >> ~/.R/Makevars - echo "OBJCFLAGS=" >> ~/.R/Makevars - echo "OBJCXXFLAGS=" >> ~/.R/Makevars - export _R_CHECK_CRAN_INCOMING_=FALSE - export _R_CHECK_URLS_USE_CURL_=FALSE - export _R_CHECK_LENGTH_1_CONDITION_=TRUE - export _R_CHECK_LENGTH_1_LOGIC2_=TRUE - export _R_CHECK_TOPLEVEL_FILES_=TRUE - export _R_CHECK_VC_DIRS_=TRUE - export _R_CHECK_TIMINGS_=10 - export _R_CHECK_INSTALL_DEPENDS_=TRUE - export _R_CHECK_SUGGESTS_ONLY_=TRUE - export _R_CHECK_NO_RECOMMENDED_=TRUE - export _R_CHECK_EXECUTABLES_EXCLUSIONS_=FALSE - export _R_CHECK_DOC_SIZES2_=TRUE - export _R_CHECK_CODE_ASSIGN_TO_GLOBALENV_=TRUE - export _R_CHECK_CODE_ATTACH_=TRUE - export _R_CHECK_CODE_DATA_INTO_GLOBALENV_=TRUE - export _R_CHECK_CODE_USAGE_VIA_NAMESPACES_=TRUE - export _R_CHECK_DOT_FIRSTLIB_=TRUE - export _R_CHECK_DEPRECATED_DEFUNCT_=TRUE - export _R_CHECK_REPLACING_IMPORTS_=TRUE - export _R_CHECK_SCREEN_DEVICE_=stop - export _R_CHECK_TOPLEVEL_FILES_=TRUE - export _R_CHECK_S3_METHODS_NOT_REGISTERED_=TRUE - export _R_CHECK_OVERWRITE_REGISTERED_S3_METHODS_=TRUE - export _R_CHECK_PRAGMAS_=TRUE - export _R_CHECK_CRAN_INCOMING_USE_ASPELL_=TRUE - export _R_CHECK_COMPILATION_FLAGS_=TRUE - export _R_CHECK_R_DEPENDS_=warn - export _R_CHECK_SERIALIZATION_=TRUE - export _R_CHECK_R_ON_PATH_=TRUE - export _R_CHECK_PACKAGES_USED_IN_TESTS_USE_SUBDIRS_=TRUE - export _R_CHECK_SHLIB_OPENMP_FLAGS_=TRUE - export _R_CHECK_CONNECTIONS_LEFT_OPEN_=TRUE - export _R_CHECK_FUTURE_FILE_TIMESTAMPS_=TRUE - export _R_CHECK_AUTOCONF_=TRUE + wget "https://git.sr.ht/~hrbrmstr/sourcehut/blob/master/inst/helpers/build-helper.sh" + chmod 755 build-helper.sh + . ./build-helper.sh + setup_locale + setup_cran_repo + setup_compiler + setup_java + - build: | - sudo R CMD javareconf - Rscript -e "devtools::install_deps(pkg = '${R_PACKAGE}', upgrade = 'never', dependencies = TRUE)" - R CMD build ${R_PACKAGE} - R CMD check --as-cran ${R_PACKAGE}_*gz + . ./build-helper.sh + install_deps + build_and_check diff --git a/DESCRIPTION b/DESCRIPTION index 0401a17..f030176 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -15,6 +15,6 @@ Encoding: UTF-8 License: MIT + file LICENSE Suggests: covr, tinytest Depends: R (>= 3.2.0) -Imports: httr, jsonlite, usethis, crayon, desc, rprojroot, glue +Imports: httr, jsonlite, usethis, crayon, desc, rprojroot, glue, utils Roxygen: list(markdown = TRUE) RoxygenNote: 6.1.1 diff --git a/NAMESPACE b/NAMESPACE index 3679961..d58dcf2 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,8 @@ # Generated by roxygen2: do not edit by hand +export(build_pkg) +export(get_build_manifest) +export(get_build_status) export(git_repos) export(git_user) export(meta_audit) @@ -8,6 +11,7 @@ export(meta_profile) export(meta_ssh_keys) export(sourcehut_pat) export(sourcehut_user) +export(start_build) export(use_builds) import(crayon) import(desc) @@ -19,3 +23,4 @@ importFrom(crayon,blue) importFrom(crayon,make_style) importFrom(desc,desc_get) importFrom(jsonlite,fromJSON) +importFrom(utils,browseURL) diff --git a/R/build-pkg.R b/R/build-pkg.R new file mode 100644 index 0000000..229bf09 --- /dev/null +++ b/R/build-pkg.R @@ -0,0 +1,102 @@ +#' Submits a build job for a package +#' +#' This function assumes you know what you're doing and expects a valid +#' `.build.yml` in the `package` directory. +#' +#' @param package path to package (assumes current directory) +#' @param note human-friendly description of this build (markdown, optional) +#' @param tags character vector of strings that identify this build and can be +#' used to navigate the dashboard. Each string must use only lowercase +#' alphanumeric characters, or any of "-_." +#' @param read_access,write_access character vector of users that have read or write access to the job. +#' @param execute start build immediately? (Default: `TRUE`) +#' @param secrets provide secrets to the build? (Default: `TRUE`) +#' @param browse open browser to the builder URL? (Default: `FALSE`) +#' @param username see [sourcehut_user()] +#' @param pat see [sourcehut_pat()] +#' @references +#' @export +build_pkg <- function(package = ".", + note = "New build", + tags = c("package", "rstats"), + read_access = NULL, + write_access = NULL, + execute = TRUE, + secrets = TRUE, + browse = FALSE, + username = sourcehut_user(), + pat = sourcehut_pat()) { + + stopifnot(is_package(package)) + + build_yml <- rprojroot::find_package_root_file(".build.yml", path = package) + build_yml <- paste0(readLines(build_yml, warn = FALSE), collapse="\n") + + httr::POST( + url = "https://builds.sr.ht/api/jobs", + body = list( + `manifest` = build_yml, + `note` = note, + `tags` = tags, + `access:read` = read_access, + `access:write` = write_access, + `execute` = execute, + `secrets` = secrets + ), + encode = "json", + sh_authorize(pat), + .SOURCEHUT_UA + ) -> res + + out <- process_response(res) + + if ("id" %in% names(out)) { + if (browse) { + browseURL(glue::glue("https://builds.sr.ht/{username}/job/{out[['id']]}")) + } + } + +} + +#' @rdname build_pkg +#' @param job_id `builds.sr.ht` job id +#' @export +get_build_status <- function(job_id, pat = sourcehut_pat()) { + + route_url <- glue::glue("https://builds.sr.ht/api/jobs/{job_id}") + + res <- make_get_request(route_url) + process_response(res) + +} + +#' @rdname build_pkg +#' @export +get_build_manifest <- function(job_id, pat = sourcehut_pat()) { + + route_url <- glue::glue("https://builds.sr.ht/api/jobs/{job_id}/manifest") + + res <- make_get_request(route_url) + + httr::stop_for_status(res) + + httr::content(res, as = "text", encoding = "UTF-8") + +} + +#' @rdname build_pkg +#' @export +start_build <- function(job_id, pat = sourcehut_pat()) { + + route_url <- glue::glue("https://builds.sr.ht/api/jobs/{job_id}/start") + + httr::POST( + url = route_url, + encode = "json", + sh_authorize(pat), + .SOURCEHUT_UA + ) -> res + + process_response(res) + +} diff --git a/R/sourcehut-package.R b/R/sourcehut-package.R index af75be9..23982cd 100644 --- a/R/sourcehut-package.R +++ b/R/sourcehut-package.R @@ -10,4 +10,5 @@ #' @author Bob Rudis (bob@@rud.is) #' @import httr usethis crayon desc rprojroot glue #' @importFrom jsonlite fromJSON +#' @importFrom utils browseURL "_PACKAGE" diff --git a/README.md b/README.md index a67eda1..92a0e33 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ wide variety of SourceHut servies. The following functions are implemented: + - `build_pkg`: Submits a build job for a package - `git_repos`: Retrieve metadata about repositories - `git_user`: Retrieve metadata about yourself or another SourceHut user @@ -70,8 +71,8 @@ packageVersion("sourcehut") | Lang | \# Files | (%) | LoC | (%) | Blank lines | (%) | \# Lines | (%) | | :--- | -------: | ---: | --: | ---: | ----------: | ---: | -------: | ---: | -| R | 13 | 0.93 | 155 | 0.95 | 67 | 0.82 | 72 | 0.72 | -| Rmd | 1 | 0.07 | 8 | 0.05 | 15 | 0.18 | 28 | 0.28 | +| R | 14 | 0.93 | 211 | 0.96 | 88 | 0.85 | 96 | 0.77 | +| Rmd | 1 | 0.07 | 8 | 0.04 | 15 | 0.15 | 28 | 0.23 | ## Code of Conduct diff --git a/inst/helpers/build-helper.sh b/inst/helpers/build-helper.sh new file mode 100644 index 0000000..0457891 --- /dev/null +++ b/inst/helpers/build-helper.sh @@ -0,0 +1,111 @@ +#!/bin/bash +# -*- sh-basic-offset: 2; sh-indentation: 2 -*- +# Helpers for running R pkg builds on builds.sr.ht +# +# https://git.sr.ht/~hrbrmstr/sourcehut/blob/master/inst/helpers/build-helper.sh + +setup_locale() { + + echo "en_US.UTF-8 UTF-8" > loc + + sudo cp loc /etc/locale.gen + sudo locale-gen --purge "en_US.UTF-8" + sudo /usr/sbin/update-locale LANG=en_US.UTF-8 + + export LC_ALL=en_US.UTF-8 + export LANG=en_US.UTF-8 + export LANGUAGE=en_US.UTF-8 + + rm -f loc + +} + +setup_cran_repo() { + + mkdir -p ~/packages + + echo 'options(repos = c(CRAN = "https://cloud.r-project.org"))' > ~/.Rprofile + echo 'R_LIBS_USER=/home/build/packages' > ~/.Renviron + +} + +setup_compiler() { + + mkdir ~/.R + echo "CFLAGS=" > ~/.R/Makevars + echo "CPPFLAGS= " >> ~/.R/Makevars + echo "CXXFLAGS=" >> ~/.R/Makevars + echo "FCFLAGS=" >> ~/.R/Makevars + echo "FFLAGS=" >> ~/.R/Makevars + echo "GCJFLAGS=" >> ~/.R/Makevars + echo "LDFLAGS =" >> ~/.R/Makevars + echo "OBJCFLAGS=" >> ~/.R/Makevars + echo "OBJCXXFLAGS=" >> ~/.R/Makevars + +} + +setup_java() { + + export LC_ALL=en_US.UTF-8 + export LANG=en_US.UTF-8 + export LANGUAGE=en_US.UTF-8 + + sudo R CMD javareconf + +} + +install_deps() { + + export LC_ALL=en_US.UTF-8 + export LANG=en_US.UTF-8 + export LANGUAGE=en_US.UTF-8 + + Rscript -e "devtools::install_deps(pkg = '${R_PACKAGE}', upgrade = 'never', dependencies = TRUE)" + +} + +build_and_check() { + + export LC_ALL=en_US.UTF-8 + export LANG=en_US.UTF-8 + export LANGUAGE=en_US.UTF-8 + + export _R_CHECK_CRAN_INCOMING_=FALSE + export _R_CHECK_URLS_USE_CURL_=FALSE + export _R_CHECK_LENGTH_1_CONDITION_=TRUE + export _R_CHECK_LENGTH_1_LOGIC2_=TRUE + export _R_CHECK_TOPLEVEL_FILES_=TRUE + export _R_CHECK_VC_DIRS_=TRUE + export _R_CHECK_TIMINGS_=10 + export _R_CHECK_INSTALL_DEPENDS_=TRUE + export _R_CHECK_SUGGESTS_ONLY_=TRUE + export _R_CHECK_NO_RECOMMENDED_=TRUE + export _R_CHECK_EXECUTABLES_EXCLUSIONS_=FALSE + export _R_CHECK_DOC_SIZES2_=TRUE + export _R_CHECK_CODE_ASSIGN_TO_GLOBALENV_=TRUE + export _R_CHECK_CODE_ATTACH_=TRUE + export _R_CHECK_CODE_DATA_INTO_GLOBALENV_=TRUE + export _R_CHECK_CODE_USAGE_VIA_NAMESPACES_=TRUE + export _R_CHECK_DOT_FIRSTLIB_=TRUE + export _R_CHECK_DEPRECATED_DEFUNCT_=TRUE + export _R_CHECK_REPLACING_IMPORTS_=TRUE + export _R_CHECK_SCREEN_DEVICE_=stop + export _R_CHECK_TOPLEVEL_FILES_=TRUE + export _R_CHECK_S3_METHODS_NOT_REGISTERED_=TRUE + export _R_CHECK_OVERWRITE_REGISTERED_S3_METHODS_=TRUE + export _R_CHECK_PRAGMAS_=TRUE + export _R_CHECK_CRAN_INCOMING_USE_ASPELL_=TRUE + export _R_CHECK_COMPILATION_FLAGS_=TRUE + export _R_CHECK_R_DEPENDS_=warn + export _R_CHECK_SERIALIZATION_=TRUE + export _R_CHECK_R_ON_PATH_=TRUE + export _R_CHECK_PACKAGES_USED_IN_TESTS_USE_SUBDIRS_=TRUE + export _R_CHECK_SHLIB_OPENMP_FLAGS_=TRUE + export _R_CHECK_CONNECTIONS_LEFT_OPEN_=TRUE + export _R_CHECK_FUTURE_FILE_TIMESTAMPS_=TRUE + export _R_CHECK_AUTOCONF_=TRUE + + R CMD build ${R_PACKAGE} + R CMD check --as-cran ${R_PACKAGE}_*gz + +} \ No newline at end of file diff --git a/inst/templates/build.yml b/inst/templates/build.yml index 5ef0011..b050361 100644 --- a/inst/templates/build.yml +++ b/inst/templates/build.yml @@ -79,61 +79,15 @@ sources: - https://git.sr.ht/{{{username}}}/{{{pkg}}} tasks: - setup: | - echo "en_US.UTF-8 UTF-8" > loc - sudo cp loc /etc/locale.gen - sudo locale-gen --purge "en_US.UTF-8" - sudo /usr/sbin/update-locale LANG=en_US.UTF-8 - export LC_ALL=en_US.UTF-8 - export LANG=en_US.UTF-8 - export LANGUAGE=en_US.UTF-8 - echo 'options(repos = c(CRAN = "https://cloud.r-project.org"))' > ~/.Rprofile - echo 'R_LIBS_USER=/home/build/packages' > ~/.Renviron - mkdir -p ~/packages ~/.R - echo "CFLAGS=" > ~/.R/Makevars - echo "CPPFLAGS= " >> ~/.R/Makevars - echo "CXXFLAGS=" >> ~/.R/Makevars - echo "FCFLAGS=" >> ~/.R/Makevars - echo "FFLAGS=" >> ~/.R/Makevars - echo "GCJFLAGS=" >> ~/.R/Makevars - echo "LDFLAGS =" >> ~/.R/Makevars - echo "OBJCFLAGS=" >> ~/.R/Makevars - echo "OBJCXXFLAGS=" >> ~/.R/Makevars - export _R_CHECK_CRAN_INCOMING_=FALSE - export _R_CHECK_URLS_USE_CURL_=FALSE - export _R_CHECK_LENGTH_1_CONDITION_=TRUE - export _R_CHECK_LENGTH_1_LOGIC2_=TRUE - export _R_CHECK_TOPLEVEL_FILES_=TRUE - export _R_CHECK_VC_DIRS_=TRUE - export _R_CHECK_TIMINGS_=10 - export _R_CHECK_INSTALL_DEPENDS_=TRUE - export _R_CHECK_SUGGESTS_ONLY_=TRUE - export _R_CHECK_NO_RECOMMENDED_=TRUE - export _R_CHECK_EXECUTABLES_EXCLUSIONS_=FALSE - export _R_CHECK_DOC_SIZES2_=TRUE - export _R_CHECK_CODE_ASSIGN_TO_GLOBALENV_=TRUE - export _R_CHECK_CODE_ATTACH_=TRUE - export _R_CHECK_CODE_DATA_INTO_GLOBALENV_=TRUE - export _R_CHECK_CODE_USAGE_VIA_NAMESPACES_=TRUE - export _R_CHECK_DOT_FIRSTLIB_=TRUE - export _R_CHECK_DEPRECATED_DEFUNCT_=TRUE - export _R_CHECK_REPLACING_IMPORTS_=TRUE - export _R_CHECK_SCREEN_DEVICE_=stop - export _R_CHECK_TOPLEVEL_FILES_=TRUE - export _R_CHECK_S3_METHODS_NOT_REGISTERED_=TRUE - export _R_CHECK_OVERWRITE_REGISTERED_S3_METHODS_=TRUE - export _R_CHECK_PRAGMAS_=TRUE - export _R_CHECK_CRAN_INCOMING_USE_ASPELL_=TRUE - export _R_CHECK_COMPILATION_FLAGS_=TRUE - export _R_CHECK_R_DEPENDS_=warn - export _R_CHECK_SERIALIZATION_=TRUE - export _R_CHECK_R_ON_PATH_=TRUE - export _R_CHECK_PACKAGES_USED_IN_TESTS_USE_SUBDIRS_=TRUE - export _R_CHECK_SHLIB_OPENMP_FLAGS_=TRUE - export _R_CHECK_CONNECTIONS_LEFT_OPEN_=TRUE - export _R_CHECK_FUTURE_FILE_TIMESTAMPS_=TRUE - export _R_CHECK_AUTOCONF_=TRUE + wget "https://git.sr.ht/~hrbrmstr/sourcehut/blob/master/inst/helpers/build-helper.sh" + chmod 755 build-helper.sh + . ./build-helper.sh + setup_locale + setup_cran_repo + setup_compiler + setup_java + - build: | - sudo R CMD javareconf - Rscript -e "devtools::install_deps(pkg = '${R_PACKAGE}', upgrade = 'never', dependencies = TRUE)" - R CMD build ${R_PACKAGE} - R CMD check --as-cran ${R_PACKAGE}_*gz + . ./build-helper.sh + install_deps + build_and_check diff --git a/man/build_pkg.Rd b/man/build_pkg.Rd new file mode 100644 index 0000000..db5fb5e --- /dev/null +++ b/man/build_pkg.Rd @@ -0,0 +1,50 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/build-pkg.R +\name{build_pkg} +\alias{build_pkg} +\alias{get_build_status} +\alias{get_build_manifest} +\alias{start_build} +\title{Submits a build job for a package} +\usage{ +build_pkg(package = ".", note = "New build", tags = c("package", + "rstats"), read_access = NULL, write_access = NULL, execute = TRUE, + secrets = TRUE, browse = FALSE, username = sourcehut_user(), + pat = sourcehut_pat()) + +get_build_status(job_id, pat = sourcehut_pat()) + +get_build_manifest(job_id, pat = sourcehut_pat()) + +start_build(job_id, pat = sourcehut_pat()) +} +\arguments{ +\item{package}{path to package (assumes current directory)} + +\item{note}{human-friendly description of this build (markdown, optional)} + +\item{tags}{character vector of strings that identify this build and can be +used to navigate the dashboard. Each string must use only lowercase +alphanumeric characters, or any of "-_."} + +\item{read_access, write_access}{character vector of users that have read or write access to the job.} + +\item{execute}{start build immediately? (Default: \code{TRUE})} + +\item{secrets}{provide secrets to the build? (Default: \code{TRUE})} + +\item{browse}{open browser to the builder URL? (Default: \code{FALSE})} + +\item{username}{see \code{\link[=sourcehut_user]{sourcehut_user()}}} + +\item{pat}{see \code{\link[=sourcehut_pat]{sourcehut_pat()}}} + +\item{job_id}{\code{builds.sr.ht} job id} +} +\description{ +This function assumes you know what you're doing and expects a valid +\code{.build.yml} in the \code{package} directory. +} +\references{ +\url{https://man.sr.ht/builds.sr.ht/api.md} +}