Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
zoi-aoba committed Apr 15, 2021
0 parents commit a7bd5c3
Show file tree
Hide file tree
Showing 8 changed files with 300 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/notion.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: issue_to_notion_card
on:
issues:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Notion Card Creator
uses: zoi-aoba/issue-to-notion@v1.1.1
env:
NOTION_TOKEN: ${{ secrets.NOTION_TOKEN }}
DATABASE_URL: { DATABASE_URL }
PROPERTY_NAME : state
132 changes: 132 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
main_test.py

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
Pipfile

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/
17 changes: 17 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM python:3.7-slim

LABEL "com.github.actions.name"="Issue to Notion"
LABEL "com.github.actions.description"="From github Issue Make Notion Card"
LABEL "com.github.actions.icon" = "activity"
LABEL "com.github.actions.color" = "yellow"

LABEL "repository"="https://github.com/zoi-aoba/issue-to-notion"
LABEL "maintainer"="zoi <zoi@zoi.zoi>"

WORKDIR /usr/src/app
COPY requirements.txt ./
COPY main.py ./
RUN ls
RUN pip install --no-cache-dir -r requirements.txt

ENTRYPOINT ["python", "/usr/src/app/main.py"]
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- 該当DBをfav
- NOTION_TOKENはcookieのtoken_v2
- DATABASE_URLにURLを
9 changes: 9 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name : issue-to-notion
description : Make github Issue Event affect Notion Card
author : Dohyeon Park
branding:
icon : activity
color : yellow
runs:
using: 'docker'
image: 'Dockerfile'
1 change: 1 addition & 0 deletions body.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# this file will be charged by your issue body
122 changes: 122 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import sys
import os
import json
from notion.client import NotionClient
from notion.block import PageBlock, BookmarkBlock
from md2notion.upload import upload, convert, uploadBlock

# Get data from github environment
path = os.environ.get("GITHUB_EVENT_PATH")
token = os.environ.get("NOTION_TOKEN")
database_url = os.environ.get("DATABASE_URL")
property_name = os.environ.get("PROPERTY_NAME","status")
state_open = os.environ.get("STATE_OPEN","open")
state_closed = os.environ.get("STATE_CLOSED","closed")
issue_link_property_name = os.environ.get("ISSUE_LINK_PROPERTY_NAME","github_url")


# Get the event string from github
with open(path,"r") as f:
github_event_str = f.read()

# Convert event string to json
github_event_json = json.loads(github_event_str)

# Login and go into collection
client = NotionClient(token_v2=token)
cv = client.get_collection_view(database_url)

def main():
print("main() is excuted")

global github_event_json
global cv

# Get issue title, body and link
action_type = github_event_json["action"]
issue_number = github_event_json["issue"]["number"]
issue_title = github_event_json["issue"]["title"]
issue_link = github_event_json["issue"]["html_url"]

print("action_type is",action_type)

# Check action type
if action_type == "opened":

row = createRow(cv,issue_number,issue_title)

# Add Bookmark for issue
row.children.add_new(BookmarkBlock, title=issue_title, link=issue_link)
upload_body_with_markdown(row)
setattr(row,issue_link_property_name,"https://github.com/OnetapInc/locked/issues/"+str(issue_number))
else:
row = get_or_create_row(cv,issue_number,issue_title)
setattr(row,issue_link_property_name,"https://github.com/OnetapInc/locked/issues/"+str(issue_number))

if action_type == "edited":
clear_page(row)
row.children.add_new(BookmarkBlock, title=issue_title, link=issue_link)
upload_body_with_markdown(row)

elif action_type == "closed":
setattr(row,property_name,state_closed)

elif action_type == "deleted":
pass
# TODO
elif action_type == "reopened":
setattr(row,property_name,state_open)

elif action_type == "labeled" or action_type == "unlabeled":
pass
# TODO


def upload_body_with_markdown(row):
global github_event_json

body = github_event_json["issue"]["body"]

# Make markdown file from issue body
f= open("body.md","w+")
f.write(body)
f.close()

# Upload issue body markdown file to row
with open("body.md", "r", encoding="utf-8") as mdFile:
upload(mdFile,row)

def clear_page(row):
for child in row.children:
child.remove()

def get_row_with_IssueNumber(number):
global cv
inputNumber ="[#"+str(number)+"]"
print('issue number is',inputNumber)

exact_ID_filter_params = {
'filters': [{'property': "title", 'filter': {'operator': "string_starts_with", 'value': {'type': "exact", 'value': inputNumber}}}],
'operator': "and"
}
rows = list(filter(lambda row : row.title.startswith(inputNumber),cv.build_query(filter=exact_ID_filter_params).execute()))
print('filtered rows :',rows)
if len(rows) == 0:
return None
return rows[0]

def createRow(cv, issue_number, issue_title):
# Add row to notion collection
row = cv.collection.add_row()
row.name = "[#"+str(issue_number)+"] "+issue_title
setattr(row,property_name,state_open)

return row

def get_or_create_row(cv, issue_number, issue_title):
row = get_row_with_IssueNumber(issue_number)
if not row:
row = createRow(cv, issue_number, issue_title)
return row

main()
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
notion==0.0.25
md2notion==2.1.1

0 comments on commit a7bd5c3

Please sign in to comment.