Skip to content

Commit

Permalink
Respect LogLevel of GHA by default (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericphanson authored Sep 19, 2023
1 parent 93defad commit e988cc2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "GitHubActions"
uuid = "6b79fd1a-b13a-48ab-b6b0-aaee1fee41df"
authors = ["Chris de Graaf <me@cdg.dev> and contributors"]
version = "0.1.6"
version = "0.1.7"

[deps]
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
Expand Down
29 changes: 24 additions & 5 deletions src/GitHubActions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export
set_secret,
start_group

using Logging: Logging, AbstractLogger, Debug, Info, Warn, Error
using Logging: Logging, AbstractLogger, LogLevel, Debug, Info, Warn, Error

using JSON: json

Expand Down Expand Up @@ -213,14 +213,33 @@ function set_failed(msg)
end

"""
GitHubActionsLogger()
GitHubActionsLogger(level)
A logger that prints to standard output in the format expected by GitHub Actions.
By default, GitHubActionsLogger inspects the
[`RUNNER_DEBUG` environment variable](https://docs.github.com/en/actions/learn-github-actions/variables)
to match the log-level to that set in GitHub Actions.
"""
struct GitHubActionsLogger <: AbstractLogger end
struct GitHubActionsLogger <: AbstractLogger
level::LogLevel
end

function get_gha_level()
env_str = get(ENV, "RUNNER_DEBUG", "false")
# `tryparse(Bool, "1")` does not work on Julia 1.0, so we special-case that value
# (which is the only documented value for `RUNNER_DEBUG`, so any other case should be
# due to the user setting it manually somehow).
env_str == "1" && return Debug
# Fallback for all other values:
is_debug = something(tryparse(Bool, env_str), false)
return is_debug ? Debug : Info
end

GitHubActionsLogger() = GitHubActionsLogger(get_gha_level())

Logging.catch_exceptions(::GitHubActionsLogger) = true
Logging.min_enabled_level(::GitHubActionsLogger) = Debug
Logging.min_enabled_level(logger::GitHubActionsLogger) = logger.level
Logging.shouldlog(::GitHubActionsLogger, args...) = true

function Logging.handle_message(
Expand All @@ -229,7 +248,7 @@ function Logging.handle_message(
location=nothing, kwargs...,
)
file, line = something(location, (file, line))

workspace = get(ENV, "GITHUB_WORKSPACE", nothing)
if workspace !== nothing
# In order for inline annotations to work correctly:
Expand Down
28 changes: 23 additions & 5 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Logging: with_logger
using Logging: Logging, with_logger
using Test: @test, @testset, @test_throws

using GitHubActions
Expand Down Expand Up @@ -110,16 +110,34 @@ const GHA = GitHubActions
return Regex("^::$level file=$(file),line=\\d+::a")
end

with_logger(GitHubActionsLogger()) do
with_logger(GitHubActionsLogger(Logging.Debug)) do
@test match(rx("debug"), (@capture_out @debug "a")) !== nothing
@test match(rx("warning"), (@capture_out @warn "a")) !== nothing
@test match(rx("error"), (@capture_out @error "a")) !== nothing
@test (@capture_out @info "a") == "a\n"

@test (@capture_out @info "a" b=1 c=2 d=Text("e\nf")) == "a\n b = 1\n c = 2\n d = \n e\n f\n"
@test endswith((@capture_out @warn "a" b=1 c=2), "::a%0A b = 1%0A c = 2\n")
@test (@capture_out @info "a" b = 1 c = 2 d = Text("e\nf")) == "a\n b = 1\n c = 2\n d = \n e\n f\n"
@test endswith((@capture_out @warn "a" b = 1 c = 2), "::a%0A b = 1%0A c = 2\n")

expected = "::warning file=test/bar,line=1::foo\n"
@test (@capture_out @warn "foo" location=("bar", 1)) == expected
@test (@capture_out @warn "foo" location = ("bar", 1)) == expected
end

with_logger(GitHubActionsLogger(Logging.Info)) do
@test match(rx("debug"), (@capture_out @debug "a")) === nothing
end

@testset "get_gha_level" begin
for val in ("1", "true")
withenv("RUNNER_DEBUG" => val) do
@test GHA.get_gha_level() === Logging.Debug
end
end

for val in ("0", "false", "", nothing, "garbage")
withenv("RUNNER_DEBUG" => val) do
@test GHA.get_gha_level() === Logging.Info
end
end
end
end

2 comments on commit e988cc2

@christopher-dG
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/91710

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.7 -m "<description of version>" e988cc204e34bf1b12b28c3f1f00e5ae347af30f
git push origin v0.1.7

Please sign in to comment.