Skip to content

Commit 6d244cb

Browse files
committed
Fix audit errors
1 parent fefc479 commit 6d244cb

13 files changed

+149
-91
lines changed

lib/command_line.ex

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,59 @@
11
defmodule ElixirScript.CommandLine do
2+
@moduledoc """
3+
Main entrypoint for ElixirScript.
4+
Manages the parsing of command-line arguments, configures logger, and delegates work into sub-systems.
5+
"""
6+
27
alias ElixirScript.Core
38
alias ElixirScript.CustomLogger, as: Logger
49
alias ElixirScript.ScriptRunner
510

6-
def main(args \\ []) do
7-
Logger.debug("Running in debug mode")
11+
defmodule ParsedArgs do
12+
@moduledoc """
13+
Struct for parsed args
14+
"""
15+
defstruct debug?: false, help?: false, script: nil
16+
end
17+
18+
def main(args, opts \\ []) do
19+
parsed_args = parse_args!(args)
20+
log_level = if parsed_args.debug?, do: :debug, else: :info
21+
Logger.configure(level: log_level)
822

9-
Logger.debug(
10-
"All Environment Variables: #{inspect(System.get_env(), limit: :infinity, printable_limit: :infinity)}"
11-
)
23+
runner = Keyword.get(opts, :runner, &ScriptRunner.run/1)
24+
Logger.debug("Running in debug mode, using runner: #{inspect(runner)}")
1225

13-
{opts, _, _} = OptionParser.parse(args, strict: [help: :boolean])
14-
Logger.debug("Parsed options: #{inspect(opts, limit: :infinity, printable_limit: :infinity)}")
26+
Logger.debug("Environment Variables: #{inf_inspect(System.get_env())}")
1527

16-
if opts[:help] do
28+
Logger.debug("Parsed args: #{inf_inspect(parsed_args)}")
29+
30+
if parsed_args.help? do
1731
print_help()
32+
System.halt(0)
1833
else
19-
result = ScriptRunner.run(get_script())
34+
Logger.debug("Script input: #{inf_inspect(parsed_args.script)}")
35+
result = runner.(parsed_args.script)
2036
Core.set_output(result, "result")
21-
22-
Logger.debug(
23-
"Result output: #{inspect(result, limit: :infinity, printable_limit: :infinity)}"
24-
)
37+
Logger.debug("Result output: #{inspect(result, pretty: true)}")
2538
end
2639
end
2740

28-
defp get_script do
29-
script = Core.get_env_input("script", required: true)
30-
Logger.debug("Script input: #{inspect(script, limit: :infinity, printable_limit: :infinity)}")
31-
script
41+
def parse_args!(args) do
42+
{parsed, _remaining_args} =
43+
OptionParser.parse!(args,
44+
strict: [script: :string, debug: :boolean, help: :boolean],
45+
aliases: [debug: :d, help: :h, script: :s]
46+
)
47+
48+
debug? = Keyword.get(parsed, :debug, System.get_env("INPUT_DEBUG") == "true")
49+
script = Keyword.get(parsed, :script, System.get_env("INPUT_SCRIPT"))
50+
help? = Keyword.get(parsed, :help, false)
51+
52+
%ParsedArgs{
53+
debug?: debug?,
54+
help?: help?,
55+
script: script
56+
}
3257
end
3358

