Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some tests and github actions #2

Merged
merged 5 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@
^\.Rproj\.user$
^LICENSE\.md$
^updateme-demo\.gif$
^\.github$
^_pkgdown\.yml$
^docs$
^pkgdown$
1 change: 1 addition & 0 deletions .github/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.html
49 changes: 49 additions & 0 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

name: R-CMD-check

jobs:
R-CMD-check:
runs-on: ${{ matrix.config.os }}

name: ${{ matrix.config.os }} (${{ matrix.config.r }})

strategy:
fail-fast: false
matrix:
config:
- {os: macos-latest, r: 'release'}
- {os: windows-latest, r: 'release'}
- {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
- {os: ubuntu-latest, r: 'release'}
- {os: ubuntu-latest, r: 'oldrel-1'}

env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
R_KEEP_PKG_SOURCE: yes

steps:
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-pandoc@v2

- uses: r-lib/actions/setup-r@v2
with:
r-version: ${{ matrix.config.r }}
http-user-agent: ${{ matrix.config.http-user-agent }}
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::rcmdcheck
needs: check

- uses: r-lib/actions/check-r-package@v2
with:
upload-snapshots: true
48 changes: 48 additions & 0 deletions .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]
release:
types: [published]
workflow_dispatch:

name: pkgdown

jobs:
pkgdown:
runs-on: ubuntu-latest
# Only restrict concurrency for non-PR jobs
concurrency:
group: pkgdown-${{ github.event_name != 'pull_request' || github.run_id }}
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
permissions:
contents: write
steps:
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-pandoc@v2

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::pkgdown, local::.
needs: website

- name: Build site
run: pkgdown::build_site_github_pages(new_process = FALSE, install = FALSE)
shell: Rscript {0}

- name: Deploy to GitHub pages 🚀
if: github.event_name != 'pull_request'
uses: JamesIves/github-pages-deploy-action@v4.5.0
with:
clean: false
branch: gh-pages
folder: docs
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
**/.DS_Store

