Skip to content

Commit

Permalink
update all sub-modules to be name-spaced Gogs.ModuleName as per Libra…
Browse files Browse the repository at this point in the history
…ry guidelines #36
  • Loading branch information
nelsonic committed May 13, 2022
1 parent 5dfda14 commit 008777f
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 54 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ Gogs.remote_repo_create(org_name, repo_name, private)
### 2. _Clone_ the Repo

```elixir
git_repo_url = GogsHelpers.remote_url_ssh(org_name, repo_name)
git_repo_url = Gogs.Helpers.remote_url_ssh(org_name, repo_name)
Gogs.clone(git_repo_url)
```

Expand Down
4 changes: 2 additions & 2 deletions lib/git_mock.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ defmodule Gogs.GitMock do
if String.contains?(url, "error") do
{:error, %Git.Error{message: "git clone error (mock)"}}
else
{:ok, %Git.Repository{path: GogsHelpers.local_repo_path("test-org", "test-repo")}}
{:ok, %Git.Repository{path: Gogs.Helpers.local_repo_path("test-org", "test-repo")}}
end
end
end
Expand All @@ -49,7 +49,7 @@ defmodule Gogs.GitMock do
@spec push(Git.Repository.t(), [any]) :: {:ok, any}
def push(%Git.Repository{path: repo_path}, _args) do
Logger.info("Gogs.GitMock.push #{repo_path}")
repo_name = GogsHelpers.get_repo_name_from_url(repo_path <> ".git")
repo_name = Gogs.Helpers.get_repo_name_from_url(repo_path <> ".git")
{:ok, "To ssh://gogs-server.fly.dev:10022/myorg/#{repo_name}.git\n"}
end
end
10 changes: 5 additions & 5 deletions lib/gogs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule Gogs do
If anything is unclear, please open an issue:
[github.com/dwyl/**gogs/issues**](https://github.com/dwyl/gogs/issues)
"""
import GogsHelpers
import Gogs.Helpers
require Logger

@mock Application.compile_env(:gogs, :mock)
Expand Down Expand Up @@ -44,7 +44,7 @@ defmodule Gogs do
readme: repo_name
}

GogsHttp.post(url, params)
Gogs.Http.post(url, params)
end

@doc """
Expand All @@ -56,7 +56,7 @@ defmodule Gogs do
def remote_repo_delete(org_name, repo_name) do
url = api_base_url() <> "repos/#{org_name}/#{repo_name}"
Logger.info("remote_repo_delete: #{url}")
GogsHttp.delete(url)
Gogs.Http.delete(url)
end

@doc """
Expand All @@ -78,7 +78,7 @@ defmodule Gogs do
def remote_read_raw(org_name, repo_name, file_name, branch_name \\ "master") do
url = api_base_url() <> "repos/#{org_name}/#{repo_name}/raw/#{branch_name}/#{file_name}"
Logger.debug("Gogs.remote_read_raw: #{url}")
GogsHttp.get_raw(url)
Gogs.Http.get_raw(url)
end

@doc """
Expand Down Expand Up @@ -109,7 +109,7 @@ defmodule Gogs do
Logger.info("remote_render_markdown_html/4 #{url}")
# temp_context = "https://github.com/gogs/gogs"
# Ask Gogs to redner the Raw Markdown to HTML:
GogsHttp.post_raw_html(url, raw_markdown)
Gogs.Http.post_raw_html(url, raw_markdown)
# I agree, this is clunky ... We wont use it for latency-sensitive apps.
# But it could be useful for a quick/easy Static Site / Blog App. 💭
end
Expand Down
10 changes: 5 additions & 5 deletions lib/helpers.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule GogsHelpers do
defmodule Gogs.Helpers do
@moduledoc """
Helper functions that can be unit tested independently of the main functions.
If you spot any way to make these better, please share:
Expand All @@ -14,7 +14,7 @@ defmodule GogsHelpers do
`api_base_url/0` returns the `Gogs` Server REST API url for API requests.
## Examples
iex> GogsHelpers.api_base_url()
iex> Gogs.Helpers.api_base_url()
"https://gogs-server.fly.dev/api/v1/"
"""
@spec api_base_url() :: String.t()
Expand All @@ -27,10 +27,10 @@ defmodule GogsHelpers do
If the `port` is set it will be a custom Gogs instance.
## Examples
iex> GogsHelpers.make_url("gogs-server.fly.dev", "10022")
iex> Gogs.Helpers.make_url("gogs-server.fly.dev", "10022")
"ssh://[email protected]:10022/"
iex> GogsHelpers.make_url("github.com")
iex> Gogs.Helpers.make_url("github.com")
"[email protected]:"
"""
Expand All @@ -54,7 +54,7 @@ defmodule GogsHelpers do
def remote_url_ssh(org, repo) do
url = Envar.get("GOGS_URL")
port = Envar.get("GOGS_SSH_PORT")
git_url = GogsHelpers.make_url(url, port)
git_url = Gogs.Helpers.make_url(url, port)
remote_url(git_url, org, repo)
end

Expand Down
36 changes: 22 additions & 14 deletions lib/http.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule GogsHttp do
defmodule Gogs.Http do
@moduledoc """
Documentation for the `HTTP` "verb" functions.
Should be self-explanatory.
Expand All @@ -8,18 +8,26 @@ defmodule GogsHttp do
"""
require Logger

@access_token Envar.get("GOGS_ACCESS_TOKEN")
# HTTP Headers don't change so hard-coded here:
@auth_header {"Authorization", "token #{@access_token}"}
@headers [
{"Accept", "application/json"},
@auth_header,
{"Content-Type", "application/json"}
]
@mock Application.compile_env(:gogs, :mock)
Logger.debug("GogsHttp > config :gogs, mock: #{to_string(@mock)}")
@httpoison (@mock && Gogs.HTTPoisonMock) || HTTPoison

defp access_token do
Envar.get("GOGS_ACCESS_TOKEN")
end

defp auth_header do
{"Authorization", "token #{access_token()}"}
end

defp json_headers do
[
{"Accept", "application/json"},
auth_header(),
{"Content-Type", "application/json"}
]
end

@doc """
`inject_poison/0` injects a TestDouble of HTTPoison in Test.
see: https://github.com/dwyl/elixir-auth-google/issues/35
Expand Down Expand Up @@ -55,7 +63,7 @@ defmodule GogsHttp do
@spec get(String.t()) :: {:ok, map} | {:error, any}
def get(url) do
Logger.debug("GogsHttp.get #{url}")
inject_poison().get(url, @headers)
inject_poison().get(url, json_headers())
|> parse_body_response()
end

Expand All @@ -70,7 +78,7 @@ defmodule GogsHttp do
@spec get_raw(String.t()) :: {:ok, map} | {:error, any}
def get_raw(url) do
Logger.debug("GogsHttp.get_raw #{url}")
inject_poison().get(url, [@auth_header])
inject_poison().get(url, [auth_header()])
end

@doc """
Expand All @@ -86,7 +94,7 @@ defmodule GogsHttp do
# Logger.debug("raw_markdown: #{raw_markdown}")
headers = [
{"Accept", "text/html"},
@auth_header,
auth_header(),
]
inject_poison().post(url, raw_markdown, headers)
end
Expand All @@ -101,7 +109,7 @@ defmodule GogsHttp do
def post(url, params \\ %{}) do
Logger.debug("GogsHttp.post #{url}")
body = Jason.encode!(params)
inject_poison().post(url, body, @headers)
inject_poison().post(url, body, json_headers())
|> parse_body_response()
end

Expand All @@ -112,7 +120,7 @@ defmodule GogsHttp do
@spec delete(String.t()) :: {:ok, map} | {:error, any}
def delete(url) do
Logger.debug("GogsHttp.delete #{url}")
inject_poison().delete(url <> "?token=#{@access_token}")
inject_poison().delete(url <> "?token=#{access_token()}")
|> parse_body_response()
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/httpoison_mock.ex
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ defmodule Gogs.HTTPoisonMock do
true ->
@raw_response
false ->
repo_name = GogsHelpers.get_repo_name_from_url(url)
repo_name = Gogs.Helpers.get_repo_name_from_url(url)
response_body =
make_repo_create_post_response_body(repo_name)
|> Jason.encode!()
Expand Down
8 changes: 4 additions & 4 deletions test/git_mock_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ defmodule GogsGitMockTest do
test "Gogs.GitMock.clone {:error, %Git.Error{message}}" do
repo = "error"
org = "myorg"
url = GogsHelpers.remote_url_ssh(org, repo)
url = Gogs.Helpers.remote_url_ssh(org, repo)
{:error, %Git.Error{message: response_message}} = Gogs.GitMock.clone(url)
expected = "git clone error (mock)"
assert expected == response_message
end

test "Gogs.GitMock.clone returns {:ok, %Git.Repository{path: local_path}}" do
expected = GogsHelpers.local_repo_path("test-org", "test-repo")
expected = Gogs.Helpers.local_repo_path("test-org", "test-repo")
{:ok, %Git.Repository{path: local_path}} = Gogs.GitMock.clone("any-url")
assert expected == local_path
end

test "Gogs.GitMock.clone with list recurses using the first param as url" do
expected = GogsHelpers.local_repo_path("test-org", "test-repo")
expected = Gogs.Helpers.local_repo_path("test-org", "test-repo")

{:ok, %Git.Repository{path: local_path}} =
Gogs.GitMock.clone(["any-url", "/path/to/local/repo"])
Expand All @@ -32,7 +32,7 @@ defmodule GogsGitMockTest do
org_name = "test-org"
repo_name = "test-repo"
expected = "To ssh://gogs-server.fly.dev:10022/myorg/#{repo_name}.git\n"
git_repo = %Git.Repository{path: GogsHelpers.local_repo_path(org_name, repo_name)}
git_repo = %Git.Repository{path: Gogs.Helpers.local_repo_path(org_name, repo_name)}
{:ok, msg} = Gogs.GitMock.push(git_repo, ["any"])
assert expected == msg
end
Expand Down
20 changes: 10 additions & 10 deletions test/gogs_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defmodule GogsTest do
defp create_test_git_repo(org_name) do
repo_name = test_repo()
Gogs.remote_repo_create(org_name, repo_name, false)
git_repo_url = GogsHelpers.remote_url_ssh(org_name, repo_name)
git_repo_url = Gogs.Helpers.remote_url_ssh(org_name, repo_name)
# Logger.debug("create_test_git_repo/1 git_repo_url: #{git_repo_url}")
Gogs.clone(git_repo_url)

Expand All @@ -30,7 +30,7 @@ defmodule GogsTest do

# Cleanup helper functions
defp delete_local_directory(repo_name) do
path = Path.join(GogsHelpers.temp_dir(@git_dir), repo_name)
path = Path.join(Gogs.Helpers.temp_dir(@git_dir), repo_name)
Logger.debug("GogsTest.delete_local_directory: #{path}")
File.rm_rf(path)
end
Expand Down Expand Up @@ -80,10 +80,10 @@ defmodule GogsTest do
test "Gogs.clone clones a known remote repository Gogs on Fly.io" do
org = "nelsonic"
repo = "public-repo"
git_repo_url = GogsHelpers.remote_url_ssh(org, repo)
git_repo_url = Gogs.Helpers.remote_url_ssh(org, repo)

path = Gogs.clone(git_repo_url)
# assert path == GogsHelpers.local_repo_path(repo)
# assert path == Gogs.Helpers.local_repo_path(repo)

# Attempt to clone it a second time to test the :error branch:
path2 = Gogs.clone(git_repo_url)
Expand All @@ -96,17 +96,17 @@ defmodule GogsTest do
test "Gogs.clone error (simulate unhappy path)" do
repo = "error"
org = "myorg"
git_repo_url = GogsHelpers.remote_url_ssh(org, repo)
git_repo_url = Gogs.Helpers.remote_url_ssh(org, repo)
path = Gogs.clone(git_repo_url)
assert path == GogsHelpers.local_repo_path(org, repo)
assert path == Gogs.Helpers.local_repo_path(org, repo)
end

test "local_branch_create/1 creates a new branch on the localhost" do
org_name = "myorg"
repo_name = create_test_git_repo(org_name)
# # delete draft branch if exists:
Git.branch(GogsHelpers.local_git_repo(org_name, repo_name), ["-m", repo_name])
Git.branch(GogsHelpers.local_git_repo(org_name, repo_name), ~w(-d draft))
Git.branch(Gogs.Helpers.local_git_repo(org_name, repo_name), ["-m", repo_name])
Git.branch(Gogs.Helpers.local_git_repo(org_name, repo_name), ~w(-d draft))

{:ok, res} = Gogs.local_branch_create(org_name, repo_name, "draft")
assert res == "Switched to a new branch 'draft'\n"
Expand Down Expand Up @@ -150,7 +150,7 @@ defmodule GogsTest do
Gogs.local_file_write_text(org_name, repo_name, file_name, "text #{repo_name}")

# Confirm the text was written to the file:
file_path = Path.join([GogsHelpers.local_repo_path(org_name, repo_name), file_name])
file_path = Path.join([Gogs.Helpers.local_repo_path(org_name, repo_name), file_name])
assert {:ok, "text #{repo_name}"} == File.read(file_path)

{:ok, msg} =
Expand All @@ -173,7 +173,7 @@ defmodule GogsTest do
Gogs.local_file_write_text(org_name, repo_name, file_name, text)

# Confirm the text was written to the file:
file_path = Path.join([GogsHelpers.local_repo_path(org_name, repo_name), file_name])
file_path = Path.join([Gogs.Helpers.local_repo_path(org_name, repo_name), file_name])
assert {:ok, "text #{repo_name}"} == File.read(file_path)

# Commit the updated text:
Expand Down
18 changes: 9 additions & 9 deletions test/gogshelpers_test.exs
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
defmodule GogsHelpersTest do
defmodule Gogs.HelpersTest do
use ExUnit.Case, async: true
doctest GogsHelpers
doctest Gogs.Helpers

test "GogsHelpers.api_base_url/0 returns the API URL for the Gogs Server" do
assert GogsHelpers.api_base_url() == "https://gogs-server.fly.dev/api/v1/"
test "Gogs.Helpers.api_base_url/0 returns the API URL for the Gogs Server" do
assert Gogs.Helpers.api_base_url() == "https://gogs-server.fly.dev/api/v1/"
end

test "temp_dir/1 returns cwd if no dir supplied" do
dir = Envar.get("GITHUB_WORKSPACE", "tmp")
assert GogsHelpers.temp_dir(dir) == dir
assert Gogs.Helpers.temp_dir(dir) == dir
end

test "make_url/2 (without port) returns a valid GitHub Base URL" do
url = "github.com"
git_url = GogsHelpers.make_url(url)
git_url = Gogs.Helpers.make_url(url)
assert git_url == "[email protected]:"
end

test "make_url/2 returns a valid Gogs (Fly.io) Base URL" do
git_url = GogsHelpers.make_url("gogs-server.fly.dev", "10022")
git_url = Gogs.Helpers.make_url("gogs-server.fly.dev", "10022")
assert git_url == "ssh://[email protected]:10022/"
end

test "remote_url/3 returns a valid Gogs (Fly.io) remote URL" do
git_url = GogsHelpers.make_url("gogs-server.fly.dev", "10022")
remote_url = GogsHelpers.remote_url(git_url, "nelsonic", "public-repo")
git_url = Gogs.Helpers.make_url("gogs-server.fly.dev", "10022")
remote_url = Gogs.Helpers.remote_url(git_url, "nelsonic", "public-repo")
assert remote_url == "ssh://[email protected]:10022/nelsonic/public-repo.git"
end
end
6 changes: 3 additions & 3 deletions test/gogshttp_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ defmodule GogsHttpTest do

# can you think of a better way of testing/simulating this error condition?
test "GogsHttp.parse_body_response({:error, err})" do
assert GogsHttp.parse_body_response({:error, "err"}) == {:error, "err"}
assert Gogs.Http.parse_body_response({:error, "err"}) == {:error, "err"}
end

# We've seen an empty body returned in practice. But how to test it ...?
test "GogsHttp.parse_body_response({:ok, response}) with empty/nil body" do
res = %{body: ""}
assert GogsHttp.parse_body_response({:ok, res}) == {:error, :no_body}
assert Gogs.Http.parse_body_response({:ok, res}) == {:error, :no_body}
end

test "GogsHttp.get/1 gets (or mocks) an HTTP GET request to Gogs Server" do
repo_name = "public-repo"
url = "https://gogs-server.fly.dev/api/v1/repos/myorg/#{repo_name}"
{:ok, response} = GogsHttp.get(url)
{:ok, response} = Gogs.Http.get(url)
# remove unpredictable fields from response when mock:false
drop_fields = ~w(created_at default_branch description id readme size updated_at watchers_count)a
response = Map.drop(response, drop_fields)
Expand Down

0 comments on commit 008777f

Please sign in to comment.