3459
defp print_help do
@@ -37,10 +62,16 @@ defmodule ElixirScript.CommandLine do
3762
script [OPTIONS]
3863
3964
Options:
40-
--help Show this help message and exit.
65+
--script,-s Specifies the script to run [INPUT_SCRIPT]
66+
--debug, -d Enables debug mode [INPUT_DEBUG]
67+
--help, -h Show this help message and exit
4168
4269
Example:
43-
INPUT_SCRIPT="IO.puts('Hello, world!')" script
70+
script --script "IO.puts('Hello, world!')"
4471
""")
4572
end
73+
74+
def inf_inspect(exec) do
75+
inspect(exec, pretty: true, limit: :infinity, printable_limit: :infinity)
76+
end
4677
end

lib/context.ex

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
defmodule ElixirScript.Context do
2+
@moduledoc """
3+
Constructs a structured context of the GitHub Actions environment variables, for use within ElixirScript.
4+
"""
25
alias __MODULE__
36

7+
@derive Jason.Encoder
48
defstruct [
59
:payload,
610
:event_name,
@@ -17,44 +21,45 @@ defmodule ElixirScript.Context do
1721
:graphql_url
1822
]
1923

20-
defimpl Jason.Encoder, for: Context do
21-
def encode(%Context{} = context, opts) do
22-
Map.from_struct(context) |> Jason.Encode.map(opts)
23-
end
24+
def from_github_environment do
25+
%Context{
26+
payload: read_payload(),
27+
event_name: fetch_env("GITHUB_EVENT_NAME", ""),
28+
sha: fetch_env("GITHUB_SHA", ""),
29+
ref: fetch_env("GITHUB_REF", ""),
30+
workflow: fetch_env("GITHUB_WORKFLOW", ""),
31+
action: fetch_env("GITHUB_ACTION", ""),
32+
actor: fetch_env("GITHUB_ACTOR", ""),
33+
job: fetch_env("GITHUB_JOB", ""),
34+
run_number: fetch_env("GITHUB_RUN_NUMBER"),
35+
run_id: fetch_env("GITHUB_RUN_ID") |> parse_int(),
36+
api_url: fetch_env("GITHUB_API_URL", "https://api.github.com"),
37+
server_url: fetch_env("GITHUB_SERVER_URL", "https://github.com"),
38+
graphql_url: fetch_env("GITHUB_GRAPHQL_URL", "https://api.github.com/graphql")
39+
}
2440
end
2541

26-
def from_github_environment() do
27-
payload =
28-
if System.get_env("GITHUB_EVENT_PATH") do
29-
path = System.get_env("GITHUB_EVENT_PATH")
30-
31-
if File.exists?(path) do
32-
File.read!(path) |> Jason.decode!()
33-
else
34-
IO.puts("GITHUB_EVENT_PATH #{path} does not exist")
35-
%{}
36-
end
37-
else
38-
%{}
39-
end
42+
defp read_payload do
43+
fetch_env("GITHUB_EVENT_PATH")
44+
|> maybe_read_file()
45+
end
4046

41-
%Context{
42-
payload: payload,
43-
event_name: System.get_env("GITHUB_EVENT_NAME") || "",
44-
sha: System.get_env("GITHUB_SHA") || "",
45-
ref: System.get_env("GITHUB_REF") || "",
46-
workflow: System.get_env("GITHUB_WORKFLOW") || "",
47-
action: System.get_env("GITHUB_ACTION") || "",
48-
actor: System.get_env("GITHUB_ACTOR") || "",
49-
job: System.get_env("GITHUB_JOB") || "",
50-
run_number: System.get_env("GITHUB_RUN_NUMBER") |> parse_int(),
51-
run_id: System.get_env("GITHUB_RUN_ID") |> parse_int(),
52-
api_url: System.get_env("GITHUB_API_URL") || "https://api.github.com",
53-
server_url: System.get_env("GITHUB_SERVER_URL") || "https://github.com",
54-
graphql_url: System.get_env("GITHUB_GRAPHQL_URL") || "https://api.github.com/graphql"
55-
}
47+
defp maybe_read_file(nil), do: %{}
48+
49+
defp maybe_read_file(path) do
50+
case File.read(path) do
51+
{:ok, contents} ->
52+
contents |> Jason.decode!()
53+
54+
{:error, _reason} ->
55+
IO.puts("Error reading GITHUB_EVENT_PATH #{path}")
56+
%{}
57+
end
5658
end
5759

60+
defp fetch_env(var), do: System.get_env(var)
61+
defp fetch_env(var, default), do: fetch_env(var) || default
62+
5863
defp parse_int(nil), do: nil
5964
defp parse_int(value), do: String.to_integer(value)
6065
end

lib/core.ex

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
defmodule ElixirScript.Core do
2-
alias ElixirScript.GitHubActions.Command
3-
alias ElixirScript.GitHubActions.CommandUtils
4-
alias ElixirScript.GitHubActions.EnvironmentFileCommand
5-
6-
def parse_args(args) do
7-
aliases = [script: :s, debug: :d]
8-
parsed = OptionParser.parse(args, aliases: aliases)
2+
@moduledoc """
3+
Provides core functionalities for ElixirScript,
4+
handling the retrieval and sanitization of GitHub Actions environment inputs and outputs.
5+
"""
96

10-
case parsed do
11-
{opts, _remaining_args, _invalid_opts} ->
12-
%{script: Map.get(opts, :script), debug: Map.get(opts, :debug)}
13-
end
14-
end
7+
alias ElixirScript.GitHubActions.EnvironmentFileCommand
8+
alias ElixirScript.GitHubActions.WorkflowCommand
9+
alias ElixirScript.GitHubActions.WorkflowCommandUtils
1510

1611
def get_env_input(name, opts \\ []) do
1712
required = Keyword.get(opts, :required, false)
@@ -38,11 +33,15 @@ defmodule ElixirScript.Core do
3833
"OUTPUT",
3934
EnvironmentFileCommand.prepare_key_value_message(
4035
name,
41-
CommandUtils.to_command_value(value)
36+
WorkflowCommandUtils.to_command_value(value)
4237
)
4338
)
4439
else
45-
Command.issue_command(~c"set-output", name, CommandUtils.to_command_value(value))
40+
WorkflowCommand.issue_command(
41+
~c"set-output",
42+
name,
43+
WorkflowCommandUtils.to_command_value(value)
44+
)
4645
end
4746
end
4847

lib/custom_logger.ex

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ defmodule ElixirScript.CustomLogger do
77
if debug_mode?(), do: log(:debug, message)
88
end
99

10-
defp debug_mode?() do
11-
System.get_env("INPUT_DEBUG") == "true"
10+
def configure(opts \\ []) do
11+
level = Keyword.get(opts, :level, :info)
12+
Application.put_env(:elixir_script, :log_level, level)
13+
end
14+
15+
defp debug_mode? do
16+
Application.get_env(:elixir_script, :log_level, :info) == :debug
1217
end
1318

1419
defp log(level, message) do

lib/e2e.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ defmodule ElixirScript.E2e.Entry do
77
end
88

99
defmodule ElixirScript.E2e do
10+
@moduledoc """
11+
Provides functionality for reading and processing end-to-end (E2E) test data,
12+
transforming the data into Entry structs that can be consumed in different contexts.
13+
"""
1014
alias ElixirScript.E2e.Entry
1115

1216
def read_test_file(file_path \\ "test/e2e_data.exs") do
@@ -24,7 +28,7 @@ defmodule ElixirScript.E2e do
2428
file = Map.get(entry, :file)
2529
expected = Map.get(entry, :expected)
2630

27-
if(!script && !file) do
31+
if !script && !file do
2832
raise(KeyError, "key :script or :file not found in: #{inspect(entry)}")
2933
end
3034

lib/github_actions/environment_file_command.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule ElixirScript.GitHubActions.EnvironmentFileCommand do
44
55
This module is designed to interact with GitHub Actions by issuing
66
workflow commands to environment files as specified in the GitHub Actions documentation:
7-
[Workflow Commands for GitHub Actions](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#environment-files).
7+
[Workflow Environment File Commands for GitHub Actions](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#environment-files).
88
"""
99

