Metadata-Version: 2.4
Name: issue-folder-sync
Version: 0.1.0
Summary: Keep GitHub issues and a local folder in step, both ways: text, sub-issues, attachments, milestones.
Keywords: github,issues,sync,markdown,notes,google-drive
Classifier: Programming Language :: Python :: 3
Classifier: Operating System :: OS Independent
Classifier: Topic :: Utilities
Requires-Python: >=3.9
Description-Content-Type: text/markdown
Requires-Dist: requests>=2.25
Provides-Extra: keyring
Requires-Dist: keyring>=23; extra == "keyring"
Provides-Extra: test
Requires-Dist: pytest>=7; extra == "test"

# issue-folder-sync

Keep the issues in a GitHub repository and a folder on your computer in step, both ways.
Each issue becomes a folder with a Markdown file you can edit in any editor. Edits, new folders, ticked boxes,
dropped-in files and moved folders go back to GitHub. Put the folder inside Google Drive, Dropbox or OneDrive and
your issues are on every device too.

```
my-folder/
  _SYNC-STATUS.md                  when the last run was and what it did; read this first
  Open/<issue title>/              open issues with no milestone
  Milestones/<milestone>/<title>/  open issues in a milestone
  Closed/<issue title>/            closed issues
    issue.md        "# <title>", a blank line, then the issue's text
    tasks.md        the issue's sub-issues as tick boxes (only when it has some)
    attachments/    files attached to the issue
    .issue.json     which issue this folder is; do not edit or copy it
```

## Install

```
pip install issue-folder-sync
```

Python 3.9 or newer. It needs only `requests`. On macOS and Linux, also install `keyring` so the token can be kept in
the system's password store: `pip install "issue-folder-sync[keyring]"`.
On Windows the token goes in Windows Credential Manager, with no extra package.

## Set up

1. Make a **fine-grained personal access token** on GitHub (Settings → Developer settings → Fine-grained tokens),
   for this one repository, with **Issues: Read and write** and **Contents: Read and write**. Contents is used only
   for attachments; without it, set `attachments` to `false` (see Settings).
2. Run:

```
issue-folder-sync init --repo owner/name --root "~/Issues"
issue-folder-sync sync              # the first run only copies down; nothing is sent to GitHub
issue-folder-sync sync              # from now on, both ways
issue-folder-sync schedule --minutes 5    # Windows: a scheduled task. Elsewhere: prints a crontab line
```

`init` asks you to paste the token. To read it from a password manager instead, give a command that prints it:
`--token-command "op read op://Private/GitHub/token"`. Add `--keep-command` to run that command every time instead
of saving the token at all.

For several repositories, give each its own profile: `issue-folder-sync --profile work init ...`, then
`issue-folder-sync --profile work sync`.

## Commands

| Command | What it does |
|---|---|
| `init` | set the repository, the folder and the token; checks the token works before keeping it |
| `sync` | run now, both ways |
| `sync --dry-run` | say what it would send to GitHub; change nothing anywhere |
| `sync --pull-only` | copy GitHub down only; send nothing |
| `sync --allow-many` | lift the changes-per-run safety limit, for one run |
| `status` | the folder, whether a token was found, the last run, anything that needs you |
| `config` / `config --set KEY=VALUE` / `config --unset KEY` | show or change settings |
| `schedule` / `unschedule` | run by itself every few minutes |

## What goes which way

| Change made in the folder | What happens on GitHub |
|---|---|
| Edit `issue.md` (the `# ` line is the title) | the issue's title and text are updated |
| Tick, untick or rename a line in `tasks.md` | the sub-issue is closed, reopened or renamed |
| Add a line `- [ ] what to do` to `tasks.md` | a new sub-issue |
| Drop a file into `attachments/` | the file is put in the repository under `attachments/` and linked from a comment |
| New folder with an `issue.md` (or a loose `.md` file) in `Open/` or `Milestones/<m>/` | a new issue, in that milestone (made if new) |
| Move a folder to `Closed/`, another milestone or another group | the issue is closed, reopened, moved or relabelled to match |

Everything on GitHub (text, titles, labels, milestones, sub-issues, attachments, closing) comes down every run.

## Safety

- **Never deletes** an issue, a comment or a file on GitHub. A folder deleted locally is made again; a deleted
  attachment stays deleted locally and is listed in `_SYNC-STATUS.md`.
- **Both sides changed the same issue:** GitHub's version goes in `issue.md`, and the local version is kept beside it
  as `issue.conflict-<time>.md`. Nothing is lost; merge by hand and delete the conflict file.
- **Quiet time:** a file changed in the last 45 seconds is left for the next run, because it may still be being typed
  or synced.
- **At most 25 changes are sent per run.** More than that is treated as a mistake and waits until you run
  `sync --allow-many`.
- **The first run in a folder only copies down.**
- **A new folder whose title matches an open issue is not sent**, because it is probably a copy.
- Issues with a label in `skip_labels` are never copied down. Use this for anything that may hold a password.

## Settings

Kept per profile in `~/.issue-folder-sync/<profile>/config.json`, with the state, the log and the last report.
Change them with `issue-folder-sync config --set KEY=VALUE` (the value is JSON or plain text).

| Setting | Default | Meaning |
|---|---|---|
| `repo`, `root` | — | the repository (`owner/name`) and the folder |
| `ignore_issues` | `[]` | issue numbers never synced, such as a pinned control issue |
| `skip_labels` | `[]` | issues with any of these labels are never copied down |
| `group_labels` | `{}` | a top level of folders by label, such as `{"work": "Work", "home": "Home"}`. Moving a folder between them changes the label |
| `ungrouped_folder` | `Unsorted` | where issues with none of the group labels go |
| `open_folder`, `closed_folder`, `milestones_folder` | `Open`, `Closed`, `Milestones` | folder names |
| `issue_file`, `tasks_file`, `meta_file`, `attachments_folder` | `issue.md`, `tasks.md`, `.issue.json`, `attachments` | file names |
| `status_file`, `status_title` | `_SYNC-STATUS.md`, `Sync status` | the status page |
| `new_issue_labels` | `[]` | labels put on issues made from new local folders |
| `attachments` | `true` | sync attachments (needs Contents permission) |
| `attachments_repo_dir` | `attachments` | where uploaded files go in the repository |
| `attachment_marker` | `issue-folder-sync-attachment` | the hidden marker on attachment comments |
| `max_writes_per_run`, `quiet_seconds`, `max_attachment_mb` | `25`, `45`, `25` | the safety limits |
| `token_command` | none | a command that prints the token, run when no token is saved |
| `preset` | none | ready-made settings; see below |

### Presets

`notes`: the Kaiju Notes app, which keeps notes as issues. It uses `Work`/`Personal`/`Unsorted` groups,
`Notes`/`Archived` folders and `note.md`, and it skips issue #1 and the `secret-check` label. It also hides the
parts of a note that the app maintains itself.
Use it with `init --preset notes`.

## Use it from Python

```python
from pathlib import Path
from issue_folder_sync import GitHub, Sync, settings

cfg = settings.build({"repo": "owner/name", "root": "/path/to/folder", "skip_labels": ["private"]})
gh = GitHub(cfg["repo"], token, dry=False)
report = Sync(cfg, gh, home=Path("~/.my-sync").expanduser()).run()
print(report["pushed"], report["pulled"], report["needs_you"])
```

`home` holds this computer's state for that folder; use one per folder.

## Develop

```
pip install -e ".[test]"
pytest
```

The tests run against a pretend GitHub kept in memory, with no network and no token.
