Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 56 additions & 2 deletions lib/algora/bounties/bounties.ex
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ defmodule Algora.Bounties do
end
end

def create_bounty(_params, opts \\ [])

@spec create_bounty(
%{
creator: User.t(),
Expand All @@ -118,7 +120,7 @@ defmodule Algora.Bounties do
amount: amount,
ticket_ref: %{owner: repo_owner, repo: repo_name, number: number} = ticket_ref
},
opts \\ []
opts
) do
command_id = opts[:command_id]
shared_with = opts[:shared_with] || []
Expand Down Expand Up @@ -177,6 +179,49 @@ defmodule Algora.Bounties do
end)
end

@spec create_bounty(
%{
creator: User.t(),
owner: User.t(),
amount: Money.t(),
title: String.t(),
description: String.t()
},
opts :: [
strategy: strategy(),
visibility: Bounty.visibility() | nil,
shared_with: [String.t()] | nil
]
) ::
{:ok, Bounty.t()} | {:error, atom()}
def create_bounty(%{creator: creator, owner: owner, amount: amount, title: title, description: description}, opts) do
shared_with = opts[:shared_with] || []

Repo.transact(fn ->
with {:ok, ticket} <-
%Ticket{type: :issue}
|> Ticket.changeset(%{title: title, description: description})
|> Repo.insert(),
{:ok, bounty} <-
do_create_bounty(%{
creator: creator,
owner: owner,
amount: amount,
ticket: ticket,
visibility: opts[:visibility],
shared_with: shared_with
}),
{:ok, _job} <- notify_bounty(%{owner: owner, bounty: bounty}) do
broadcast()
{:ok, bounty}
else
{:error, _reason} = error ->
Algora.Admin.alert("Error creating bounty: #{inspect(error)}", :error)
error
end
end)
end

defp claim_to_solution(claim) do
%{
type: :claim,
Expand Down Expand Up @@ -337,6 +382,8 @@ defmodule Algora.Bounties do
end
end

def notify_bounty(bounty, opts \\ [])

@spec notify_bounty(
%{
owner: User.t(),
Expand All @@ -346,7 +393,7 @@ defmodule Algora.Bounties do
opts :: [installation_id: integer(), command_id: integer(), command_source: :ticket | :comment]
) ::
{:ok, Oban.Job.t()} | {:error, atom()}
def notify_bounty(%{owner: owner, bounty: bounty, ticket_ref: ticket_ref}, opts \\ []) do
def notify_bounty(%{owner: owner, bounty: bounty, ticket_ref: ticket_ref}, opts) do
%{
owner_login: owner.provider_login,
amount: Money.to_string!(bounty.amount, no_fraction_if_integer: true),
Expand All @@ -362,6 +409,13 @@ defmodule Algora.Bounties do
|> Oban.insert()
end

@spec notify_bounty(%{owner: User.t(), bounty: Bounty.t()}, opts :: []) ::
{:ok, nil} | {:error, atom()}
def notify_bounty(%{owner: _owner, bounty: bounty}, _opts) do
Algora.Admin.alert("Notify bounty: #{inspect(bounty)}", :error)
{:ok, nil}
end

@spec do_claim_bounty(%{
provider_login: String.t(),
token: String.t(),
Expand Down
2 changes: 2 additions & 0 deletions lib/algora/organizations/schemas/member.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ defmodule Algora.Organizations.Member do
join: o in assoc(m, :org),
where: o.id == ^org_id
end

def can_create_bounty?(role), do: role in [:admin, :mod]
end
7 changes: 7 additions & 0 deletions lib/algora/workspace/schemas/ticket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ defmodule Algora.Workspace.Ticket do
timestamps()
end

def changeset(ticket, params) do
ticket
|> cast(params, [:title, :description, :url])
|> validate_required([:title])
|> generate_id()
end

def github_changeset(meta, repo) do
params = %{
provider_id: to_string(meta["id"]),
Expand Down
10 changes: 6 additions & 4 deletions lib/algora_web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ defmodule AlgoraWeb do
use Phoenix.LiveView,
layout: {AlgoraWeb.Layouts, :app}

import Tails, only: [classes: 1]

unquote(html_helpers())
end
end
Expand Down Expand Up @@ -87,13 +85,17 @@ defmodule AlgoraWeb do
quote do
use Gettext, backend: AlgoraWeb.Gettext

# Core UI components and translation
import AlgoraWeb.CoreComponents

# Enable rendering Svelte components
import LiveSvelte

# HTML escaping functionality
import Phoenix.HTML
# Core UI components and translation

# Enable rendering Svelte components
# class helpers
import Tails, only: [classes: 1]

# Shortcut for generating JS commands
alias Phoenix.LiveView.JS
Expand Down
18 changes: 18 additions & 0 deletions lib/algora_web/components/layouts/user.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,24 @@
</.button>
</ul>
<% end %>
<%= if main_bounty_form = Map.get(assigns, :main_bounty_form) do %>
<.button phx-click="open_main_bounty_form">Create new bounty</.button>
<.drawer
show={@main_bounty_form_open?}
direction="right"
on_cancel="close_main_bounty_form"
>
<.drawer_header>
<.drawer_title>Create new bounty</.drawer_title>
<.drawer_description>
Create and fund a bounty for an issue
</.drawer_description>
</.drawer_header>
<.drawer_content class="mt-4">
<AlgoraWeb.Forms.BountyForm.bounty_form form={main_bounty_form} />
</.drawer_content>
</.drawer>
<% end %>
<div class="ml-auto flex items-center gap-x-6">
<.link
class="group w-fit outline-none"
Expand Down
Loading