# History files
.Rhistory
.Rapp.history
Expand Down Expand Up @@ -47,3 +49,4 @@ po/*~

# RStudio Connect folder
rsconnect/
docs
7 changes: 4 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@ Description: When a package is loaded, the source repository is checked for
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
URL: https://github.com/wurli/updateme
URL: https://github.com/wurli/updateme, https://wurli.github.io/updateme/
BugReports: https://github.com/wurli/updateme/issues
Imports:
cachem,
cli,
curl,
memoise,
rlang,
utils
RoxygenNote: 7.2.3
Suggests:
curl,
gitcreds,
knitr,
rmarkdown,
testthat (>= 3.0.0)
testthat (>= 3.0.0),
withr
VignetteBuilder: knitr
Roxygen: list(markdown = TRUE)
Config/testthat/edition: 3
113 changes: 68 additions & 45 deletions R/available_version.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,51 +118,11 @@ available_version_impl_repo <- function(pkg, repo = NULL) {
}


available_version_impl_github <- function(pkg, username, repo, use_curl = TRUE) {
file_url <- paste0(
"https://raw.githubusercontent.com/",
username, "/", repo,
"/HEAD/DESCRIPTION"
)

if (use_curl %||% is_installed("curl")) {
handle <- curl::new_handle()
pat <- get_github_pat()
if (!is.null(pat))
curl::handle_setheaders(handle, Authorization = paste("token", get_github_pat()))
con <- curl::curl(file_url, handle = handle)
} else {
con <- url(file_url)
}

response <- tryCatch(
readLines(con, warn = FALSE),
error = function(e) {
msg <- e$message

private_repo_msg <- if (is.null(get_github_pat()))
"If the repo is private, consider setting a PAT using {.fun gitcreds::gitcreds_set}"

no_curl_msg <- if (!is_installed("curl"))
"Using private repos requires {.pkg curl} to be installed"

warning_msg <- if (grepl("404", msg))
c("{.val 404} error: DESCRIPTION not found", i = private_repo_msg, i = no_curl_msg)
else if (grepl("302", msg))
"{.val 302} response: not yet implemented" # TODO
else if (grepl("403", msg))
"{.val 403} error: access forbidden"
else
"DESCRIPTION file not accessible: {msg}"

cli::cli_warn(c(warning_msg, i = "Error occurred accessing URL {.url {file_url}}"))

NULL
}
)
available_version_impl_github <- function(pkg, username, repo) {
response <- desc_from_github(username, repo, pkg)

if (is.null(response))
return(NULL)
return(response)

desc <- parse_description(response)
github_name <- desc[["Package"]] %||% pkg
Expand All @@ -187,9 +147,72 @@ available_version_impl_github <- function(pkg, username, repo, use_curl = TRUE)

}

desc_from_github <- function(username, repo, pkg = repo, use_curl = TRUE) {
file_url <- paste0(
"https://raw.githubusercontent.com/",
username, "/", repo,
"/HEAD/DESCRIPTION"
)

handle <- curl::new_handle()
pat <- get_github_pat()

if (!is.null(pat))
curl::handle_setheaders(handle, Authorization = paste("token", get_github_pat()))

con <- curl::curl(file_url, handle = handle)

tryCatch(
readLines(con, warn = FALSE),
error = function(e) {
msg <- e$message

private_repo_msg <- if (is.null(get_github_pat()))
"If the repo is private, consider setting a PAT using {.fun gitcreds::gitcreds_set}"

warning_msg <- if (grepl("404", msg)) {
c(
i = "{.val 404} error: DESCRIPTION not found",
i = private_repo_msg,
i = paste(
"Is the repo private? Perhaps you need to configure",
"an {.topic [access token](updateme::`private-repos`)}."
)
)
} else if (grepl("302", msg)) {
c(i = "{.val 302} response: not yet implemented") # TODO
} else if (grepl("403", msg)) {
c(i = "{.val 403} error: access forbidden")
} else {
c(i = "DESCRIPTION file not accessible: {msg}")
}

cli::cli_warn(c(
"Failed attempting to get a package version for {.pkg {pkg}} from GitHub",
warning_msg,
i = "Error occurred accessing URL {.url {file_url}}"
))

NULL
}
)
}

get_github_pat <- function() {
if (is_installed("gitcreds"))
return(gitcreds::gitcreds_get()[["password"]])
# 1. check special updateme env var
updateme_github_pat <- env_var("UPDATEME_GITHUB_PAT")
if (!is.null(updateme_github_pat))
return(updateme_github_pat)

# 2. check w/{gitcreds} pkg
if (is_installed("gitcreds")) {
# gitcreds may error if no git installed, no creds set, etc
try(silent = TRUE, {
pat <- gitcreds::gitcreds_get()[["password"]]
return(pat)
})
}

# 3. Check standard env vars
env_var("GITHUB_PAT") %||% env_var("GITHUB_TOKEN")
}
70 changes: 0 additions & 70 deletions R/inform_load.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,76 +66,6 @@ available_packages_impl <- function(repo) {
subset(select = c(Repository, Package, Version))
}

package_installation_info <- function(pkg, lib.loc = NULL) {

desc <- package_description(
pkg,
lib.loc = lib.loc,
fields = c(
"Version", "URL", "Repository", "RemoteType",
"RemoteUsername", "RemoteRepo", "GithubUsername", "GithubRepo",
"RemoteUrl", "biocViews"
)
)

version <- desc[["Version"]]
repo <- desc[["Repository"]]
remote_type <- desc[["RemoteType"]]
gh_username <- desc[["GithubUsername"]] %||% desc[["RemoteUsername"]]
gh_repo <- desc[["GithubRepo"]] %||% desc[["RemoteRepo"]]
remote_url <- desc[["RemoteUrl"]]
bioc_views <- desc[["biocViews"]]
pkg_urls <- desc[["URL"]]

if (is.null(desc[["Version"]]))
return(NULL)

# If no github info set, try getting it from the URL field
if ((is.null(gh_username) || is.null(gh_repo)) && !is.null(pkg_urls)) {
pkg_urls <- strsplit(pkg_urls, ",\\s*")[[1]]
github_url <- pkg_urls[is_valid_github_url(pkg_urls)]
if (length(github_url) > 0) {
github_url <- github_url[1]
gh_username <- github_username_from_url(github_url)
gh_repo <- github_repo_from_url(github_url)
}
}

available_sources <- c(
if (!is.null(repo)) "repo",
if (!is.null(gh_repo) && !is.null(gh_username)) "github",
if (!is.null(remote_url)) "remote",
if (!is.null(bioc_views)) "bioc"
)

list(
Available_Sources = available_sources,
Package = pkg,
Version_Installed = version,
Repository = repo,
Github_Username = gh_username,
Github_Repository = gh_repo,
Remote_URL = remote_url,
Bioc_Views = bioc_views
)

}



package_description <- function(pkg, lib.loc = NULL, fields = NULL) {
tryCatch(
pkg |>
packageDescription(lib.loc = lib.loc, fields = fields, drop = FALSE) |>
keep(\(x) !is.na(x) && x != ""),
warning = function(w) {
cli::cli_abort(c(
"No DESCRIPTION file found for {.pkg {pkg}}",
i = "Original warning: {w$message}"
))
}
)
}

maybe_as_version <- function(x) {
try(x <- package_version(x), silent = TRUE)
Expand Down
Loading