Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: remote_render_markdown_html/4 #23 #30

Merged
merged 3 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ by adding `gogs` to the list of dependencies in your `mix.exs` file:
```elixir
def deps do
[
{:gogs, "~> 0.9.0"}
{:gogs, "~> 1.0.0"}
]
end
```
Expand Down Expand Up @@ -351,16 +351,16 @@ You will see output similar to the following:

```sh
Finished in 0.1 seconds (0.1s async, 0.00s sync)
3 doctests, 25 tests, 0 failures
3 doctests, 27 tests, 0 failures

Randomized with seed 158554
Randomized with seed 715101
----------------
COV FILE LINES RELEVANT MISSED
100.0% lib/git_mock.ex 55 7 0
100.0% lib/gogs.ex 182 37 0
100.0% lib/gogs.ex 212 41 0
100.0% lib/helpers.ex 131 17 0
100.0% lib/http.ex 101 16 0
100.0% lib/httpoison_mock.ex 106 15 0
100.0% lib/http.ex 119 18 0
100.0% lib/httpoison_mock.ex 124 20 0
[TOTAL] 100.0%
----------------
```
Expand All @@ -381,22 +381,22 @@ You should see the same output:

```sh
Finished in 5.5 seconds (5.5s async, 0.00s sync)
3 doctests, 25 tests, 0 failures
3 doctests, 27 tests, 0 failures

Randomized with seed 5018
Randomized with seed 388372
----------------
COV FILE LINES RELEVANT MISSED
100.0% lib/git_mock.ex 55 7 0
100.0% lib/gogs.ex 182 37 0
100.0% lib/gogs.ex 212 41 0
100.0% lib/helpers.ex 131 17 0
100.0% lib/http.ex 101 16 0
100.0% lib/httpoison_mock.ex 106 15 0
100.0% lib/http.ex 119 18 0
100.0% lib/httpoison_mock.ex 124 20 0
[TOTAL] 100.0%
----------------
```

The only difference is the time it takes to run the test suite.
The outcome (all tests passing and 100% coverage) should be identical.
The only difference is the ***time*** it takes to run the test suite. <br />
The outcome (all tests passing and **100% coverage**) should be ***identical***.

If you add a feature to the package,
please ensure that the tests pass
Expand Down
32 changes: 31 additions & 1 deletion lib/gogs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ defmodule Gogs do
end

@doc """
`remote_read_file/3` reads a file from the remote repo.
`remote_read_file/4` reads a file from the remote repo.
Accepts 4 arguments: `org_name`, `repo_name`, `file_name` and `branch_name`.
The 4<sup>th</sup> argument is *optional* and defaults to `"master"`
(the default branch for a repo hosted on `Gogs`).
Expand All @@ -82,6 +82,36 @@ defmodule Gogs do
GogsHttp.get_raw(url)
end

@doc """
`remote_render_markdown_html/4` uses `Gog` built-in Markdown processor
to render a Markdown Doc e.g. the `README.md` so you can easily use the HTML.
Accepts 4 arguments: `org_name`, `repo_name`, `file_name` and `branch_name`.
The 4<sup>th</sup> argument is *optional* and defaults to `"master"`
(the default branch for a repo hosted on `Gogs`).
Makes a `GET` request to the remote `Gogs` instance as defined
by the environment variable `GOGS_URL`.
Returns `{:ok, %HTTPoison.Response{ body: response_body}}`
Uses REST API Endpoint:
```sh
POST /markdown/raw
```
Ref: https://github.com/dwyl/gogs/issues/23
"""
@spec remote_render_markdown_html(String.t(), String.t(), String.t(), String.t()) ::
{:ok, map} | {:error, any}
def remote_render_markdown_html(org_name, repo_name, file_name, branch_name \\ "master") do
# First retrieve the Raw Markdown Text we want to render:
{:ok, %HTTPoison.Response{body: raw_markdown}} =
Gogs.remote_read_raw(org_name, repo_name, file_name, branch_name)
url = @api_base_url <> "markdown/raw"
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)
# 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

@doc """
`clone/1` clones a remote git repository based on `git_repo_url`
returns the path of the _local_ copy of the repository.
Expand Down
8 changes: 6 additions & 2 deletions lib/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,15 @@ defmodule GogsHelpers do
def local_repo_path(org, repo) do
# coveralls-ignore-start
if @mock do
Path.join([temp_dir(@git_dir), "test-repo"]) |> Path.expand()
if String.contains?(repo, "no-repo") do
# in branch test we need to simulate a full path not a test-repo one ...
Path.join([temp_dir(@git_dir), org, repo]) |> Path.expand()
else
Path.join([temp_dir(@git_dir), "test-repo"]) |> Path.expand()
end
else
Path.join([temp_dir(@git_dir), org, repo]) |> Path.expand()
end

# coveralls-ignore-stop
end

Expand Down
24 changes: 21 additions & 3 deletions lib/http.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ defmodule GogsHttp do

