Deleting repositories hosted in github is not that easy, each time a repository is going to be deleted, github requires ower to type the repository’s name, not to say to delete multiple repositories.
Even if github does not require repository’s ower to type repository’s name to confirm the delete operation, deleting mass repositories is also a big challenge. So is there a convenient way to delete all deprecated repositories?
Github API v3 makes it easy for this task to be accomplished, the central idea is listing repositories and deleting them.
How To
Listing repositories
According to documentation, listing an authenticated user’s repositories, the following API should be used.
1
GET /user/repos
To examin the API and see what does it output, curl can be used, according to documentation, basic authentication is enough, so the command should be like this:
By observing the output, it can be concluded that curl did nothing but built a http request packet in conjunction with user’s name and password by using basic access authentication, sent it to github and printed result back to user.
So it’s might be a little easy to do the same thing in Python code, just build and send a request and wait for result.
import json import base64 import urllib.request from urllib.error import HTTPError
classGithub(object): def__init__(self, user, passwd): """ :param user: User name of github :param passwd: Pass word of github :return: """ self.__user = user self.__passwd = passwd
# Obtain repositories list deflist_repos(self): """ :return: A python object contains all repositories information """ request = self.__request('https://api.github.com/user/repos') result = urllib.request.urlopen(request).read().decode('utf-8') result = json.loads(result)
if response.code == 204: print('repo %s has been successfully deleted' % repo['name']) else: return response.code except HTTPError as error:
if error.code == 403: print('Repository %s is unavailable due to DMCA takedown.' % repo_name) skip_count += 1 else: raise error
repos = self.list_repos()
Conclusion
Manipulating repositories by using Github API v3 is easy, but one of its main drawback is that there is no way to delete repositories that are unavailable due to DMCA Takedown Policy but to contact github manually for help.