Skip to content

Commit 173ab9c

Browse files
committed
implement remote_read_raw/4 for #21
1 parent 9ebca23 commit 173ab9c

File tree

5 files changed

+71
-11
lines changed

5 files changed

+71
-11
lines changed

lib/gogs.ex

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ defmodule Gogs do
1717

1818
@api_base_url GogsHelpers.api_base_url()
1919
@mock Application.compile_env(:gogs, :mock)
20-
Logger.info("config :gogs, mock: #{to_string(@mock)}")
20+
Logger.debug("config :gogs, mock: #{to_string(@mock)}")
2121
@git (@mock && Gogs.GitMock) || Git
2222

2323
@doc """
@@ -59,6 +59,29 @@ defmodule Gogs do
5959
GogsHttp.delete(url)
6060
end
6161

62+
@doc """
63+
`remote_read_file/3` reads a file from the remote repo.
64+
Accepts 4 arguments: `org_name`, `repo_name`, `file_name` and `branch_name`.
65+
The 4<sup>th</sup> argument is *optional* and defaults to `"master"`
66+
(the default branch for a repo hosted on `Gogs`).
67+
Makes a `GET` request to the remote `Gogs` instance as defined
68+
by the environment variable `GOGS_URL`.
69+
Returns `{:ok, %HTTPoison.Response{ body: response_body}}`
70+
Uses REST API Endpoint:
71+
```sh
72+
GET /repos/:username/:reponame/raw/:branchname/:path
73+
```
74+
Ref: https://github.com/gogs/docs-api/blob/master/Repositories/Contents.md#get-contents
75+
"""
76+
@spec remote_read_raw(String.t(), String.t(), String.t(), String.t()) :: {:ok, map} | {:error, any}
77+
def remote_read_raw(org_name, repo_name, file_name, branch_name \\ "master") do
78+
url = @api_base_url <> "repos/#{org_name}/#{repo_name}/raw/#{branch_name}/#{file_name}"
79+
Logger.debug("Gogs.remote_read_file: #{url}")
80+
GogsHttp.get_raw(url)
81+
82+
end
83+
84+
6285
@doc """
6386
`clone/1` clones a remote git repository based on `git_repo_url`
6487
returns the path of the _local_ copy of the repository.

lib/http.ex

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ defmodule GogsHttp do
3333
def parse_body_response({:error, err}), do: {:error, err}
3434

3535
def parse_body_response({:ok, response}) do
36-
# Logger.debug(response)
36+
# Logger.debug(response) # very noisy!
3737
body = Map.get(response, :body)
3838
if body == nil || byte_size(body) == 0 do
3939
Logger.warning("GogsHttp.parse_body_response: response body is nil!")
@@ -53,11 +53,26 @@ defmodule GogsHttp do
5353
"""
5454
@spec get(String.t()) :: {:ok, map} | {:error, any}
5555
def get(url) do
56-
Logger.info("GogsHttp.get #{url}")
56+
Logger.debug("GogsHttp.get #{url}")
5757
inject_poison().get(url, @headers)
5858
|> parse_body_response()
5959
end
6060

61+
@doc """
62+
`get_raw/1` as it's name suggests gets the raw data
63+
(expects the reponse to be plaintext not JSON)
64+
accepts one argument: `url` the REST API endpoint.
65+
Makes an `HTTP GET` request to the specified `url`.
66+
Auth Headers and Content-Type are implicit.
67+
returns `{:ok, map}`
68+
"""
69+
@spec get_raw(String.t()) :: {:ok, map} | {:error, any}
70+
def get_raw(url) do
71+
Logger.debug("GogsHttp.get_raw #{url}")
72+
headers = [{"Authorization", "token #{@access_token}"}]
73+
inject_poison().get(url, headers)
74+
end
75+
6176
@doc """
6277
`post/2` accepts two arguments: `url` and `params`.
6378
Makes an `HTTP POST` request to the specified `url`
@@ -66,7 +81,7 @@ defmodule GogsHttp do
6681
"""
6782
@spec post(String.t(), map) :: {:ok, map} | {:error, any}
6883
def post(url, params \\ %{}) do
69-
Logger.info("GogsHttp.post #{url}")
84+
Logger.debug("GogsHttp.post #{url}")
7085
body = Jason.encode!(params)
7186
inject_poison().post(url, body, @headers)
7287
|> parse_body_response()
@@ -78,7 +93,7 @@ defmodule GogsHttp do
7893
"""
7994
@spec delete(String.t()) :: {:ok, map} | {:error, any}
8095
def delete(url) do
81-
Logger.info("GogsHttp.delete #{url}")
96+
Logger.debug("GogsHttp.delete #{url}")
8297
inject_poison().delete(url <> "?token=#{@access_token}")
8398
|> parse_body_response()
8499
end

lib/httpoison_mock.ex

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,28 @@ defmodule Gogs.HTTPoisonMock do
5656
})
5757
end
5858

59+
@raw_response {:ok, %HTTPoison.Response{
60+
body: "# public-repo\n\nplease don't update this. the tests read it.",
61+
status_code: 200
62+
}}
63+
5964
@doc """
6065
`get/2` mocks the HTTPoison.get/2 function when parameters match test vars.
6166
Feel free refactor this if you can make it pretty.
6267
"""
6368
def get(url, _headers) do
6469
Logger.debug("Gogs.HTTPoisonMock.get/2 #{url}")
65-
repo_name = GogsHelpers.get_repo_name_from_url(url)
66-
response_body =
67-
make_repo_create_post_response_body(repo_name)
68-
|> Jason.encode!()
69-
{:ok, %{body: response_body}}
70+
case String.contains?(url, "/raw/") do
71+
true ->
72+
@raw_response
73+
false ->
74+
Logger.debug("Gogs.HTTPoisonMock.get/2 #{url}")
75+
repo_name = GogsHelpers.get_repo_name_from_url(url)
76+
response_body =
77+
make_repo_create_post_response_body(repo_name)
78+
|> Jason.encode!()
79+
{:ok, %{body: response_body}}
80+
end
7081
end
7182

7283
@doc """

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ defmodule Gogs.MixProject do
66
def project do
77
[
88
app: :gogs,
9-
version: "0.6.0",
9+
version: "0.7.0",
1010
elixir: @elixir_requirement,
1111
start_permanent: Mix.env() == :prod,
1212
deps: deps(),

test/gogs_test.exs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ defmodule GogsTest do
4343
Gogs.remote_repo_delete(org_name, repo_name)
4444
end
4545

46+
test "remote_read_raw/3 retrieves the contents of the README.md file" do
47+
org_name = "myorg"
48+
repo_name = "public-repo"
49+
file_name = "README.md"
50+
{:ok, %HTTPoison.Response{ body: response_body}} =
51+
Gogs.remote_read_raw(org_name, repo_name, file_name)
52+
IO.inspect(response_body)
53+
expected = "# public-repo\n\nplease don't update this. the tests read it."
54+
assert expected == response_body
55+
end
56+
4657
test "Gogs.clone clones a known remote repository Gogs on Fly.io" do
4758
org = "nelsonic"
4859
repo = "public-repo"

0 commit comments

Comments
 (0)