From 473431c6d44066fca322c51bf4d9569060cb1d1f Mon Sep 17 00:00:00 2001 From: Thierry Onkelinx Date: Wed, 13 Jan 2021 10:41:07 +0100 Subject: [PATCH] fix create_release() --- R/set_tag.R | 38 ++++++++++++++++++++------------------ entrypoint_package.sh | 2 +- man/set_tag.Rd | 4 +++- 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/R/set_tag.R b/R/set_tag.R index 33c4bb41..b76be173 100644 --- a/R/set_tag.R +++ b/R/set_tag.R @@ -4,10 +4,11 @@ #' Otherwise it will only return a message. #' @export #' @inheritParams read_checklist +#' @param token The GitHub access token #' @importFrom assertthat assert_that #' @importFrom git2r config is_detached repository tag tags #' @family package -set_tag <- function(x = ".") { +set_tag <- function(x = ".", token) { if ( !as.logical(Sys.getenv("GITHUB_ACTIONS", "false")) || Sys.getenv("GITHUB_REF") != "refs/heads/master" || @@ -57,48 +58,49 @@ set_tag <- function(x = ".") { user.name = "Checklist bot", user.email = "checklist@inbo.be" ) - tag( - repo, - name = paste0("v", version), - message = paste(news[seq(start[current], end[current])], collapse = "\n") + tag_message <- paste(news[seq(start[current], end[current])], collapse = "\n") + tag(repo, name = paste0("v", version), message = tag_message) + cmd <- sprintf( + "cd %s; git push origin; git push origin v%s", repo$path, version ) - cmd <- sprintf("cd %s; git push origin v%s", repo$path, version) - message(cmd) system(cmd) - create_release(repo = repo, version = version, message = message) + create_release( + repo = repo, version = version, tag_message = tag_message, token = token + ) return(invisible(NULL)) } #' @importFrom httr add_headers POST #' @importFrom git2r remote_url -create_release <- function(repo, version, message) { +create_release <- function(repo, version, tag_message, token) { url <- remote_url(repo, "origin") if (!grepl("github.com", url)) { warning("no `origin` or `origin` not on GitHub.") return(invisible(NULL)) } - owner <- gsub(".*github.com:(.*?)/(.*?)\\.git", "\\1", url) - repo <- gsub(".*github.com:(.*?)/(.*?)\\.git", "\\1", url) + owner <- tolower(gsub(".*github.com:(.*?)/(.*?)\\.git", "\\1", url)) + repo <- gsub(".*github.com:(.*?)/(.*?)\\.git", "\\2", url) url <- sprintf("https://api.github.com/repos/%s/%s/releases", owner, repo) body <- c( - tag_name = paste0("v", version), - name = paste("Version", version), - body = message, + tag_name = paste0("\"v", version, "\""), + name = paste("\"Version", version, "\""), + body = paste0("\"", tag_message, "\""), draft = "false", - release = "false" + prerelease = "false" ) body <- sprintf( "{\n%s\n}", paste( - sprintf(" \"%s\" : \"%s\"", names(body), body), - collapse = "\n" + sprintf(" \"%s\" : %s", names(body), body), + collapse = ",\n" ) ) POST( url = url, config = add_headers( - "User-Agent" = "inbo/checklist package", + # "User-Agent" = "inbo/checklist package", + Authorization = paste("Bearer", token), accept = "application/vnd.github.v3+json" ), body = body, diff --git a/entrypoint_package.sh b/entrypoint_package.sh index 7d40de1f..a6c63e87 100755 --- a/entrypoint_package.sh +++ b/entrypoint_package.sh @@ -54,7 +54,7 @@ elif [ "$GITHUB_REF" != "refs/heads/master" ]; then else git checkout master echo '\nUpdating tag...\n'; - Rscript --no-save --no-restore -e 'checklist::set_tag()'; + Rscript --no-save --no-restore -e 'checklist::set_tag(token = "$2")'; echo '\nPush pkgdown website...\n' cp -R docs ../docs diff --git a/man/set_tag.Rd b/man/set_tag.Rd index b17a860a..4fce9404 100644 --- a/man/set_tag.Rd +++ b/man/set_tag.Rd @@ -4,11 +4,13 @@ \alias{set_tag} \title{Set a New Tag and Create a Release} \usage{ -set_tag(x = ".") +set_tag(x = ".", token) } \arguments{ \item{x}{Either a \code{Checklist} object or a path to the source code. Defaults to \code{.}.} + +\item{token}{The GitHub access token} } \description{ This function only works when run in a GitHub Action on the master branch.