Created
February 11, 2021 22:06
-
-
Save chaithyagr/010f1fef5785aca5d0065a76f8182c15 to your computer and use it in GitHub Desktop.
Notion Github Issue push Integration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#This is a bare minimum integration where github issues result in new task / bugs etc in notion timelines | |
from github import Github | |
from notion.client import NotionClient | |
import numpy as np | |
import datetime | |
import pytz | |
git = Github("<GITHUB API Token>") | |
client = NotionClient(token_v2="<Notion Token>") | |
timeline = client.get_collection_view("<Hyperlink to notion timeline>") | |
# Read collection | |
all_current_rows = timeline.collection.get_rows() | |
# Generally i map a repository to an epic in notion timeline, so that things are more easily organized. | |
# This dictionary can be updated with all the maps between repositories and respective epics | |
repo_to_epic_map = { | |
"<repository_name>": "<epic_name>", | |
} | |
# In order to keep things fast, we save the last updated date time here. | |
try: | |
LastUpdated = np.load('LastUpdated.npy', allow_pickle=True).item() | |
except: | |
# First run, take 20 years back as last updated! This way we update everything | |
LastUpdated = datetime.datetime.now() - datetime.timedelta(years=365*20) | |
def get_epic(rows, title): | |
"""This function looks for an epic with a titles""" | |
for c in rows: | |
if c.get_property('type') == 'Epic ⛰️': | |
if c.title == title: | |
return c | |
def append_epic(row, epic): | |
"""This function appends and epic to current list of epics""" | |
epics = row.get_property('epic') | |
epics.append(epic) | |
row.set_property('epic', epics) | |
def set_labels(row, git_labels): | |
"""We can setup different github lables to do different things here""" | |
for label in git_labels: | |
if label.name == 'task': | |
row.set_property('type', 'Task 🔨') | |
if label.name == 'bug': | |
row.set_property('type', 'Bug 🐞') | |
if label.name == 'working': | |
row.set_property('status', 'In Progress') | |
def check_row_or_make(collection, title): | |
"""Look for a row in collection or just make a new row""" | |
for row in collection.get_rows(): | |
if row.title == title: | |
return row | |
return timeline.collection.add_row() | |
for repo in repo_to_epic_map: | |
git_repo = git.get_repo(repo) | |
for issue in git_repo.get_issues(): | |
# Loop through issues | |
if pytz.utc.localize(LastUpdated) > issue.updated_at.replace(tzinfo=pytz.UTC): | |
# If issue was updated after the `LastUpdate` | |
row = check_row_or_make(timeline.collection, issue.title) | |
row.title = issue.title | |
append_epic(row, get_epic(all_current_rows, repo_to_epic_map[repo])) | |
if issue.state == 'open' and row.get_property('status') != 'In Progress': | |
row.set_property('status', 'Not Started') | |
elif issue.state == 'closed': | |
row.set_property('status', 'Complete 🙌') | |
set_labels(row, issue.labels) | |
print(issue) | |
# Save the lastUpdated time | |
np.save('LastUpdated.npy', datetime.datetime.now()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please do leave some comments, issues or any code updates.