Skip to content

Commit

Permalink
add remote_repo_create/3 for #9 (comment)
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed May 2, 2022
1 parent 104befe commit 28aa2fa
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 10 deletions.
2 changes: 1 addition & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ import Config
config :gogs,
# client_id: "d6fca75c63daa014c187",
# client_secret: "8eeb143935d1a505692aaef856db9b4da8245f3c",
httpoison_mock: true
httpoison_mock: false
74 changes: 67 additions & 7 deletions lib/gogs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@ defmodule Gogs do
We would _obviously_ prefer if everything was one or the other,
but sadly, some things cannot be done via `Git` or `REST`
so we have adopted a "hybrid" approach.
"""
The functions in this file are defined in the order that we
are _using_ them. So they tell a story.
If you are reading this and prefer to order them alphabetically
or some other way, please share by opening an issue:
github.com/dwyl/gogs/issues
"""
@access_token Envar.get("GOGS_ACCESS_TOKEN")
@api_base_url GogsHelpers.api_base_url()
@httpoison (Application.compile_env(:gogs, :httpoison_mock) &&
Gogs.HTTPoisonMock) || HTTPoison

Expand Down Expand Up @@ -48,12 +55,64 @@ defmodule Gogs do
"#{base_url}#{org}/#{repo}.git"
end

@doc """
`parse_body_response/1` parses the response returned by the Gogs Server
so your app can use the resulting JSON.
"""
@spec parse_body_response({atom, String.t()} | {:error, any}) :: {:ok, map} | {:error, any}
def parse_body_response({:error, err}), do: {:error, err}

def parse_body_response({:ok, response}) do
# IO.inspect(response)
body = Map.get(response, :body)
# make keys of map atoms for easier access in templates
if body == nil || byte_size(body) == 0 do
IO.inspect("response body is nil")
{:error, :no_body}
else
{:ok, str_key_map} = Jason.decode(body)
{:ok, Useful.atomize_map_keys(str_key_map)}
end

# https://stackoverflow.com/questions/31990134
end

@doc """
`post/2` accepts two arguments: `url` and `params`.
"""
@spec post(String.t(), map) :: {:ok, map} | {:error, any}
def post(url, params \\ %{}) do
body = Jason.encode!(params)
headers = [
{"Accept", "application/json"},
{"Authorization", "token #{@access_token}"},
{"Content-Type", "application/json"}
]
inject_poison().post(url, body, headers)
|> parse_body_response()
end


@doc """
`remote_repo_create/3` accepts two arguments: `org_name`, `repo_name` & `private`.
It creates a repo on the remote `Gogs` instance as defined
by the environment variable `GOGS_URL`.
For convenience it assumes that you only have _one_ `Gogs` instance.
If you have more or different requirements, please share!
"""
def remote_repo_create(org_name, repo_name, private \\ false) do
url = @api_base_url <> "org/#{org_name}/repos"
IO.inspect(url, label: "remote repo url")
params = %{
name: repo_name,
private: private
}
post(url, params)
end

@doc """
clone/1 clones a remote git repository based on `git_repo_url`
returns the path of the _local_ copy of the repository.
"""
def clone(git_repo_url) do
IO.inspect("git clone #{git_repo_url}")
Expand All @@ -67,11 +126,6 @@ defmodule Gogs do
end
end

# def commit do

# end


# Feel free to refactor/simplify this function if you want.
def get_repo_name_from_url(url) do
String.split(url, "/") |> List.last() |> String.split(".git") |> List.first()
Expand All @@ -81,7 +135,13 @@ defmodule Gogs do
temp_dir() <> "/" <> repo
end

# Made this a function in case we want to
defp temp_dir do
File.cwd!
end


# def commit do

# end
end
12 changes: 12 additions & 0 deletions lib/gogs_helpers.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
defmodule GogsHelpers do
@doc """
`api_base_url/0` returns the `Gogs` Server REST API url for API requests.
## Examples
iex> Gogs.api_base_url()
"https://gogs-server.fly.dev/api/v1/"
"""
def api_base_url do
"https://" <> Envar.get("GOGS_URL") <> "/api/v1/"
end
end
6 changes: 5 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ defmodule Gogs.MixProject do
defp deps do
[
# Make HTTP Requests: github.com/edgurgel/httpoison
{:httpoison, "~> 1.8.0"},
{:httpoison, "~> 1.8.1"},

# Parse JSON data: github.com/michalmuskala/jason
{:jason, "~> 1.2"},
Expand All @@ -46,6 +46,9 @@ defmodule Gogs.MixProject do
# Git interface: github.com/danhper/elixir-git-cli
{:git_cli, "~> 0.3"},

# Useful functions: github.com/dwyl/useful
{:useful, "~> 1.0.0"},

# Check test coverage: github.com/parroty/excoveralls
{:excoveralls, "~> 0.14.4", only: :test},

Expand All @@ -54,6 +57,7 @@ defmodule Gogs.MixProject do

# Keep Code Tidy: https://github.com/rrrene/credo
{:credo, "~> 1.6.4", only: [:dev, :test], runtime: false},

]
end

Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@
"parse_trans": {:hex, :parse_trans, "3.3.1", "16328ab840cc09919bd10dab29e431da3af9e9e7e7e6f0089dd5a2d2820011d8", [:rebar3], [], "hexpm", "07cd9577885f56362d414e8c4c4e6bdf10d43a8767abb92d24cbe8b24c54888b"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"},
"useful": {:hex, :useful, "1.0.0", "bc500df3007cfb1a18978e624270a0261d39f16cf015a3e862ba403641e0f12d", [:mix], [], "hexpm", "5787c05c99af86c598e14cd43e5e9f860a4571b9a8402b0380d4e871542200cc"},
}
30 changes: 29 additions & 1 deletion test/gogs_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ defmodule GogsTest do
assert remote_url == "ssh://[email protected]:10022/nelsonic/public-repo.git"
end

test "remote_repo_create/3 creates a new repo on the Gogs server" do
org_name = "myorg"
repo_name = "test-repo" <> Integer.to_string(System.unique_integer([:positive]))
response = Gogs.remote_repo_create(org_name, repo_name, false)
IO.inspect(response)


assert true == true
end



# test "Gogs.create_org creates a new organisation on the Gogs instance" do
# res = Gogs.clone()
# IO.inspect(res)
Expand All @@ -34,7 +46,13 @@ defmodule GogsTest do
# delete_local_directory("public-repo")
# end

test "Gogs.clone clones a remote repository" do
# test "remote_url/3 returns a valid Gogs (Fly.io) remote URL" do
# git_url = Gogs.make_url("gogs-server.fly.dev", "10022")
# remote_url = Gogs.remote_url(git_url, "nelsonic", "public-repo")
# assert remote_url == "ssh://[email protected]:10022/nelsonic/public-repo.git"
# end

test "Gogs.clone clones a remote repository Gogs on Fly.io" do
url = Envar.get("GOGS_URL")
port = Envar.get("GOGS_SSH_PORT")
git_url = Gogs.make_url(url, port)
Expand All @@ -54,4 +72,14 @@ defmodule GogsTest do
delete_local_directory("public-repo")
end

test "Gogs.clone clones a GitHub Repo just for completeness" do
repo = "studio" # just cause it's empty but still a valid repo.
git_repo_url = "[email protected]:dwyl/#{repo}.git"
path = Gogs.clone(git_repo_url)
IO.inspect(path)
assert path == Gogs.local_repo_path(repo)

# Clean up:
delete_local_directory(repo)
end
end

0 comments on commit 28aa2fa

Please sign in to comment.