12. | Artikel anlegen in GitLab

Für das heutige Türchen habe ich aufgeschrieben, wie ich in der Journalstruktur von GitLab neue Artikel (fast) automatisch anlege. Dabei hilft (natürlich!) wieder Python.

Das folgende Script tut zusammengefasst das Folgende:

  1. Download des Beispielartikels auf den eigenen Rechner durch Export in GitLab
  2. Import des Downloads als neuen Artikel in der angegeben Gruppe des Journal-Issues
  3. Einrichten, dass das HTML-Artefakt des Artikels auf einem Webserver deployt werden kann. Wir verwenden dies für unsere Tests zum Peer Review mit Hypothesis.
  4. Generieren einiger Tipps zum weiteren Arbeiten.

Da bisher nicht alle erforderlichen Schritte mit der Python GitLab-API abgebildet werden können, sind noch manuelle Arbeiten notwendig:

  1. Anlegen eines Application Tokens in GitLab, um Trigger anlegen zu können.
  2. Definieren des default branch
  3. Einschalten des shared runners
  4. Hochladen eines Logos
  5. Optional: Einrichten eines Cronjobs für regelmäßige Builds

Es folgt das Skript, das bei uns produktiv im Einsatz ist. Die Mühe hat sich gelohnt, da wir ca. fünfzig Artikel für das Journal kommunikation@gesellschaft angelegt haben. Dabei haben wir den Scholarly article immer mehr verfeinert, sodass er unsere Schablone geworden ist.

#!/usr/bin/env python3

import gitlab
import time

# Load the local configuration
gl = gitlab.Gitlab.from_config('tuhh')

# Ask for some code like year or number
code = input("Code of the article: ")
# Ask or a slug or author name
new_article_name = input("Name or slug of the article: ")

# Combine them both
new_article_name = f"{code}_{new_article_name}"

print("""

Please wait, this will take a minute or two...

""")

# parent group to create the new project in
this_issues_group_id = 1234
# Repo to export from
scholarly_article_id = 4226  
# can differ but needs to be unprotected in order to create the review app
new_branch_name = '01-iteration'
issue_name = "02-issue"

# For a preview of the files and peer review with Hypothesis
# let's deploy the artifacts to a webserver
journal_preview_path = "www.your-review-server.com"
# Protect access to the Apache
htpasswd = "bar:vp3qwXRTH3f"

api_root = "https://collaborating.tuhh.de/api/v4"
base_url = "https://collaborating.tuhh.de"

# Overwrite existing article when importing
overwrite = True

output = {}
output["id"] = 0

# Export the Scholarly Article repo
p = gl.projects.get(scholarly_article_id)
export = p.exports.create()

# Wait for the 'finished' status
export.refresh()
while export.export_status != 'finished':
    time.sleep(1)
    export.refresh()

# Download the result
with open('/tmp/export.tgz', 'wb') as f:
    export.download(streamed=True, action=f.write)

# Import the exported project
output = gl.projects.import_project(
    open('/tmp/export.tgz', 'rb'), new_article_name, namespace=this_issues_group_id, overwrite=overwrite)
# Get a ProjectImport object to track the import status
project_import = gl.projects.get(output['id'], lazy=True).imports.get()

while project_import.import_status != 'finished':
    time.sleep(1)
    project_import.refresh()

# Create a branch
p = gl.projects.get(output['id'])
branch = p.branches.create({'branch': new_branch_name,
                            'ref': 'master'})

# Make the new branch default
# This does not yet work with the Python GitLab API!
# TODO

# Create project runner variables
feedback_namespace = f'{output["id"]}-{new_article_name}'
feedback_namespace = f'{new_article_name}'

# All of the following are needed for the deployment
# of the review app
var = p.variables.create({'key': 'REVIEW_CMS_APPID', 'value': 'value1'})
var = p.variables.create({'key': 'REVIEW_CMS_DISPLAYURL',
                          'value': f'https://{journal_preview_path}/{feedback_namespace}/~{new_branch_name}/'})
var = p.variables.create(
    {'key': 'REVIEW_CMS_REPO', 'value': output['path_with_namespace']})
var = p.variables.create({'key': 'REVIEW_CMS_SITEURL',
                          'value': f'https://{journal_preview_path}/{feedback_namespace}/~{new_branch_name}/'})
var = p.variables.create(
    {'key': 'REVIEW_FEEDBACK_NAMESPACE', 'value': feedback_namespace})
var = p.variables.create(
    {'key': 'REVIEW_HTPASSWD', 'value': htpasswd})

# Create a trigger
trigger = p.triggers.create({'description': new_article_name})

# Enable shared runner
# Not yet available with the Python GitLab API
# TODO

# Give some useful hints
print("------------------------------------")
print("Done!")
print("")
print("Suggested TODOs:")
print("1. For the backend to work correctly on the branch")
print("register a new application and store the key")
print("in the Runner variable REVIEW_CMS_APPID")
print("Use the following URL for the field redirect URI:")
print("")
print(
    f'https://{journal_preview_path}{feedback_namespace}/~{new_branch_name}/admin/')
print("")
print("2. Make the branch 01-iteration the default branch.")
print("")
print("3. Activate a/the shared runner for the project")
print("")
print("4. Open the review app in the environments.")
print("")
print("5. Configure the project for more convenience")
print("")
print(f"""

You can now also

- Upload a logo for the project.
- Add a cron job for nightly builds
- Clone the article
- Enter the title in the backend and some other meta data if you like.
- Copy text over to the project.
- Copy images over to the project.
- Write an issue to inform others about the article.

""")

Avatar
Axel Dürkop
Teamleiter, Systemarchitekt und technische Umsetzung

Ähnliches