@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"},
{"Authorization", "token #{@access_token}"},
@auth_header,
{"Content-Type", "application/json"}
]
@mock Application.compile_env(:gogs, :mock)
Expand Down Expand Up @@ -69,8 +70,25 @@ defmodule GogsHttp do
@spec get_raw(String.t()) :: {:ok, map} | {:error, any}
def get_raw(url) do
Logger.debug("GogsHttp.get_raw #{url}")
headers = [{"Authorization", "token #{@access_token}"}]
inject_poison().get(url, headers)
inject_poison().get(url, [@auth_header])
end

@doc """
`post_raw_html/2` accepts two arguments: `url` and `raw_markdown`.
Makes an `HTTP POST` request to the specified `url`
passing in the `params` as the request body.
Does NOT attempt to parse the response body as JSON.
Auth Headers and Content-Type are implicit.
"""
@spec post_raw_html(String.t(), String.t()) :: {:ok, String.t()} | {:error, any}
def post_raw_html(url, raw_markdown) do
Logger.debug("GogsHttp.post_raw #{url}")
# Logger.debug("raw_markdown: #{raw_markdown}")
headers = [
{"Accept", "text/html"},
@auth_header,
]
inject_poison().post(url, raw_markdown, headers)
end

@doc """
Expand Down
28 changes: 23 additions & 5 deletions lib/httpoison_mock.ex
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,30 @@ defmodule Gogs.HTTPoisonMock do
`post/3` mocks the HTTPoison.post/3 function when parameters match test vars.
Feel free refactor this if you can make it pretty.
"""
def post(url, body, _headers) do
def post(url, body, headers) do
Logger.debug("Gogs.HTTPoisonMock.post/3 #{url}")
body_map = Jason.decode!(body) |> Useful.atomize_map_keys()
response_body =
make_repo_create_post_response_body(body_map.name)
|> Jason.encode!()
if String.contains?(url, "markdown/raw") do
post_raw_html(url, body, headers)
else
body_map = Jason.decode!(body) |> Useful.atomize_map_keys()
response_body =
make_repo_create_post_response_body(body_map.name)
|> Jason.encode!()
{:ok, %HTTPoison.Response{body: response_body, status_code: 200}}
end
end

def raw_html do
"<h1>public-repo</h1>\n\n<p>please don&#39;t update this. the tests read it.</p>\n"
end

@doc """
`post_raw/3` mocks the GogsHttp.post_raw/3 function.
Feel free refactor this if you can make it pretty.
"""
def post_raw_html(url, _body, _headers) do
Logger.debug("Gogs.HTTPoisonMock.post_raw/3 #{url}")
response_body = raw_html()
{:ok, %HTTPoison.Response{body: response_body, status_code: 200}}
end

Expand Down
5 changes: 3 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.9.0",
version: "1.0.0",
elixir: @elixir_requirement,
start_permanent: Mix.env() == :prod,
deps: deps(),
Expand All @@ -20,7 +20,7 @@ defmodule Gogs.MixProject do
aliases: aliases(),
deps: deps(),
package: package(),
description: "Interface with a Gogs (Git) Server"
description: "Simple Elixir interface with a Gogs (Git) Server"
]
end

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

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

# Check environment variables: github.com/dwyl/envar
{:envar, "~> 1.0.5"},

Expand Down
14 changes: 13 additions & 1 deletion test/gogs_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ defmodule GogsTest do
Gogs.remote_repo_delete(org_name, repo_name)
end

test "Gogs.remote_read_raw/3 retrieves the contents of the README.md file" do
test "Gogs.remote_read_raw/4 retrieves the contents of the README.md file" do
org_name = "myorg"
repo_name = "public-repo"
file_name = "README.md"
Expand All @@ -60,6 +60,18 @@ defmodule GogsTest do
assert expected == response_body
end

test "Gogs.remote_render_markdown_html/4 renders README.md file as HTML!" do
org_name = "myorg"
repo_name = "public-repo"
file_name = "README.md"

{:ok, %HTTPoison.Response{body: response_body}} =
Gogs.remote_render_markdown_html(org_name, repo_name, file_name)

# IO.inspect(response_body)
assert Gogs.HTTPoisonMock.raw_html() == response_body
end

test "Gogs.clone clones a known remote repository Gogs on Fly.io" do
org = "nelsonic"
repo = "public-repo"
Expand Down
7 changes: 7 additions & 0 deletions test/httpoison_mock_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,11 @@ defmodule HttPoisonMockTest do
{:ok, %HTTPoison.Response{status_code: status}} = Gogs.HTTPoisonMock.delete("any?url")
assert status == 200
end

test "Gogs.HTTPoisonMock.post when url is markdown/raw should return status 200" do
{:ok, %HTTPoison.Response{status_code: status, body: body}} =
Gogs.HTTPoisonMock.post("markdown/raw", "any", "any")
assert status == 200
assert body == Gogs.HTTPoisonMock.raw_html()
end
end