Skip to content

Commit

Permalink
implement remote_repo_delete/2 which calls delete/1 resolves #12
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed May 2, 2022
1 parent b36389c commit b1075a5
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 52 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

# `gogs`

Interface with ***`gogs`*** from **`Elixir`**.
Interface with a **`Gogs`** instance from **`Elixir`**.

[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/dwyl/gogs/Elixir%20CI?label=build&style=flat-square)](https://github.com/dwyl/gogs/actions/workflows/ci.yml)
[![codecov.io](https://img.shields.io/codecov/c/github/dwyl/gogs/master.svg?style=flat-square)](http://codecov.io/github/dwyl/gogs?branch=main)
[![Hex.pm](https://img.shields.io/hexpm/v/gogs?color=brightgreen&style=flat-square)](https://hex.pm/packages/auth)
[![Hex.pm](https://img.shields.io/hexpm/v/gogs?color=brightgreen&style=flat-square)](https://hex.pm/packages/gogs)
[![Libraries.io dependency status](https://img.shields.io/librariesio/release/hex/gogs?logoColor=brightgreen&style=flat-square)](https://libraries.io/hex/gogs)
[![docs](https://img.shields.io/badge/docs-maintained-brightgreen?style=flat-square)](https://hexdocs.pm/gogs/api-reference.html)
[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat-square)](https://github.com/dwyl/gogs/issues)
Expand All @@ -26,14 +26,17 @@ This package is that interface.

## What?

Very much still work-in-progress.
But we already have a handful of functions working.
See: https://hexdocs.pm/gogs/Gogs.html

## Who?

For now this is "for us by us".
But we already have a potential reuse case for this.

## _How_?



## Installation

Install the package from [hex.pm](https://hex.pm/docs/publish),
Expand All @@ -48,6 +51,8 @@ def deps do
end
```

Complete function reference, see: https://hexdocs.pm/gogs/Gogs.html

<!--
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
Expand Down
64 changes: 38 additions & 26 deletions lib/gogs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand Down
20 changes: 20 additions & 0 deletions lib/gogs_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 7 additions & 1 deletion lib/httpoison_mock.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ defmodule Gogs.HTTPoisonMock do
{:ok, %{body: response_body}}
end


@doc """
`post/3` stubs the HTTPoison post function when parameters match test vars.
feel free refactor this if you can make it pretty.
"""
def delete(url) do
{:ok, %{body: Jason.encode!(%{deleted: List.first(String.split(url, "?"))})}}
end

# @body_email_nil %{
# access_token: "12345",
Expand Down
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Gogs.MixProject do
def project do
[
app: :gogs,
version: "0.1.0",
version: "0.2.0",
elixir: @elixir_requirement,
start_permanent: Mix.env() == :prod,
deps: deps(),
Expand Down Expand Up @@ -38,7 +38,7 @@ defmodule Gogs.MixProject do
{:httpoison, "~> 1.8.1"},

# Parse JSON data: github.com/michalmuskala/jason
{:jason, "~> 1.2"},
{:jason, "~> 1.3.0"},

# Check environment variables: github.com/dwyl/envar
{:envar, "~> 1.0.5"},
Expand Down
23 changes: 4 additions & 19 deletions test/gogs_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions test/gogshelpers_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit b1075a5

Please sign in to comment.