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

Create Repo on Fly.io Gogs instance #9

Closed
3 tasks done
nelsonic opened this issue Apr 30, 2022 · 4 comments
Closed
3 tasks done

Create Repo on Fly.io Gogs instance #9

nelsonic opened this issue Apr 30, 2022 · 4 comments

Comments

@nelsonic
Copy link
Member

nelsonic commented Apr 30, 2022

The first step (after creating an Org #8 ...) in using Gogs as a Backup for GitHub is to create a Repository!
Reading the API: https://github.com/gogs/docs-api/tree/master/Repositories#create

It appears this should be straightforward:

POST /org/:org/repos

Todo

@nelsonic
Copy link
Member Author

nelsonic commented May 1, 2022

Creating a new repo via cURL (REST API) is working on my localhost!

curl -X POST -H "Content-Type: application/json" -d '{"name": "created-by-curl", "description": "This is your first repository",  "private": false}'  'https://gogs-server.fly.dev/api/v1/org/myorg/repos?token=yourtoken'

image

response:

{
	"id": 5,
	"owner": {
		"id": 2,
		"username": "myorg",
		"login": "myorg",
		"full_name": "",
		"email": "",
		"avatar_url": "https://gogs-server.fly.dev/avatars/2"
	},
	"name": "created-by-curl",
	"full_name": "myorg/created-by-curl",
	"description": "This is your first repository",
	"private": false,
	"fork": false,
	"parent": null,
	"empty": false,
	"mirror": false,
	"size": 0,
	"html_url": "https://gogs-server.fly.dev/myorg/created-by-curl",
	"ssh_url": "ssh://[email protected]:10022/myorg/created-by-curl.git",
	"clone_url": "https://gogs-server.fly.dev/myorg/created-by-curl.git",
	"website": "",
	"stars_count": 0,
	"forks_count": 0,
	"watchers_count": 0,
	"open_issues_count": 0,
	"default_branch": "",
	"created_at": "0001-01-01T00:00:00Z",
	"updated_at": "0001-01-01T00:00:00Z",
	"permissions": {
		"admin": true,
		"push": true,
		"pull": true
	}
}

This means it will work via HTTPoison tomorrow morning. 😴
And once that is working, we're off to the races!! 🐎

@nelsonic
Copy link
Member Author

nelsonic commented May 2, 2022

I've setup all the code to make REST API Requests from Elixir using HTTPoison (_piggy-backing on previous work done in https://github.com/dwyl/elixir-auth-google/blame/9c2b4b96f6829719faa767f1db57c9ba09131380/lib/elixir_auth_google.ex#L89 ...

But getting a 415 error: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/415

%HTTPoison.Response{
  body: "[{\"classification\":\"ContentTypeError\",\"message\":\"Unsupported Content-Type\"}]",
  headers: [
    {"content-type", "application/json; charset=utf-8"},
    {"set-cookie", "lang=en-US; Path=/; Max-Age=2147483647"},
    {"set-cookie", "i_like_gogs=ba89f24ea6c11533; Path=/; HttpOnly"},
    {"set-cookie",
     "_csrf=xOXKySlsS26hdUvN1acemGQ5aOQ6MTY1MTQ4NzkyMzg5MTUwMDYzNA; Path=/; Domain=gogs-server.fly.dev; Expires=Tue, 03 May 2022 10:38:43 GMT; HttpOnly; Secure"},
    {"x-content-type-options", "nosniff"},
    {"x-frame-options", "DENY"},
    {"date", "Mon, 02 May 2022 10:38:43 GMT"},
    {"content-length", "76"},
    {"server", "Fly/f2e6d63f (2022-04-27)"},
    {"via", "1.1 fly.io"},
    {"fly-request-id", "01G225KT6TNN28MXJX3K40V18P-lhr"}
  ],
  request: %HTTPoison.Request{
    body: "{\"name\":\"test-repo6\",\"private\":false}",
    headers: [
      {"Authorization", "token supersecret"}
    ],
    method: :post,
    options: [],
    params: %{},
    url: "https://gogs-server.fly.dev/api/v1/org/myorg/repos"
  },
  request_url: "https://gogs-server.fly.dev/api/v1/org/myorg/repos",
  status_code: 415
}
{:ok,
 [
   %{
     "classification" => "ContentTypeError",
     "message" => "Unsupported Content-Type"
   }
 ]}

Did a bit of goggling ... 🔍
Found: https://www.npmjs.com/package/gogs-client
Which specifies the Content-Type header as:

      headers: {'Content-Type': 'application/json'}

https://github.com/unfoldingWord-dev/node-gogs-client/blob/6b16291e1a1e05882cc2d0628128a6ab6d3047a5/lib/request.js#L44

And now our REST API Request works:

{:ok,
 %{
   clone_url: "https://gogs-server.fly.dev/myorg/test-repo7.git",
   created_at: "0001-01-01T00:00:00Z",
   default_branch: "",
   description: "",
   empty: false,
   fork: false,
   forks_count: 0,
   full_name: "myorg/test-repo7",
   html_url: "https://gogs-server.fly.dev/myorg/test-repo7",
   id: 6,
   mirror: false,
   name: "test-repo7",
   open_issues_count: 0,
   owner: %{
     avatar_url: "https://gogs-server.fly.dev/avatars/2",
     email: "",
     full_name: "",
     id: 2,
     login: "myorg",
     username: "myorg"
   },
   parent: nil,
   permissions: %{admin: true, pull: true, push: true},
   private: false,
   size: 0,
   ssh_url: "ssh://[email protected]:10022/myorg/test-repo7.git",
   stars_count: 0,
   updated_at: "0001-01-01T00:00:00Z",
   watchers_count: 0,
   website: ""
 }}

https://gogs-server.fly.dev/myorg/test-repo7
image

#milestone

@nelsonic nelsonic changed the title Create New Repo on Fly.io Gogs instance Create Repo on Fly.io Gogs instance May 2, 2022
@nelsonic
Copy link
Member Author

nelsonic commented May 2, 2022

The test & TestDouble code is 4x the code for the actual implementation. 🙄

@nelsonic
Copy link
Member Author

nelsonic commented May 5, 2022

Ref:

gogs/lib/gogs.ex

Lines 56 to 74 in ec08eb4

@doc """
`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.
If you have more or different requirements, please share!
"""
@spec remote_repo_create(String.t(), String.t(), boolean) :: {:ok, map} | {:error, any}
def remote_repo_create(org_name, repo_name, private \\ false) do
url = @api_base_url <> "org/#{org_name}/repos"
# IO.inspect(url, label: "remote_repo_create url")
params = %{
name: repo_name,
private: private,
description: repo_name,
readme: repo_name
}
post(url, params)
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant