<reponame>AlexanderDzhoganov/KerbalStuff
from KerbalStuff.config import _cfg
from github import Github
from flask import url_for
import subprocess
import json
import os
import re

def send_to_ckan(mod):
    if not _cfg("netkan_repo_path"):
        return
    json_blob = {
        'spec_version': 1,
        'identifier': re.sub(r'\W+', '', mod.name),
        '$kref': '#/ckan/kerbalstuff/' + str(mod.id),
        'x_netkan_license_ok': True
    }
    wd = _cfg("netkan_repo_path")
    if os.path.exists(os.path.join(wd, 'NetKAN', json_blob['identifier'] + '.netkan')):
        num = 1
        while os.path.exists(os.path.join(wd, 'NetKAN', json_blob['identifier'] + str(num) + '.netkan')):
            num += 1
        json_blob['identifier'] = json_blob['identifier'] + str(num)
    path = os.path.join(wd, 'NetKAN', json_blob['identifier'] + '.netkan')
    with open(path, 'w') as f:
        f.write(json.dumps(json_blob, indent=4))
    subprocess.call(['git', 'fetch', 'upstream'], cwd=wd)
    subprocess.call(['git', 'checkout', '-b', 'add-' + json_blob['identifier'], 'upstream/master'], cwd=wd)
    subprocess.call(['git', 'add', '-A'], cwd=wd)
    subprocess.call(['git', 'commit', '-m', 'Add {0} from Kerbal Stuff\n\nThis is an automated commit on behalf of {1}'\
            .format(mod.name, mod.user.username), '--author={0} <{1}>'.format(mod.user.username, mod.user.email)], cwd=wd)
    subprocess.call(['git', 'push', '-u', 'origin', 'add-' + json_blob['identifier']], cwd=wd)
    g = Github(_cfg('github_user'), _cfg('github_pass'))
    r = g.get_repo("KSP-CKAN/NetKAN")
    r.create_pull(title="Add {0} from Kerbal Stuff".format(mod.name), base=r.default_branch, head="KerbalStuffBot:add-" + json_blob['identifier'], body=\
"""\
This pull request was automatically generated by Kerbal Stuff on behalf of {0}, to add [{1}]({4}{2}) to CKAN.

Please direct questions about this pull request to [{0}]({4}{3}).
""".format(mod.user.username, mod.name,\
    url_for('mods.mod', mod_name=mod.name, id=mod.id),\
    url_for("profile.view_profile", username=mod.user.username),\
    _cfg("protocol") + "://" + _cfg("domain")))

from KerbalStuff.config import _cfg
from github import Github
from flask import url_for
import subprocess
import json
import os
import re

def send_to_ckan(mod):
    if not _cfg("netkan_repo_path"):
        return
    json_blob = {
        'spec_version': 1,
        'identifier': re.sub(r'\W+', '', mod.name),
        '$kref': '#/ckan/kerbalstuff/' + str(mod.id),
        'x_netkan_license_ok': True
    }
    wd = _cfg("netkan_repo_path")
    if os.path.exists(os.path.join(wd, 'NetKAN', json_blob['identifier'] + '.netkan')):
        num = 1
        while os.path.exists(os.path.join(wd, 'NetKAN', json_blob['identifier'] + str(num) + '.netkan')):
            num += 1
        json_blob['identifier'] = json_blob['identifier'] + str(num)
    path = os.path.join(wd, 'NetKAN', json_blob['identifier'] + '.netkan')
    with open(path, 'w') as f:
        f.write(json.dumps(json_blob, indent=4))
    subprocess.call(['git', 'fetch', 'upstream'], cwd=wd)
    subprocess.call(['git', 'checkout', '-b', 'add-' + json_blob['identifier'], 'upstream/master'], cwd=wd)
    subprocess.call(['git', 'add', '-A'], cwd=wd)
    subprocess.call(['git', 'commit', '-m', 'Add {0} from Kerbal Stuff\n\nThis is an automated commit on behalf of {1}'\
            .format(mod.name, mod.user.username), '--author={0} <{1}>'.format(mod.user.username, mod.user.email)], cwd=wd)
    subprocess.call(['git', 'push', '-u', 'origin', 'add-' + json_blob['identifier']], cwd=wd)
    g = Github(_cfg('github_user'), _cfg('github_pass'))
    r = g.get_repo("KSP-CKAN/NetKAN")
    r.create_pull(title="Add {0} from Kerbal Stuff".format(mod.name), base=r.default_branch, head="KerbalStuffBot:add-" + json_blob['identifier'], body=\
"""\
This pull request was automatically generated by Kerbal Stuff on behalf of {0}, to add [{1}]({4}{2}) to CKAN.

Please direct questions about this pull request to [{0}]({4}{3}).
""".format(mod.user.username, mod.name,\
    url_for('mods.mod', mod_name=mod.name, id=mod.id),\
    url_for("profile.view_profile", username=mod.user.username),\
    _cfg("protocol") + "://" + _cfg("domain")))
\ This pull request was automatically generated by Kerbal Stuff on behalf of {0}, to add [{1}]({4}{2}) to CKAN.  Please direct questions about this pull request to [{0}]({4}{3}).