From 26a96aff83b8706ee355a5938ba49f61265a5a99 Mon Sep 17 00:00:00 2001
From: Mohan Boddu <mboddu@bhujji.com>
Date: Mar 18 2021 18:27:39 +0000
Subject: [PATCH 1/3] Default branch changes


Signed-off-by: Mohan Boddu <mboddu@bhujji.com>

---

diff --git a/fedscm_admin/__init__.py b/fedscm_admin/__init__.py
index e5757e4..363709e 100644
--- a/fedscm_admin/__init__.py
+++ b/fedscm_admin/__init__.py
@@ -34,6 +34,12 @@ STANDARD_BRANCH_SLAS = {
     'rawhide': {
         'rawhide': '2222-01-01'
     },
+    'stable': {
+        'rawhide': '2222-01-01'
+    },
+    'main': {
+        'rawhide': '2222-01-01'
+    },
     'epel8': {
         'stable_api': '2029-05-31',
         'security_fixes': '2029-05-31',
diff --git a/fedscm_admin/git.py b/fedscm_admin/git.py
index 4f73821..1732c94 100644
--- a/fedscm_admin/git.py
+++ b/fedscm_admin/git.py
@@ -23,6 +23,9 @@ import os
 import sys
 import click
 
+from .pagure import get_project_default_branch
+
+
 
 class GitException(Exception):
     """
@@ -117,20 +120,19 @@ class GitRepo(object):
             else:
                 raise
 
-    @property
-    def initialized_remotely(self):
+    def initialized_remotely(self, namespace, repo):
         """
         Determines if the git repo is initialized by using `git ls-remote`.
         """
         command = ['git', 'ls-remote', self.git_url]
         output = self._run_git_cmd(command, return_stdout=True)
-        return 'refs/remotes/origin/rawhide' in output or \
-            'refs/heads/rawhide' in output
+        default_branch = get_project_default_branch(namespace, repo)
+        return 'refs/remotes/origin/{0}'.format(default_branch) in output or \
+            'refs/heads/{0}'.format(default_branch) in output
 
-    @property
-    def first_commit(self):
+    def first_commit(self, namespace, repo):
         """
-        Get the first commit on the rawhide branch
+        Get the first commit on the default branch
         :return: a string of the first commit or None
         """
         self._assert_cloned()
@@ -138,7 +140,8 @@ class GitRepo(object):
         if not self.initialized:
             return None
 
-        command = ['git', 'rev-list', '--max-parents=0', 'rawhide']
+        default_branch = get_project_default_branch(namespace, repo)
+        command = ['git', 'rev-list', '--max-parents=0', default_branch]
         commits = self._run_git_cmd(command, return_stdout=True)
         if commits:
             # Return the last one on the screen which is the first commit
@@ -172,9 +175,10 @@ class GitRepo(object):
         checkout_cmd.append(branch_name)
         self._run_git_cmd(checkout_cmd)
 
-    def new_branch(self, branch_name):
+    def new_branch(self, branch_name, namespace, repo):
         """
         Creates a new branch and pushes it to the remote Git server
+        Currently unused but kept
         :param branch_name: a string of the branch to checkout
         :return: None or GitException
         """
@@ -185,8 +189,9 @@ class GitRepo(object):
         self.checkout_branch(branch_name, new=True)
         # Push the new branch to the git server
         self._run_git_cmd(push_cmd)
-        # Checkout rawhide once we're done pushing the new branch
-        self.checkout_branch('rawhide')
+        # Checkout default branch once we're done pushing the new branch
+        default_branch = get_project_default_branch(namespace, repo)
+        self.checkout_branch(default_branch)
 
     def add(self, file_name):
         """
diff --git a/fedscm_admin/pagure.py b/fedscm_admin/pagure.py
index 0118de1..bb31345 100644
--- a/fedscm_admin/pagure.py
+++ b/fedscm_admin/pagure.py
@@ -172,6 +172,21 @@ def get_project_git_url(namespace, repo, url_type='ssh', username=None):
                 username=username, rest=url[6:])
     return url
 
+def get_project_default_branch(namespace, repo):
+    """
+    Get the default branch of a project
+    :param namespace: a string representing the namespace of the project
+    :param repo: a string of the project/repo name
+    :return: a string of the default branch
+    """
+    pagure_url = get_config_item(CONFIG, 'pagure_dist_git_url').rstrip('/')
+    pagure_git_url_api_url = '{0}/api/0/{1}/{2}/git/branches'.format(
+        pagure_url, namespace, repo)
+    rv = requests_wrapper(
+        pagure_git_url_api_url, timeout=60, service_name='Pagure')
+    rv_json = get_request_json(rv, 'getting a project\'s default git branch', 'error')
+    default_branch = rv_json['default']
+    return default_branch
 
 def get_scm_requests_git_url(url_type='ssh', username=None):
     """
@@ -200,7 +215,7 @@ def get_scm_requests_git_url(url_type='ssh', username=None):
     return url
 
 
-def new_project(namespace, repo, description, upstreamurl,
+def new_project(namespace, repo, description, upstreamurl, default_branch,
                 initial_commit=True):
     """
     Create a new Pagure project
@@ -209,6 +224,7 @@ def new_project(namespace, repo, description, upstreamurl,
     :param repo: a string of the project/repo name
     :param description: a string of the description of the project
     :param upstreamurl: a string of the URL of the upstream project
+    :param default_branch: a string of the default branch of the project
     :param bool initial_commit: indicate whether to create an initial commit
     :return: None
     """
@@ -216,12 +232,13 @@ def new_project(namespace, repo, description, upstreamurl,
     pagure_new_project_url = \
         '{0}/api/0/new'.format(pagure_url.rstrip('/'))
     pagure_new_git_alias_url = \
-        '{0}/api/0/rpms/{1}/git/alias/new'.format(pagure_url.rstrip('/'), repo)
+        '{0}/api/0/{1}/{2}/git/alias/new'.format(pagure_url.rstrip('/'), namespace, repo)
 
     headers = get_pagure_auth_header('global')
     payload = {
         'namespace': namespace,
         'name': repo,
+        'default_branch': default_branch,
         'description': description or 'The {0} package\n'.format(repo),
         'url': upstreamurl or '',
         'wait': True
@@ -242,13 +259,14 @@ def new_project(namespace, repo, description, upstreamurl,
         'alias_from': 'main',
         'alias_to': 'rawhide',
     }
-    click.echo('- Creating main alias to rawhide branch')
-    rv = requests_wrapper(
-        pagure_new_git_alias_url, data=payload, headers=headers, timeout=90,
-        http_verb='post', service_name='Pagure')
-    # We won't actually use the returned output from this function call but
-    # it does error checking for us
-    get_request_json(rv, 'creating main alias to rawhide branch')
+    if namespace in ['rpms', 'container']:
+        click.echo('- Creating main alias to rawhide branch')
+        rv = requests_wrapper(
+            pagure_new_git_alias_url, data=payload, headers=headers, timeout=90,
+            http_verb='post', service_name='Pagure')
+        # We won't actually use the returned output from this function call but
+        # it does error checking for us
+        get_request_json(rv, 'creating main alias to rawhide branch')
     return None
 
 
diff --git a/fedscm_admin/utils.py b/fedscm_admin/utils.py
index ab5caf5..45c30f6 100644
--- a/fedscm_admin/utils.py
+++ b/fedscm_admin/utils.py
@@ -31,6 +31,7 @@ from . import MONITOR_CHOICES, STANDARD_BRANCH_SLAS, git, is_epel, pagure, pdc
 from .config import get_config_item
 from .exceptions import ValidationError
 from .request_utils import get_request_json, requests_wrapper
+from .pagure import get_project_default_branch
 
 
 def login_to_bugzilla_with_user_input():
@@ -379,16 +380,26 @@ def prompt_for_new_repo(issue_json, issue_body_json, force=False,
     description = issue_body_json.get('description', '').strip()
     upstreamurl = issue_body_json.get('upstreamurl', '').strip()
     component_type = pdc.component_type_to_singular(namespace)
-    rawhide_branch = None
-    if branch_name != 'rawhide':
-        click.echo('- Checking if rawhide branch already exists in PDC.')
-        rawhide_branch = pdc.get_branch(
-            repo, 'rawhide', component_type)
+    if namespace in ['rpms', 'container']:
+        default_branch = 'rawhide'
+    elif namespace in ['flatpaks']:
+        default_branch = 'stable'
+    elif namespace in ['modules']:
+        default_branch = branch_name
+    elif namespace in ['tests']:
+        default_branch = 'main'
+    else:
+        print("Needs failure")
+    existing_branch = None
+    if branch_name != default_branch:
+        click.echo('- Checking if {0} default branch already exists in PDC.'.format(default_branch))
+        existing_branch = pdc.get_branch(
+            repo, default_branch, component_type)
 
     click.echo('- Checking if {0} already exists in PDC.'.format(branch_name))
     branch = pdc.get_branch(repo, branch_name, component_type)
 
-    if rawhide_branch or branch:
+    if existing_branch or branch:
         prompt_to_close_bad_ticket(
             issue_json, 'The PDC branch already exists')
         return
@@ -439,15 +450,15 @@ def prompt_for_new_repo(issue_json, issue_body_json, force=False,
         # Pagure uses plural names for namespaces, but PDC does not use the
         # plural version for branch types
         branch_type = pdc.component_type_to_singular(namespace)
-        # If the branch requested isn't rawhide, still create a rawhide branch
+        # If the branch requested isn't default branch, still create a default branch
         # in PDC anyways.
         # Skip pdc magic for tests namespace
         if namespace != 'tests':
-            if branch_name != 'rawhide':
-                pdc.new_branch(repo, 'rawhide', branch_type)
-                for sla, eol in STANDARD_BRANCH_SLAS['rawhide'].items():
+            if branch_name != default_branch:
+                pdc.new_branch(repo, default_branch, branch_type)
+                for sla, eol in STANDARD_BRANCH_SLAS[default_branch].items():
                     pdc.new_sla_to_branch(
-                        sla, eol, repo, 'rawhide', branch_type)
+                        sla, eol, repo, default_branch, branch_type)
 
             pdc.new_branch(repo, branch_name, branch_type)
             for sla, eol in issue_body_json['sls'].items():
@@ -456,19 +467,19 @@ def prompt_for_new_repo(issue_json, issue_body_json, force=False,
 
         # Create the Pagure repo
         pagure.new_project(
-            namespace, repo, description, upstreamurl,
+            namespace, repo, description, upstreamurl, default_branch,
             initial_commit=initial_commit)
-        # If the branch requested isn't rawhide, create that branch in git. The
-        # rawhide branch is already created at this point.
-        if branch_name != 'rawhide':
-            new_git_branch(namespace, repo, branch_name, use_rawhide=True)
+        # If the branch requested isn't default branch, create that branch in git. The
+        # default branch is already created at this point.
+        if branch_name != default_branch:
+            new_git_branch(namespace, repo, branch_name, use_default_branch=True)
 
         pagure.set_monitoring_status(
             namespace, repo, issue_body_json['monitor'].strip())
         pagure.change_project_main_admin(
             namespace, repo, issue_owner)
 
-        if branch_name == 'rawhide':
+        if branch_name == default_branch:
             new_repo_comment = ('The Pagure repository was created at {0}'
                                 .format(dist_git_url))
         else:
@@ -797,26 +808,27 @@ def assert_git_repo_initialized_remotely(namespace, repo):
         namespace, repo, url_type='git',
         username=FAS_CLIENT.client.username)
     git_obj = git.GitRepo(git_url)
-    if not git_obj.initialized_remotely:
+    if not git_obj.initialized_remotely(namespace, repo):
         raise ValidationError('The git repository is not initialized. The git '
                               'branch can\'t be created.')
 
 
-def new_git_branch(namespace, repo, branch, use_rawhide=False):
+def new_git_branch(namespace, repo, branch, use_default_branch=False):
     """
     Create a new branch in git using Pagure. This does some sanity checking
     before sending off the API request.
     :param namespace: a string of the namespace of the project
     :param project: a string of the project name
     :param branch: a string of the branch to create
-    :param use_rawhide: a boolean that determines whether to use the rawhide branch
-    branch or the first commit of the rawhide branch as the starting point for
+    :param use_default_branch: a boolean that determines whether to use the default
+    branch or the first commit of the default branch as the starting point for
     the new branch
     :return: None or ValidationError
     """
-    if use_rawhide is True:
+    if use_default_branch is True:
+        default_branch = get_project_default_branch(namespace, repo)
         pagure.new_branch(
-            namespace, repo, branch, from_branch='rawhide')
+            namespace, repo, branch, from_branch=default_branch)
     else:
         # Even though the branches are created using pagure api which dont
         # require ssh, but the code supports adding package.cfg file.
@@ -830,7 +842,7 @@ def new_git_branch(namespace, repo, branch, use_rawhide=False):
             raise ValidationError('The git repository is not initialized. A '
                                   'git branch can\'t be created.')
         pagure.new_branch(
-            namespace, repo, branch, from_commit=git_obj.first_commit)
+            namespace, repo, branch, from_commit=git_obj.first_commit(namespace, repo))
 
 
 def ticket_requires_approval(issue_type, issue):

From 2a486b327d2c049ac226e77a30439f12e31ccde9 Mon Sep 17 00:00:00 2001
From: Mohan Boddu <mboddu@bhujji.com>
Date: Mar 18 2021 18:27:39 +0000
Subject: [PATCH 2/3] Warning message in repo reqs if the requested branch is not the default branch


Signed-off-by: Mohan Boddu <mboddu@bhujji.com>

---

diff --git a/fedscm_admin/utils.py b/fedscm_admin/utils.py
index 45c30f6..a9788dc 100644
--- a/fedscm_admin/utils.py
+++ b/fedscm_admin/utils.py
@@ -389,7 +389,9 @@ def prompt_for_new_repo(issue_json, issue_body_json, force=False,
     elif namespace in ['tests']:
         default_branch = 'main'
     else:
-        print("Needs failure")
+        msg = ('    WARNING:          The requester requested a non default '
+               'branch. Please verify it before proceeding.')
+        click.secho(msg, fg='yellow')
     existing_branch = None
     if branch_name != default_branch:
         click.echo('- Checking if {0} default branch already exists in PDC.'.format(default_branch))

From f586a79dc74862f1d03400ec859563be2757010c Mon Sep 17 00:00:00 2001
From: Mohan Boddu <mboddu@bhujji.com>
Date: Mar 18 2021 18:27:39 +0000
Subject: [PATCH 3/3] Close the ticket if the requested default branch is master


Signed-off-by: Mohan Boddu <mboddu@bhujji.com>

---

diff --git a/fedscm_admin/utils.py b/fedscm_admin/utils.py
index a9788dc..fc32dcd 100644
--- a/fedscm_admin/utils.py
+++ b/fedscm_admin/utils.py
@@ -380,6 +380,11 @@ def prompt_for_new_repo(issue_json, issue_body_json, force=False,
     description = issue_body_json.get('description', '').strip()
     upstreamurl = issue_body_json.get('upstreamurl', '').strip()
     component_type = pdc.component_type_to_singular(namespace)
+    # Close the ticket if the requested default branch is master
+    if branch_name == 'master':
+        prompt_to_close_bad_ticket(
+            issue_json, 'Branch `master` cannot be created, please request the right branch.')
+        return
     if namespace in ['rpms', 'container']:
         default_branch = 'rawhide'
     elif namespace in ['flatpaks']:

