pre-commit-ci / issues

public issues for https://pre-commit.ci
17 stars 4 forks source link

Auto-delete pre-commit.ci branches after merge #56

Closed janosh closed 3 years ago

janosh commented 3 years ago

Would be great if pre-commit.ci automatically removed branches after they are merged/rebased.

If this seems too harsh for default behavior, maybe a config option?

Screen Shot 2021-04-19 at 19 25 54

asottile commented 3 years ago

pre-commit.ci actually doesn't have permission to do that at the moment -- but you can configure your repository to do this automatically:

image

janosh commented 3 years ago

I know. I have that setting enabled on some of my repos but it's a hassle to go into the settings UI for each new repo and press a bunch of buttons. With a config file, you put it in once and just copy it into new repos. Btw, I searched but couldn't find a way to enable that setting on new repos by default. Do you know of one?

asottile commented 3 years ago

I use this -- it was recently added to the api: https://github.com/asottile/set-delete-branch-on-merge

janosh commented 3 years ago

That's cool! 👍

asottile commented 3 years ago

there's a more efficient way to do it with the gql api but it works fine enough for me at the moment -- patches welcome!

janosh commented 3 years ago

More efficient in what sense? You wouldn't have to run repeatedly on all repos?

asottile commented 3 years ago

ah right now what it does is:

  1. bulk get of all repositories
  2. for each repository: (idempotent) send PATCH call to set the flag to true

the reason it's for-each is because the bulk get does not tell us whether it is enabled or not

with gql we could do:

  1. bulk get and filter on the setting
  2. for each which doesn't have the setting enabled: set it to enabled

so right now it always sends ~100 requests for my repos, after it could be ~1 when nothing needs updating and ~11 when there need to be 10 things updated

janosh commented 3 years ago

I see. I might look into this tomorrow. Will share it if I put something together.

Btw, found this nice tool in case you weren't aware.

asottile commented 3 years ago

yeah I've played with the gql explorer -- according to github support this is approximately the query we'd need? I haven't tried it at all though:

{
  viewer {
    login
    repositories(first: 100, affiliations: ORGANIZATION_MEMBER) {
      nodes {
        nameWithOwner
        deleteBranchOnMerge
      }
    }
  }
}
janosh commented 3 years ago

This might be start. Returns all my repos:

import requests

headers = {"Authorization": f"Bearer {os.environ['GH_TOKEN']}"}

def run_query(query: str) -> dict:
    request = requests.post(
        "https://api.github.com/graphql", json={"query": query}, headers=headers
    )
    if request.status_code == 200:
        return request.json()
    else:
        raise Exception(f"{query=} failed with code {request.status_code}.")

repos_query = """
{
  search(query: "user:janosh", type: REPOSITORY, first: 100) {
    repositoryCount
    edges {
      node {
        ... on Repository {
          name
        }
      }
    }
  }
}
"""

result = run_query(repos_query)
print(f"{result=}")
janosh commented 3 years ago

I put something together at janosh/auto-repo-config that does exactly what I need but it no longer shares any code with asottile/set-delete-branch-on-merge. Still happy to submit a PR if you're interested.