Skip to content
Cian Gallagher edited this page Nov 4, 2022 · 1 revision

Tips

Below is a curated list of tips and tricks to help improve your experience with gomerge.

Getting a list of all your repositories

This tip comes from @Bluscream.

First use this to cache all repositories you have access to locally

import github.GithubException
from github import Github
from time import sleep
from pathlib import Path
from json import dump, dumps
from os import makedirs

dir = Path("repos")

github_instance = Github(YOUR_GITHUB_TOKEN_HERE)
# f = file.open(mode="w+", encoding="utf-8")
for repo in github_instance.get_user().get_repos():
    orgpath = dir / f"{repo.owner.login}"
    if not orgpath.exists(): makedirs(orgpath)
    filepath = orgpath / f"{repo.name}.json"
    err = None
    try: raw_str = dumps(repo.raw_data, indent=4)
    except github.GithubException as e:
        err = e
        filepath = orgpath / f"{repo.name}.txt"
    if not filepath.exists():
        print("New file for repo", filepath)
        with open(filepath, 'w') as f:
            if not err: f.write(raw_str)
            else: f.write(str(err))
        # file.write_text(f"{repo.name};{repo.description};{repo.language};{repo.created_at};{repo.default_branch};{repo.size};{repo.pushed_at};{repo.forks_count};{}")
        sleep(.01)

then use this to generate config files and a shell script for gomerge:

import github.GithubException
from github import Github
from time import sleep
from pathlib import Path
import json
from os import makedirs
import yaml
root = Path("repos")

github_token = YOUR_GITHUB_TOKEN_HERE
# github_instance = Github()


def get_users(dir: Path):
    orgs = {}
    for org in dir.glob('*/'):
        if org.is_file(): continue
        orgs[org.name] = []
        # print("Found new org", org.name)
        for repo in org.glob("*.json"):
            # print("Found new repo", repo.name)
            orgs[org.name].append(repo.stem)
    print("Loaded", sum([len(l) for l in orgs]), "repos from", len(orgs), "orgs/users")
    return orgs
# def get_repos(Path: dir):
    # repos = []
    # dirs = 0
    # for path_object in dir.glob('**/*'):
        # if path_object.is_dir(): dirs += 1; continue
        # if not path_object.name.endswith(".json"): continue
        # with path_object.open() as f:
            # try:  repos.append(json.load(f))
            # except: print("Failed to parse json file",path_object)
    # print("Loaded", len(repos), "repos from", len(dirs), "orgs/users")
    # return repos


# files = get_repos(root)
orgs = get_users(root)

sh_merge = Path(root / "gomerge.sh").open("w", newline='\n')
sh_list = Path(root / "gomerge_list.sh").open("w", newline='\n')
sh_close = Path(root / "gomerge_close.sh").open("w", newline='\n')
for org, repos in orgs.items():
    cfg = Path(root / org / "config.yaml")
    yml = { "organization": org, "repositories": repos }
    with cfg.open("w") as cfgf: yaml.dump(yml, cfgf)
    sh_merge.write(f"gomerge list -t {github_token} -c {org}/config.yaml -a -s\n")
    sh_list.write(f"gomerge list -t {github_token} -c {org}/config.yaml -s\n")
    sh_close.write(f"gomerge list -t {github_token} -c {org}/config.yaml --close -s\n")

then just run repos/gomerge.sh on your machine

Clone this wiki locally