summaryrefslogtreecommitdiff
path: root/sync-github-mirror
blob: e6178647f92169db456d67dd296c9269a888f081 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/python2
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
"""
Copyright (c) 2013 Alberto Ruiz <aruiz@gnome.org>
All rights reserved.

Hacked up to work for freedesktop.org by Ray Strode <rstrode@redhat.com>

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

  * Redistributions of source code must retain the above copyright notice,
    this list of conditions and the following disclaimer.
  * Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.
  * Neither the name of Pioneers of the Inevitable, Songbird, nor the names
    of its contributors may be used to endorse or promote products derived
    from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""
import codecs
import os
import os.path
import sys
import requests
import subprocess
import shlex
import ConfigParser
import xml.etree.ElementTree as et
import smtplib
from email.mime.text import MIMEText
import tempfile
import json

ORGANIZATION="freedesktop"
name_maps = {"gtk+":       "gtk",
             "libxml++":   "libxmlmm"}

class GitHub:
    def __init__ (self):
        config = ConfigParser.ConfigParser()
        try:
            config.read(os.path.expanduser('/etc/github-mirror/mirror.cfg'))
            self.user = config.get('Github', 'user')
            self.pw   = config.get('Github', 'password')
        except ConfigParser.NoSectionError:
            raise Exception ("~/.gitmirrorrc non existant or missing [Github] section with user and password keys")
        except ConfigParser.NoOptionError:
            raise Exception ("~/.gitmirrorrc misses user or/and password keys in the [Github] section")

    def check_if_repo_exists (self, name):
        rq = requests.get('https://api.github.com/repos/'+ORGANIZATION+'/'+name,
                          auth=(self.user, self.pw))
        if rq.status_code != 200:
            return False

        return True

    def create_github_repo (self, name):
        if self.check_if_repo_exists (name):
            return

        try:
            description_file = codecs.open('description', encoding='utf-8')
            description = description_file.read().replace('\n', ' ').rstrip()
            description_file.close()
        except:
            description = ''

        payload = json.dumps({
                             'name': self.normalize_name(name),
                             'description': description,
                             'has_wiki': False,
                             'has_issues': False
                             })
        rq = requests.post('https://api.github.com/orgs/'+ORGANIZATION+'/repos',
                           auth=(self.user, self.pw),
                           data=payload)
        if rq.status_code == 201:
            return

        raise Exception("There was an error attempting to create the repo %s in github:\n\nStatus: %d\nText:\n%s" % (name, rq.status_code, rq.text))

    def normalize_name (self, name):
        if name in name_maps.keys():
            return name_maps[name]

        if "+" in name:
            raise Exception("%s has a '+' character in it which is unsupported by Github.\nYou have to add it to the exception maps in the post-update hook." % name)

        return name

def get_repo_name ():
    repo_namespace = os.getcwd ().split("/")[-2]

    if repo_namespace == "git":
        repo_namespace = None

    repo_parts = "".join(os.getcwd ().split("/git/")[1:]).split('/')

    if repo_namespace and repo_parts[1].startswith(repo_namespace + '-'):
        repo_parts[1] = repo_parts[1].replace(repo_namespace + '-', '', 1)
    elif repo_namespace and repo_parts[1] == repo_namespace + ".git":
        repo_parts.remove(repo_parts[1])
    elif repo_namespace and repo_parts[1] == repo_namespace:
        repo_parts.remove(repo_parts[1])

    if repo_parts[-1] == ".git":
        repo_parts = repo_parts[:-1]

    repo_name = "-".join(repo_parts)

    if repo_name.endswith(".git"):
        repo_name = repo_name[0:-4]

    return repo_name

def main ():
    gh = GitHub ()
    repo_name = get_repo_name ()
    github_name = gh.normalize_name (repo_name)
    if not gh.check_if_repo_exists(repo_name) and os.path.exists("git-daemon-export-ok"):
        gh.create_github_repo (repo_name)
    try:
        out = tempfile.NamedTemporaryFile (prefix="github",suffix="std")
        err = tempfile.NamedTemporaryFile (prefix="github",suffix="err")
        command = 'git push --force --no-verify --prune git@github.com:%s/%s refs/heads/*:refs/heads/*' % (ORGANIZATION, github_name)
        subprocess.check_call(shlex.split(command), stderr=err, stdout=out)
        command = 'git push --force --no-verify --prune git@github.com:%s/%s refs/tags/*:refs/tags/*' % (ORGANIZATION, github_name)
        subprocess.check_call(shlex.split(command), stderr=err, stdout=out)
        command = 'git push --force --no-verify --prune git@github.com:%s/%s refs/notes/*:refs/notes/*' % (ORGANIZATION, github_name)
        subprocess.check_call(shlex.split(command), stderr=err, stdout=out)
        out.close()
        err.close()
    except subprocess.CalledProcessError:
        out = open(out.name, "r")
        err = open(err.name, "r")
        raise Exception("Error trying to push branch %s\nSTDOUT:\n%s\nSTDERR\n%s" % (repo_name, out.read(), err.read()))

if __name__ == "__main__":
    try:
        main ()
    except Exception as e:
        msg = MIMEText(str(e))
        msg['Subject'] = "[GITHUB HOOK] ERROR trying to push %s" %  os.getcwd ()
        msg['From']    = "noreply@freedesktop.org"
        msg['To']      = "halfline+fdo-github-mirror@freedesktop.org"
        msg['X-FDO-SERVICE'] = "github-mirror"
        server = smtplib.SMTP("localhost")
        server.sendmail (msg['From'], msg['To'], msg.as_string())
        server.quit ()