diff --git a/gitlab-changelog.py b/gitlab-changelog.py index a37a9360fd4e4209fdac05c276aea599674ee920..fc713accc60294a59a3cb2f8f03ea0338a1869e9 100755 --- a/gitlab-changelog.py +++ b/gitlab-changelog.py @@ -23,6 +23,13 @@ Usage: gitlab-changelog.py -H https://gitlab.gnome.org/ -t GNOME/glib 2.58.2.. then put the output into NEWS/ChangeLog and make the release. +The -H and -t arguments can be omitted by putting the following in +`~/.config/gitlab-changelog.ini`: + [gitlab-changelog] + default-hostname = https://gitlab.gnome.org/ + [https://gitlab.gnome.org/] + token = + To generate an authentication token for the script, go to https://gitlab.gnome.org/profile/personal_access_tokens and generate a token with the `api` scope. @@ -78,7 +85,7 @@ def get_issues_and_merge_requests(commit, gl_project_name): # FIXME: Would be good if gitlab put metadata in merge commits, then we # could parse those instead, rather than the human-readable commit message. - # See https://gitlab.com/gitlab-org/gitlab-ce/issues/56635 + # See https://gitlab.com/gitlab-org/gitlab/-/issues/26266 lines = commit.message.split('\n') issues = set() @@ -120,13 +127,14 @@ def main(): 'gitlab-changelog.ini') config.read(config_path) + gl_default_hostname = None + gl_default_token = None + try: - gl_hostname = config['gitlab-changelog']['hostname'] - gl_token = config['gitlab-changelog']['token'] + gl_default_hostname = config['gitlab-changelog']['default-hostname'] + gl_default_token = config[gl_default_hostname]['token'] write_config = False except KeyError: - gl_hostname = None - gl_token = None write_config = True # Parse command line arguments. @@ -142,26 +150,40 @@ def main(): parser.add_argument('-C', dest='path', default='.', help='repository to use (default: current directory)') - parser.add_argument('-H', '--hostname', default=gl_hostname, - required=(gl_hostname is None), + parser.add_argument('-H', '--hostname', default=gl_default_hostname, + required=(gl_default_hostname is None), help='GitLab hostname (for example: ' '‘https://gitlab.gnome.org/’, default: ' 'load from {})'.format(config_path)) - parser.add_argument('-t', '--token', default=gl_token, - required=(gl_token is None), + parser.add_argument('-t', '--token', default=None, required=False, help='GitLab authentication token (default: ' 'load from {})'.format(config_path)) args = parser.parse_args() + # Try and look up the token for the given hostname, if not given on the + # command line. + try: + if not args.token: + args.token = config[args.hostname]['token'] + except KeyError: + parser.error('Configuration key {}.{} not found in {}'.format( + args.hostname, 'token', config_path)) + sys.exit(1) + # Authenticate with GitLab to check the config. gl = gitlab.Gitlab(args.hostname, private_token=args.token) gl_project_name = args.gl_project gl_project = gl.projects.get(gl_project_name) if write_config: - config['gitlab-changelog'] = { - 'hostname': args.hostname, + if 'gitlab-changelog' not in config.sections() or \ + 'default-hostname' not in config['gitlab-changelog']: + config['gitlab-changelog'] = { + 'default-hostname': args.hostname, + } + + config[args.hostname] = { 'token': args.token, }