Skip to content

Commit

Permalink
start pipeline config from thevalidatoR
Browse files Browse the repository at this point in the history
  • Loading branch information
yannfeat committed Jan 8, 2024
1 parent d5216ca commit 37b826a
Show file tree
Hide file tree
Showing 22 changed files with 473 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Roche

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
123 changes: 123 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
name: R Package Validation Report
author: Roche
description: A Github Action that generates a validation report for an R package.

inputs:
report_pkg_dir:
description: Path to package's root.
required: false
default: "."
report_template_path:
description: |
File path of the R markdown template to use for the report.
The default template is available [here.](./template.Rmd)
required: false
default: "./template.Rmd"
report_rmarkdown_format:
description: |
The file format of the validation report. See `rmarkdown::render`'s
`output_format` parameter for accepted values.
required: false
default: "all"
report_output_prefix:
description: >
The output filename prefix for the validation report. If left blank,
it defaults to the following convention:
`<package name>-<package version>-validation-report`.
required: false
default: ""
additional_tlmgr_packages:
description: |
Additional tex packages to install with tlmgr.
required: false
default: "courier ec"
no_cache:
description: "Disable github action R dependency caching"
required: false
default: "false"
cache_version:
description: "Version of the cache. To clean cache bump this version."
required: false
default: "v1"
disable_install_dev_deps:
description: |
Disable installation of dev dependencies while
building the report.
required: false
default: "false"
outputs:
report_output_filename:
description: Filename of the generated report.
value: ${{ steps.report-generator.outputs.output-filename }}
branding: # https://feathericons.com/
icon: "award"
color: "blue"

runs:
using: "composite"

steps:
- name: Get R version
id: r_version
run: echo "R_VERSION=$(R --version | head -1 | awk '{print $3}')" >> $GITHUB_OUTPUT
shell: bash

- name: Set R Library home on Linux
run: |
mkdir -p ${RUNNER_TEMP}/Library
echo ".libPaths(c('${RUNNER_TEMP}/Library', .libPaths()))" > ~/.Rprofile
cat ~/.Rprofile
shell: bash

- name: Set tlmgr cache folder
if: "contains(inputs.no_cache, 'false')"
id: texlive_version
run: |
echo "TEX_LIVE_VERSION=$(tlmgr --version |tail -1 |awk '{print $NF}')" >> $GITHUB_OUTPUT
shell: bash

- name: Cache R packages
if: "contains(inputs.no_cache, 'false')"
uses: actions/cache@v2
with:
path: /home/runner/work/_temp/Library
key: ${{ inputs.cache_version }}-${{ runner.os }}-${{ steps.r_version.outputs.R_VERSION }}-${{ hashFiles('DESCRIPTION') }}
restore-keys: ${{ inputs.cache_version }}-${{ runner.os }}-${{ steps.r_version.outputs.R_VERSION }}

- name: Cache Tex packages
if: "contains(inputs.no_cache, 'false')"
uses: actions/cache@v2
with:
path: /home/runner/work/_temp/TinyTeX
key: ${{ inputs.cache_version }}-${{ runner.os }}-${{ steps.texlive_version.outputs.TEX_LIVE_VERSION }}-${{ hashFiles(inputs.report_template_path) }}
restore-keys: ${{ inputs.cache_version }}-${{ runner.os }}-${{ steps.texlive_version.outputs.TEX_LIVE_VERSION }}

- name: Install dependencies
run: |
${GITHUB_ACTION_PATH}/dependencies.R
export PATH=${RUNNER_TEMP}/TinyTeX/bin/x86_64-linux:${PATH}
echo "PATH=${PATH}" >> $GITHUB_ENV
tlmgr path add
tlmgr update --self
[ ! -f "./template.Rmd" ] && cp ${GITHUB_ACTION_PATH}/template.Rmd . || echo "./template.Rmd Already exists"
shell: bash

- name: Install additional tex packages
run: tlmgr install ${{ inputs.additional_tlmgr_packages }}
shell: bash

