Skip to content

Instantly share code, notes, and snippets.

@sshivaditya2019
Created October 3, 2024 14:30
Show Gist options
  • Save sshivaditya2019/872694b0a21dd92b57374f76017f45d6 to your computer and use it in GitHub Desktop.
Save sshivaditya2019/872694b0a21dd92b57374f76017f45d6 to your computer and use it in GitHub Desktop.
import json
from github import Github
# Authentication is defined via github.Auth
from github import Auth
import os
from dotenv import load_dotenv
load_dotenv()
auth = Auth.Token(os.getenv('GITHUB_TOKEN'))
g = Github(auth=auth)
class Issue:
def __init__(self, url, repository_url, labels_url, comments_url, events_url, html_url, id, node_id, number, title, user, labels, state, locked, assignee, assignees, milestone, comments, created_at, updated_at, closed_at, author_association, active_lock_reason, body, closed_by, reactions, timeline_url, performed_via_github_app, state_reason):
self.url = url
self.repository_url = repository_url
self.labels_url = labels_url
self.comments_url = comments_url
self.events_url = events_url
self.html_url = html_url
self.id = id
self.node_id = node_id
self.number = number
self.title = title
self.user = user
self.labels = labels
self.state = state
self.locked = locked
self.assignee = assignee
self.assignees = assignees
self.milestone = milestone
self.comments = comments
self.created_at = created_at
self.updated_at = updated_at
self.closed_at = closed_at
self.author_association = author_association
self.active_lock_reason = active_lock_reason
self.body = body
self.closed_by = closed_by
self.reactions = reactions
self.timeline_url = timeline_url
self.performed_via_github_app = performed_via_github_app
self.state_reason = state_reason
def __repr__(self):
return f"Issue(id={self.id}, title={self.title}, state={self.state}, created_at={self.created_at}, updated_at={self.updated_at})"
def fetch_issue_comments(self):
repo_org = self.repository_url.split('/')[4]
repo_name = self.repository_url.split('/')[5]
repo = g.get_repo(f"{repo_org}/{repo_name}")
issue = repo.get_issue(self.number)
comments = issue.get_comments()
return comments
def get_repo_org(self):
repo_org = self.repository_url.split('/')[4]
repo_name = self.repository_url.split('/')[5]
return f"{repo_org}-{repo_name}#issue-{self.number}"
def fetch_associated_prs(self):
repo_org = self.repository_url.split('/')[4]
repo_name = self.repository_url.split('/')[5]
repo = g.get_repo(f"{repo_org}/{repo_name}")
# Search for PRs that mention the issue number in their body or title
prs = repo.get_pulls(state='all')
associated_prs = []
issue_ref = f"{self.number}" # Reference to the issue number
for pr in prs:
# Ensure pr.body and pr.title are not None before checking
# Check if issue_ref is in pr.body
if pr.body is not None and issue_ref in pr.body:
print(f"PR {pr.number} mentions issue {self.number} in body")
associated_prs.append(pr)
return associated_prs
def fetch_comments_from_prs(self):
prs = self.fetch_associated_prs()
pr_comments = []
for pr in prs:
temp = pr.get_comments() # Fetch comments from PRs
temp_list = list(temp) # Convert PaginatedList to a list
for comment in temp_list:
pr_comments.append(comment)
return pr_comments
def fetch_all_comments_related(self):
# Convert PaginatedList to a list for issue comments
issue_comments = list(self.fetch_issue_comments()) # Convert PaginatedList to a list
pr_comments = self.fetch_comments_from_prs() # PR comments is already a list
new_com = []
for x in issue_comments:
new_com.append({"issue_node_id": x.node_id, "comment": x.body})
for x in pr_comments:
new_com.append({"pr_node_id": x.raw_data["node_id"], "comment": x.body})
return new_com
def import_json():
with open('devpool-issues.json') as f:
data = json.load(f)
return data
def get_issues():
data = import_json()
issues = []
for issue in data:
issues.append(Issue(
url=issue['url'], repository_url=issue['repository_url'], labels_url=issue['labels_url'],
comments_url=issue['comments_url'], events_url=issue['events_url'], html_url=issue['html_url'],
id=issue['id'], node_id=issue['node_id'], number=issue['number'], title=issue['title'],
user=issue['user'], labels=issue['labels'], state=issue['state'], locked=issue['locked'],
assignee=issue['assignee'], assignees=issue['assignees'], milestone=issue['milestone'],
comments=issue['comments'], created_at=issue['created_at'], updated_at=issue['updated_at'],
closed_at=issue['closed_at'], author_association=issue['author_association'],
active_lock_reason=issue['active_lock_reason'], body=issue['body'], closed_by=issue['closed_by'],
reactions=issue['reactions'], timeline_url=issue['timeline_url'], performed_via_github_app=issue['performed_via_github_app'],
state_reason=issue['state_reason']
))
tot_text = 0
for issue in issues:
comments = issue.fetch_all_comments_related()
#pr_comments = issue.fetch_comments_from_prs()
#comments.extend(pr_comments)
print(f"Number of comments for issue {issue.number}: {len(comments)}")
tot_text += len(comments)
#Dump to json
with open(f"comments/{issue.get_repo_org()}.json", 'w') as f:
json.dump(comments, f)
print(f"Total number of comments: {tot_text}")
return data
if __name__ == '__main__':
get_issues()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment