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

Delete all Repos in an Org During Testing #19

Open
nelsonic opened this issue May 6, 2022 · 4 comments
Open

Delete all Repos in an Org During Testing #19

nelsonic opened this issue May 6, 2022 · 4 comments

Comments

@nelsonic
Copy link
Member

nelsonic commented May 6, 2022

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
Copy link
Member Author

nelsonic commented May 6, 2022

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
Copy link
Member Author

nelsonic commented May 6, 2022

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
Copy link
Member Author

nelsonic commented May 6, 2022

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
Copy link
Member Author

nelsonic commented May 6, 2022

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

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