Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

"""LICENSE 

Copyright 2017 Hermann Krumrey <hermann@krumreyh.com> 

 

This file is part of gitlab-cloner. 

 

gitlab-cloner is free software: you can redistribute it and/or modify 

it under the terms of the GNU General Public License as published by 

the Free Software Foundation, either version 3 of the License, or 

(at your option) any later version. 

 

gitlab-cloner is distributed in the hope that it will be useful, 

but WITHOUT ANY WARRANTY; without even the implied warranty of 

MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

GNU General Public License for more details. 

 

You should have received a copy of the GNU General Public License 

along with gitlab-cloner. If not, see <http://www.gnu.org/licenses/>. 

LICENSE""" 

 

 

import os 

import json 

import shutil 

import requests 

from typing import List, Dict, Any 

from subprocess import check_output 

from colorama import Fore, Style 

 

 

def get_projects(gitlab_url: str, private_token: str) \ 

-> List[Dict[str, Any]]: 

""" 

Retrieves a list of repositories that the provided private token 

grants access to. 

:param gitlab_url: The URL of the gitlab instance, including https etc. 

:param private_token: The private token to use 

:return: A list of gitlab projects 

""" 

 

url = gitlab_url + "/api/v4/projects?private_token=" + private_token 

response = requests.get(url) 

 

projects = json.loads(response.text) 

 

while response.headers["X-Next-Page"]: 

next_page = response.headers["X-Next-Page"] 

response = requests.get(url + "&page=" + next_page) 

projects += json.loads(response.text) 

 

return projects 

 

 

def clone_repo(repo: Dict[str, Any], private_token: str): 

""" 

Clones a git repository. If the repository already exists, 

the user will be prompted to delete the old repository. 

If the user refuses, the repository will not be cloned. 

:param repo: The repository to clone 

:param private_token: The private token to use when cloning 

:return: None 

""" 

 

63 ↛ 64line 63 didn't jump to line 64, because the condition on line 63 was never true if os.path.isdir(repo["name"]): 

resp = input("Repository " + repo["name"] + "exists. Delete? (y|n)") 

if resp == "y": 

shutil.rmtree(repo["name"]) 

else: 

return 

 

oauth = "oauth2:" + private_token + "@" 

url = repo["http_url_to_repo"]\ 

.replace("http://", "http://" + oauth)\ 

.replace("https://", "https://" + oauth) 

 

command = ["git", "clone", url] 

print(Fore.CYAN + " ".join(command) + Style.RESET_ALL) 

output = check_output(command) 

print(Fore.MAGENTA + output.decode() + Style.RESET_ALL)