From b59a2de873b370fea263a96a013f39bef83b1114 Mon Sep 17 00:00:00 2001
From: Antoine Lambert <anlambert@softwareheritage.org>
Date: Thu, 22 Dec 2022 17:13:39 +0100
Subject: [PATCH] jobs/swh-docs: Add job triggerable by gitlab

Documentation will be fully built when pushing changes to the swh-docs
GitLab repository or when submitting merge requests.
---
 jobs/defaults.yaml                            |  5 +++
 jobs/swh-docs.yaml                            | 22 ++++++++++-
 jobs/swh-packages.yaml                        |  2 -
 jobs/templates/incoming-tag.yaml              |  1 -
 .../swh-docs-pipeline-diff.groovy.j2          | 39 +++++++++++++++++--
 jobs/templates/swh-pipeline-diff.yaml         |  1 -
 jobs/templates/swh-pipeline.yaml              |  1 -
 jobs/templates/swh-pypi.yaml                  |  1 -
 jobs/tools/jenkins-jobs-builder.yaml          |  2 -
 jobs/tools/setup-gitlab-webhooks.groovy.j2    |  2 +
 jobs/tools/setup-gitlab-webhooks.yaml         |  2 +-
 11 files changed, 65 insertions(+), 13 deletions(-)
 create mode 100644 jobs/defaults.yaml

diff --git a/jobs/defaults.yaml b/jobs/defaults.yaml
new file mode 100644
index 0000000..cb1e056
--- /dev/null
+++ b/jobs/defaults.yaml
@@ -0,0 +1,5 @@
+- defaults:
+    name: global
+    gitlab_url: https://gitlab-staging.swh.network
+    gitlab_connection_name: gitlab-staging
+    gitlab_project: false
diff --git a/jobs/swh-docs.yaml b/jobs/swh-docs.yaml
index 5ff6e02..af6ec77 100644
--- a/jobs/swh-docs.yaml
+++ b/jobs/swh-docs.yaml
@@ -6,6 +6,7 @@
       - "{name}/publish"
       - "{name}/dev"
       - "{name}/build-on-diff"
+      - "{name}/gitlab-builds"
 
 - job-template:
     name: "{name}/publish"
@@ -122,7 +123,7 @@
           keep-all: false
           includes: "**/*"
 
-- job-template:
+- job-template: &doc_build_on_diff
     name: "{name}/build-on-diff"
     display_name: Phab. diff
     project-type: pipeline
@@ -154,3 +155,22 @@
           description: URI of the staging repository
 
     dsl: !include-jinja2: templates/swh-docs-pipeline-diff.groovy.j2
+
+- job-template:
+    name: "{name}/gitlab-builds"
+    display_name: GitLab builds
+    gitlab_project: true
+    auth-token:
+    parameters:
+    properties:
+      - gitlab:
+          connection: "{gitlab_connection_name}"
+    triggers:
+      - gitlab:
+          trigger-push: true
+          trigger-merge-request: true
+          add-ci-message: true
+          cancel-pending-builds-on-update: true
+          # secret jenkins token is generated when executing tox
+          secret-token: !include-raw: jobs/templates/jenkins-token
+    <<: *doc_build_on_diff
diff --git a/jobs/swh-packages.yaml b/jobs/swh-packages.yaml
index 037a7df..5ee4775 100644
--- a/jobs/swh-packages.yaml
+++ b/jobs/swh-packages.yaml
@@ -1,7 +1,5 @@
 - job-group:
     name: "swh-jobs-{name}"
-    gitlab_url: https://gitlab-staging.swh.network
-    gitlab_connection_name: gitlab-staging
     gitlab_project_name: "swh/devel/{repo_name}"
     jobs:
       - "{name}"
diff --git a/jobs/templates/incoming-tag.yaml b/jobs/templates/incoming-tag.yaml
index 97dd831..ece06c3 100644
--- a/jobs/templates/incoming-tag.yaml
+++ b/jobs/templates/incoming-tag.yaml
@@ -5,7 +5,6 @@
     auth-token: "ph4br1cat0r"
     incoming_tag_auto_pypi_host: pypi.org
     sandbox: true
-    gitlab_project: false
     properties:
       - build-discarder:
           num-to-keep: 20
