|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# |
| 4 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 5 | +# contributor license agreements. See the NOTICE file distributed with |
| 6 | +# this work for additional information regarding copyright ownership. |
| 7 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 8 | +# (the "License"); you may not use this file except in compliance with |
| 9 | +# the License. You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | +# |
| 19 | + |
| 20 | +import os |
| 21 | +import subprocess |
| 22 | +import sys |
| 23 | +import traceback |
| 24 | + |
| 25 | +try: |
| 26 | + import jira.client |
| 27 | + |
| 28 | + JIRA_IMPORTED = True |
| 29 | +except ImportError: |
| 30 | + JIRA_IMPORTED = False |
| 31 | + |
| 32 | +# ASF JIRA access token |
| 33 | +JIRA_ACCESS_TOKEN = os.environ.get("JIRA_ACCESS_TOKEN") |
| 34 | +JIRA_API_BASE = "https://issues.apache.org/jira" |
| 35 | + |
| 36 | + |
| 37 | +def fail(msg): |
| 38 | + print(msg) |
| 39 | + sys.exit(-1) |
| 40 | + |
| 41 | + |
| 42 | +def run_cmd(cmd): |
| 43 | + print(cmd) |
| 44 | + if isinstance(cmd, list): |
| 45 | + return subprocess.check_output(cmd).decode("utf-8") |
| 46 | + else: |
| 47 | + return subprocess.check_output(cmd.split(" ")).decode("utf-8") |
| 48 | + |
| 49 | + |
| 50 | +def create_jira_issue(title): |
| 51 | + asf_jira = jira.client.JIRA( |
| 52 | + {"server": JIRA_API_BASE}, |
| 53 | + token_auth=JIRA_ACCESS_TOKEN |
| 54 | + ) |
| 55 | + |
| 56 | + issue_dict = { |
| 57 | + 'project': {'key': 'ORC'}, |
| 58 | + 'summary': title, |
| 59 | + 'description': '', |
| 60 | + 'issuetype': {'name': 'Improvement'}, |
| 61 | + } |
| 62 | + |
| 63 | + try: |
| 64 | + new_issue = asf_jira.create_issue(fields=issue_dict) |
| 65 | + return new_issue.key |
| 66 | + except Exception as e: |
| 67 | + fail("Failed to create JIRA issue: %s" % e) |
| 68 | + |
| 69 | + |
| 70 | +def create_and_checkout_branch(jira_id): |
| 71 | + try: |
| 72 | + run_cmd("git checkout -b %s" % jira_id) |
| 73 | + print("Created and checked out branch: %s" % jira_id) |
| 74 | + except subprocess.CalledProcessError as e: |
| 75 | + fail("Failed to create branch %s: %s" % (jira_id, e)) |
| 76 | + |
| 77 | + |
| 78 | +def create_commit(jira_id, title): |
| 79 | + try: |
| 80 | + run_cmd(['git', 'commit', '-m', '%s: %s' % (jira_id, title)]) |
| 81 | + print("Created a commit with message: %s: %s" % (jira_id, title)) |
| 82 | + except subprocess.CalledProcessError as e: |
| 83 | + fail("Failed to create commit: %s" % e) |
| 84 | + |
| 85 | + |
| 86 | +def main(): |
| 87 | + if not JIRA_IMPORTED: |
| 88 | + fail("Could not find jira-python library. Run 'sudo pip3 install jira' to install.") |
| 89 | + |
| 90 | + if not JIRA_ACCESS_TOKEN: |
| 91 | + fail("The env-var JIRA_ACCESS_TOKEN is not set.") |
| 92 | + |
| 93 | + if len(sys.argv) < 2: |
| 94 | + fail("Usage: %s <JIRA title>" % sys.argv[0]) |
| 95 | + |
| 96 | + title = sys.argv[1] |
| 97 | + print("Creating JIRA issue with title: %s" % title) |
| 98 | + |
| 99 | + jira_id = create_jira_issue(title) |
| 100 | + print("Created JIRA issue: %s" % jira_id) |
| 101 | + |
| 102 | + create_and_checkout_branch(jira_id) |
| 103 | + |
| 104 | + create_commit(jira_id, title) |
| 105 | + |
| 106 | + |
| 107 | +if __name__ == "__main__": |
| 108 | + try: |
| 109 | + main() |
| 110 | + except BaseException: |
| 111 | + traceback.print_exc() |
| 112 | + sys.exit(-1) |
0 commit comments