Skip to content

Commit 65b1fa5

Browse files
committed
Change context to expose an atom-map
1 parent 176a6e9 commit 65b1fa5

File tree

3 files changed

+257
-19
lines changed

3 files changed

+257
-19
lines changed

lib/context.ex

+20-19
Original file line numberDiff line numberDiff line change
@@ -24,42 +24,43 @@ defmodule ElixirScript.Context do
2424
def from_github_environment do
2525
%Context{
2626
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")
27+
event_name: get_env("GITHUB_EVENT_NAME", ""),
28+
sha: get_env("GITHUB_SHA", ""),
29+
ref: get_env("GITHUB_REF", ""),
30+
workflow: get_env("GITHUB_WORKFLOW", ""),
31+
action: get_env("GITHUB_ACTION", ""),
32+
actor: get_env("GITHUB_ACTOR", ""),
33+
job: get_env("GITHUB_JOB", ""),
34+
run_number: get_env("GITHUB_RUN_NUMBER") |> parse_int(),
35+
run_id: get_env("GITHUB_RUN_ID") |> parse_int(),
36+
api_url: get_env("GITHUB_API_URL", "https://api.github.com"),
37+
server_url: get_env("GITHUB_SERVER_URL", "https://github.com"),
38+
graphql_url: get_env("GITHUB_GRAPHQL_URL", "https://api.github.com/graphql")
3939
}
4040
end
4141

4242
defp read_payload do
43-
fetch_env("GITHUB_EVENT_PATH")
44-
|> maybe_read_file()
43+
get_env("GITHUB_EVENT_PATH") |> maybe_read_file(keys: :atoms)
44+
# ↑ The GitHub event is a well-defined data-structure, it's fine to keep as atoms.
4545
end
4646

47-
defp maybe_read_file(nil), do: %{}
47+
defp maybe_read_file(nil, _), do: %{}
4848

49-
defp maybe_read_file(path) do
49+
defp maybe_read_file(path, opts) do
5050
case File.read(path) do
5151
{:ok, contents} ->
52-
contents |> Jason.decode!()
52+
contents |> Jason.decode!(opts)
5353

5454
{:error, _reason} ->
5555
IO.puts("Error reading GITHUB_EVENT_PATH #{path}")
5656
%{}
5757
end
5858
end
5959

60-
defp fetch_env(var), do: System.get_env(var)
61-
defp fetch_env(var, default), do: fetch_env(var) || default
60+
defp get_env(var, default \\ nil), do: system_env_impl().get_env(var, default)
6261

6362
defp parse_int(nil), do: nil
6463
defp parse_int(value), do: String.to_integer(value)
64+
65+
defp system_env_impl, do: Application.get_env(:context, :system_env, SystemEnvImpl)
6566
end

test/context_test.exs

