Skip to content

Commit

Permalink
flake8 and formatted with docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
hrshdhgd committed Dec 8, 2022
1 parent 61fcce2 commit d4292f2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
32 changes: 19 additions & 13 deletions src/ontobot_change_agent/api.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
# -*- coding: utf-8 -*-
"""API section."""

import os
import re
from os.path import join, splitext
from pathlib import Path
from typing import Generator, Optional

import click
import kgcl_schema.grammar.parser as kgcl_parser
from github import Github
from github.Issue import Issue
Expand All @@ -32,15 +30,6 @@
SRC = Path(__file__).parent
TOKEN_FILE = join(SRC, "token.txt")

if os.getenv("GITHUB_ENV"):
click.echo(
"${{ secrets.GH_TOKEN }} >> " + TOKEN_FILE
)

with open(TOKEN_FILE, "r") as t:
TOKEN = t.read().rstrip()

g = Github(TOKEN)
# Example for API: https://pygithub.readthedocs.io/en/latest/examples.html

RAW_DATA = "_rawData"
Expand All @@ -62,6 +51,7 @@

def get_issues(
repository_name: str,
token: str = None,
title_search: Optional[str] = None,
label: Optional[str] = None,
number: Optional[int] = 0,
Expand All @@ -70,10 +60,19 @@ def get_issues(
"""Get issues of specific states from a Github repository.
:param repository_name: Name of the repository [org/repo]
:param title_search: Regex for title of the issue.
:param token: Github token for authorization, defaults to None
:param title_search: Regex for title of the issue., defaults to None
:param label: Issue label, defaults to None
:param number: Issue number, defaults to 0
:param state: State of the issue e.g. open, close etc., defaults to "open"
:yield: Issue names that match the regex/label/number.
"""
if token is None:
token_file = TOKEN_FILE
with open(token_file, "r") as t:
token = t.read().rstrip()

g = Github(token)
repo = g.get_repo(repository_name)
label_object = None
if label:
Expand Down Expand Up @@ -114,12 +113,19 @@ def _make_sense_of_body(body: str) -> str:
return body.replace("<", "").replace(">", "")


def get_all_labels_from_repo(repository_name: str) -> dict:
def get_all_labels_from_repo(repository_name: str, token: str = None) -> dict:
"""Get all labels available in a repository for tagging issues on creation.
:param repository_name: Name of the repository.
:param token: Github token for authorization, defaults to None
:return: A dictionary of {name: description}
"""
if token is None:
token_file = TOKEN_FILE
with open(token_file, "r") as t:
token = t.read().rstrip()

g = Github(token)
repo = g.get_repo(repository_name)
return {label.name: label.description for label in repo.get_labels()}

Expand Down
22 changes: 18 additions & 4 deletions src/ontobot_change_agent/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ def main(verbose: int, quiet: bool):
required=True,
help="Org/name of the github repo.",
)
token_option = click.option(
"-g",
"--token",
help="Github token for the repository.",
)

issue_number_option = click.option(
"-n",
Expand Down Expand Up @@ -88,10 +93,12 @@ def main(verbose: int, quiet: bool):
"--title-search",
help="Filter based on a search for pattern within title of issue.",
)
@token_option
@label_option
@issue_number_option
def issues(
repo: str,
token: str,
state: str,
title_search: str,
label: str,
Expand All @@ -108,6 +115,7 @@ def issues(
"""
for issue in get_issues(
repository_name=repo,
token=token,
state=state,
title_search=title_search,
label=label,
Expand All @@ -118,22 +126,26 @@ def issues(

@main.command("get-labels")
@repo_option
def get_labels(repo: str):
@token_option
def get_labels(repo: str, token: str):
"""Get all labels for issues.
:param repo: GitHub repository name [org/repo_name]
"""
print(get_all_labels_from_repo(repo))
print(get_all_labels_from_repo(repo, token))


@main.command()
@input_argument
@repo_option
@token_option
@label_option
@issue_number_option
@state_option
@output_option
def process_issue(input: str, repo: str, label: str, number: int, state: str, output: str):
def process_issue(
input: str, repo: str, token: str, label: str, number: int, state: str, output: str
):
"""Run processes based on issue label.
:param repo: GitHub repository name [org/repo_name]
Expand All @@ -142,7 +154,9 @@ def process_issue(input: str, repo: str, label: str, number: int, state: str, ou
"""
formatted_body = "The following commands were executed: </br> "

for issue in get_issues(repository_name=repo, label=label, number=number, state=state):
for issue in get_issues(
repository_name=repo, token=token, label=label, number=number, state=state
):
# Make sure ontobot_change_agent needs to be triggered or no.
if issue:
if re.match(r"(.*)ontobot(.*)apply(.*):(.*)", issue[BODY]):
Expand Down

0 comments on commit d4292f2

Please sign in to comment.