Skip to content
Merged

Dev #90

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion PyBugReporter/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.0.15'
__version__ = '1.0.17'
73 changes: 68 additions & 5 deletions PyBugReporter/src/BugReporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

from python_graphql_client import GraphqlClient

BUG_LABEL_NAME: str = "bug"
AUTO_LABEL_NAME: str = "auto generated"
BUG_LABEL_COLOR: str = "d73a4a"
AUTO_LABEL_COLOR: str = "ededed"

class NotCreatedError(Exception):
"""Raised when someone tries to report a bug to a repo that has not been set up as a reporting destination through setVars.
"""
Expand Down Expand Up @@ -226,8 +231,8 @@ async def _sendBugReport_async(self, repoName: str, errorTitle: str, errorMessag

# query variables
repoId = await self._getRepoId_async(self.handlers[repoName])
bugLabel = "LA_kwDOJ3JPj88AAAABU1q15w"
autoLabel = "LA_kwDOJ3JPj88AAAABU1q2DA"
bugLabel = await self._get_or_create_label_id_async(self.handlers[repoName], repoId, BUG_LABEL_NAME, BUG_LABEL_COLOR)
autoLabel = await self._get_or_create_label_id_async(self.handlers[repoName], repoId, AUTO_LABEL_NAME, AUTO_LABEL_COLOR)

# Create new issue
createIssue = """
Expand Down Expand Up @@ -344,7 +349,7 @@ async def _checkIfIssueExists_async(self, handler: BugHandler, errorTitle: str)
headers = {"Authorization": f"Bearer {handler.githubKey}"}

# query variables
autoLabel = "auto generated"
autoLabel = AUTO_LABEL_NAME

# Query to return all issues with auto gen label
findIssue = """
Expand Down Expand Up @@ -413,6 +418,64 @@ async def _getRepoId_async(self, handler: BugHandler) -> str:
repoID = await client.execute_async(query=getID, variables=variables, headers=headers)
return repoID['data']['repository']['id']

@classmethod
async def _get_or_create_label_id_async(cls, handler: BugHandler, repo_id: str, label_name: str, color: str = "ededed") -> str:
"""Gets a label ID from the repository, creating it if it does not exist.

Args:
handler (BugHandler): the object of reporting details
repo_id (str): the repository ID
label_name (str): the name of the label
color (str): the hex color code for creating the label

Returns:
str: the label ID
"""
client = GraphqlClient(endpoint="https://api.github.com/graphql")
headers = {"Authorization": f"Bearer {handler.githubKey}"}

getLabelQuery = """
query getLabel($owner: String!, $name: String!, $labelName: String!) {
repository(owner: $owner, name: $name) {
label(name: $labelName) {
id
}
}
}
"""

variables = {
"owner": handler.orgName,
"name": handler.repoName,
"labelName": label_name
}

result = await client.execute_async(query=getLabelQuery, variables=variables, headers=headers)
labelData = result.get("data", {}).get("repository", {}).get("label")
if labelData and labelData.get("id"):
return labelData["id"]

createLabelMutation = """
mutation createLabel($input: CreateLabelInput!) {
createLabel(input: $input) {
label {
id
}
}
}
"""

createVariables = {
"input": {
"repositoryId": repo_id,
"name": label_name,
"color": color
}
}

createResult = await client.execute_async(query=createLabelMutation, variables=createVariables, headers=headers)
return createResult["data"]["createLabel"]["label"]["id"]

@classmethod
def manualBugReport(cls, repoName: str, errorTitle: str, errorMessage: str) -> None:
"""Manually sends a bug report to the Github repository.
Expand Down Expand Up @@ -444,8 +507,8 @@ async def manualBugReportAsync(cls, repoName: str, errorTitle: str, errorMessage

# query variables
repoId = await cls._getRepoId_async(cls, handler)
bugLabel = "LA_kwDOJ3JPj88AAAABU1q15w"
autoLabel = "LA_kwDOJ3JPj88AAAABU1q2DA"
bugLabel = await cls._get_or_create_label_id_async(handler, repoId, BUG_LABEL_NAME, BUG_LABEL_COLOR)
autoLabel = await cls._get_or_create_label_id_async(handler, repoId, AUTO_LABEL_NAME, AUTO_LABEL_COLOR)

# Create new issue
createIssue = """
Expand Down
Loading