-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dry Run + On-Demand PR generation data models (#606)
- Loading branch information
1 parent
eb361c7
commit e0e5bd5
Showing
23 changed files
with
544 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
defmodule Console.Deployments.Pr.Config do | ||
alias Console.Schema.PrAutomation | ||
|
||
@doc """ | ||
Generate onfig for executing a pr template | ||
""" | ||
@spec config(PrAutomation.t, map) :: {:ok, binary} | Console.error | ||
def config(%PrAutomation{} = pr, ctx) do | ||
with {:ok, f} <- Briefly.create(), | ||
{:ok, doc} <- Ymlr.document(structure(pr, ctx)), | ||
:ok <- File.write(f, String.trim_leading(doc, "---\n")), | ||
do: {:ok, f} | ||
end | ||
|
||
defp structure(pr, ctx) do | ||
%{ | ||
apiVersion: "pr.plural.sh/v1alpha1", | ||
kind: "PrTemplate", | ||
spec: Console.mapify(pr.spec) |> Map.put(:message, pr.message), | ||
context: ctx | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
defmodule Console.Deployments.Pr.Dispatcher do | ||
import Console.Deployments.Pr.Git | ||
alias Console.Repo | ||
alias Console.Deployments.Pr.Config | ||
alias Console.Commands.Plural | ||
alias Console.Deployments.Pr.Impl.{Github, Gitlab} | ||
alias Console.Schema.{PrAutomation, ScmConnection} | ||
|
||
@type pr_resp :: {:ok, binary} | Console.error | ||
|
||
@callback create(pr :: PrAutomation.t, identifier :: binary, branch :: binary, context :: map) :: pr_resp | ||
|
||
@doc """ | ||
Fully creates a pr against the working dispatcher implementation | ||
""" | ||
@spec create(PrAutomation.t, binary, binary, map) :: pr_resp | ||
def create(%PrAutomation{} = pr, identifier, branch, ctx) do | ||
%{scm_connection: conn} = pr = Repo.preload(pr, [:scm_connection]) | ||
impl = dispatcher(conn) | ||
with {:ok, conn} <- setup(conn, identifier, branch), | ||
{:ok, f} <- Config.config(pr, ctx), | ||
{:ok, _} <- Plural.template(f), | ||
{:ok, _} <- push(conn, branch), | ||
do: impl.create(pr, identifier, branch, ctx) | ||
end | ||
|
||
defp dispatcher(%ScmConnection{type: :github}), do: Github | ||
defp dispatcher(%ScmConnection{type: :gitlab}), do: Gitlab | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
defmodule Console.Deployments.Pr.Git do | ||
alias Console.Schema.ScmConnection | ||
|
||
@type git_resp :: {:ok, binary} | Console.error | ||
|
||
@spec setup(ScmConnection.t, binary, binary) :: {:ok, ScmConnection.t} | Console.error | ||
def setup(%ScmConnection{} = conn, id, branch) do | ||
with {:ok, dir} <- Briefly.create(directory: true), | ||
conn = %{conn | dir: dir}, | ||
{:ok, _} <- git(conn, "clone", [url(conn, id), dir]), | ||
{:ok, _} <- git(conn, "checkout", ["-b", branch]), | ||
do: {:ok, conn} | ||
end | ||
|
||
@spec commit(ScmConnection.t, binary) :: git_resp | ||
def commit(%ScmConnection{} = conn, msg), do: git(conn, "commit", ["-m", msg]) | ||
|
||
@spec push(ScmConnection.t, binary) :: git_resp | ||
def push(%ScmConnection{} = conn, branch), do: git(conn, "push", ["--set-upstream", "origin", branch]) | ||
|
||
defp git(%ScmConnection{} = conn, cmd, args) when is_list(args) do | ||
case System.cmd("git", [cmd | args], opts(conn)) do | ||
{out, 0} -> {:ok, out} | ||
{out, _} -> {:error, out} | ||
end | ||
end | ||
|
||
defp url(%ScmConnection{username: nil} = conn, id), do: url(%{conn | username: "apikey"}, id) | ||
defp url(%ScmConnection{username: username} = conn, id) do | ||
base = url(conn) | ||
uri = URI.parse("#{base}/#{id}.git") | ||
URI.to_string(%{uri | userinfo: username}) | ||
end | ||
|
||
defp url(%ScmConnection{base_url: base}) when is_binary(base), do: base | ||
defp url(%ScmConnection{type: :github}), do: "https://github.com/" | ||
defp url(%ScmConnection{type: :gitlab}), do: "https://gitlab.com/" | ||
|
||
defp opts(%ScmConnection{dir: dir} = conn), do: [env: env(conn), cd: dir, stderr_to_stdout: true] | ||
|
||
defp env(%ScmConnection{token: password}) when is_binary(password), | ||
do: [{"GIT_ACCESS_TOKEN", password}, {"GIT_ASKPASS", git_askpass()}] | ||
defp env(_), do: [] | ||
|
||
defp git_askpass(), do: Console.conf(:git_askpass) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
defmodule Console.Deployments.Pr.Impl.Github do | ||
@behaviour Console.Deployments.Pr.Dispatcher | ||
|
||
def create(_, _, _, _), do: {:ok, ""} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
defmodule Console.Deployments.Pr.Impl.Gitlab do | ||
@behaviour Console.Deployments.Pr.Dispatcher | ||
|
||
def create(_, _, _, _), do: {:ok, ""} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
defmodule Console.Deployments.Pr.Impl.Pass do | ||
@behaviour Console.Deployments.Pr.Dispatcher | ||
|
||
def create(_, _, _, _), do: {:ok, ""} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.