[meld] Update maint script to create Gitlab release using the API



commit a876aafceab8bb996b6ca00f7dd81c1bf41979b1
Author: Kai Willadsen <kai willadsen gmail com>
Date:   Mon Mar 26 09:04:26 2018 +1000

    Update maint script to create Gitlab release using the API

 maint | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 56 insertions(+)
---
diff --git a/maint b/maint
index 7776f0c9..7d7d586d 100755
--- a/maint
+++ b/maint
@@ -2,11 +2,15 @@
 
 import collections
 import datetime
+import json
 import os
 import re
 import subprocess
+import urllib.parse
 
 import click
+import keyring
+import requests
 from jinja2 import Environment
 
 import meld.conf
@@ -16,6 +20,8 @@ HELP_DIR = "help"
 RELEASE_BRANCH_RE = r'%s-\d+-\d+' % meld.conf.__package__
 VERSION_RE = r'__version__\s*=\s*"(?P<version>.*)"'
 UPLOAD_SERVER = 'master.gnome.org'
+GITLAB_API_BASE = 'https://gitlab.gnome.org/api/v4'
+GITLAB_PROJECT_ID = 'GNOME/meld'
 
 NEWS_TEMPLATE = """
 {{ [date, app, version]|join(' ') }}
@@ -289,6 +295,44 @@ def push():
     call_with_output(cmd, echo_stdout=True)
 
 
+def gitlab_release_tag(tag):
+    auth_token = keyring.get_password('gitlab-api', GITLAB_PROJECT_ID)
+    if not auth_token:
+        raise click.ClickException(
+            "No password in keychain for {id}\n\n"
+            "Set password using the Python `keyring` package. "
+            "From the command line:\n"
+            "  $ keyring set gitlab-api {id}\n"
+            "and enter your secret token from the Gitlab UI.\n".format(
+                id=GITLAB_PROJECT_ID)
+        )
+
+    cmd = ['git', 'tag', '-l', "--format='%(contents)'", tag]
+    description = subprocess.check_output(cmd).decode('utf-8')
+
+    endpoint = '{base}/projects/{id}/repository/tags/{tag_name}/release'
+    release_url = endpoint.format(
+        base=GITLAB_API_BASE,
+        id=urllib.parse.quote_plus(GITLAB_PROJECT_ID),
+        tag_name=tag,
+    )
+    release_data = {
+        "tag_name": tag,
+        "description": description
+    }
+
+    # TODO: Should probably sanity-check that it's not already a
+    # release tag.
+
+    requests.post(
+        release_url,
+        data=json.dumps(release_data),
+        headers={
+            'Private-Token': auth_token,
+        },
+    )
+
+
 @click.group()
 def cli():
     pass
@@ -379,6 +423,17 @@ def tag():
     call_with_output(cmd, echo_stdout=True)
 
 
+@cli.command()
+def release_tag():
+    last_release = get_last_release_tag()
+    confirm = click.confirm(
+        'Annotate {} as a Gitlab release?'.format(last_release), default=True)
+    if not confirm:
+        return
+
+    gitlab_release_tag(last_release)
+
+
 @cli.command()
 @click.argument('path', type=click.Path(exists=True))
 def upload(path):
@@ -428,6 +483,7 @@ def make_release(ctx):
     push()
     archive_path = ctx.forward(dist)
     ctx.forward(tag)
+    ctx.forward(release_tag)
     ctx.forward(upload, path=archive_path)
     file_prefix = '%s-%s' % (meld.conf.__package__, meld.conf.__version__)
     ctx.forward(email, filename=file_prefix + '-email')


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