- name: Run report generator
id: report-generator
run: |
${GITHUB_ACTION_PATH}/report-generator.R
filename=$(basename $(cat /tmp/report_file_path.txt))
echo "output-filename=${filename}" >> $GITHUB_OUTPUT
shell: bash
env:
# Composite action doesn't set inputs as env vars.
# We need to do this manually...
INPUT_REPORT_PKG_DIR: ${{ inputs.report_pkg_dir }}
INPUT_REPORT_TEMPLATE_PATH: ${{ inputs.report_template_path }}
INPUT_REPORT_RMARKDOWN_FORMAT: ${{ inputs.report_rmarkdown_format }}
INPUT_REPORT_OUTPUT_PREFIX: ${{ inputs.report_output_prefix }}
DISABLE_INSTALL_DEV_DEPS: ${{ inputs.disable_install_dev_deps }}
42 changes: 42 additions & 0 deletions dependencies.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env Rscript

# Install latest ackages from Github
# Requires dev version of covr (>= 3.5.1.9003)
# Also install other package dependencies (for tests)
github_packages <- c(
"r-lib/covr",
"pharmaR/riskmetric",
"Genentech/covtracer"
)
lapply(github_packages, remotes::install_github)

# CRAN
options(repos = c("https://cloud.r-project.org/"))
ncores <- parallel::detectCores(all.tests = FALSE, logical = TRUE)
if (!require("git2r")) install.packages("git2r", upgrade = "never", Ncpus = ncores)
if (!require("kableExtra")) install.packages("kableExtra", upgrade = "never", Ncpus = ncores)
if (!require("tinytex")) install.packages("tinytex", upgrade = "never", Ncpus = ncores)

