Skip to content

Commit 9e44515

Browse files
committed
Create Tentacat client and put it into script bindings
1 parent 081cb03 commit 9e44515

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

lib/script_runner.ex

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,23 @@ defmodule ElixirScript.ScriptRunner do
44
"""
55
alias ElixirScript.Context
66

7-
def run(script) do
8-
{value, _binding} = Code.eval_string(script, context: Context.from_github_environment())
7+
def run(script, opts \\ []) do
8+
token = Keyword.get(opts, :github_token)
9+
10+
client =
11+
if token != nil,
12+
do: tentacat_client().new(%{access_token: token}),
13+
else: tentacat_client().new()
14+
15+
{value, _binding} =
16+
Code.eval_string(
17+
script,
18+
context: Context.from_github_environment(),
19+
client: client
20+
)
21+
922
value
1023
end
24+
25+
defp tentacat_client, do: Application.get_env(:script_runner, :tentacat_client, Tentacat.Client)
1126
end

test/e2e_test.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ defmodule ElixirScript.E2eTest do
6060
GitHubWorkflowRun.env()[varname] || default
6161
end)
6262

63+
stub(TentacatMock.ClientMock, :new, fn -> %{auth: nil, endpoint: "github"} end)
64+
6365
E2e.read_test_file()
6466
|> Enum.each(&Runner.run_test/1)
6567
end

test/script_runner_test.exs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ defmodule ElixirScript.ScriptRunnerTest do
1111
GitHubWorkflowRun.env()[varname] || default
1212
end)
1313

14+
stub(TentacatMock.ClientMock, :new, fn -> %{auth: nil, endpoint: "github"} end)
15+
1416
:ok
1517
end
1618

@@ -31,4 +33,27 @@ defmodule ElixirScript.ScriptRunnerTest do
3133
ScriptRunner.run(script)
3234
end
3335
end
36+
37+
describe "github access" do
38+
test "executes with an un-authenticated Tentacat GitHub client by default" do
39+
expect(TentacatMock.ClientMock, :new, fn -> %{auth: nil, endpoint: "github"} end)
40+
41+
assert ScriptRunner.run("true")
42+
end
43+
44+
test "passes the Tentacat client into bindings" do
45+
expect(TentacatMock.ClientMock, :new, fn -> %{auth: nil, endpoint: "github"} end)
46+
47+
assert ScriptRunner.run("client") == %{auth: nil, endpoint: "github"}
48+
end
49+
50+
test "passes access token opts into the client, if available" do
51+
expect(TentacatMock.ClientMock, :new, fn %{access_token: token} ->
52+
%{auth: token, endpoint: "github"}
53+
end)
54+
55+
opts = [github_token: "token"]
56+
assert ScriptRunner.run("client", opts) == %{auth: "token", endpoint: "github"}
57+
end
58+
end
3459
end

0 commit comments

Comments
 (0)