[sysadmin-bin] Split setting default branch and deleting master branch



commit 45854e715ac2e7658f329ffcba2839f3ed245fff
Author: Bartłomiej Piotrowski <bpiotrowski gnome org>
Date:   Tue Oct 15 13:55:42 2019 +0200

    Split setting default branch and deleting master branch

 git/post-receive-mirror-github | 39 ++++++++++++++++++++++++++++-----------
 1 file changed, 28 insertions(+), 11 deletions(-)
---
diff --git a/git/post-receive-mirror-github b/git/post-receive-mirror-github
index 54be0f3..9e48085 100755
--- a/git/post-receive-mirror-github
+++ b/git/post-receive-mirror-github
@@ -188,13 +188,21 @@ def main():
 
     if repo_exists:
         description, homepage, default_branch, branches = gh.fetch_github_repo(github_name)
+        refs = ["refs/heads/{}".format(branch) for branch in branches]
+
+        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"])
     else:
         gh.create_github_repo(settings["name"], settings["description"], settings["homepage"])
+        default_branch = ""
+        refs = ""
 
     for organization in [gh.organization] + ADDITIONAL_ORGANIZATIONS.get(repo_name, []):
         try:
-            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))
+            command = 'git push --force git github com:{}/{} {}: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)
@@ -205,18 +213,27 @@ 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 repo_exists:
-        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 default_branch != 'mainline':
+        gh.update_github_repo(github_name, 'default_branch', 'mainline')
     else:
         gh.update_github_repo(github_name, 'default_branch', settings["default_branch"])
 
+    # Drop master branch in separate loop as GitHub API disallows to drop the
+    # default branch and also expects new default branch to exists
+    for organization in [gh.organization] + ADDITIONAL_ORGANIZATIONS.get(repo_name, []):
+        try:
+            command = 'git push --force git github com:{}/{} :master'.format(organization, github_name)
+            out = tempfile.NamedTemporaryFile(prefix="github",suffix="std")
+            err = tempfile.NamedTemporaryFile(prefix="github",suffix="err")
+            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 repo %s/%s\nSTDOUT:\n%s\nSTDERR\n%s" % (organization, 
repo_name, out.read(), err.read()))
+
+
 
 if __name__ == "__main__":
     try:


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