superlint/test/linters/python_black/python_good_1.py

202 lines
6 KiB
Python
Raw Normal View History

2020-02-04 09:49:58 -05:00
import json
2020-10-28 13:50:33 -04:00
import sys
2020-02-04 10:03:29 -05:00
from os import getenv, path
from pprint import pprint
2020-02-04 09:49:58 -05:00
2020-07-21 20:54:30 -04:00
import click # pylint: disable=import-error
import requests # pylint: disable=import-error
2020-10-28 13:50:33 -04:00
from dotenv import load_dotenv # pylint: disable=import-error
2020-02-04 10:03:29 -05:00
env = load_dotenv()
2020-08-20 10:44:11 -04:00
api_url = getenv("API_URL", default="https://api.github.com/graphql")
2020-02-04 10:03:29 -05:00
github_token = getenv("GITHUB_TOKEN", default=None)
m = [1, 2, 3]
print(m[len("t") :])
2020-02-04 10:03:29 -05:00
if github_token is None:
2020-08-20 10:44:11 -04:00
sys.exit(
"GitHub Token is not set."
+ "Please set the GITHUB_TOKEN env variable in your system or "
+ "the .env file of your project."
)
2020-02-04 10:03:29 -05:00
2020-08-20 10:44:11 -04:00
client_id = getenv("CLIENT_ID", default="copy_labels.py")
2020-02-04 10:03:29 -05:00
headers = {
2020-08-20 10:44:11 -04:00
"Authorization": "bearer {github_token}".format(github_token=github_token),
"Accept": "application/vnd.github.bane-preview+json",
"Content-Type": "application/json",
2020-02-04 10:03:29 -05:00
}
2020-07-21 20:54:30 -04:00
def make_request(query, query_variables):
payload = {"query": query, "variables": query_variables}
response = requests.post(api_url, data=json.dumps(payload), headers=headers)
return response
2020-02-04 10:03:29 -05:00
def create_label(repo_id, label):
"""
Create label in the supplied repo.
:param repo_id: Unique ID that represents the repo in GitHub
:type repo_id: str
:param label: Object with label information.
:type label: dict
:return: GitHub API request response
"""
query_variables = {
"createLabelInput": {
"color": label["color"],
"description": label["description"],
"name": label["name"],
2020-08-20 10:44:11 -04:00
"repositoryId": repo_id,
2020-02-04 10:03:29 -05:00
}
}
2020-08-20 10:44:11 -04:00
with open(
path.join(path.dirname(__file__), "queries/create_label.gql"), "r"
) as query_file:
2020-02-04 10:03:29 -05:00
query = "".join(query_file.readlines())
response = make_request(query, query_variables).json()
2020-08-20 10:44:11 -04:00
print("Created label {label}".format(label=label["name"]))
2020-02-04 10:03:29 -05:00
return response
2020-08-20 11:04:36 -04:00
2020-02-04 10:03:29 -05:00
def get_labels(owner, repo):
"""
Gets a list of labels from the supplied repo.
:param owner: Repo owner GitHub login.
:type owner: str
:param repo: Repository name.
:type repo: str
:return: A tuple with the GitHub id for the repository and a list of labels defined in the repository
"""
2020-08-20 10:44:11 -04:00
query_variables = {
"owner": owner,
"name": repo,
}
2020-02-04 10:03:29 -05:00
2020-08-20 10:44:11 -04:00
with open(
path.join(path.dirname(__file__), "queries/get_repo_data.gql"), "r"
) as query_file:
2020-02-04 10:03:29 -05:00
query = "".join(query_file.readlines())
response = make_request(query, query_variables)
2020-02-04 10:03:29 -05:00
status_code = response.status_code
result = response.json()
if status_code >= 200 and status_code <= 300:
repo_id = result["data"]["repository"]["id"]
labels = result["data"]["repository"]["labels"]["nodes"]
return repo_id, labels
2020-02-04 09:49:58 -05:00
else:
2020-02-04 10:03:29 -05:00
raise Exception(
2020-08-20 10:44:11 -04:00
"[ERROR] getting issue labels. Status Code: {status_code} - Message: {result}".format(
status_code=status_code, result=result["message"]
)
)
2020-07-21 20:54:30 -04:00
2020-08-20 11:04:36 -04:00
2020-02-04 10:03:29 -05:00
def delete_label(label_id):
"""
Delete the specified label
:param label_id: Label's node id.
:type label_id: str
:return: GitHub API request response.
2020-02-04 10:03:29 -05:00
"""
query_variables = {
2020-08-20 10:44:11 -04:00
"deleteLabelInput": {"clientMutationId": client_id, "id": label_id}
2020-02-04 10:03:29 -05:00
}
2020-08-20 10:44:11 -04:00
with open(
path.join(path.dirname(__file__), "queries/delete_label.gql"), "r"
) as query_file:
2020-02-04 10:03:29 -05:00
query = "".join(query_file.readlines())
payload = {"query": query, "variables": query_variables}
result = requests.post(api_url, data=json.dumps(payload), headers=headers).json()
return result
2020-08-20 11:04:36 -04:00
2020-02-04 10:03:29 -05:00
@click.command()
2020-08-20 10:44:11 -04:00
@click.option("--dry", is_flag=True)
@click.argument("source_repo")
@click.argument("target_repo")
2020-02-04 10:03:29 -05:00
def copy_labels(source_repo, target_repo, dry):
"""
Copy labels from the source repository to the target repository.
\f
:param source: The full name of a GitHub repo from where the labels will be copied from. Eg. github/opensourcefriday
:type source: str
:param target: The full name of a GitHub repo to where the labels will be copied. Eg. github/opensourcefriday
:type target: str
:return:
"""
source_owner, source_repo_name = source_repo.split("/")
target_owner, target_repo_name = target_repo.split("/")
2020-02-04 09:49:58 -05:00
try:
2020-08-20 10:44:11 -04:00
print(
"Fetching labels for {source_repo_name} repo.".format(
source_repo_name=source_repo_name
)
)
2020-02-04 10:03:29 -05:00
_, source_repo_labels = get_labels(source_owner, source_repo_name)
2020-08-20 10:44:11 -04:00
print(
"Fetched labels for {source_repo_name}".format(
source_repo_name=source_repo_name
)
)
print(
"Fetching labels for {target_repo_name} repo.".format(
target_repo_name=target_repo_name
)
)
2020-02-04 10:03:29 -05:00
target_repo_id, target_repo_labels = get_labels(target_owner, target_repo_name)
2020-08-20 10:44:11 -04:00
print(
"Fetched labels for {target_repo_name}".format(
target_repo_name=target_repo_name
)
)
2020-02-04 10:03:29 -05:00
2020-08-20 10:44:11 -04:00
filtered_labels = list(
filter(lambda x: x not in target_repo_labels, source_repo_labels)
)
2020-02-04 10:03:29 -05:00
if dry:
2020-08-20 10:44:11 -04:00
print("This is just a dry run. No labels will be copied/created.")
print(
"{label_count} labels would have been created.".format(
label_count=len(filtered_labels)
)
)
2020-02-04 10:03:29 -05:00
pprint(filtered_labels, indent=4)
else:
2020-08-20 10:44:11 -04:00
print(
"Preparing to created {label_count} labels in {target_repo}".format(
label_count=len(filtered_labels), target_repo=target_repo
)
)
2020-02-04 10:03:29 -05:00
for label in filtered_labels:
create_label(target_repo_id, label)
except Exception as error:
sys.exit(error)
2020-08-20 10:44:11 -04:00
print("Done")
2020-07-21 20:54:30 -04:00
2020-08-20 11:04:36 -04:00
2020-02-04 10:03:29 -05:00
if __name__ == "__main__":
# Pylint doesn't know that @click.command takes care of injecting the
# function parameters. Disabling Pylint error.
2020-07-21 20:54:30 -04:00
copy_labels() # pylint: disable=no-value-for-parameter