# Conditionally install TinyTex
if(!dir.exists(paste(Sys.getenv("RUNNER_TEMP"), "TinyTeX", sep="/"))) {
# nolint start
tinytex_installer <- '
export TINYTEX_DIR=${RUNNER_TEMP}/TinyTeX
wget -qO- "https://raw.githubusercontent.com/yihui/tinytex/master/tools/install-unx.sh" | sh -s - --admin --no-path
mkdir -p ${RUNNER_TEMP}/TinyTeX
cp -r ~/.TinyTeX/. ${RUNNER_TEMP}/TinyTeX
rm -rf ~/.TinyTeX
${RUNNER_TEMP}/TinyTeX/bin/*/tlmgr path add
tlmgr update --self
tlmgr install latex-bin luatex xetex ae bibtex context inconsolata listings makeindex metafont mfware parskip pdfcrop tex tools url xkeyval
'
# nolint end
system(tinytex_installer)
tinytex::r_texmf()
permission_update <- '
chown -R root:staff ${RUNNER_TEMP}/TinyTeX
chmod -R g+w ${RUNNER_TEMP}/TinyTeX
chmod -R g+wx ${RUNNER_TEMP}/TinyTeX/bin
'
system(permission_update)
}
66 changes: 66 additions & 0 deletions report-generator.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env Rscript

# Location
cat(sprintf("Current dir is: '%s'", getwd()))

# CVE-2022-24765 exception
git_safe_dir <- system(
sprintf("git config --global --add safe.directory '%s'", getwd())
)

# Get the action inputs from preset env vars
pkg_dir <- normalizePath(Sys.getenv("INPUT_REPORT_PKG_DIR", "."))
template_path <- Sys.getenv("INPUT_REPORT_TEMPLATE_PATH", "/template.Rmd")
report_format <- Sys.getenv("INPUT_REPORT_RMARKDOWN_FORMAT", "all")
report_output_prefix <- Sys.getenv("INPUT_REPORT_OUTPUT_PREFIX", "")
disable_install_dev_deps <- tolower(
Sys.getenv("DISABLE_INSTALL_DEV_DEPS")
) %in% c("yes", "y", "t", "true")

# fail with meaningful message if REPORT_PKG_DIR does not appear to be a package
desc_file <- file.path(pkg_dir, "DESCRIPTION")
if (!file.exists(desc_file)) {
stop(sprintf(
paste(sep = "\n",
"Could not find package at '%s'",
" ",
" Specify a directory by definining environment variable:",
" INPUT_REPORT_PKG_DIR=<repository subdirectory>",
" "
),
pkg_dir
))
}

# Install package dependencies
if (!disable_install_dev_deps) {
options("remotes.git_credentials" = git2r::cred_user_pass(
username = "token",
password = remotes:::github_pat()
))
devtools::install_dev_deps(pkg_dir, upgrade = "never")
}

# Set the output file name
if (report_output_prefix == "") {
desc <- read.dcf(desc_file)
pkg_name <- toString(desc[, "Package"])
pkg_version <- toString(desc[, "Version"])
report_output_prefix <- paste0(
pkg_name, "-", pkg_version, "-validation-report"
)
}

# allow rmarkdown to choose appropriate file extension for output format
report_file_path <- rmarkdown::render(
template_path,
output_dir = getwd(), # create report wherever R script was called
output_file = report_output_prefix,
output_format = report_format,
params = list(pkg_dir = pkg_dir)
)

# Create a tmp file which contains the final report filename
writeLines(report_file_path, "/tmp/report_file_path.txt")

cat(sprintf("Created report at: '%s'\n\n", report_file_path))
3 changes: 3 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Tests

Directory for artifacts used in testing Github Actions workflows.
3 changes: 3 additions & 0 deletions tests/packages/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Packages for testing

Example R packages that can be used for testing Github Actions within this repository.
12 changes: 12 additions & 0 deletions tests/packages/aragog/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Package: aragog
Version: 0.1.0
Title: Hagrid's giant spider
Description: Aragog was an Acromantula owned by Rubeus Hagrid.
Aragog was able to communicate with humans with speech.
Author: JK Rowling <[email protected]>
Maintainer: Insights Engineering <[email protected]>
License: file LICENSE
Type: Package
Encoding: UTF-8
RoxygenNote: 7.0.2
Suggests: testthat
1 change: 1 addition & 0 deletions tests/packages/aragog/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Wizarding World
3 changes: 3 additions & 0 deletions tests/packages/aragog/NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Generated by roxygen2: do not edit by hand

export(aragog)
10 changes: 10 additions & 0 deletions tests/packages/aragog/R/main.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#' aragog
#'
#' Get Hagrid's spider's name
#' @author Insights Engineering <[email protected]>
#' @title Get the spider's name
#' @return string. The spider's name
#' @export
aragog <- function() {
return("Aragog")
}
3 changes: 3 additions & 0 deletions tests/packages/aragog/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# aragog

An R package about [Aragog](https://harrypotter.fandom.com/wiki/Aragog).
20 changes: 20 additions & 0 deletions tests/packages/aragog/man/aragog.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions tests/packages/aragog/tests/test_main.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
library(aragog)
library(testthat)

check_spider <- function() {
stopifnot(identical(aragog(), "Aragog"))
}

test_that("The spider's name is Aragog", {
check_spider()
})
14 changes: 14 additions & 0 deletions tests/packages/buckbeak/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Package: buckbeak
Version: 0.1.0
Title: Hagrid's hippogriff
Description: Buckbeak, later renamed Witherwings, was a male hippogriff.
He lived with Rubeus Hagrid during Harry Potter's third year at Hogwarts,
along with a few other hippogriffs.
Author: JK Rowling <[email protected]>
Maintainer: Insights Engineering <[email protected]>
License: GPL-3
Type: Package
Encoding: UTF-8
Depends: aragog, dplyr
Suggests: testthat
RoxygenNote: 7.0.2
4 changes: 4 additions & 0 deletions tests/packages/buckbeak/NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Generated by roxygen2: do not edit by hand

export(buckbeak)
export(hagrids_pets)
23 changes: 23 additions & 0 deletions tests/packages/buckbeak/R/main.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
library(aragog)

#' buckbeak
#'
#' Hagrid's hippogriff name
#' @author Insights Engineering <[email protected]>
#' @title Get the hippogriff's name
#' @return string. The hippogriff's name
#' @export
buckbeak <- function() {
return("Buckbeak")
}

#' hagrids_pets
#'
#' Hagrid's pets' names
#' @author Insights Engineering <[email protected]>
#' @title Get Hagrid's pets' names
#' @return string. The spider and the hippogriff's name
#' @export
hagrids_pets <- function() {
return(paste(c("Buckbeak", "and", aragog()), collapse = " "))
}
3 changes: 3 additions & 0 deletions tests/packages/buckbeak/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# buckbeak

An R package about [Buckbeak](https://harrypotter.fandom.com/wiki/Buckbeak).
20 changes: 20 additions & 0 deletions tests/packages/buckbeak/man/buckbeak.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 37b826a

Please sign in to comment.