Skip to content
This repository has been archived by the owner on Mar 25, 2023. It is now read-only.

Commit

Permalink
Update remote_org_create parameters
Browse files Browse the repository at this point in the history
Use a Map instead of keyword list for optional API parameters
see: #8 (comment)
  • Loading branch information
SimonLab committed May 17, 2022
1 parent 572c6dc commit d1b0f45
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
16 changes: 7 additions & 9 deletions lib/gitea.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,15 @@ defmodule Gitea do
The second argument opts is a keyword list value
and define the description, full_name and visibility
"""
@spec remote_org_create(String.t(), list()) :: {:ok, map} | {:error, any}
def remote_org_create(org_name, opts \\ []) do
@spec remote_org_create(%{
required(:username) => String.t(),
optional(:description) => String.t(),
optional(:full_name) => String.t(),
optional(:visibility) => String.t()
}) :: {:ok, map} | {:error, any}
def remote_org_create(params) do
url = api_base_url() <> "orgs"

params = %{
descriptions: opts[:description] || "org description",
full_name: opts[:full_name] || org_name,
username: org_name,
visibility: opts[:visibility] || "private"
}

Gitea.Http.post(url, params)
end

Expand Down
4 changes: 4 additions & 0 deletions lib/http.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ defmodule Gitea.Http do
"""
@spec parse_body_response({atom, String.t()} | {:error, any}) :: {:ok, map} | {:error, any}
def parse_body_response({:error, err}), do: {:error, err}
# Deleting a repository or an organisation returns an empty string for the body value
# Instead of returning {:error, :no_body) (see next parse_body_response definition)
# the function returns {:ok, response} when the status code response is 204
# see https://github.com/dwyl/gitea/pull/8#discussion_r874618485
def parse_body_response({:ok, response = %{status_code: 204}}), do: {:ok, response}

def parse_body_response({:ok, response}) do
Expand Down
18 changes: 7 additions & 11 deletions test/gitea_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,15 @@ defmodule GiteaTest do

test "remote_org_create\2 create a new organistaion" do
org_name = "new_org"
{:ok, response} = Gitea.remote_org_create(org_name)

assert response.username
# delete organisation to allow test to run again
{:ok, response_delete} = Gitea.remote_org_delete(org_name)
assert response_delete.status_code == 204
end

test "remote_org_create\2 create a new organistaion with options" do
org_name = "new_org2"
params = %{
username: "new_org",
description: "org description",
full_name: "new organisation",
visibility: "public"
}

{:ok, response} =
Gitea.remote_org_create(org_name, description: "org desc", visibility: "public")
{:ok, response} = Gitea.remote_org_create(params)

assert response.username
# delete organisation to allow test to run again
Expand Down

0 comments on commit d1b0f45

Please sign in to comment.