[sysadmin-bin] Rename master branch to mainline on GitHub mirrors



commit 28cc389803eca2a88732e55f086c998c25dae284
Author: Bartłomiej Piotrowski <bpiotrowski gnome org>
Date:   Tue Oct 15 12:55:40 2019 +0200

    Rename master branch to mainline on GitHub mirrors
    
    According to robots.txt, only master branch is being indexed by bots.
    In attempt to sabotage SEO of GitHub mirrors, delete GitHub's master
    branch and push it under 'mainline' name.

 git/post-receive-mirror-github | 51 +++++++++++++++++++++++++-----------------
 1 file changed, 30 insertions(+), 21 deletions(-)
---
diff --git a/git/post-receive-mirror-github b/git/post-receive-mirror-github
index 01f0dd4..ffa79a6 100755
--- a/git/post-receive-mirror-github
+++ b/git/post-receive-mirror-github
@@ -104,16 +104,24 @@ class GitHub:
         raise Exception("There was an error attempting to update the repo %s in github:\n\nStatus: 
%d\nText:\n%s" % (name, rq.status_code, rq.text))
 
     def fetch_github_repo(self, name):
-        rq = requests.get('https://api.github.com/repos/'+self.organization+'/'+name, auth=(self.user, 
self.pw))
+        api_url = 'https://api.github.com/repos/{}/{}'.format(self.organization, name)
+
+        rq = requests.get(api_url, auth=(self.user, self.pw))
+        if rq.status_code != 200:
+            raise Exception("There was an error attempting to fetch the repo %s in github:\n\nStatus: 
%d\nText:\n%s" % (name, rq.status_code, rq.text))
 
         description = rq.json()['description']
         homepage = rq.json()['homepage']
         default_branch = rq.json()['default_branch']
 
-        if rq.status_code == 200:
-            return description, homepage, default_branch
+        rq = requests.get("{}/branches".format(api_url), auth=(self.user, self.pw))
+        branches = {branch['name'] for branch in rg.json()}
+
+        if rq.status_code != 200:
+            raise Exception("There was an error attempting to fetch branches of the repo %s in 
github:\n\nStatus: %d\nText:\n%s" % (name, rq.status_code, rq.text))
+
+        return description, homepage, default_branch, branches
 
-        raise Exception("There was an error attempting to fetch 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():
@@ -157,7 +165,6 @@ def get_repo_settings(repo_name):
     name = name.text if name != None else repo_name
     desc = desc.text if desc != None else name
 
-    homepage = homepage.get(resource) if homepage != None else 'https://gitlab.gnome.org/GNOME/%s' % 
repo_name
     homepage = homepage if homepage != None else 'https://gitlab.gnome.org/GNOME/%s' % repo_name
 
     return {
@@ -178,24 +185,10 @@ def main():
 
     settings = get_repo_settings(repo_name)
 
-    if not gh.check_if_repo_exists(repo_name):
-        gh.create_github_repo(settings["name"], settings["description"], settings["homepage"])
-        gh.update_github_repo(github_name, 'default_branch', settings["default_branch"])
-    else:
-        description, homepage, default_branch = gh.fetch_github_repo(github_name)
-
-        if description != settings["description"]:
-            gh.update_github_repo(github_name, 'description', settings["description"])
-
-        if homepage != settings["homepage"]:
-            gh.update_github_repo(github_name, 'homepage', settings["homepage"])
-
-        if default_branch != settings["default_branch"]:
-            gh.update_github_repo(github_name, 'default_branch', settings["default_branch"])
-    
     for organization in [gh.organization] + ADDITIONAL_ORGANIZATIONS.get(repo_name, []):
         try:
-            command = 'git push --mirror git github com:%s/%s' % (organization, github_name)
+            refs = ["refs/heads/{}".format(branch) for branch in branches]
+            command = 'git push --force git github com:{}/{} :master {}:mainline {}'.format(organization, 
github_name, default_branch, " ".join(refs))
             out = tempfile.NamedTemporaryFile(prefix="github",suffix="std")
             err = tempfile.NamedTemporaryFile(prefix="github",suffix="err")
             subprocess.check_call(shlex.split(command), stderr=err, stdout=out)
@@ -206,6 +199,22 @@ def main():
             err = open(err.name, "r")
             raise Exception("Error trying to push repo %s/%s\nSTDOUT:\n%s\nSTDERR\n%s" % (organization, 
repo_name, out.read(), err.read()))
 
+    if not gh.check_if_repo_exists(repo_name):
+        gh.create_github_repo(settings["name"], settings["description"], settings["homepage"])
+        gh.update_github_repo(github_name, 'default_branch', settings["default_branch"])
+    else:
+        description, homepage, default_branch, branches = gh.fetch_github_repo(github_name)
+
+        if description != settings["description"]:
+            gh.update_github_repo(github_name, 'description', settings["description"])
+
+        if homepage != settings["homepage"]:
+            gh.update_github_repo(github_name, 'homepage', settings["homepage"])
+
+        if default_branch != 'mainline':
+            gh.update_github_repo(github_name, 'default_branch', 'mainline')
+
+
 if __name__ == "__main__":
     try:
         main()


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]