From 3aad8411ba8bf359b29c20e4d31e2ab3281c9958 Mon Sep 17 00:00:00 2001 From: Thierry Carrez Date: Thu, 31 Jan 2019 14:40:41 +0100 Subject: [PATCH] Add Zuul job for testing GitHub WIP status Add a kata-github-wip Zuul job that can be used to test for presence of specific WIP PR labels or specific WIP keywords in the PR title and block the PR accordingly. Suppress the placeholder job that was added just to pass initial tests. Change-Id: I4f50c77e19f1e76bf65ebfcb9ea587e0556dda03 --- playbooks/github-wip/run.yaml | 4 ++ playbooks/placeholder/run.yaml | 5 -- roles/check-github-pr-is-wip/README.rst | 17 +++++ .../files/github_wip.py | 68 +++++++++++++++++++ roles/check-github-pr-is-wip/tasks/main.yaml | 2 + roles/placeholder/tasks/main.yaml | 2 - zuul.d/jobs.yaml | 12 ++++ 7 files changed, 103 insertions(+), 7 deletions(-) create mode 100644 playbooks/github-wip/run.yaml delete mode 100644 playbooks/placeholder/run.yaml create mode 100644 roles/check-github-pr-is-wip/README.rst create mode 100755 roles/check-github-pr-is-wip/files/github_wip.py create mode 100644 roles/check-github-pr-is-wip/tasks/main.yaml delete mode 100644 roles/placeholder/tasks/main.yaml diff --git a/playbooks/github-wip/run.yaml b/playbooks/github-wip/run.yaml new file mode 100644 index 0000000..81d9056 --- /dev/null +++ b/playbooks/github-wip/run.yaml @@ -0,0 +1,4 @@ +- hosts: localhost + roles: + - role: check-github-pr-is-wip + github_pr: "{{ zuul.change_url }}" diff --git a/playbooks/placeholder/run.yaml b/playbooks/placeholder/run.yaml deleted file mode 100644 index e756c71..0000000 --- a/playbooks/placeholder/run.yaml +++ /dev/null @@ -1,5 +0,0 @@ -- hosts: localhost - tasks: - - name: Call the placeholder role - include_role: - name: placeholder diff --git a/roles/check-github-pr-is-wip/README.rst b/roles/check-github-pr-is-wip/README.rst new file mode 100644 index 0000000..5d4392a --- /dev/null +++ b/roles/check-github-pr-is-wip/README.rst @@ -0,0 +1,17 @@ +Fail if any WIP labels are attached to a given GitHub PR, or if +any WIP keywords are included in a given GitHub PR title. + +**Role Variables** + +.. zuul:rolevar:: github_pr + + URL for the GitHub PR to check. Should be something like: + https://github.com/kata-containers/runtime/pull/1112 + +.. zuul:rolevar:: wip_labels + + Space-separated label names to consider as WIP labels. + +.. zuul:rolevar:: wip_keywords + + Space-separated keywords to consider as WIP keywords in PR titles. diff --git a/roles/check-github-pr-is-wip/files/github_wip.py b/roles/check-github-pr-is-wip/files/github_wip.py new file mode 100755 index 0000000..053ea16 --- /dev/null +++ b/roles/check-github-pr-is-wip/files/github_wip.py @@ -0,0 +1,68 @@ +#!/usr/bin/python3 +# +# Job testing a GitHub PR for presence of specific WIP labels +# or specific WIP keywords in PR title, in order to block merging. +# +# Copyright 2018 Thierry Carrez +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import argparse +import json +import re +import sys +import urllib.request + + +def get_github_PR(change_url): + match = re.search('https://github.com/(.+)/pull/(\d+)', change_url) + repository = match.group(1) + prnumber = match.group(2) + + url = 'https://api.github.com/repos/%s/pulls/%s' % (repository, prnumber) + + with urllib.request.urlopen(url) as link: + data = json.loads(link.read().decode()) + return data + + +def is_wip(change, wip_keywords, wip_labels): + for wip_keyword in wip_keywords: + if wip_keyword in change['title']: + print('%s is mentioned in PR title' % wip_keyword) + return True + + for label in change['labels']: + for wip_label in wip_labels: + if label['name'] == wip_label: + print('PR is labeled %s' % wip_label) + return True + + print('No blocking marker found, this PR is OK to merge') + return False + + +def main(): + parser = argparse.ArgumentParser(description='Is the PR blocked ?') + parser.add_argument('change_url', help='GitHub PR URL') + parser.add_argument('keywords', help='Key words in PR title to block on') + parser.add_argument('labels', help='Names of labels to block on') + args = parser.parse_args() + + change = get_github_PR(args.change_url) + sys.exit(is_wip(change, args.keywords.split(), args.labels.split())) + + +if __name__ == "__main__": + main() diff --git a/roles/check-github-pr-is-wip/tasks/main.yaml b/roles/check-github-pr-is-wip/tasks/main.yaml new file mode 100644 index 0000000..907433c --- /dev/null +++ b/roles/check-github-pr-is-wip/tasks/main.yaml @@ -0,0 +1,2 @@ +- name: Check for WIP labels or WIP title keywords on a GitHub PR + script: github_wip.py {{ github_pr }} '{{ wip_keywords }}' '{{ wip_labels }}' diff --git a/roles/placeholder/tasks/main.yaml b/roles/placeholder/tasks/main.yaml deleted file mode 100644 index 7089a18..0000000 --- a/roles/placeholder/tasks/main.yaml +++ /dev/null @@ -1,2 +0,0 @@ -- name: Placeholder task - debug: msg=placeholder diff --git a/zuul.d/jobs.yaml b/zuul.d/jobs.yaml index e8743a8..5c8dac5 100644 --- a/zuul.d/jobs.yaml +++ b/zuul.d/jobs.yaml @@ -14,3 +14,15 @@ tox_envlist: linters tox_environment: ANSIBLE_ROLES_PATH: ~/src/git.openstack.org/openstack-infra/zuul-jobs/roles:~/src/git.openstack.org/kata-containers/zuul-config/roles + +- job: + name: kata-github-wip + description: | + This job calls the github-pr playbook with a Kata-curated set of + WIP labels and WIP title keywords + run: playbooks/github-wip/run.yaml + nodeset: + nodes: [] + vars: + wip_labels: 'do-not-merge wip rfc' + wip_keywords: 'WIP RFC'