11# -*- coding: utf-8 -*-
22"""API section."""
33
4- import os
54import re
65from os .path import join , splitext
76from pathlib import Path
87from typing import Generator , Optional
98
10- import click
119import kgcl_schema .grammar .parser as kgcl_parser
1210from github import Github
1311from github .Issue import Issue
3230SRC = Path (__file__ ).parent
3331TOKEN_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
4635RAW_DATA = "_rawData"
6251
6352def 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
0 commit comments