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

Start testing for GH stuff #10

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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: 3 additions & 1 deletion .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
^LICENSE\.md$
^CITATION\.cff$
^codemeta.json$
^.lintr$
^\.lintr$
^_pkgdown\.yml$
^docs$
^pkgdown$
Expand All @@ -16,3 +16,5 @@
^check$
^artifacts$
^logo.png$
^\.env$
^\.git-crypt$
Binary file added .env
Binary file not shown.
4 changes: 4 additions & 0 deletions .git-crypt/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Do not edit this file. To specify the files to encrypt, create your own
# .gitattributes file in the directory where your files are.
* !filter !diff
*.gpg binary
Binary file not shown.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## Encrypted Files
.env filter=git-crypt diff=git-crypt
7 changes: 7 additions & 0 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ with S3 cloud storage, a MinIO server is run in the background to serve as an
S3 API endpoint. You will need the `minio` command line tool installed as well
as the `mc` MinIO client.

For testing against the GitHub API, repositories are generated and deleted. This
requires a fine-grained GitHub personal access token with `administration`, `contents`, and `commit statuses` scopes.
This is set as the environment variable `RELIC_TESTING_GITHUB_PAT`, and the organization
under which to create repositories is set as `RELIC_TESTING_GITHUB_ORG`. In the
absence of these, GitHub API tests will be skipped. These are available in the
encrypted `.env` file, which is secured by [`git-crypt`](https://www.agwa.name/projects/git-crypt/)

## Lifecycle Statement

`relic` is a new package and its API is still under development.
Expand Down
9 changes: 9 additions & 0 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,20 @@ jobs:
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
R_KEEP_PKG_SOURCE: yes
GIT_CRYPT_KEY64: ${{ secrets.GIT_CRYPT_KEY64 }}
steps:
- uses: actions/checkout@v3
- name: Do a pre-emptive apt-update
run: sudo apt-get update -qq
if: ${{ !env.ACT }}
- name: Decrypt repository using symmetric key
if: ${{ env.GIT_CRYPT_KEY64 }}
run: |
sudo apt-get install -y --no-install-recommends git-crypt
echo $GIT_CRYPT_KEY64 > git_crypt_key.key64 && base64 -di git_crypt_key.key64 > git_crypt_key.key && git-crypt unlock git_crypt_key.key
rm git_crypt_key.key git_crypt_key.key64
# Select all values in .env and print with "::add-mask::" to obfuscate
grep -v '^#' .env | sed -E 's/(.*)=(.*)/\2/' | xargs -I '{}' echo "::add-mask::{}"
- uses: r-lib/actions/setup-pandoc@v2
- uses: r-lib/actions/setup-r@v2
with:
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/helper-github.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
skip_if_no_github <- function() {
skip_if_offline("github.com")
skip_on_cran()

skip_if(!nzchar(Sys.getenv("RELIC_TESTING_GITHUB_PAT")), "No RELIC_TESTING_GITHUB_PAT env var")
skip_if(!nzchar(Sys.getenv("RELIC_TESTING_GITHUB_ORG")), "No RELIC_TESTING_GITHUB_ORG env var")

}
16 changes: 14 additions & 2 deletions tests/testthat/setup.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
withr::local_envvar(
"R_USER_CACHE_DIR" = tempdir()
# Read in credentials if the file is unencrypted
env_file <- fs::path(rprojroot::find_root(rprojroot::is_r_package), ".env")
if (file.exists(env_file)) {
x <- try(readRenviron(env_file), silent = TRUE)
if (!inherits(x, "try-error")) {
Sys.setenv("GITHUB_PAT" = Sys.getenv("RELIC_TESTING_GITHUB_PAT"))
}
}

# Set a temporary location for the cache
withr::local_envvar(list(
"R_USER_CACHE_DIR" = tempdir(),
"RELIC_TESTING_GITHUB_PAT" = Sys.getenv("RELIC_TESTING_GITHUB_PAT"),
"GITHUB_PAT" = Sys.getenv("RELIC_TESTING_GITHUB_PAT"))
)

## Run a MinIO server in the background to test S3 object storage with `targets`
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-github.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
test_that("GitHub testing access works", {
skip_if_no_github()

#Look up the GitHub organization
expect_no_error(gh_response <- gh::gh("/orgs/{org}", org = Sys.getenv("RELIC_TESTING_GITHUB_ORG")))
token_expiry <- attr(gh_response, "response")$`github-authentication-token-expiration`
if (as.numeric(as.POSIXct(token_expiry) - Sys.time(), "days") < 7) {
warning("GitHub token expires in less than a week. Please update the token.")
}
})
Loading