Skip to content

Commit d4292f2

Browse files
committed
flake8 and formatted with docstrings
1 parent 61fcce2 commit d4292f2

File tree

2 files changed

+37
-17
lines changed

2 files changed

+37
-17
lines changed

src/ontobot_change_agent/api.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
# -*- coding: utf-8 -*-
22
"""API section."""
33

4-
import os
54
import re
65
from os.path import join, splitext
76
from pathlib import Path
87
from typing import Generator, Optional
98

10-
import click
119
import kgcl_schema.grammar.parser as kgcl_parser
1210
from github import Github
1311
from github.Issue import Issue
@@ -32,15 +30,6 @@
3230
SRC = Path(__file__).parent
3331
TOKEN_FILE = join(SRC, "token.txt")
3432

35-
if os.getenv("GITHUB_ENV"):
36-
click.echo(
37-
"${{ secrets.GH_TOKEN }} >> " + TOKEN_FILE
38-
)
39-
40-
with open(TOKEN_FILE, "r") as t:
41-
TOKEN = t.read().rstrip()
42-
43-
g = Github(TOKEN)
4433
# Example for API: https://pygithub.readthedocs.io/en/latest/examples.html
4534

4635
RAW_DATA = "_rawData"
@@ -62,6 +51,7 @@
6251

6352
def get_issues(
6453
repository_name: str,
54+
token: str = None,
6555
title_search: Optional[str] = None,
6656
label: Optional[str] = None,
6757
number: Optional[int] = 0,
@@ -70,10 +60,19 @@ def get_issues(
7060
"""Get issues of specific states from a Github repository.
7161
7262
:param repository_name: Name of the repository [org/repo]
73-
:param title_search: Regex for title of the issue.
63+
:param token: Github token for authorization, defaults to None
64+
:param title_search: Regex for title of the issue., defaults to None
65+
:param label: Issue label, defaults to None
66+
:param number: Issue number, defaults to 0
7467
:param state: State of the issue e.g. open, close etc., defaults to "open"
7568
:yield: Issue names that match the regex/label/number.
7669
"""
70+
if token is None:
71+
token_file = TOKEN_FILE
72+
with open(token_file, "r") as t:
73+
token = t.read().rstrip()
74+
75+
g = Github(token)
7776
repo = g.get_repo(repository_name)
7877
label_object = None
7978
if label:
@@ -114,12 +113,19 @@ def _make_sense_of_body(body: str) -> str:
114113
return body.replace("<", "").replace(">", "")
115114

116115

117-
def get_all_labels_from_repo(repository_name: str) -> dict:
116+
def get_all_labels_from_repo(repository_name: str, token: str = None) -> dict:
118117
"""Get all labels available in a repository for tagging issues on creation.
119118
120119
:param repository_name: Name of the repository.
120+
:param token: Github token for authorization, defaults to None
121121
:return: A dictionary of {name: description}
122122
"""
123+
if token is None:
124+
token_file = TOKEN_FILE
125+
with open(token_file, "r") as t:
126+
token = t.read().rstrip()
127+
128+
g = Github(token)
123129
repo = g.get_repo(repository_name)
124130
return {label.name: label.description for label in repo.get_labels()}
125131

src/ontobot_change_agent/cli.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ def main(verbose: int, quiet: bool):
5151
required=True,
5252
help="Org/name of the github repo.",
5353
)
54+
token_option = click.option(
55+
"-g",
56+
"--token",
57+
help="Github token for the repository.",
58+
)
5459

5560
issue_number_option = click.option(
5661
"-n",
@@ -88,10 +93,12 @@ def main(verbose: int, quiet: bool):
8893
"--title-search",
8994
help="Filter based on a search for pattern within title of issue.",
9095
)
96+
@token_option
9197
@label_option
9298
@issue_number_option
9399
def issues(
94100
repo: str,
101+
token: str,
95102
state: str,
96103
title_search: str,
97104
label: str,
@@ -108,6 +115,7 @@ def issues(
108115
"""
109116
for issue in get_issues(
110117
repository_name=repo,
118+
token=token,
111119
state=state,
112120
title_search=title_search,
113121
label=label,
@@ -118,22 +126,26 @@ def issues(
118126

119127
@main.command("get-labels")
120128
@repo_option
121-
def get_labels(repo: str):
129+
@token_option
130+
def get_labels(repo: str, token: str):
122131
"""Get all labels for issues.
123132
124133
:param repo: GitHub repository name [org/repo_name]
125134
"""
126-
print(get_all_labels_from_repo(repo))
135+
print(get_all_labels_from_repo(repo, token))
127136

128137

129138
@main.command()
130139
@input_argument
131140
@repo_option
141+
@token_option
132142
@label_option
133143
@issue_number_option
134144
@state_option
135145
@output_option
136-
def process_issue(input: str, repo: str, label: str, number: int, state: str, output: str):
146+
def process_issue(
147+
input: str, repo: str, token: str, label: str, number: int, state: str, output: str
148+
):
137149
"""Run processes based on issue label.
138150
139151
:param repo: GitHub repository name [org/repo_name]
@@ -142,7 +154,9 @@ def process_issue(input: str, repo: str, label: str, number: int, state: str, ou
142154
"""
143155
formatted_body = "The following commands were executed: </br> "
144156

145-
for issue in get_issues(repository_name=repo, label=label, number=number, state=state):
157+
for issue in get_issues(
158+
repository_name=repo, token=token, label=label, number=number, state=state
159+
):
146160
# Make sure ontobot_change_agent needs to be triggered or no.
147161
if issue:
148162
if re.match(r"(.*)ontobot(.*)apply(.*):(.*)", issue[BODY]):

0 commit comments

Comments
 (0)