2 # SPDX-License-Identifier: Apache-2.0
3 ##############################################################################
4 # Copyright (c) 2018 The Linux Foundation and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
19 import configparser as ConfigParser
25 from requests.compat import quote
26 from requests.exceptions import RequestException
28 from pygerrit2.rest import GerritRestAPI
29 from pygerrit2.rest.auth import HTTPDigestAuthFromNetrc, HTTPBasicAuthFromNetrc
32 logging.basicConfig(level=logging.INFO)
35 def quote_branch(arguments):
37 Quote is used here to escape the '/' in branch name. By
38 default '/' is listed in 'safe' characters which aren't escaped.
39 quote is not used in the data of the PUT request, as quoting for
40 arguments is handled by the request library
42 new_args = arguments.copy()
43 new_args['branch'] = quote(new_args['branch'], '')
47 def create_branch(api, arguments):
49 Create a branch using the Gerrit REST API
51 logger = logging.getLogger(__file__)
56 "revision": "%(commit)s"
59 # First verify the commit exists, otherwise the branch will be
62 request = api.get("/projects/%(project)s/commits/%(commit)s" %
65 logger.debug("Commit exists: %(commit)s", arguments)
66 except RequestException as err:
67 if hasattr(err, 'response') and err.response.status_code in [404]:
68 logger.warn("Commit %(commit)s for %(project)s:%(branch)s does"
69 " not exist. Not creating branch.", arguments)
71 logger.error("Error: %s", str(err))
72 # Skip trying to create the branch
75 # Try to create the branch and let us know if it already exist.
77 request = api.put("/projects/%(project)s/branches/%(branch)s" %
78 quote_branch(arguments), branch_data)
79 logger.info("Branch %(branch)s for %(project)s successfully created",
81 except RequestException as err:
82 if hasattr(err, 'response') and err.response.status_code in [412, 409]:
83 logger.info("Branch %(branch)s already created for %(project)s",
86 logger.error("Error: %s", str(err))
90 """Given a yamlfile that follows the release syntax, create branches
91 in Gerrit listed under branches"""
93 config = ConfigParser.ConfigParser()
94 config.read(os.path.join(os.path.abspath(os.path.dirname(__file__)),
96 config.read([os.path.expanduser('~/releases.cfg'), 'releases.cfg'])
98 gerrit_url = config.get('gerrit', 'url')
100 parser = argparse.ArgumentParser()
101 parser.add_argument('--file', '-f',
102 type=argparse.FileType('r'),
104 parser.add_argument('--basicauth', '-b', action='store_true')
105 args = parser.parse_args()
107 GerritAuth = HTTPDigestAuthFromNetrc
109 GerritAuth = HTTPBasicAuthFromNetrc
112 auth = GerritAuth(url=gerrit_url)
113 except ValueError as err:
114 logging.error("%s for %s", err, gerrit_url)
116 restapi = GerritRestAPI(url=gerrit_url, auth=auth)
118 project = yaml.safe_load(args.file)
120 create_branches(restapi, project)
123 def create_branches(restapi, project):
124 """Create branches for a specific project defined in the release
128 for branch in project['branches']:
129 repo, ref = next(iter(branch['location'].items()))
132 'branch': branch['name'],
136 for branch in branches:
137 create_branch(restapi, branch)
140 if __name__ == "__main__":