+229
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
defmodule ElixirScript.ContextTest do
2+
use ExUnit.Case, async: true
3+
import Mox
4+
5+
alias ElixirScript.Context
6+
alias Test.Fixtures.GitHubWorkflowRun
7+
8+
setup :verify_on_exit!
9+
10+
describe "from_github_environment/0" do
11+
test "returns a Context struct with default values when environment variables are not set" do
12+
stub(SystemEnvBehaviourMock, :get_env, fn varname, default ->
13+
GitHubWorkflowRun.env()[varname] || default
14+
end)
15+
16+
assert Context.from_github_environment() == %Context{
17+
action: "script",
18+
actor: "gaggle",
19+
api_url: "https://api.github.com",
20+
event_name: "push",
21+
graphql_url: "https://api.github.com/graphql",
22+
job: "event-context-is-available",
23+
payload: %{
24+
after: "2ea150fa30b30dec0aeb85eb93469eda2d95c7d7",
25+
compare:
26+
"https://github.com/gaggle/elixir_script/compare/5ace270b6afc...2ea150fa30b3",
27+
ref: "refs/heads/atom-context",
28+
repository: %{
29+
id: 675_841_662,
30+
contents_url:
31+
"https://api.github.com/repos/gaggle/elixir_script/contents/{+path}",
32+
url: "https://github.com/gaggle/elixir_script",
33+
allow_forking: true,
34+
pulls_url: "https://api.github.com/repos/gaggle/elixir_script/pulls{/number}",
35+
collaborators_url:
36+
"https://api.github.com/repos/gaggle/elixir_script/collaborators{/collaborator}",
37+
trees_url: "https://api.github.com/repos/gaggle/elixir_script/git/trees{/sha}",
38+
branches_url:
39+
"https://api.github.com/repos/gaggle/elixir_script/branches{/branch}",
40+
deployments_url:
41+
"https://api.github.com/repos/gaggle/elixir_script/deployments",
42+
has_projects: false,
43+
description: "Run simple Elixir scripts, easily",
44+
size: 105,
45+
subscription_url:
46+
"https://api.github.com/repos/gaggle/elixir_script/subscription",
47+
has_issues: true,
48+
issue_comment_url:
49+
"https://api.github.com/repos/gaggle/elixir_script/issues/comments{/number}",
50+
ssh_url: "[email protected]:gaggle/elixir_script.git",
51+
default_branch: "main",
52+
assignees_url:
53+
"https://api.github.com/repos/gaggle/elixir_script/assignees{/user}",
54+
git_refs_url:
55+
"https://api.github.com/repos/gaggle/elixir_script/git/refs{/sha}",
56+
forks: 0,
57+
open_issues: 0,
58+
notifications_url:
59+
"https://api.github.com/repos/gaggle/elixir_script/notifications{?since,all,participating}",
60+
created_at: 1_691_443_361,
61+
pushed_at: 1_700_430_826,
62+
blobs_url: "https://api.github.com/repos/gaggle/elixir_script/git/blobs{/sha}",
63+
stargazers: 0,
64+
node_id: "R_kgDOKEiGfg",
65+
has_wiki: false,
66+
labels_url: "https://api.github.com/repos/gaggle/elixir_script/labels{/name}",
67+
license: nil,
68+
hooks_url: "https://api.github.com/repos/gaggle/elixir_script/hooks",
69+
html_url: "https://github.com/gaggle/elixir_script",
70+
contributors_url:
71+
"https://api.github.com/repos/gaggle/elixir_script/contributors",
72+
topics: [],
73+
language: "Elixir",
74+
milestones_url:
75+
"https://api.github.com/repos/gaggle/elixir_script/milestones{/number}",
76+
keys_url: "https://api.github.com/repos/gaggle/elixir_script/keys{/key_id}",
77+
releases_url:
78+
"https://api.github.com/repos/gaggle/elixir_script/releases{/id}",
79+
disabled: false,
80+
open_issues_count: 0,
81+
commits_url: "https://api.github.com/repos/gaggle/elixir_script/commits{/sha}",
82+
name: "elixir_script",
83+
updated_at: "2023-11-18T16:35:54Z",
84+
mirror_url: nil,
85+
languages_url: "https://api.github.com/repos/gaggle/elixir_script/languages",
86+
watchers: 0,
87+
forks_count: 0,
88+
has_pages: false,
89+
issue_events_url:
90+
"https://api.github.com/repos/gaggle/elixir_script/issues/events{/number}",
91+
forks_url: "https://api.github.com/repos/gaggle/elixir_script/forks",
92+
private: false,
93+
merges_url: "https://api.github.com/repos/gaggle/elixir_script/merges",
94+
git_url: "git://github.com/gaggle/elixir_script.git",
95+
statuses_url:
96+
"https://api.github.com/repos/gaggle/elixir_script/statuses/{sha}",
97+
teams_url: "https://api.github.com/repos/gaggle/elixir_script/teams",
98+
subscribers_url:
99+
"https://api.github.com/repos/gaggle/elixir_script/subscribers",
100+
full_name: "gaggle/elixir_script",
101+
downloads_url: "https://api.github.com/repos/gaggle/elixir_script/downloads",
102+
master_branch: "main",
103+
events_url: "https://api.github.com/repos/gaggle/elixir_script/events",
104+
fork: false,
105+
compare_url:
106+
"https://api.github.com/repos/gaggle/elixir_script/compare/{base}...{head}",
107+
clone_url: "https://github.com/gaggle/elixir_script.git",
108+
watchers_count: 0,
109+
archived: false,
110+
has_discussions: false,
111+
svn_url: "https://github.com/gaggle/elixir_script",
112+
has_downloads: true,
113+
archive_url:
114+
"https://api.github.com/repos/gaggle/elixir_script/{archive_format}{/ref}",
115+
tags_url: "https://api.github.com/repos/gaggle/elixir_script/tags",
116+
is_template: false,
117+
issues_url:
118+
"https://api.github.com/repos/gaggle/elixir_script/issues{/number}",
119+
homepage: "https://github.com/marketplace/actions/elixir-script",
120+
git_tags_url:
121+
"https://api.github.com/repos/gaggle/elixir_script/git/tags{/sha}",
122+
stargazers_url: "https://api.github.com/repos/gaggle/elixir_script/stargazers",
123+
web_commit_signoff_required: false,
124+
comments_url:
125+
"https://api.github.com/repos/gaggle/elixir_script/comments{/number}",
126+
stargazers_count: 0,
127+
owner: %{
128+
id: 2_316_447,
129+
name: "gaggle",
130+
type: "User",
131+
url: "https://api.github.com/users/gaggle",
132+
133+
events_url: "https://api.github.com/users/gaggle/events{/privacy}",
134+
html_url: "https://github.com/gaggle",
135+
node_id: "MDQ6VXNlcjIzMTY0NDc=",
136+
avatar_url: "https://avatars.githubusercontent.com/u/2316447?v=4",
137+
followers_url: "https://api.github.com/users/gaggle/followers",
138+
following_url: "https://api.github.com/users/gaggle/following{/other_user}",
139+
gists_url: "https://api.github.com/users/gaggle/gists{/gist_id}",
140+
gravatar_id: "",
141+
login: "gaggle",
142+
organizations_url: "https://api.github.com/users/gaggle/orgs",
143+
received_events_url: "https://api.github.com/users/gaggle/received_events",
144+
repos_url: "https://api.github.com/users/gaggle/repos",
145+
site_admin: false,
146+
starred_url: "https://api.github.com/users/gaggle/starred{/owner}{/repo}",
147+
subscriptions_url: "https://api.github.com/users/gaggle/subscriptions"
148+
},
149+
git_commits_url:
150+
"https://api.github.com/repos/gaggle/elixir_script/git/commits{/sha}",
151+
visibility: "public"
152+
},
153+
base_ref: nil,
154+
before: "5ace270b6afc55dd3194d7e4cbf684ed753c2008",
155+
commits: [
156+
%{
157+
id: "2ea150fa30b30dec0aeb85eb93469eda2d95c7d7",
158+
message: "Output GITHUB_EVENT_PATH",
159+
timestamp: "2023-11-19T22:53:38+01:00",
160+
author: %{
161+
name: "Jon Lauridsen",
162+
163+
username: "gaggle"
164+
},
165+
url:
166+
"https://github.com/gaggle/elixir_script/commit/2ea150fa30b30dec0aeb85eb93469eda2d95c7d7",
167+
committer: %{
168+
name: "Jon Lauridsen",
169+
170+
username: "gaggle"
171+
},
172+
distinct: true,
173+
tree_id: "e7fa0dcfd93820a600f21dd33e5e30f3eae6c150"
174+
}
175+
],
176+
created: false,
177+
deleted: false,
178+
forced: true,
179+
head_commit: %{
180+
id: "2ea150fa30b30dec0aeb85eb93469eda2d95c7d7",
181+
message: "Output GITHUB_EVENT_PATH",
182+
timestamp: "2023-11-19T22:53:38+01:00",
183+
author: %{
184+
name: "Jon Lauridsen",
185+
186+
username: "gaggle"
187+
},
188+
url:
189+
"https://github.com/gaggle/elixir_script/commit/2ea150fa30b30dec0aeb85eb93469eda2d95c7d7",
190+
committer: %{
191+
name: "Jon Lauridsen",
192+
193+
username: "gaggle"
194+
},
195+
distinct: true,
196+
tree_id: "e7fa0dcfd93820a600f21dd33e5e30f3eae6c150"
197+
},
198+
pusher: %{name: "gaggle", email: "[email protected]"},
199+
sender: %{
200+
id: 2_316_447,
201+
type: "User",
202+
url: "https://api.github.com/users/gaggle",
203+
events_url: "https://api.github.com/users/gaggle/events{/privacy}",
204+
html_url: "https://github.com/gaggle",
205+
node_id: "MDQ6VXNlcjIzMTY0NDc=",
206+
avatar_url: "https://avatars.githubusercontent.com/u/2316447?v=4",
207+
followers_url: "https://api.github.com/users/gaggle/followers",
208+
following_url: "https://api.github.com/users/gaggle/following{/other_user}",
209+
gists_url: "https://api.github.com/users/gaggle/gists{/gist_id}",
210+
gravatar_id: "",
211+
login: "gaggle",
212+
organizations_url: "https://api.github.com/users/gaggle/orgs",
213+
received_events_url: "https://api.github.com/users/gaggle/received_events",
214+
repos_url: "https://api.github.com/users/gaggle/repos",
215+
site_admin: false,
216+
starred_url: "https://api.github.com/users/gaggle/starred{/owner}{/repo}",
217+
subscriptions_url: "https://api.github.com/users/gaggle/subscriptions"
218+
}
219+
},
220+
ref: "refs/heads/main",
221+
run_id: 6_922_972_178,
222+
run_number: 15,
223+
server_url: "https://github.com",
224+
sha: "77cefc59a7095f6b86508e8e26ff9f866da4b5e5",
225+
workflow: "Examples"
226+
}
227+
end
228+
end
229+
end

test/e2e_test.exs

+8
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ defmodule ElixirScript.E2eTest do
44
ensuring that the test file reading and parsing behaviors are working as expected.
55
"""
66
use ExUnit.Case
7+
import Mox
78

89
alias ElixirScript.E2e
910
alias ElixirScript.E2eTest.Runner
11+
alias Test.Fixtures.GitHubWorkflowRun
12+
13+
setup :verify_on_exit!
1014

1115
describe "read_test_file" do
1216
test "when providing a name it gets slugified" do
@@ -52,6 +56,10 @@ defmodule ElixirScript.E2eTest do
5256

5357
describe "end-to-end tests" do
5458
test "run e2e tests" do
59+
stub(SystemEnvBehaviourMock, :get_env, fn varname, default ->
60+
GitHubWorkflowRun.env()[varname] || default
61+
end)
62+
5563
E2e.read_test_file()
5664
|> Enum.each(&Runner.run_test/1)
5765
end

0 commit comments

Comments
 (0)