1010
def issue_file_command(command, command_value) do

lib/github_actions/workflow_command.ex

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
defmodule ElixirScript.GitHubActions.Command do
1+
defmodule ElixirScript.GitHubActions.WorkflowCommand do
2+
@moduledoc """
3+
Handles GitHub Actions Workflow Commands.
4+
5+
This module is designed to interact with GitHub Actions by issuing
6+
workflow commands as specified in the GitHub Actions documentation:
7+
[Workflow Commands for GitHub Actions](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#about-workflow-commands).
8+
"""
9+
210
def issue_command(command, name, command_value) do
311
IO.puts("::#{command} name=#{name}::#{command_value}")
412
end

lib/github_actions/workflow_command_utils.ex

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
defmodule ElixirScript.GitHubActions.CommandUtils do
1+
defmodule ElixirScript.GitHubActions.WorkflowCommandUtils do
2+
@moduledoc """
3+
Utility functions for converting a a string format compatible with GitHub Actions commands.
4+
This includes handling `nil` values, binary strings, and encoding complex data types into JSON strings.
5+
"""
6+
27
def to_command_value(nil), do: ""
38

49
def to_command_value(input) when is_binary(input), do: input

lib/mix/tasks/docker.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
defmodule Mix.Tasks.Docker do
2+
@moduledoc """
3+
A Mix task for managing Docker operations
4+
"""
25
use Mix.Task
36

4-
@shortdoc "Manages Docker operations for the project."
5-
67
def run(args) do
78
case args do
89
["build"] -> build()

lib/mix/tasks/e2e/set_github_matrix.ex

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
defmodule Mix.Tasks.E2e.SetGithubMatrix do
2+
@moduledoc """
3+
Mix task to generate matrix information from E2E test data for GitHub Actions workflows.
4+
"""
25
use Mix.Task
36

47
alias ElixirScript.Core
58
alias ElixirScript.E2e
69
alias ElixirScript.E2e.Entry
710

8-
@moduledoc """
9-
Mix task to generate matrix information from E2E test data for GitHub Actions workflows.
10-
"""
11-
12-
@shortdoc "Generates matrix information from the E2E data file, to feed into a GitHub Actions matrix."
13-
1411
def run(args) do
1512
output_key = List.first(args) || "matrix"
1613

0 commit comments

Comments
 (0)