Skip to content

Commit

Permalink
Merge pull request #1067 from OpenFn/1058-demo-prc
Browse files Browse the repository at this point in the history
Rename project_repo_connections (follow spec and v1), setup PRCs during nightly demo reset
  • Loading branch information
taylordowns2000 authored Aug 30, 2023
2 parents c18a6be + 83a41cd commit 1691359
Show file tree
Hide file tree
Showing 14 changed files with 143 additions and 83 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ and this project adheres to
project.yaml export [#1050](https://github.com/OpenFn/Lightning/issues/1050)
- Allows the demo script to set a project id during creation to help with cli
deploy/pull/Github integration testing.
- Fixed demo project_repo_connection failing after nightly demo resets
[1058](https://github.com/OpenFn/Lightning/issues/1058)
- Fixed an issue where the monaco suggestion tooltip was offset from the main
editor
editor [1030](https://github.com/OpenFn/Lightning/issues/1030)

## [v0.7.3] - 2023-08-15

Expand Down
21 changes: 16 additions & 5 deletions lib/lightning/setup_utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ defmodule Lightning.SetupUtils do
Workflows,
Repo,
Credentials,
AttemptRun
AttemptRun,
WorkOrderService,
VersionControl
}

alias Lightning.WorkOrderService
alias Lightning.Invocation.Run
alias Ecto.Multi

Expand All @@ -29,8 +30,10 @@ defmodule Lightning.SetupUtils do
Creates initial data and returns the created records.
"""
def setup_demo(opts \\ [create_super: false]) do
users = create_users(opts)

%{super_user: super_user, admin: admin, editor: editor, viewer: viewer} =
create_users(opts)
users

%{
project: openhie_project,
Expand All @@ -39,12 +42,20 @@ defmodule Lightning.SetupUtils do
workorder: openhie_workorder
} =
create_openhie_project([
%{user_id: super_user.id, role: :admin},
%{user_id: super_user.id, role: :owner},
%{user_id: admin.id, role: :admin},
%{user_id: editor.id, role: :editor},
%{user_id: viewer.id, role: :viewer}
])

Repo.insert!(%VersionControl.ProjectRepoConnection{
github_installation_id: "39991761",
repo: "OpenFn/demo-openhie",
branch: "main",
project_id: openhie_project.id,
user_id: super_user.id
})

%{
project: dhis2_project,
workflow: dhis2_workflow,
Expand Down Expand Up @@ -104,7 +115,7 @@ defmodule Lightning.SetupUtils do
password: "welcome123"
})

Lightning.Repo.insert!(%Lightning.Accounts.UserToken{
Repo.insert!(%Lightning.Accounts.UserToken{
user_id: super_user.id,
context: "api",
token:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Lightning.VersionControl.ProjectRepo do
defmodule Lightning.VersionControl.ProjectRepoConnection do
@moduledoc """
Ecto model for project repo connections
"""
Expand All @@ -21,7 +21,7 @@ defmodule Lightning.VersionControl.ProjectRepo do

@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "project_repos" do
schema "project_repo_connections" do
field :github_installation_id, :string
field :repo, :string
field :branch, :string
Expand All @@ -33,8 +33,8 @@ defmodule Lightning.VersionControl.ProjectRepo do

@fields ~w(github_installation_id repo branch)a
@required_fields ~w(user_id project_id)a
def changeset(project_repo, attrs) do
project_repo
def changeset(project_repo_connection, attrs) do
project_repo_connection
|> cast(attrs, @fields ++ @required_fields)
|> validate_required(@required_fields)
end
Expand Down
38 changes: 21 additions & 17 deletions lib/lightning/version_control/version_control.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,66 +8,70 @@ defmodule Lightning.VersionControl do

import Ecto.Query, warn: false
alias Lightning.Repo
alias Lightning.VersionControl.ProjectRepo
alias Lightning.VersionControl.ProjectRepoConnection
alias Lightning.VersionControl.GithubClient

@doc """
Creates a connection between a project and a github repo
"""
def create_github_connection(attrs) do
%ProjectRepo{}
|> ProjectRepo.changeset(attrs)
%ProjectRepoConnection{}
|> ProjectRepoConnection.changeset(attrs)
|> Repo.insert()
end

@doc """
Deletes a github connection used when re installing
"""
def remove_github_connection(project_id) do
Repo.one(from(p in ProjectRepo, where: p.project_id == ^project_id))
Repo.one(
from(prc in ProjectRepoConnection, where: prc.project_id == ^project_id)
)
|> Repo.delete()
end

def get_repo_connection(project_id) do
Repo.one(from(p in ProjectRepo, where: p.project_id == ^project_id))
Repo.one(
from(prc in ProjectRepoConnection, where: prc.project_id == ^project_id)
)
end

def add_github_installation_id(user_id, installation_id) do
pending_installation =
Repo.one(
from(p in ProjectRepo,
where: p.user_id == ^user_id and is_nil(p.github_installation_id)
from(prc in ProjectRepoConnection,
where: prc.user_id == ^user_id and is_nil(prc.github_installation_id)
)
)

pending_installation
|> ProjectRepo.changeset(%{github_installation_id: installation_id})
|> ProjectRepoConnection.changeset(%{github_installation_id: installation_id})
|> Repo.update()
end

def add_github_repo_and_branch(project_id, repo, branch) do
pending_installation =
Repo.one(
from(p in ProjectRepo,
where: p.project_id == ^project_id
from(prc in ProjectRepoConnection,
where: prc.project_id == ^project_id
)
)

pending_installation
|> ProjectRepo.changeset(%{repo: repo, branch: branch})
|> ProjectRepoConnection.changeset(%{repo: repo, branch: branch})
|> Repo.update()
end

def fetch_installation_repos(project_id) do
with %ProjectRepo{} = repo_connection <-
Repo.get_by(ProjectRepo, project_id: project_id) do
with %ProjectRepoConnection{} = repo_connection <-
Repo.get_by(ProjectRepoConnection, project_id: project_id) do
GithubClient.installation_repos(repo_connection.github_installation_id)
end
end

def fetch_repo_branches(project_id, repo_name) do
with %ProjectRepo{} = repo_connection <-
Repo.get_by(ProjectRepo, project_id: project_id) do
with %ProjectRepoConnection{} = repo_connection <-
Repo.get_by(ProjectRepoConnection, project_id: project_id) do
GithubClient.get_repo_branches(
repo_connection.github_installation_id,
repo_name
Expand All @@ -76,8 +80,8 @@ defmodule Lightning.VersionControl do
end

def run_sync(project_id, user_name) do
with %ProjectRepo{} = repo_connection <-
Repo.get_by(ProjectRepo, project_id: project_id) do
with %ProjectRepoConnection{} = repo_connection <-
Repo.get_by(ProjectRepoConnection, project_id: project_id) do
GithubClient.fire_repository_dispatch(
repo_connection.github_installation_id,
repo_connection.repo,
Expand Down
6 changes: 4 additions & 2 deletions lib/lightning_web/controllers/version_control_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ defmodule LightningWeb.VersionControlController do
# add installation id to project repo
# {:error, %{reason: "Can't find a pending connection."}}

{:ok, project_repo} =
{:ok, project_repo_connection} =
VersionControl.add_github_installation_id(
conn.assigns.current_user.id,
params["installation_id"]
)

# get project repo connection and forward to project settings
redirect(conn, to: ~p"/projects/#{project_repo.project_id}/settings#vcs")
redirect(conn,
to: ~p"/projects/#{project_repo_connection.project_id}/settings#vcs"
)
end
end
31 changes: 19 additions & 12 deletions lib/lightning_web/live/project_live/settings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ defmodule LightningWeb.ProjectLive.Settings do
socket.assigns.project
)

{show_github_setup, show_repo_setup, show_sync_button, project_repo} =
{show_github_setup, show_repo_setup, show_sync_button,
project_repo_connection} =
repo_settings(socket)

if show_repo_setup do
collect_project_repos(socket.assigns.project.id)
collect_project_repo_connections(socket.assigns.project.id)
end

{:ok,
Expand All @@ -69,7 +70,7 @@ defmodule LightningWeb.ProjectLive.Settings do
show_github_setup: show_github_setup,
show_repo_setup: show_repo_setup,
show_sync_button: show_sync_button,
project_repo: project_repo,
project_repo_connection: project_repo_connection,
repos: [],
branches: [],
loading_branches: false,
Expand All @@ -89,16 +90,16 @@ defmodule LightningWeb.ProjectLive.Settings do
repo_connection =
VersionControl.get_repo_connection(socket.assigns.project.id)

project_repo = %{"repo" => nil, "branch" => nil}
project_repo_connection = %{"repo" => nil, "branch" => nil}

# {show_github_setup, show_repo_setup, show_sync_button}
repo_settings =
case repo_connection do
nil ->
{true, false, false, project_repo}
{true, false, false, project_repo_connection}

%{repo: nil} ->
{false, true, false, project_repo}
{false, true, false, project_repo_connection}

%{repo: r, branch: b} ->
{false, true, true, %{"repo" => r, "branch" => b}}
Expand All @@ -108,7 +109,7 @@ defmodule LightningWeb.ProjectLive.Settings do
end

# we should only run this if repo setting is pending
defp collect_project_repos(project_id) do
defp collect_project_repo_connections(project_id) do
pid = self()

Task.start(fn ->
Expand Down Expand Up @@ -324,7 +325,10 @@ defmodule LightningWeb.ProjectLive.Settings do
|> assign(
show_repo_setup: false,
show_sync_button: true,
project_repo: %{"branch" => params["branch"], "repo" => params["repo"]}
project_repo_connection: %{
"branch" => params["branch"],
"repo" => params["repo"]
}
)}
end

Expand Down Expand Up @@ -356,16 +360,19 @@ defmodule LightningWeb.ProjectLive.Settings do
socket
|> assign(
loading_branches: true,
project_repo: %{socket.assigns.project_repo | "repo" => params["repo"]}
project_repo_connection: %{
socket.assigns.project_repo_connection
| "repo" => params["repo"]
}
)}
end

def handle_event("branch_selected", params, socket) do
{:noreply,
socket
|> assign(
project_repo: %{
socket.assigns.project_repo
project_repo_connection: %{
socket.assigns.project_repo_connection
| "branch" => params["branch"]
}
)}
Expand All @@ -390,7 +397,7 @@ defmodule LightningWeb.ProjectLive.Settings do
{:ok, [_ | _] = repos} ->
{:noreply, socket |> assign(repos: repos)}

# while it's possible to trigger this state when testing
# while it's possible to trigger this state when testing
# Github makes it pretty impossible to arrive here
_ ->
{:noreply, socket}
Expand Down
18 changes: 12 additions & 6 deletions lib/lightning_web/live/project_live/settings.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@

<.form
:let={f}
for={@project_repo}
for={@project_repo_connection}
id="project-vcs-form"
phx-submit="save_repo"
>
Expand All @@ -396,7 +396,7 @@
Repository
<% end %>
<%= if @show_sync_button do %>
Repository: <%= @project_repo["repo"] %>
Repository: <%= @project_repo_connection["repo"] %>
<% end %>
</span>
</small>
Expand All @@ -408,7 +408,10 @@
name={:repo}
id="project-repo"
phx-change="repo_selected"
prompt={@project_repo["repo"] || "Select a Repository"}
prompt={
@project_repo_connection["repo"] ||
"Select a Repository"
}
values={@repos}
/>
</div>
Expand All @@ -424,7 +427,7 @@
Branch
<% end %>
<%= if @show_sync_button do %>
Branch: <%= @project_repo["branch"] %>
Branch: <%= @project_repo_connection["branch"] %>
<% end %>
</span>
</small>
Expand All @@ -436,7 +439,9 @@
name={:branch}
id="project-branch"
values={@branches}
prompt={@project_repo["branch"] || "Select a branch"}
prompt={
@project_repo_connection["branch"] || "Select a branch"
}
/>
</div>
</div>
Expand All @@ -449,7 +454,8 @@
:if={!@show_sync_button}
phx-disable-with="Saving"
disabled={
!@project_repo["repo"] || !@project_repo["branch"]
!@project_repo_connection["repo"] ||
!@project_repo_connection["branch"]
}
>
Connect Branch
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
defmodule Lightning.Repo.Migrations.CreateRepoProjectConnection do
defmodule Lightning.Repo.Migrations.CreateProjectRepoConnections do
use Ecto.Migration

def change do
create table(:project_repos, primary_key: false) do
create table(:project_repo_connections, primary_key: false) do
add :id, :binary_id, primary_key: true
add :github_installation_id, :string
add :repo, :string
Expand Down
Loading

0 comments on commit 1691359

Please sign in to comment.