Skip to content

Commit

Permalink
Merge pull request #2534 from jupyter-naas/2529-github-get-reactions-…
Browse files Browse the repository at this point in the history
…from-comment

2529 GitHub get reactions from comment
  • Loading branch information
FlorentLvr authored Jun 20, 2024
2 parents 17bb14e + bf270e2 commit cc7bbeb
Show file tree
Hide file tree
Showing 11 changed files with 8,504 additions and 61 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ dmypy.json
.github/documentation/documentation_update.py

# template data files
YahooFinance/STOCK_CLUSTERS.csv
*.csv
*.html
*.xz

# template outputs
Expand Down
306 changes: 306 additions & 0 deletions GitHub/GitHub_Get_comments_and_reactions_from_issue.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,306 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "84cadd0c",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"<img width=\"8%\" alt=\"GitHub.png\" src=\"https://raw.githubusercontent.com/jupyter-naas/awesome-notebooks/master/.github/assets/logos/GitHub.png\" style=\"border-radius: 15%\">"
]
},
{
"cell_type": "markdown",
"id": "0d43ed38",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"# GitHub - Get comments and reactions from issue\n",
"<a href=\"https://bit.ly/3JyWIk6\">Give Feedback</a> | <a href=\"https://github.com/jupyter-naas/awesome-notebooks/issues/new?assignees=&labels=bug&template=bug_report.md&title=GitHub+-+Add+new+issues+as+page+in+Notion+database:+Error+short+description\">Bug report</a>"
]
},
{
"cell_type": "markdown",
"id": "d9313642",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Tags:** #github #reaction #issue #comment #automation"
]
},
{
"cell_type": "markdown",
"id": "8faf487c",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Author:** [Varsha Kumar](https://www.linkedin.com/in/varsha-kumar-590466305/)"
]
},
{
"cell_type": "markdown",
"id": "3dba1c73-548d-4008-82ad-fdb2cb376771",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Last update:** 2024-06-05 (Created: 2024-06-04)"
]
},
{
"cell_type": "markdown",
"id": "naas-description",
"metadata": {
"papermill": {},
"tags": [
"description"
]
},
"source": [
"**Description:** This notebook allows users to retrieve the comments and reactions from an issue."
]
},
{
"cell_type": "markdown",
"id": "7412988b",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Input"
]
},
{
"cell_type": "markdown",
"id": "4f7623e8-ae47-4070-95f1-3d5a4b7410e8",
"metadata": {},
"source": [
"### Import libraries"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "353ef79c",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"import requests\n",
"import re\n",
"import pandas as pd\n",
"import naas_python"
]
},
{
"cell_type": "markdown",
"id": "68b48858",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Setup variables\n",
"- `github_token`: personal token creates\n",
"- `issue_url`: link to the chosen issue"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "01647a55",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"github_token = naas_python.secret.get(\"GITHUB_TOKEN\").value\n",
"issue_url = \"https://github.com/pola-rs/polars/issues/16661\""
]
},
{
"cell_type": "markdown",
"id": "93347abb",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Model\n",
"### Get comments and reactions from issue"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "29e87112-879c-439c-87b3-3065f9a91085",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"def extract_repo_details(issue_url):\n",
" # Extract owner, repo name, and issue number from the issue URL\n",
" match = re.match(r'https://github\\.com/(?P<owner>[^/]+)/(?P<repo>[^/]+)/issues/(?P<number>\\d+)', issue_url)\n",
" if not match:\n",
" raise ValueError(\"Invalid issue URL\")\n",
" return match.group('owner'), match.group('repo'), match.group('number')\n",
"\n",
"def get_issue_comments(repo_owner, repo_name, issue_number):\n",
" # GitHub API URL to get issue comments\n",
" api_url = f\"https://api.github.com/repos/{repo_owner}/{repo_name}/issues/{issue_number}/comments\"\n",
" headers = {\n",
" 'Authorization': f'token {github_token}',\n",
" 'Accept': 'application/vnd.github.v3+json'\n",
" }\n",
"\n",
" response = requests.get(api_url, headers=headers)\n",
" \n",
" if response.status_code == 200:\n",
" return response.json()\n",
" else:\n",
" print(f\"Failed to fetch comments: {response.status_code}\")\n",
" return []\n",
"\n",
"def get_comment_reactions(repo_owner, repo_name, comment_id):\n",
" api_url = f\"https://api.github.com/repos/{repo_owner}/{repo_name}/issues/comments/{comment_id}/reactions\"\n",
" headers = {\n",
" 'Authorization': f'token {github_token}',\n",
" 'Accept': 'application/vnd.github.squirrel-girl-preview+json' # Required for reactions API\n",
" }\n",
"\n",
" response = requests.get(api_url, headers=headers)\n",
"\n",
" if response.status_code == 200:\n",
" return response.json()\n",
" else:\n",
" print(f\"Failed to fetch reactions for comment {comment_id}: {response.status_code}\")\n",
" return []"
]
},
{
"cell_type": "markdown",
"id": "b819d06a",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Output\n",
"### Display result"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "947ce69b-f636-4ac4-abd9-4254fca3f897",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"data = []\n",
"\n",
"try:\n",
" repo_owner, repo_name, issue_number = extract_repo_details(issue_url)\n",
" comments = get_issue_comments(repo_owner, repo_name, issue_number)\n",
"\n",
" for comment in comments:\n",
" object_url = comment['issue_url']\n",
" comment_id = comment['id']\n",
" comment_body = comment['body']\n",
" comment_created_at = comment['created_at']\n",
" comment_user_login = comment['user']['login']\n",
" comment_user_id = comment['user']['id']\n",
" \n",
" data = [{\n",
" \"OBJECT_URL\": object_url,\n",
" \"ID\": comment_id,\n",
" \"TYPE\": \"COMMENT\",\n",
" \"CONTENT\": comment_body,\n",
" \"DATE_TIME\": comment_created_at,\n",
" \"USER_LOGIN\": comment_user_login,\n",
" \"USER_ID\": comment_user_id,\n",
" }]\n",
" \n",
" reactions = get_comment_reactions(repo_owner, repo_name, comment_id)\n",
" for reaction in reactions:\n",
" reaction_id = reaction['id']\n",
" reaction_body = reaction['content']\n",
" reaction_created_at = reaction['created_at']\n",
" reaction_user_login = reaction['user']['login']\n",
" reaction_user_id = reaction['user']['id']\n",
" \n",
" data.append({\n",
" \"OBJECT_URL\": object_url,\n",
" \"ID\": comment_id,\n",
" \"TYPE\": \"REACTION\",\n",
" \"CONTENT\": reaction_body,\n",
" \"DATE_TIME\": reaction_created_at,\n",
" \"USER_LOGIN\": reaction_user_login,\n",
" \"USER_ID\": reaction_user_id,\n",
" })\n",
" \n",
" \n",
"\n",
"except ValueError as e:\n",
" print(e)\n",
" \n",
"df = pd.DataFrame(data)\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5326f466-a95b-467a-8b8e-30235d6e5a78",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
},
"naas": {
"notebook_id": "b8a92a0e4b6e40db304564f999566443fb35e93df716ab4be5021aabba8230ee",
"notebook_path": "GitHub/GitHub_Add_new_issues_as_page_in_Notion_database.ipynb"
},
"papermill": {
"default_parameters": {},
"environment_variables": {},
"parameters": {},
"version": "2.3.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit cc7bbeb

Please sign in to comment.