Skip to content

Commit

Permalink
Add validations for project/cluster pr config types (#1769)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljguarino authored Jan 18, 2025
1 parent fda0da6 commit 7624820
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/console/deployments/pr/validation.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule Console.Deployments.Pr.Validation do
alias Console.Repo
alias Console.Schema.{PrAutomation, Configuration, Project, Cluster}
alias Console.Deployments.{Settings, Clusters}
alias Console.Schema.Configuration.{Validation}

def validate(%PrAutomation{configuration: [_ | _] = config}, ctx) do
Expand All @@ -13,6 +14,20 @@ defmodule Console.Deployments.Pr.Validation do
end
def validate(_, _), do: :ok

defp do_validate(%Configuration{type: :project, name: n}, val) when is_binary(val) do
case Settings.get_project_by_name(val) do
%Project{} -> :ok
_ -> {:error, "field #{n} is not a valid project name"}
end
end

defp do_validate(%Configuration{type: :cluster, name: n}, val) when is_binary(val) do
case Clusters.get_cluster_by_handle(val) do
%Cluster{} -> :ok
_ -> {:error, "field #{n} is not a valid cluster handle"}
end
end

defp do_validate(%Configuration{type: :int}, val) when is_integer(val), do: :ok
defp do_validate(%Configuration{type: :bool}, val) when is_boolean(val), do: :ok

Expand Down
69 changes: 69 additions & 0 deletions test/console/deployments/git_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,37 @@ defmodule Console.Deployments.GitTest do
assert_receive {:event, %PubSub.PullRequestCreated{item: ^pr}}
end

test "it can can create a pull request with a project config" do
user = insert(:user)
conn = insert(:scm_connection, token: "some-pat")
insert(:project, name: "test")
deployment_settings(write_bindings: [%{user_id: user.id}])
pra = insert(:pr_automation,
identifier: "pluralsh/console",
cluster: build(:cluster),
connection: conn,
updates: %{regexes: ["regex"], match_strategy: :any, files: ["file.yaml"], replace_template: "replace"},
configuration: [%{name: "first", type: :project}]
)
expect(Plural, :template, fn f, _, _ -> File.read(f) end)
expect(Tentacat.Pulls, :create, fn _, "pluralsh", "console", %{head: "pr-test"} ->
{:ok, %{"html_url" => "https://github.com/pr/url"}, %HTTPoison.Response{}}
end)
expect(Console.Deployments.Pr.Git, :setup, fn conn, "pluralsh/console", "pr-test" -> {:ok, conn} end)
expect(Console.Deployments.Pr.Git, :commit, fn _, _ -> {:ok, ""} end)
expect(Console.Deployments.Pr.Git, :push, fn _, "pr-test" -> {:ok, ""} end)

{:ok, pr} = Git.create_pull_request(%{
"first" => "test"
}, pra.id, "pr-test", user)

assert pr.cluster_id == pra.cluster_id
assert pr.url == "https://github.com/pr/url"
assert pr.title == pra.title

assert_receive {:event, %PubSub.PullRequestCreated{item: ^pr}}
end

test "it will reject a pull request w/o valid configuration" do
user = insert(:user)
conn = insert(:scm_connection, token: "some-pat")
Expand Down Expand Up @@ -439,6 +470,44 @@ defmodule Console.Deployments.GitTest do
}, pra.id, "pr-test", user)
end

test "it will reject a pull request w/ invalid project names" do
user = insert(:user)
conn = insert(:scm_connection, token: "some-pat")
insert(:project, name: "test")
pra = insert(:pr_automation,
identifier: "pluralsh/console",
cluster: build(:cluster),
connection: conn,
updates: %{regexes: ["regex"], match_strategy: :any, files: ["file.yaml"], replace_template: "replace"},
write_bindings: [%{user_id: user.id}],
create_bindings: [%{user_id: user.id}],
configuration: [%{name: "project", type: :project}]
)

{:error, _} = Git.create_pull_request(%{
"first" => "wrong",
}, pra.id, "pr-test", user)
end

test "it will reject a pull request w/ invalid cluster handles" do
user = insert(:user)
conn = insert(:scm_connection, token: "some-pat")
insert(:cluster, handle: "test")
pra = insert(:pr_automation,
identifier: "pluralsh/console",
cluster: build(:cluster),
connection: conn,
updates: %{regexes: ["regex"], match_strategy: :any, files: ["file.yaml"], replace_template: "replace"},
write_bindings: [%{user_id: user.id}],
create_bindings: [%{user_id: user.id}],
configuration: [%{name: "cluster", type: :cluster}]
)

{:error, _} = Git.create_pull_request(%{
"first" => "wrong",
}, pra.id, "pr-test", user)
end

test "it can create a pull request with a github app" do
user = insert(:user)
{:ok, pem_string, _} = Console.keypair("[email protected]")
Expand Down

0 comments on commit 7624820

Please sign in to comment.