-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement remote_repo_delete/2 which calls delete/1 resolves #12
- Loading branch information
Showing
7 changed files
with
97 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,27 +27,6 @@ defmodule Gogs do | |
""" | ||
def inject_poison, do: @httpoison | ||
|
||
@doc """ | ||
make_url/2 constructs the URL based on the supplied git `url` and TCP `port`. | ||
If the `port` is set it will be a custom Gogs instance. | ||
## Examples | ||
iex> Gogs.make_url("gogs-server.fly.dev", "10022") | ||
"ssh://[email protected]:10022/" | ||
iex> Gogs.make_url("github.com") | ||
"[email protected]:" | ||
""" | ||
def make_url(git_url, port \\ 0) do | ||
if port > 0 do | ||
"ssh://git@#{git_url}:#{port}/" | ||
else | ||
"git@#{git_url}:" | ||
end | ||
end | ||
|
||
|
||
@doc """ | ||
returns the remote url for cloning | ||
""" | ||
|
@@ -67,7 +46,7 @@ defmodule Gogs do | |
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") | ||
# IO.inspect("response body is nil") | ||
{:error, :no_body} | ||
else | ||
{:ok, str_key_map} = Jason.decode(body) | ||
|
@@ -76,7 +55,10 @@ defmodule Gogs do | |
end | ||
|
||
@doc """ | ||
`post/2` accepts two arguments: `url` and `params`. | ||
`post/2` accepts two arguments: `url` and `params`. | ||
Makes an `HTTP POST` request to the specified `url` | ||
passing in the `params` as the request body. | ||
Auth Headers and Content-Type are implicit. | ||
""" | ||
@spec post(String.t(), map) :: {:ok, map} | {:error, any} | ||
def post(url, params \\ %{}) do | ||
|
@@ -93,7 +75,7 @@ defmodule Gogs do | |
|
||
|
||
@doc """ | ||
`remote_repo_create/3` accepts two arguments: `org_name`, `repo_name` & `private`. | ||
`remote_repo_create/3` accepts 3 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. | ||
|
@@ -111,7 +93,29 @@ defmodule Gogs do | |
end | ||
|
||
@doc """ | ||
clone/1 clones a remote git repository based on `git_repo_url` | ||
`delete/1` accepts a single argument `url`; | ||
the `url` for the repository to be deleted. | ||
""" | ||
@spec delete(String.t()) :: {:ok, map} | {:error, any} | ||
def delete(url) do | ||
inject_poison().delete(url) | ||
|> parse_body_response() | ||
end | ||
|
||
@doc """ | ||
`remote_repo_delete/2` accepts two arguments: `org_name` and `repo_name`. | ||
It deletes the repo on the remote `Gogs` instance as defined | ||
by the environment variable `GOGS_URL`. | ||
""" | ||
def remote_repo_delete(org_name, repo_name) do | ||
url = @api_base_url <> "repos/#{org_name}/#{repo_name}?token=#{@access_token}" | ||
IO.inspect(url, label: "remote_repo_delete url") | ||
delete(url) | ||
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 | ||
|
@@ -126,11 +130,19 @@ defmodule Gogs do | |
end | ||
end | ||
|
||
# Feel free to refactor/simplify this function if you want. | ||
|
||
@doc """ | ||
`get_repo_name_from_url/1` extracts the repository name from a .git url. | ||
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() | ||
end | ||
|
||
@doc """ | ||
`local_repo_path/1` returns the full system path for the cloned repo | ||
on the `localhost` i.e. the Elixir/Phoenix server that cloned it. | ||
""" | ||
def local_repo_path(repo) do | ||
temp_dir() <> "/" <> repo | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,24 @@ defmodule GogsHelpers do | |
def api_base_url do | ||
"https://" <> Envar.get("GOGS_URL") <> "/api/v1/" | ||
end | ||
|
||
@doc """ | ||
`make_url/2` constructs the URL based on the supplied git `url` and TCP `port`. | ||
If the `port` is set it will be a custom Gogs instance. | ||
## Examples | ||
iex> GogsHelpers.make_url("gogs-server.fly.dev", "10022") | ||
"ssh://[email protected]:10022/" | ||
iex> GogsHelpers.make_url("github.com") | ||
"[email protected]:" | ||
""" | ||
def make_url(git_url, port \\ 0) do | ||
if port > 0 do | ||
"ssh://git@#{git_url}:#{port}/" | ||
else | ||
"git@#{git_url}:" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,29 +9,14 @@ defmodule GogsTest do | |
File.rm_rf(dirname) | ||
end | ||
|
||
test "make_url/2 (without port) returns a valid GitHub Base URL" do | ||
url = "github.com" | ||
git_url = Gogs.make_url(url) | ||
assert git_url == "[email protected]:" | ||
end | ||
|
||
test "make_url/2 returns a valid Gogs (Fly.io) Base URL" do | ||
git_url = Gogs.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 = 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 "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) | ||
mock_response = Gogs.HTTPoisonMock.make_repo_create_post_response_body(repo_name) | ||
assert response == {:ok, mock_response} | ||
|
||
Gogs.remote_repo_delete(org_name, repo_name) | ||
end | ||
|
||
# can you think of a better way of testing/simulating this error condition? | ||
|
@@ -57,15 +42,15 @@ defmodule GogsTest do | |
# end | ||
|
||
# test "remote_url/3 returns a valid Gogs (Fly.io) remote URL" do | ||
# git_url = Gogs.make_url("gogs-server.fly.dev", "10022") | ||
# git_url = GogsHelpers.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) | ||
git_url = GogsHelpers.make_url(url, port) | ||
org = "nelsonic" | ||
repo = "public-repo" | ||
git_repo_url = Gogs.remote_url(git_url, org, repo) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,21 @@ defmodule GogsHelpersTest do | |
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/" | ||
end | ||
|
||
test "make_url/2 (without port) returns a valid GitHub Base URL" do | ||
url = "github.com" | ||
git_url = GogsHelpers.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") | ||
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 = Gogs.remote_url(git_url, "nelsonic", "public-repo") | ||
assert remote_url == "ssh://[email protected]:10022/nelsonic/public-repo.git" | ||
end | ||
end |