dwyl / gogs

āš™ļø Interface with a Gogs (Git Server) from any Elixir App
GNU General Public License v2.0
10 stars 0 forks source link

Delete all Repos in an Org During Testing #19

Open nelsonic opened 2 years ago

nelsonic commented 2 years ago

During testing we are creating a lot of "test-123" repos: https://gogs-server.fly.dev/myorg image

The reason I'm opening this issue now is that there are enough repos in the org that my tests are failing:

image

If we attempt to drop the whole org (so we can re-create it empty) it doesn't work: https://gogs-server.fly.dev/org/myorg/settings/delete image

I don't want to include a function in the library that would allow someone to accidentally/carelessly "nuke" an entire Org ... It would be irresponsible of us to build such a weapon never mind include it in the lib.

image

So instead I propose that we create a bash script that calls the List organization repositories REST API endpoint https://github.com/gogs/docs-api/tree/master/Repositories#list-organization-repositories

GET /orgs/:orgname/repos

Then runs the delete a repo command in a loop for each repo. https://stackoverflow.com/questions/32791663/how-to-run-curl-command-with-parameter-in-a-loop-from-bash-script i.e. we can still use it for development purposes but it wouldn't be exported in the library on Hex.pm

nelsonic commented 2 years ago

This is proving more tedious than I would have hoped ... šŸ™„ When I run the following cURL command:

 curl 'https://gogs-server.fly.dev/api/v1/orgs/myorg/repos?token=0ed304' -I

not the actual token. but the real one is in the URL.

I get the following 401 response:

HTTP/2 401
set-cookie: lang=en-US; Path=/; Max-Age=2147483647
set-cookie: i_like_gogs=3644b63b9865487b; Path=/; HttpOnly
set-cookie: _csrf=gJtPjbT5YpHdqt796OT2tg9jyyw6MTY1MTg3ODA5NTgzNTAzMjE2OA; Path=/; Domain=gogs-server.fly.dev; Expires=Sat, 07 May 2022 23:01:35 GMT; HttpOnly; Secure
x-content-type-options: nosniff
x-frame-options: DENY
date: Fri, 06 May 2022 23:01:35 GMT
server: Fly/33deaca5 (2022-05-05)
via: 2 fly.io
fly-request-id: 01G2DSPQV8MJAJHXQXB09GDN9A-lhr

There's no insight why ... šŸ¤·ā€ā™‚ļø

nelsonic commented 2 years ago

Through digging around, found the following Admin page: https://gogs-server.fly.dev/admin/repos?page=1 image

This gives a relatively quick way of deleting repos:

image

nelsonic commented 2 years ago

With the following JavaScript I automated the deletion in the web Ui:

document.querySelectorAll('tbody tr').forEach((e) => {
  var a = e.querySelector("td:nth-child(3) a")
  var url = a.getAttribute("href")
  console.log(url, url.indexOf("test" > -1))
  if(url.indexOf("test" > -1)) {
    var del = e.getElementsByClassName("delete-button")
    del[0].click()
    // wait for the modal to appear
    setTimeout(function(){
      document.querySelector(".modal .ok").click()
    }, 100);
  }
})

image

So now I can get back to the actual work of creating the repos I need.

nelsonic commented 2 years ago

Parking this code:

  @doc """
  `get/1` accepts one argument: `url` the REST API endpoint. 
  Makes an `HTTP GET` request to the specified `url`.
  Auth Headers and Content-Type are implicit.
  returns `{:ok, map}`
  """
  @spec get(String.t()) :: {:ok, map} | {:error, any}
  def get(url) do
    # IO.inspect(url, label: url)
    headers = [
      {"Accept", "application/json"},
      {"Authorization", "token #{@access_token}"},
      {"Content-Type", "application/json"}
    ]
    inject_poison().get(url) |> IO.inspect()
    |> parse_body_response()
  end

  def list_org_repos(org_name) do
    url = @api_base_url <> "orgs/#{org_name}/repos"
    Logger.info("list_org_repos api endpoint: #{url}")
    get(url)
  end

And corresponding test:

  test "list_org_repos/1 returns the list of repos for the org" do
    org_name = "dwyl"
    repo_name = create_test_git_repo(org_name)
    IO.inspect(repo_name, label: "repo_name")

    {:ok, response} = Gogs.list_org_repos(org_name)
    IO.inspect(response.name)

    # Cleanup:
    Gogs.remote_repo_delete(org_name, repo_name)    
  end