Skip to content

Commit 01786df

Browse files
committed
create a local "draft" branch in the repo
1 parent b1075a5 commit 01786df

File tree

5 files changed

+57
-19
lines changed

5 files changed

+57
-19
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ This package is that interface.
2626

2727
## What?
2828

29-
Very much still work-in-progress.
30-
But we already have a handful of functions working.
31-
See: https://hexdocs.pm/gogs/Gogs.html
29+
Very much still work-in-progress. ⏳ 👨‍💻 <br />
30+
But we already have a handful of functions working. 🚀<br />
31+
See: https://hexdocs.pm/gogs/Gogs.html 📚
3232

3333
## Who?
3434

coveralls.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"coverage_options": {
3-
"minimum_coverage": 42
3+
"minimum_coverage": 100
44
}
55
}

lib/gogs.ex

+28-8
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,24 @@ defmodule Gogs do
2828
def inject_poison, do: @httpoison
2929

3030
@doc """
31-
returns the remote url for cloning
31+
`remote_url/3` returns the remote url for cloning
3232
"""
3333
def remote_url(base_url, org, repo) do
3434
"#{base_url}#{org}/#{repo}.git"
3535
end
3636

37+
38+
@doc """
39+
`remote_url_ssh/2` returns the remote ssh url for cloning.
40+
"""
41+
def remote_url_ssh(org, repo) do
42+
url = Envar.get("GOGS_URL")
43+
port = Envar.get("GOGS_SSH_PORT")
44+
git_url = GogsHelpers.make_url(url, port)
45+
remote_url(git_url, org, repo)
46+
end
47+
48+
3749
@doc """
3850
`parse_body_response/1` parses the response returned by the Gogs Server
3951
so your app can use the resulting JSON.
@@ -87,7 +99,8 @@ defmodule Gogs do
8799
params = %{
88100
name: repo_name,
89101
private: private,
90-
description: repo_name
102+
description: repo_name,
103+
readme: repo_name
91104
}
92105
post(url, params)
93106
end
@@ -98,7 +111,7 @@ defmodule Gogs do
98111
"""
99112
@spec delete(String.t()) :: {:ok, map} | {:error, any}
100113
def delete(url) do
101-
inject_poison().delete(url)
114+
inject_poison().delete(url <> "?token=#{@access_token}")
102115
|> parse_body_response()
103116
end
104117

@@ -108,12 +121,13 @@ defmodule Gogs do
108121
by the environment variable `GOGS_URL`.
109122
"""
110123
def remote_repo_delete(org_name, repo_name) do
111-
url = @api_base_url <> "repos/#{org_name}/#{repo_name}?token=#{@access_token}"
112-
IO.inspect(url, label: "remote_repo_delete url")
124+
url = @api_base_url <> "repos/#{org_name}/#{repo_name}"
125+
# IO.inspect(url, label: "remote_repo_delete url")
113126
delete(url)
114127
end
115128

116129

130+
117131
@doc """
118132
`clone/1` clones a remote git repository based on `git_repo_url`
119133
returns the path of the _local_ copy of the repository.
@@ -122,7 +136,7 @@ defmodule Gogs do
122136
# IO.inspect("git clone #{git_repo_url}")
123137
case Git.clone(git_repo_url) do
124138
{:ok, %Git.Repository{path: path}} ->
125-
# IO.inspect(path)
139+
IO.inspect(path)
126140
path
127141
{:error, %Git.Error{message: _message}} ->
128142
# IO.inspect("Attempted to clone #{git_repo_url}, got: #{message}")
@@ -143,15 +157,21 @@ defmodule Gogs do
143157
`local_repo_path/1` returns the full system path for the cloned repo
144158
on the `localhost` i.e. the Elixir/Phoenix server that cloned it.
145159
"""
146-
def local_repo_path(repo) do
147-
temp_dir() <> "/" <> repo
160+
def local_repo_path(repo_name) do
161+
temp_dir() <> "/" <> repo_name
148162
end
149163

150164
# Made this a function in case we want to
151165
defp temp_dir do
152166
File.cwd!
153167
end
154168

169+
def local_branch_create(repo_name, _branch_name \\ "draft") do
170+
path = local_repo_path(repo_name)
171+
repo = %Git.Repository{path: path}
172+
Git.checkout(repo, ~w(-b draft)) # ["-b", branch_name])
173+
end
174+
155175

156176
# def commit do
157177

lib/httpoison_mock.ex

+4-3
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,13 @@ defmodule Gogs.HTTPoisonMock do
4747
full_name: "myorg/#{repo_name}",
4848
html_url: "https://gogs-server.fly.dev/myorg/#{repo_name}",
4949
ssh_url: "ssh://[email protected]:10022/myorg/#{repo_name}.git",
50+
readme: repo_name
5051
})
5152
end
5253

5354
@doc """
5455
`post/3` stubs the HTTPoison post function when parameters match test vars.
55-
feel free refactor this if you can make it pretty.
56+
Feel free refactor this if you can make it pretty.
5657
"""
5758
def post("https://gogs-server.fly.dev/api/v1/org/myorg/repos", body, _headers) do
5859
# IO.inspect("Gogs.HTTPoisonMock.post/3 called!")
@@ -64,8 +65,8 @@ defmodule Gogs.HTTPoisonMock do
6465
end
6566

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

test/gogs_test.exs

+21-4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,26 @@ defmodule GogsTest do
3131
end
3232

3333

34+
test "local_branch_create/1 creates a new branch on the localhost" do
35+
org_name = "myorg"
36+
repo_name = "test-repo" <> Integer.to_string(System.unique_integer([:positive]))
37+
Gogs.remote_repo_create(org_name, repo_name, false)
38+
IO.puts "waiting for repo to be crated" ; :timer.sleep(1000); IO.puts "done."
39+
git_repo_url = Gogs.remote_url_ssh(org_name, repo_name)
40+
_path = Gogs.clone(git_repo_url)
41+
# IO.inspect(path, label: "path:41")
42+
43+
{:ok, res} = Gogs.local_branch_create(repo_name, "draft")
44+
# IO.inspect(res, label: "local_branch_create(repo_name) res")
45+
# IO.inspect(response, label: "org_create response")
46+
47+
assert res == "Switched to a new branch 'draft'\n"
48+
49+
# Cleanup!
50+
Gogs.remote_repo_delete(org_name, repo_name)
51+
delete_local_directory(repo_name)
52+
end
53+
3454

3555

3656
# test "Gogs.create_org creates a new organisation on the Gogs instance" do
@@ -48,12 +68,9 @@ defmodule GogsTest do
4868
# end
4969

5070
test "Gogs.clone clones a remote repository Gogs on Fly.io" do
51-
url = Envar.get("GOGS_URL")
52-
port = Envar.get("GOGS_SSH_PORT")
53-
git_url = GogsHelpers.make_url(url, port)
5471
org = "nelsonic"
5572
repo = "public-repo"
56-
git_repo_url = Gogs.remote_url(git_url, org, repo)
73+
git_repo_url = Gogs.remote_url_ssh(org, repo)
5774

5875
path = Gogs.clone(git_repo_url)
5976
IO.inspect(path)

0 commit comments

Comments
 (0)