├── .github ├── ISSUE_TEMPLATE.md └── workflows │ └── sync-md2notion-pr-merge.yml ├── .gitignore ├── LICENSE ├── README.md ├── docs ├── NestedTest │ └── nested_example.md └── example.md ├── git_notion ├── __init__.py ├── cli.py └── git_notion.py ├── requirements.in ├── setup.cfg ├── setup.py └── tests ├── __init__.py └── test_git_notion.py /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | * git-notion version: 2 | * Python version: 3 | * Operating System: 4 | 5 | ### Description 6 | 7 | Describe what you were trying to get done. 8 | Tell us what happened, what went wrong, and what you expected to happen. 9 | 10 | ### What I Did 11 | 12 | ``` 13 | Paste the command(s) you ran and the output. 14 | If there was a crash, please include the traceback here. 15 | ``` 16 | -------------------------------------------------------------------------------- /.github/workflows/sync-md2notion-pr-merge.yml: -------------------------------------------------------------------------------- 1 | name: Sync md files to notion when PR merged 2 | 3 | on: 4 | pull_request: 5 | types: [closed] 6 | 7 | jobs: 8 | sync: 9 | timeout-minutes: 10 10 | runs-on: ubuntu-latest 11 | steps: 12 | - uses: actions/checkout@v2 13 | - name: Setup Python 14 | uses: actions/setup-python@v2 15 | with: 16 | python-version: '3.x' 17 | - name: Cache pip 18 | uses: actions/cache@v2 19 | with: 20 | # This path is specific to Ubuntu 21 | path: ~/.cache/pip 22 | # Look to see if there is a cache hit for the corresponding requirements file 23 | key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }} 24 | restore-keys: | 25 | ${{ runner.os }}-pip- 26 | ${{ runner.os }}- 27 | - name: Install dependencies 28 | run: | 29 | python -m pip install --upgrade pip git-notion 30 | - name: Sync md doc 31 | run: | 32 | git-notion 33 | env: 34 | NOTION_IGNORE_REGEX: ${{ secrets.NOTION_IGNORE_REGEX }} 35 | NOTION_ROOT_PAGE: ${{ secrets.NOTION_ROOT_PAGE }} 36 | NOTION_TOKEN_V2: ${{ secrets.NOTION_TOKEN_V2 }} -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | .pytest_cache/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | 58 | # Flask stuff: 59 | instance/ 60 | .webassets-cache 61 | 62 | # Scrapy stuff: 63 | .scrapy 64 | 65 | # Sphinx documentation 66 | docs/_build/ 67 | 68 | # PyBuilder 69 | target/ 70 | 71 | # Jupyter Notebook 72 | .ipynb_checkpoints 73 | 74 | # pyenv 75 | .python-version 76 | 77 | # celery beat schedule file 78 | celerybeat-schedule 79 | 80 | # SageMath parsed files 81 | *.sage.py 82 | 83 | # dotenv 84 | .env 85 | 86 | # virtualenv 87 | .venv 88 | venv/ 89 | ENV/ 90 | 91 | # Spyder project settings 92 | .spyderproject 93 | .spyproject 94 | 95 | # Rope project settings 96 | .ropeproject 97 | 98 | # mkdocs documentation 99 | /site 100 | 101 | # mypy 102 | .mypy_cache/ 103 | 104 | # IDE settings 105 | .vscode/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020, NarekA 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Git Notion 2 | ========== 3 | 4 | Syncs Github markdown files in your repository to Notion. 5 | 6 | This utility is described in the following [blog post](https://www.swiftlane.com/blog/syncing-docs-from-code-repositories-to-notion/). 7 | 8 | See example [Notion page](https://www.notion.so/git_notion-195c08d3d14140eb9a35ac00f9a0f078). 9 | 10 | ## Installation 11 | ``` 12 | pip install git-notion 13 | ``` 14 | 15 | or for local installation: 16 | 17 | ```bash 18 | git clone https://github.com/NarekA/git-notion.git 19 | cd git-notion 20 | pip install -e . 21 | ``` 22 | 23 | ## Configuring 24 | 25 | `NOTION_TOKEN_V2` - Can be found in your [browser cookies](https://www.redgregory.com/notion/2020/6/15/9zuzav95gwzwewdu1dspweqbv481s5) for Notion's website. 26 | `NOTION_ROOT_PAGE` - URL for notion page. Repo docs will be a new page under this page. 27 | `NOTION_IGNORE_REGEX` - Regex for paths to ignore. 28 | 29 | These environment variables can be set. 30 | ```bash 31 | export NOTION_TOKEN_V2= 32 | export NOTION_ROOT_PAGE="https://www.notion.so/..." # Can be in setup.cfg as well 33 | export NOTION_IGNORE_REGEX="models/.*" # Can be in setup.cfg as well 34 | ``` 35 | 36 | These parameters can be set in the `setup.cfg` for the repo. 37 | ``` 38 | [git-notion] 39 | ignore_regex = models/.* 40 | notion_root_page = https://www.notion.so/... 41 | ``` 42 | 43 | If you want to map specific Github folders to Notion subpages besides the `notion_root_page`, you can add the folder names and subpage URLs as parameters in the `setup.cfg` for the repo: 44 | ``` 45 | [folders] 46 | # docs = # This can be any subpage of the Notion root page 47 | # docs/NestedTest = # This can be the same subpage as above, or any other subpage of the Notion root page 48 | ``` 49 | 50 | ## Usage 51 | 52 | ```bash 53 | # To upload your current directory 54 | git-notion 55 | 56 | # To upload another directory 57 | git-notion --path path/to/your/repo 58 | ``` 59 | 60 | 61 | ## Pushing to PYPI 62 | 63 | ```bash 64 | bumpversion patch # Look-up bumpversion 65 | rm -rf dist/ 66 | python3 setup.py sdist bdist_wheel 67 | python3 -m twine upload dist/* 68 | ``` 69 | -------------------------------------------------------------------------------- /docs/NestedTest/nested_example.md: -------------------------------------------------------------------------------- 1 | ### Heading for Test 2 | Here's an example of a markdown file that's in a nested folder within the folder `docs`. We can also add the folder path `docs/NestedDocs` in `setup.cfg` to pick up this file to the specified Notion URL. 3 | -------------------------------------------------------------------------------- /docs/example.md: -------------------------------------------------------------------------------- 1 | Example 2 | == 3 | 4 | This is an example markdown file to sync. 5 | 6 | If you add the `docs` folder name to the `[folders]` config settings in `setup.cfg`, this file will sync to the specified Notion subpage URL. 7 | -------------------------------------------------------------------------------- /git_notion/__init__.py: -------------------------------------------------------------------------------- 1 | """Top-level package for git-notion.""" 2 | 3 | __author__ = """NarekA""" 4 | __version__ = '0.2.4' 5 | 6 | from git_notion.git_notion import * 7 | -------------------------------------------------------------------------------- /git_notion/cli.py: -------------------------------------------------------------------------------- 1 | """Console script for git_notion.""" 2 | import sys 3 | import click 4 | import git_notion 5 | 6 | 7 | @click.command() 8 | @click.option('--path', default=".", help='The path to the repo you want to sync') 9 | def main(path): 10 | """Console script for git_notion.""" 11 | click.echo("running sync") 12 | git_notion.sync_to_notion(path) 13 | return 0 14 | 15 | 16 | if __name__ == "__main__": 17 | sys.exit(main()) # pragma: no cover 18 | -------------------------------------------------------------------------------- /git_notion/git_notion.py: -------------------------------------------------------------------------------- 1 | """Main module.""" 2 | import hashlib 3 | import os 4 | import glob 5 | from configparser import ConfigParser 6 | import re 7 | 8 | from notion.block import PageBlock 9 | from notion.block import TextBlock 10 | from notion.client import NotionClient 11 | from md2notion.upload import upload 12 | 13 | 14 | TOKEN = os.getenv("NOTION_TOKEN_V2", "") 15 | _client = None 16 | 17 | 18 | def get_client(): 19 | global _client 20 | if not _client: 21 | _client = NotionClient(token_v2=TOKEN) 22 | return _client 23 | 24 | 25 | def get_or_create_page(base_page, title): 26 | page = None 27 | for child in base_page.children.filter(PageBlock): 28 | if child.title == title: 29 | page = child 30 | 31 | if not page: 32 | page = base_page.children.add_new(PageBlock, title=title) 33 | return page 34 | 35 | 36 | def upload_file(base_page, filename: str, page_title=None): 37 | page_title = page_title or filename 38 | page = get_or_create_page(base_page, page_title) 39 | hasher = hashlib.md5() 40 | with open(filename, "rb") as mdFile: 41 | buf = mdFile.read() 42 | hasher.update(buf) 43 | if page.children and hasher.hexdigest() in str(page.children[0]): 44 | return page 45 | 46 | for child in page.children: 47 | child.remove() 48 | page.children.add_new(TextBlock, title=f"MD5: {hasher.hexdigest()}") 49 | 50 | with open(filename, "r", encoding="utf-8") as mdFile: 51 | upload(mdFile, page) 52 | return page 53 | 54 | 55 | def sync_to_notion(repo_root: str = "."): 56 | os.chdir(repo_root) 57 | config = ConfigParser() 58 | config.read(os.path.join(repo_root, "setup.cfg")) 59 | repo_name = os.path.basename(os.getcwd()) 60 | 61 | root_page_url = os.getenv("NOTION_ROOT_PAGE") or config.get('git-notion', 'notion_root_page') 62 | ignore_regex = os.getenv("NOTION_IGNORE_REGEX") or config.get('git-notion', 'ignore_regex', fallback=None) 63 | root_page = get_client().get_block(root_page_url) 64 | repo_page = get_or_create_page(root_page, repo_name) 65 | 66 | for file in glob.glob("**/*.md", recursive=True): 67 | if ignore_regex is None or not re.match(ignore_regex, file): 68 | 69 | # Extract folder from the file path 70 | folder = os.path.dirname(file) 71 | 72 | # Use folder-specific URL if available, otherwise use the default repo_page URL 73 | folder_url = config.get('folders', folder, fallback=None) 74 | upload_file(repo_page if folder_url is None else get_client().get_block(folder_url), file) 75 | if folder_url: 76 | print(file, "uploaded to: ", folder_url) 77 | else: 78 | print(file, "uploaded to: default repo page") 79 | 80 | 81 | 82 | # Example call: 83 | # sync_to_notion(repo_root="/path/to/repo", config_file_path="notion_config.ini") 84 | -------------------------------------------------------------------------------- /requirements.in: -------------------------------------------------------------------------------- 1 | click 2 | md2notion -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [bumpversion] 2 | current_version = 0.2.4 3 | commit = True 4 | tag = True 5 | 6 | [bumpversion:file:setup.py] 7 | search = version='{current_version}' 8 | replace = version='{new_version}' 9 | 10 | [bumpversion:file:git_notion/__init__.py] 11 | search = __version__ = '{current_version}' 12 | replace = __version__ = '{new_version}' 13 | 14 | [bdist_wheel] 15 | universal = 1 16 | 17 | [flake8] 18 | exclude = docs 19 | 20 | [aliases] 21 | test = pytest 22 | 23 | [tool:pytest] 24 | collect_ignore = ['setup.py'] 25 | 26 | [git-notion] 27 | notion_root_page = https://www.notion.so/Git-Documents-6cf97b2d7ab64a7d964c7a7bcc42f9aa 28 | 29 | [folders] 30 | # docs = # This can be any subpage of the Notion root page 31 | # docs/NestedTest = # This can be the same subpage as above, or any other subpage of the Notion root page 32 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from setuptools import setup, find_packages 4 | import os 5 | 6 | base_dir = os.path.abspath(os.path.dirname(__file__)) 7 | 8 | with open(os.path.join('requirements.in')) as f: 9 | requirements = [l.strip() for l in f.readlines() if l.strip()] 10 | 11 | with open(os.path.join(base_dir, 'README.md'), encoding='utf-8') as f: 12 | long_description = f.read() 13 | 14 | setup_requirements = ['pytest-runner', ] 15 | 16 | test_requirements = ['pytest>=3', ] 17 | 18 | setup( 19 | author="NarekA", 20 | python_requires='>=3.5', 21 | classifiers=[ 22 | 'Development Status :: 2 - Pre-Alpha', 23 | 'Intended Audience :: Developers', 24 | 'License :: OSI Approved :: MIT License', 25 | 'Natural Language :: English', 26 | 'Programming Language :: Python :: 3', 27 | 'Programming Language :: Python :: 3.5', 28 | 'Programming Language :: Python :: 3.6', 29 | 'Programming Language :: Python :: 3.7', 30 | 'Programming Language :: Python :: 3.8', 31 | ], 32 | description="Syncs Github Mmarkdown files to Notion", 33 | entry_points={ 34 | 'console_scripts': [ 35 | 'git-notion=git_notion.cli:main', 36 | ], 37 | }, 38 | install_requires=requirements, 39 | license="MIT license", 40 | include_package_data=True, 41 | keywords='git_notion', 42 | long_description=long_description, 43 | long_description_content_type='text/markdown', 44 | name='git_notion', 45 | packages=find_packages(include=['git_notion', 'git_notion.*']), 46 | setup_requires=setup_requirements, 47 | test_suite='tests', 48 | tests_require=test_requirements, 49 | url='https://github.com/NarekA/git-notion', 50 | version='0.2.5', 51 | zip_safe=False, 52 | ) 53 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- 1 | """Unit test package for git_notion.""" 2 | -------------------------------------------------------------------------------- /tests/test_git_notion.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """Tests for `git_notion` package.""" 4 | 5 | import pytest 6 | 7 | from click.testing import CliRunner 8 | 9 | from git_notion import git_notion 10 | from git_notion import cli 11 | 12 | 13 | @pytest.fixture 14 | def response(): 15 | """Sample pytest fixture. 16 | 17 | See more at: http://doc.pytest.org/en/latest/fixture.html 18 | """ 19 | # import requests 20 | # return requests.get('https://github.com/audreyr/cookiecutter-pypackage') 21 | 22 | 23 | def test_content(response): 24 | """Sample pytest test function with the pytest fixture as an argument.""" 25 | # from bs4 import BeautifulSoup 26 | # assert 'GitHub' in BeautifulSoup(response.content).title.string 27 | 28 | 29 | def test_command_line_interface(): 30 | """Test the CLI.""" 31 | runner = CliRunner() 32 | result = runner.invoke(cli.main) 33 | assert result.exit_code == 0 34 | assert 'git_notion.cli.main' in result.output 35 | help_result = runner.invoke(cli.main, ['--help']) 36 | assert help_result.exit_code == 0 37 | assert '--help Show this message and exit.' in help_result.output 38 | --------------------------------------------------------------------------------