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 path_prefix option to lint_package for GitHub Actions annotations #845

Merged
merged 10 commits into from
Oct 9, 2021
5 changes: 4 additions & 1 deletion R/actions.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ in_github_actions <- function() {
}

# Output logging commands for any lints found
github_actions_log_lints <- function(lints) {
github_actions_log_lints <- function(lints, project_dir = "") {
for (x in lints) {
if (nzchar(project_dir)) {
x$filename <- file.path(project_dir, x$filename)
}
file_line_col <- sprintf(
"file=%s,line=%s,col=%s", x$filename, x$line_number, x$column_number
)
Expand Down
4 changes: 3 additions & 1 deletion R/methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@ print.lints <- function(x, ...) {
requireNamespace("rstudioapi", quietly = TRUE) &&
rstudioapi::hasFun("sourceMarkers")

github_annotation_project_dir <- getOption("lintr.github_annotation_project_dir", "")

if (length(x)) {
inline_data <- x[[1]][["filename"]] == "<text>"
if (!inline_data && rstudio_source_markers) {
rstudio_source_markers(x)
} else if (in_github_actions()) {
github_actions_log_lints(x)
github_actions_log_lints(x, project_dir = github_annotation_project_dir)
} else {
if (in_ci() && settings$comment_bot) {

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ location, namely in the `.github/workflows` directory of your repository. This f
pull request with the lints found and they will also be printed as [annotations](https://docs.github.com/en/free-pro-team@latest/github/collaborating-with-issues-and-pull-requests/about-status-checks#types-of-status-checks-on-github) along side the status check on GitHub. If you want to disable the commenting you can
set the environment variable `LINTR_COMMENT_BOT=false`.

If your project is in a subdirectory and you would like to use GitHub Actions annotations, you can set `options(lintr.github_annotation_project_dir = "path/to/project")` which will make sure that the annotations point to the correct paths.

#### Travis CI ###

If you want to run `lintr` on [Travis-CI](https://travis-ci.org), you will need
Expand Down
22 changes: 22 additions & 0 deletions tests/testthat/test-ci.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,28 @@ test_that("GitHub Actions functionality works", {
})
})

test_that("GitHub Actions functionality works in a subdirectory", {
# imitate being on GHA whether or not we are
pkg_path <- file.path("dummy_packages", "assignmentLinter")
withr::with_envvar(c(GITHUB_ACTIONS = "true"), {
old <- options(
lintr.rstudio_source_markers = FALSE,
lintr.github_annotation_project_dir = pkg_path
)
on.exit(options(old), add = TRUE)

read_settings(NULL)
l <- lint_package(
pkg_path, linters = list(assignment_linter()),
parse_settings = FALSE
)
expect_output(
print(l),
paste0("::warning file=", file.path(pkg_path, "R(/|\\\\)abc\\.R"))
)
})
})

test_that("Printing works for Travis", {
withr::with_envvar(c(GITHUB_ACTIONS = "false", TRAVIS_REPO_SLUG = "test/repo", LINTR_COMMENT_BOT = "true"), {
old <- options(lintr.rstudio_source_markers = FALSE)
Expand Down