|
1 | | -import json |
2 | | -import sys |
3 | | -from os import getenv, path |
4 | | -from pprint import pprint |
5 | | - |
6 | | -import click # pylint: disable=import-error |
7 | | -import requests # pylint: disable=import-error |
8 | | -from dotenv import load_dotenv # pylint: disable=import-error |
9 | | - |
10 | | -env = load_dotenv() |
11 | | -api_url = getenv("API_URL", default="https://api.github.com/graphql") |
12 | | -github_token = getenv("GITHUB_TOKEN", default=None) |
13 | | -m = [1, 2, 3] |
14 | | -print(m[len("t") :]) |
15 | | - |
16 | | -if github_token is None: |
17 | | - sys.exit( |
18 | | - "GitHub Token is not set." |
19 | | - + "Please set the GITHUB_TOKEN env variable in your system or " |
20 | | - + "the .env file of your project." |
21 | | - ) |
22 | | - |
23 | | -client_id = getenv("CLIENT_ID", default="copy_labels.py") |
24 | | -headers = { |
25 | | - "Authorization": "bearer {github_token}".format(github_token=github_token), |
26 | | - "Accept": "application/vnd.github.bane-preview+json", |
27 | | - "Content-Type": "application/json", |
28 | | -} |
29 | | - |
30 | | - |
31 | | -def make_request(query, query_variables): |
32 | | - payload = {"query": query, "variables": query_variables} |
33 | | - response = requests.post(api_url, data=json.dumps(payload), headers=headers) |
34 | | - return response |
35 | | - |
36 | | - |
37 | | -def create_label(repo_id, label): |
38 | | - """ |
39 | | - Create label in the supplied repo. |
40 | | -
|
41 | | - :param repo_id: Unique ID that represents the repo in GitHub |
42 | | - :type repo_id: str |
43 | | - :param label: Object with label information. |
44 | | - :type label: dict |
45 | | - :return: GitHub API request response |
46 | | - """ |
47 | | - |
48 | | - query_variables = { |
49 | | - "createLabelInput": { |
50 | | - "color": label["color"], |
51 | | - "description": label["description"], |
52 | | - "name": label["name"], |
53 | | - "repositoryId": repo_id, |
54 | | - } |
55 | | - } |
56 | | - |
57 | | - with open( |
58 | | - path.join(path.dirname(__file__), "queries/create_label.gql"), "r" |
59 | | - ) as query_file: |
60 | | - query = "".join(query_file.readlines()) |
61 | | - |
62 | | - response = make_request(query, query_variables).json() |
63 | | - print("Created label {label}".format(label=label["name"])) |
64 | | - |
65 | | - return response |
66 | | - |
67 | | - |
68 | | -def get_labels(owner, repo): |
69 | | - """ |
70 | | - Gets a list of labels from the supplied repo. |
71 | | - :param owner: Repo owner GitHub login. |
72 | | - :type owner: str |
73 | | - :param repo: Repository name. |
74 | | - :type repo: str |
75 | | - :return: A tuple with the GitHub id for the repository and a list of labels defined in the repository |
76 | | - """ |
77 | | - |
78 | | - query_variables = { |
79 | | - "owner": owner, |
80 | | - "name": repo, |
81 | | - } |
82 | | - |
83 | | - with open( |
84 | | - path.join(path.dirname(__file__), "queries/get_repo_data.gql"), "r" |
85 | | - ) as query_file: |
86 | | - query = "".join(query_file.readlines()) |
87 | | - |
88 | | - response = make_request(query, query_variables) |
89 | | - |
90 | | - status_code = response.status_code |
91 | | - result = response.json() |
92 | | - |
93 | | - if status_code >= 200 and status_code <= 300: |
94 | | - repo_id = result["data"]["repository"]["id"] |
95 | | - labels = result["data"]["repository"]["labels"]["nodes"] |
96 | | - |
97 | | - return repo_id, labels |
98 | | - else: |
99 | | - raise Exception( |
100 | | - "[ERROR] getting issue labels. Status Code: {status_code} - Message: {result}".format( |
101 | | - status_code=status_code, result=result["message"] |
102 | | - ) |
103 | | - ) |
104 | | - |
105 | | - |
106 | | -def delete_label(label_id): |
107 | | - """ |
108 | | - Delete the specified label |
109 | | - :param label_id: Label's node id. |
110 | | - :type label_id: str |
111 | | - :return: GitHub API request response. |
112 | | - """ |
113 | | - |
114 | | - query_variables = { |
115 | | - "deleteLabelInput": {"clientMutationId": client_id, "id": label_id} |
116 | | - } |
117 | | - |
118 | | - with open( |
119 | | - path.join(path.dirname(__file__), "queries/delete_label.gql"), "r" |
120 | | - ) as query_file: |
121 | | - query = "".join(query_file.readlines()) |
122 | | - |
123 | | - payload = {"query": query, "variables": query_variables} |
124 | | - result = requests.post(api_url, data=json.dumps(payload), headers=headers).json() |
125 | | - |
126 | | - return result |
127 | | - |
128 | | - |
129 | | -@click.command() |
130 | | -@click.option("--dry", is_flag=True) |
131 | | -@click.argument("source_repo") |
132 | | -@click.argument("target_repo") |
133 | | -def copy_labels(source_repo, target_repo, dry): |
134 | | - """ |
135 | | - Copy labels from the source repository to the target repository. |
136 | | - \f |
137 | | - :param source: The full name of a GitHub repo from where the labels will be copied from. Eg. github/opensourcefriday |
138 | | - :type source: str |
139 | | - :param target: The full name of a GitHub repo to where the labels will be copied. Eg. github/opensourcefriday |
140 | | - :type target: str |
141 | | - :return: |
142 | | - """ |
143 | | - source_owner, source_repo_name = source_repo.split("/") |
144 | | - target_owner, target_repo_name = target_repo.split("/") |
145 | | - |
146 | | - try: |
147 | | - print( |
148 | | - "Fetching labels for {source_repo_name} repo.".format( |
149 | | - source_repo_name=source_repo_name |
150 | | - ) |
151 | | - ) |
152 | | - _, source_repo_labels = get_labels(source_owner, source_repo_name) |
153 | | - print( |
154 | | - "Fetched labels for {source_repo_name}".format( |
155 | | - source_repo_name=source_repo_name |
156 | | - ) |
157 | | - ) |
158 | | - |
159 | | - print( |
160 | | - "Fetching labels for {target_repo_name} repo.".format( |
161 | | - target_repo_name=target_repo_name |
162 | | - ) |
163 | | - ) |
164 | | - target_repo_id, target_repo_labels = get_labels(target_owner, target_repo_name) |
165 | | - print( |
166 | | - "Fetched labels for {target_repo_name}".format( |
167 | | - target_repo_name=target_repo_name |
168 | | - ) |
169 | | - ) |
170 | | - |
171 | | - filtered_labels = list( |
172 | | - filter(lambda x: x not in target_repo_labels, source_repo_labels) |
173 | | - ) |
174 | | - |
175 | | - if dry: |
176 | | - print("This is just a dry run. No labels will be copied/created.") |
177 | | - print( |
178 | | - "{label_count} labels would have been created.".format( |
179 | | - label_count=len(filtered_labels) |
180 | | - ) |
181 | | - ) |
182 | | - pprint(filtered_labels, indent=4) |
183 | | - else: |
184 | | - print( |
185 | | - "Preparing to created {label_count} labels in {target_repo}".format( |
186 | | - label_count=len(filtered_labels), target_repo=target_repo |
187 | | - ) |
188 | | - ) |
189 | | - |
190 | | - for label in filtered_labels: |
191 | | - create_label(target_repo_id, label) |
192 | | - except Exception as error: |
193 | | - sys.exit(error) |
194 | | - |
195 | | - print("Done") |
196 | | - |
197 | | - |
198 | | -if __name__ == "__main__": |
199 | | - # Pylint doesn't know that @click.command takes care of injecting the |
200 | | - # function parameters. Disabling Pylint error. |
201 | | - copy_labels() # pylint: disable=no-value-for-parameter |
| 1 | +a = 1 |
| 2 | +b = 2 |
| 3 | +c = a + b |
| 4 | +print(c) |
0 commit comments