Skip to content

Commit f8768fb

Browse files
committed
Add ElixirScript.Context module
1 parent a6442ac commit f8768fb

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

lib/context.ex

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
defmodule ElixirScript.Context do
2+
alias __MODULE__
3+
4+
defstruct [
5+
:payload,
6+
:event_name,
7+
:sha,
8+
:ref,
9+
:workflow,
10+
:action,
11+
:actor,
12+
:job,
13+
:run_number,
14+
:run_id,
15+
:api_url,
16+
:server_url,
17+
:graphql_url
18+
]
19+
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+
end
25+
26+
def from_github_environment() do
27+
payload = if System.get_env("GITHUB_EVENT_PATH") do
28+
path = System.get_env("GITHUB_EVENT_PATH")
29+
if File.exists?(path) do
30+
File.read!(path) |> Jason.decode!()
31+
else
32+
IO.puts("GITHUB_EVENT_PATH #{path} does not exist")
33+
%{}
34+
end
35+
else
36+
%{}
37+
end
38+
39+
%Context{
40+
payload: payload,
41+
event_name: System.get_env("GITHUB_EVENT_NAME") || "",
42+
sha: System.get_env("GITHUB_SHA") || "",
43+
ref: System.get_env("GITHUB_REF") || "",
44+
workflow: System.get_env("GITHUB_WORKFLOW") || "",
45+
action: System.get_env("GITHUB_ACTION") || "",
46+
actor: System.get_env("GITHUB_ACTOR") || "",
47+
job: System.get_env("GITHUB_JOB") || "",
48+
run_number: System.get_env("GITHUB_RUN_NUMBER") |> parse_int(),
49+
run_id: System.get_env("GITHUB_RUN_ID") |> parse_int(),
50+
api_url: System.get_env("GITHUB_API_URL") || "https://api.github.com",
51+
server_url: System.get_env("GITHUB_SERVER_URL") || "https://github.com",
52+
graphql_url: System.get_env("GITHUB_GRAPHQL_URL") || "https://api.github.com/graphql"
53+
}
54+
end
55+
56+
defp parse_int(nil), do: nil
57+
defp parse_int(value), do: String.to_integer(value)
58+
end
59+

0 commit comments

Comments
 (0)