diff --git a/jobs/templates/swh-docs-pipeline-diff.groovy.j2 b/jobs/templates/swh-docs-pipeline-diff.groovy.j2
index b56de42..7dc72b1 100644
--- a/jobs/templates/swh-docs-pipeline-diff.groovy.j2
+++ b/jobs/templates/swh-docs-pipeline-diff.groovy.j2
@@ -3,25 +3,35 @@ pipeline {
     {%- include 'templates/includes/agent-docker.groovy.j2' -%}
   {% endfilter %}
 
+  {%- if not gitlab_project %}
   environment {
     PHAB_CONDUIT_URL = 'https://forge.softwareheritage.org/api/'
   }
+  {%- endif %}
 
   stages {
     stage('Checkout swh environment') {
       steps {
+        {%- if not gitlab_project %}
         {% filter indent(width=8) %}
           {%- include 'templates/includes/create-phabricator-artifacts.groovy.j2' -%}
         {% endfilter %}
+        {%- else %}
+          updateGitlabCommitStatus name: 'jenkins', state: 'running'
+        {%- endif %}
         checkout([
           $class: 'GitSCM',
           doGenerateSubmoduleConfigurations: false,
           extensions: [[$class: 'CloneOption', depth: 1, shallow: true]],
           gitTool: 'Default',
           submoduleCfg: [],
-          userRemoteConfigs: [
-            [url: 'https://forge.softwareheritage.org/source/swh-environment.git'],
-          ],
+          userRemoteConfigs: [[
+            {%- if not gitlab_project %}
+            url: 'https://forge.softwareheritage.org/source/swh-environment.git'
+            {%- else %}
+            url: '{{gitlab_url}}/swh/devel/swh-environment.git'
+            {%- endif %}
+          ]],
         ])
       }
     }
@@ -40,6 +50,7 @@ pipeline {
       }
     }
 
+    {%- if not gitlab_project %}
     stage('Apply phabricator diff') {
       steps {
         dir('swh-docs') {
@@ -49,11 +60,20 @@ pipeline {
         }
       }
     }
+    {%- endif %}
 
     stage('Build Software Heritage documentation') {
       steps {
         dir('swh-docs') {
           script {
+            {%- if gitlab_project %}
+            if ("${env.gitlabMergeRequestIid}" != "") {
+              sh "git fetch origin merge-requests/${env.gitlabMergeRequestIid}/head:merge_request && \
+                  git checkout merge_request"
+            } else {
+              sh "git checkout ${env.gitlabSourceBranch}"
+            }
+            {%- endif %}
             sh '''#!/bin/bash
             SPHINXOPTS='-W -q --keep-going -w errors.log' SPHINXOPTCOLOR='--no-color' tox -e sphinx-dev
             '''
@@ -64,7 +84,19 @@ pipeline {
   }
 
   post {
+    {%- if gitlab_project %}
+    failure {
+      updateGitlabCommitStatus name: 'jenkins', state: 'failed'
+    }
+    success {
+      updateGitlabCommitStatus name: 'jenkins', state: 'success'
+    }
+    aborted {
+      updateGitlabCommitStatus name: 'jenkins', state: 'canceled'
+    }
+    {%- endif %}
     always {
+      {%- if not gitlab_project %}
       step([$class: 'PhabricatorNotifier',
             commentOnSuccess: true,
             commentWithConsoleLinkOnFailure: true,
@@ -75,6 +107,7 @@ pipeline {
             lintFile: '.phabricator-lint',
             lintFileSize: '1000000',
       ])
+      {%- endif %}
 
       archiveArtifacts(
         allowEmptyArchive: true,
diff --git a/jobs/templates/swh-pipeline-diff.yaml b/jobs/templates/swh-pipeline-diff.yaml
index eb40b91..99fa975 100644
--- a/jobs/templates/swh-pipeline-diff.yaml
+++ b/jobs/templates/swh-pipeline-diff.yaml
@@ -10,7 +10,6 @@
     sandbox: true
     auth-token: "ph4br1cat0r"
     phabricator_diff: true
-    gitlab_project: false
     do_cypress: false
     timeout: 10
     max_concurrent: 0
diff --git a/jobs/templates/swh-pipeline.yaml b/jobs/templates/swh-pipeline.yaml
index f1ecdbe..0d8938e 100644
--- a/jobs/templates/swh-pipeline.yaml
+++ b/jobs/templates/swh-pipeline.yaml
@@ -16,7 +16,6 @@
           days-to-keep: 90
           artifact-num-to-keep: 20
     phabricator_diff: false
-    gitlab_project: false
     do_cypress: false
     timeout: 10
     max_concurrent: 0
diff --git a/jobs/templates/swh-pypi.yaml b/jobs/templates/swh-pypi.yaml
index 27123f3..0ca2431 100644
--- a/jobs/templates/swh-pypi.yaml
+++ b/jobs/templates/swh-pypi.yaml
@@ -5,7 +5,6 @@
     include_bdist: true
     project-type: pipeline
     sandbox: true
-    gitlab_project: false
     properties:
       - build-discarder:
           artifact-num-to-keep: 10
diff --git a/jobs/tools/jenkins-jobs-builder.yaml b/jobs/tools/jenkins-jobs-builder.yaml
index cf9e48d..c45ffed 100644
--- a/jobs/tools/jenkins-jobs-builder.yaml
+++ b/jobs/tools/jenkins-jobs-builder.yaml
@@ -3,8 +3,6 @@
     project-type: pipeline
     description: Update jenkins jobs and setup GitLab webhooks
     node: built-in
-    gitlab_url: https://gitlab-staging.swh.network
-    gitlab_connection_name: gitlab-staging
     gitlab_project_name: swh/infra/ci-cd/swh-jenkins-jobs
     properties:
       - build-discarder:
diff --git a/jobs/tools/setup-gitlab-webhooks.groovy.j2 b/jobs/tools/setup-gitlab-webhooks.groovy.j2
index 5976ce5..467f8cc 100644
--- a/jobs/tools/setup-gitlab-webhooks.groovy.j2
+++ b/jobs/tools/setup-gitlab-webhooks.groovy.j2
@@ -25,6 +25,8 @@ pipeline {
           setupGitlabWebhook("swh/infra/ci-cd/swh-jenkins-jobs",
                              "jenkins-tools/swh-jenkins-jobs-builder",
                              true, true, false)
+          setupGitlabWebhook("swh/devel/swh-docs", "DDOC/gitlab-builds",
+                             true, true, false)
 
           projects = readYaml(file: 'jobs/swh-packages.yaml')
           for (project in projects) {
diff --git a/jobs/tools/setup-gitlab-webhooks.yaml b/jobs/tools/setup-gitlab-webhooks.yaml
index f5eba6c..bd7f94b 100644
--- a/jobs/tools/setup-gitlab-webhooks.yaml
+++ b/jobs/tools/setup-gitlab-webhooks.yaml
@@ -9,7 +9,7 @@
       - string:
           name: gitlab_url
           description: URL of GitLab instance
-          default: https://gitlab-staging.swh.network
+          default: "{gitlab_url}"
       - string:
           name: jenkins_url
           description: URL of Jenkins instance
-- 
GitLab