Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: pre-commit hook #513

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 10 additions & 0 deletions bin/pre-commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh
# pre-commit hook script

set -e

echo 'Running pipeline...'

mix ci

echo 'Success running pipeline!'
22 changes: 22 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,28 @@ else
log_warn ".env.${ENV} file already exists, skipping..."
fi

log_info "pre-commit" "Installing pre-commit hooks..."

PROJECT_ROOT=$(git rev-parse --show-toplevel 2>/dev/null)

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

if [ -z "$PROJECT_ROOT" ]; then
log_info "error" "Could not find the project root with a .git directory."
exit 1
fi

SOURCE_PATH="$SCRIPT_DIR/pre-commit.sh"
DESTINATION_PATH="$PROJECT_ROOT/.git/hooks/pre-commit"

if [ ! -f "$DESTINATION_PATH" ]; then
cp "$SOURCE_PATH" "$DESTINATION_PATH"
chmod +x "$DESTINATION_PATH"
log_info "pre-commit" "Pre-commit hook installed successfully."
else
log_info "pre-commit" "A pre-commit hook already exists at $DESTINATION_PATH. Installation skipped."
fi

if [ $MODE == "local" ]; then
log_info "setup" "Installing required languages..."
if not_installed "asdf"; then
Expand Down
6 changes: 3 additions & 3 deletions lib/atomic/activities.ex
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,13 @@ defmodule Atomic.Activities do

## Examples

iex> is_participating?(activity_id, user_id)
iex> participating?(activity_id, user_id)
true

iex> is_participating?(activity_id, user_id)
iex> participating?(activity_id, user_id)
false
"""
def is_participating?(activity_id, user_id) do
def participating?(activity_id, user_id) do
ActivityEnrollment
|> where(activity_id: ^activity_id, user_id: ^user_id)
|> Repo.exists?()
Expand Down
28 changes: 3 additions & 25 deletions lib/atomic/organizations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -310,14 +310,14 @@ defmodule Atomic.Organizations do

## Examples

iex> is_member_of?(user, organization)
iex> member_of?(user, organization)
true

iex> is_member_of?(user, organization)
iex> member_of?(user, organization)
false

"""
def is_member_of?(%User{} = user, %Organization{} = organization) do
def member_of?(%User{} = user, %Organization{} = organization) do
Membership
|> where([m], m.user_id == ^user.id and m.organization_id == ^organization.id)
|> Repo.exists?()
Expand Down Expand Up @@ -354,28 +354,6 @@ defmodule Atomic.Organizations do
end
end

@doc """
Verifies if an user is an admin or owner of an organization that is organizing an activity.

## Examples

iex> is_admin?(user, departments)
true

iex> is_admin?(user, departments)
false

"""
def is_admin?(_user, []), do: false

def is_admin?(user, [department | rest]) do
case get_role(user.id, department.organization_id) do
:owner -> true
:admin -> true
_ -> is_admin?(user, rest)
end
end

@doc """
Gets a single membership.

Expand Down
18 changes: 9 additions & 9 deletions lib/atomic_web/components/table.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ defmodule AtomicWeb.Components.Table do

~H"""
<th class="border-r-[1px] px-4 py-3.5 text-left text-sm font-semibold text-zinc-900" scope="col">
<%= if is_sortable?(@field, @meta.schema) && is_filterable?(@field, @meta.schema) && should_filter(@field, @filter) do %>
<%= if sortable?(@field, @meta.schema) && filterable?(@field, @meta.schema) && should_filter(@field, @filter) do %>
<div class="flex justify-between">
<.link patch={build_sorting_query(@field, @meta)} class="mr-2 w-full">
<div class="flex justify-between">
Expand All @@ -60,15 +60,15 @@ defmodule AtomicWeb.Components.Table do
<.filter_input field={@field} meta={@meta} filter={extract_filter_type(@field, @filter)} />
</div>
<% else %>
<%= if is_sortable?(@field, @meta.schema) do %>
<%= if sortable?(@field, @meta.schema) do %>
<.link patch={build_sorting_query(@field, @meta)}>
<div class="flex justify-between">
<span><%= @label %></span>
<.sorting_arrow direction={@direction} />
</div>
</.link>
<% else %>
<%= if is_filterable?(@field, @meta.schema) && should_filter(@field, @filter) do %>
<%= if filterable?(@field, @meta.schema) && should_filter(@field, @filter) do %>
<div class="flex justify-between">
<span><%= @label %></span>
<.filter_input field={@field} meta={@meta} filter={extract_filter_type(@field, @filter)} />
Expand Down Expand Up @@ -167,17 +167,17 @@ defmodule AtomicWeb.Components.Table do
defp order_direction(nil, _), do: :asc
defp order_direction(directions, index), do: Enum.at(directions, index)

defp is_sortable?(nil, _), do: false
defp is_sortable?(_, nil), do: true
defp sortable?(nil, _), do: false
defp sortable?(_, nil), do: true

defp is_sortable?(field, module) do
defp sortable?(field, module) do
field in (module |> struct() |> Flop.Schema.sortable())
end

defp is_filterable?(nil, _), do: false
defp is_filterable?(_, nil), do: true
defp filterable?(nil, _), do: false
defp filterable?(_, nil), do: true

defp is_filterable?(field, module) do
defp filterable?(field, module) do
field in (module |> struct() |> Flop.Schema.filterable())
end
end
2 changes: 1 addition & 1 deletion lib/atomic_web/live/activity_live/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ defmodule AtomicWeb.ActivityLive.Show do
defp maybe_put_enrolled(socket) when not socket.assigns.is_authenticated?, do: false

defp maybe_put_enrolled(socket) do
Activities.is_participating?(socket.assigns.id, socket.assigns.current_user.id)
Activities.participating?(socket.assigns.id, socket.assigns.current_user.id)
end

defp has_permissions?(socket) when not socket.assigns.is_authenticated?, do: false
Expand Down
2 changes: 1 addition & 1 deletion lib/atomic_web/live/organization_live/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ defmodule AtomicWeb.OrganizationLive.Show do
do: false

defp maybe_put_following(socket, organization) do
Organizations.is_member_of?(socket.assigns.current_user, organization)
Organizations.member_of?(socket.assigns.current_user, organization)
end

defp has_permissions?(socket, _organization_id) when not socket.assigns.is_authenticated?,
Expand Down
8 changes: 7 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule Atomic.MixProject do
start_permanent: Mix.env() == :prod,
aliases: aliases(),
deps: deps(),
preferred_cli_env: [check: :test]
preferred_cli_env: [check: :test, ci: :test]
]
end

Expand Down Expand Up @@ -119,6 +119,12 @@ defmodule Atomic.MixProject do
"deps.unlock --check-unused",
"test",
"lint"
],
ci: [
"compile",
"format --check-formatted",
"lint",
"test"
]
]
end
Expand Down
Loading
Loading