3.4 KiB · text 5af55d9
defmodule GitGudWeb.InteractionPolicyLive.Edit do
use GitGudWeb, :live_view
alias GitGud.Repositories
alias GitGud.Repositories.Storage
@impl true
def mount(%{"owner" => owner, "name" => name}, _session, socket) do
repo =
Repositories.get_repository_by_path!(owner, name)
|> GitGud.Repo.preload([:owner, :organization])
{:ok,
socket
|> assign(:repo, repo)
|> assign(:handle, Storage.repo_handle(repo))
|> GitGudWeb.RepoLive.Header.assign_chrome(repo)
|> assign(:page_title, "Interaction policy — #{owner}/#{name}")
|> assign_form(repo)}
end
defp assign_form(socket, repo),
do:
assign(
socket,
:form,
to_form(GitGud.Repositories.Repository.update_changeset(repo, %{}))
)
@impl true
def handle_event("save", %{"repository" => attrs}, socket) do
case Repositories.update_repository(socket.assigns.repo, attrs) do
{:ok, repo} ->
{:noreply,
socket
|> assign(:repo, repo)
|> assign_form(repo)
|> put_flash(:info, "Policy updated.")}
{:error, cs} ->
{:noreply, assign(socket, :form, to_form(cs))}
end
end
@impl true
def render(assigns) do
~H"""
<Layouts.app flash={@flash} current_scope={@current_scope}>
<div class="space-y-6">
<GitGudWeb.RepoLive.Header.header
repo={@repo}
handle={@handle}
current_scope={@current_scope}
has_packages?={@has_packages?}
can_admin?={@can_admin?}
latest_run={@latest_run}
/>
<.repo_settings_header handle={@handle} repo={@repo} current={:interaction} />
<p class="text-sm opacity-70 max-w-xl">
Controls who can comment, open issues, and open PRs against this
repository via federation. Pushes to the repo's own branches are
covered by branch protection, not by this setting.
</p>
<.form for={@form} id="policy-form" phx-submit="save" class="space-y-3 max-w-xl">
<fieldset class="space-y-2">
<legend class="text-sm font-medium opacity-80">Who can interact</legend>
<label
:for={{value, label, hint} <- options()}
class="flex items-start gap-3 cursor-pointer"
>
<input
type="radio"
name="repository[interaction_policy]"
value={value}
checked={(@form.params["interaction_policy"] || @repo.interaction_policy) == value}
class="radio radio-sm mt-1"
/>
<span>
<span class="font-medium">{label}</span>
<span class="block text-xs opacity-60">{hint}</span>
</span>
</label>
</fieldset>
<button type="submit" class="btn btn-sm btn-primary">Save</button>
</.form>
</div>
</Layouts.app>
"""
end
defp options do
[
{"public", "Public",
"Anyone on the federation can interact. Best for open-source projects expecting outside contributions."},
{"followers", "Followers only",
"Only actors who follow this repository (and whose follow has been accepted) may interact."},
{"approved", "Approval required",
"Interactions from unknown actors are quarantined; an admin approves each one. The strictest setting — use for high-noise projects or when ramping up federation."}
]
end
end