From nobody Sun Feb 8 15:47:05 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1594638165; cv=none; d=zohomail.com; s=zohoarc; b=Z1yt74AQRTQzZCWA3Ex9G2stxH9EBHMi01WtEYbPD9W+HyrSA1PHCoN8qIIlFnUMaMlKBv5ZW90dzSnxm5dZCQm2oMRPw5p2XFJZ7bnbwdkUW24CCAeKSMHVNQeP8k9tvMu56mZANvrxB60qKNC5jF+tFGcYzy1tx1NlmTGE420= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594638165; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=RJwLpVq9Q1i8NQ8dg4KaI2orhagBONGJEIZj2Ad66Yo=; b=PQYqm6+eNpe1IA1u9+ZJdPeTbr9f9GCNFYizrvQJ9xPXR4SWT11ZaY77oEOWipaK2qHeOtKPASeGRdBzthZDFPjttohuDvRTh0CzYNMDAibv8M9ANzhmLCB3xGGCUuXQOiVRr6wVYys9dkn3fhCrwpIm4tSkpfTavIvvmhFu3E0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1594638165032418.00254266821526; Mon, 13 Jul 2020 04:02:45 -0700 (PDT) Received: from localhost ([::1]:57478 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1juwEN-0000qu-Gq for importer@patchew.org; Mon, 13 Jul 2020 07:02:43 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:32850) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1juw7o-0000Ln-JZ for qemu-devel@nongnu.org; Mon, 13 Jul 2020 06:55:56 -0400 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:44302) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1juw7m-0003yR-El for qemu-devel@nongnu.org; Mon, 13 Jul 2020 06:55:56 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-11-fDr091LWPZmZ8xiwi1O-bw-1; Mon, 13 Jul 2020 06:55:49 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 501061902EA1; Mon, 13 Jul 2020 10:55:48 +0000 (UTC) Received: from thuth.com (ovpn-112-125.ams2.redhat.com [10.36.112.125]) by smtp.corp.redhat.com (Postfix) with ESMTP id 32A5E27CCC; Mon, 13 Jul 2020 10:55:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594637753; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:content-type:content-type:in-reply-to:in-reply-to: references:references; bh=RJwLpVq9Q1i8NQ8dg4KaI2orhagBONGJEIZj2Ad66Yo=; b=P7tDexqkyUDs9xoRZLzsn73M92jVTUSaQ34h45gP4emeQ/oFRKDLqBKIlj3j5ooyVFC4EN p3/IR9qGEylod9OmTvIjb6D/L98bebT8uvw6uTmFmUtA7jLcsDMD8Vj05DCprtxlD5YCUs Ojq03VO5UKM/4SL3nbGmEXSUTTi23p0= X-MC-Unique: fDr091LWPZmZ8xiwi1O-bw-1 From: Thomas Huth To: peter.maydell@linaro.org, qemu-devel@nongnu.org Subject: [PULL 8/9] GitLab Gating CI: introduce pipeline-status contrib script Date: Mon, 13 Jul 2020 12:55:33 +0200 Message-Id: <20200713105534.10872-9-thuth@redhat.com> In-Reply-To: <20200713105534.10872-1-thuth@redhat.com> References: <20200713105534.10872-1-thuth@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=thuth@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=207.211.31.81; envelope-from=thuth@redhat.com; helo=us-smtp-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/07/13 02:19:41 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Cleber Rosa Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Cleber Rosa This script is intended to be used right after a push to a branch. By default, it will look for the pipeline associated with the commit that is the HEAD of the *local* staging branch. It can be used as a one time check, or with the `--wait` option to wait until the pipeline completes. If the pipeline is successful, then a merge of the staging branch into the master branch should be the next step. Signed-off-by: Cleber Rosa Message-Id: <20200709024657.2500558-2-crosa@redhat.com> [thuth: Added the changes suggested by Erik Skultety] Signed-off-by: Thomas Huth --- scripts/ci/gitlab-pipeline-status | 157 ++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100755 scripts/ci/gitlab-pipeline-status diff --git a/scripts/ci/gitlab-pipeline-status b/scripts/ci/gitlab-pipeline= -status new file mode 100755 index 0000000000..348a49b6a4 --- /dev/null +++ b/scripts/ci/gitlab-pipeline-status @@ -0,0 +1,157 @@ +#!/usr/bin/env python3 +# +# Copyright (c) 2019-2020 Red Hat, Inc. +# +# Author: +# Cleber Rosa +# +# This work is licensed under the terms of the GNU GPL, version 2 or +# later. See the COPYING file in the top-level directory. + +""" +Checks the GitLab pipeline status for a given commit ID +""" + +# pylint: disable=3DC0103 + +import argparse +import http.client +import json +import os +import subprocess +import time +import sys + + +def get_local_staging_branch_commit(): + """ + Returns the commit sha1 for the *local* branch named "staging" + """ + result =3D subprocess.run(['git', 'rev-parse', 'staging'], + stdin=3Dsubprocess.DEVNULL, + stdout=3Dsubprocess.PIPE, + stderr=3Dsubprocess.DEVNULL, + cwd=3Dos.path.dirname(__file__), + universal_newlines=3DTrue).stdout.strip() + if result =3D=3D 'staging': + raise ValueError("There's no local branch named 'staging'") + if len(result) !=3D 40: + raise ValueError("Branch staging HEAD doesn't look like a sha1") + return result + + +def get_pipeline_status(project_id, commit_sha1): + """ + Returns the JSON content of the pipeline status API response + """ + url =3D '/api/v4/projects/{}/pipelines?sha=3D{}'.format(project_id, + commit_sha1) + connection =3D http.client.HTTPSConnection('gitlab.com') + connection.request('GET', url=3Durl) + response =3D connection.getresponse() + if response.code !=3D http.HTTPStatus.OK: + raise ValueError("Failed to receive a successful response") + json_response =3D json.loads(response.read()) + + # As far as I can tell, there should be only one pipeline for the same + # project + commit. If this assumption is false, we can add further + # filters to the url, such as username, and order_by. + if not json_response: + raise ValueError("No pipeline found") + return json_response[0] + + +def wait_on_pipeline_success(timeout, interval, + project_id, commit_sha): + """ + Waits for the pipeline to finish within the given timeout + """ + start =3D time.time() + while True: + if time.time() >=3D (start + timeout): + print("Waiting on the pipeline timed out") + return False + + status =3D get_pipeline_status(project_id, commit_sha) + if status['status'] =3D=3D 'running': + time.sleep(interval) + print('running...') + continue + + if status['status'] =3D=3D 'success': + return True + + msg =3D "Pipeline failed, check: %s" % status['web_url'] + print(msg) + return False + + +def main(): + """ + Script entry point + """ + parser =3D argparse.ArgumentParser( + prog=3D'pipeline-status', + description=3D'check or wait on a pipeline status') + + parser.add_argument('-t', '--timeout', type=3Dint, default=3D7200, + help=3D('Amount of time (in seconds) to wait for t= he ' + 'pipeline to complete. Defaults to ' + '%(default)s')) + parser.add_argument('-i', '--interval', type=3Dint, default=3D60, + help=3D('Amount of time (in seconds) to wait betwe= en ' + 'checks of the pipeline status. Defaults ' + 'to %(default)s')) + parser.add_argument('-w', '--wait', action=3D'store_true', default=3DF= alse, + help=3D('Wether to wait, instead of checking only = once ' + 'the status of a pipeline')) + parser.add_argument('-p', '--project-id', type=3Dint, default=3D111676= 99, + help=3D('The GitLab project ID. Defaults to the pr= oject ' + 'for https://gitlab.com/qemu-project/qemu, t= hat ' + 'is, "%(default)s"')) + try: + default_commit =3D get_local_staging_branch_commit() + commit_required =3D False + except ValueError: + default_commit =3D '' + commit_required =3D True + parser.add_argument('-c', '--commit', required=3Dcommit_required, + default=3Ddefault_commit, + help=3D('Look for a pipeline associated with the g= iven ' + 'commit. If one is not explicitly given, th= e ' + 'commit associated with the local branch nam= ed ' + '"staging" is used. Default: %(default)s')) + parser.add_argument('--verbose', action=3D'store_true', default=3DFals= e, + help=3D('A minimal verbosity level that prints the= ' + 'overall result of the check/wait')) + + args =3D parser.parse_args() + + try: + if args.wait: + success =3D wait_on_pipeline_success( + args.timeout, + args.interval, + args.project_id, + args.commit) + else: + status =3D get_pipeline_status(args.project_id, + args.commit) + success =3D status['status'] =3D=3D 'success' + except Exception as error: # pylint: disable=3DW0703 + success =3D False + if args.verbose: + print("ERROR: %s" % error.args[0]) + + if success: + if args.verbose: + print('success') + sys.exit(0) + else: + if args.verbose: + print('failure') + sys.exit(1) + + +if __name__ =3D=3D '__main__': + main() --=20 2.18.1