|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 4 | +# contributor license agreements. See the NOTICE file distributed with |
| 5 | +# this work for additional information regarding copyright ownership. |
| 6 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 7 | +# (the "License"); you may not use this file except in compliance with |
| 8 | +# the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | + |
| 18 | +# This script translates invalid authors in the contributors list generated |
| 19 | +# by generate-contributors.py. When the script encounters an author name that |
| 20 | +# is considered invalid, it searches Github and JIRA in an attempt to search |
| 21 | +# for replacements. This tool runs in two modes: |
| 22 | +# |
| 23 | +# (1) Interactive mode: For each invalid author name, this script presents |
| 24 | +# all candidate replacements to the user and awaits user response. In this |
| 25 | +# mode, the user may also input a custom name. This is the default. |
| 26 | +# |
| 27 | +# (2) Non-interactive mode: For each invalid author name, this script replaces |
| 28 | +# the name with the first valid candidate it can find. If there is none, it |
| 29 | +# uses the original name. This can be enabled through the --non-interactive flag. |
| 30 | + |
| 31 | +import os |
| 32 | +import sys |
| 33 | + |
| 34 | +from releaseutils import * |
| 35 | + |
| 36 | +# You must set the following before use! |
| 37 | +JIRA_API_BASE = os.environ.get("JIRA_API_BASE", "https://issues.apache.org/jira") |
| 38 | +JIRA_USERNAME = os.environ.get("JIRA_USERNAME", None) |
| 39 | +JIRA_PASSWORD = os.environ.get("JIRA_PASSWORD", None) |
| 40 | +if not JIRA_USERNAME or not JIRA_PASSWORD: |
| 41 | + sys.exit("Both JIRA_USERNAME and JIRA_PASSWORD must be set") |
| 42 | + |
| 43 | +# Write new contributors list to <old_file_name>.new |
| 44 | +if not os.path.isfile(contributors_file_name): |
| 45 | + print "Contributors file %s does not exist!" % contributors_file_name |
| 46 | + print "Have you run ./generate-contributors.py yet?" |
| 47 | + sys.exit(1) |
| 48 | +contributors_file = open(contributors_file_name, "r") |
| 49 | +new_contributors_file_name = contributors_file_name + ".new" |
| 50 | +new_contributors_file = open(new_contributors_file_name, "w") |
| 51 | +warnings = [] |
| 52 | + |
| 53 | +# In non-interactive mode, this script will choose the first replacement that is valid |
| 54 | +INTERACTIVE_MODE = True |
| 55 | +if len(sys.argv) > 1: |
| 56 | + options = set(sys.argv[1:]) |
| 57 | + if "--non-interactive" in options: |
| 58 | + INTERACTIVE_MODE = False |
| 59 | +if INTERACTIVE_MODE: |
| 60 | + print "Running in interactive mode. To disable this, provide the --non-interactive flag." |
| 61 | + |
| 62 | +# Setup Github and JIRA clients |
| 63 | +jira_options = { "server": JIRA_API_BASE } |
| 64 | +jira_client = JIRA(options = jira_options, basic_auth = (JIRA_USERNAME, JIRA_PASSWORD)) |
| 65 | +github_client = Github() |
| 66 | + |
| 67 | +# Generate candidates for the given author. This should only be called if the given author |
| 68 | +# name does not represent a full name as this operation is somewhat expensive. Under the |
| 69 | +# hood, it makes several calls to the Github and JIRA API servers to find the candidates. |
| 70 | +# |
| 71 | +# This returns a list of (candidate name, source) 2-tuples. E.g. |
| 72 | +# [ |
| 73 | +# (NOT_FOUND, "No full name found for Github user andrewor14"), |
| 74 | +# ("Andrew Or", "Full name of JIRA user andrewor14"), |
| 75 | +# ("Andrew Orso", "Full name of SPARK-1444 assignee andrewor14"), |
| 76 | +# ("Andrew Ordall", "Full name of SPARK-1663 assignee andrewor14"), |
| 77 | +# (NOT_FOUND, "No assignee found for SPARK-1763") |
| 78 | +# ] |
| 79 | +NOT_FOUND = "Not found" |
| 80 | +def generate_candidates(author, issues): |
| 81 | + candidates = [] |
| 82 | + # First check for full name of Github user |
| 83 | + github_name = get_github_name(new_author, github_client) |
| 84 | + if github_name: |
| 85 | + candidates.append((github_name, "Full name of Github user %s" % new_author)) |
| 86 | + else: |
| 87 | + candidates.append((NOT_FOUND, "No full name found for Github user %s" % new_author)) |
| 88 | + # Then do the same for JIRA user |
| 89 | + jira_name = get_jira_name(new_author, jira_client) |
| 90 | + if jira_name: |
| 91 | + candidates.append((jira_name, "Full name of JIRA user %s" % new_author)) |
| 92 | + else: |
| 93 | + candidates.append((NOT_FOUND, "No full name found for JIRA user %s" % new_author)) |
| 94 | + # Then do the same for the assignee of each of the associated JIRAs |
| 95 | + # Note that a given issue may not have an assignee, or the assignee may not have a full name |
| 96 | + for issue in issues: |
| 97 | + jira_issue = jira_client.issue(issue) |
| 98 | + jira_assignee = jira_issue.fields.assignee |
| 99 | + if jira_assignee: |
| 100 | + user_name = jira_assignee.name |
| 101 | + display_name = jira_assignee.displayName |
| 102 | + if display_name: |
| 103 | + candidates.append((display_name, "Full name of %s assignee %s" % (issue, user_name))) |
| 104 | + else: |
| 105 | + candidates.append((NOT_FOUND, "No full name found for %s assignee %" % (issue, user_name))) |
| 106 | + else: |
| 107 | + candidates.append((NOT_FOUND, "No assignee found for %s" % issue)) |
| 108 | + # Guard against special characters in candidate names |
| 109 | + # Note that the candidate name may already be in unicode (JIRA returns this) |
| 110 | + for i, (candidate, source) in enumerate(candidates): |
| 111 | + try: |
| 112 | + candidate = unicode(candidate, "UTF-8") |
| 113 | + except TypeError: |
| 114 | + # already in unicode |
| 115 | + pass |
| 116 | + candidate = unidecode.unidecode(candidate).strip() |
| 117 | + candidates[i] = (candidate, source) |
| 118 | + return candidates |
| 119 | + |
| 120 | +# Translate each invalid author by searching for possible candidates from Github and JIRA |
| 121 | +# In interactive mode, this script presents the user with a list of choices and have the user |
| 122 | +# select from this list. Additionally, the user may also choose to enter a custom name. |
| 123 | +# In non-interactive mode, this script picks the first valid author name from the candidates |
| 124 | +# If no such name exists, the original name is used (without the JIRA numbers). |
| 125 | +print "\n========================== Translating contributor list ==========================" |
| 126 | +for line in contributors_file: |
| 127 | + author = line.split(" - ")[0] |
| 128 | + print "Processing author %s" % author |
| 129 | + if not author: |
| 130 | + print " ERROR: Expected the following format <author> - <contributions>" |
| 131 | + print " ERROR: Actual = %s" % line |
| 132 | + if not is_valid_author(author): |
| 133 | + new_author = author.split("/")[0] |
| 134 | + issues = author.split("/")[1:] |
| 135 | + candidates = generate_candidates(new_author, issues) |
| 136 | + # Print out potential replacement candidates along with the sources, e.g. |
| 137 | + # [X] No full name found for Github user andrewor14 |
| 138 | + # [0] Andrew Or - Full name of JIRA user andrewor14 |
| 139 | + # [1] Andrew Orso - Full name of SPARK-1444 assignee andrewor14 |
| 140 | + # [2] Andrew Ordall - Full name of SPARK-1663 assignee andrewor14 |
| 141 | + # [X] No assignee found for SPARK-1763 |
| 142 | + # [3] Custom |
| 143 | + candidate_names = [] |
| 144 | + for candidate, source in candidates: |
| 145 | + if candidate == NOT_FOUND: |
| 146 | + print " [X] %s" % source |
| 147 | + else: |
| 148 | + index = len(candidate_names) |
| 149 | + candidate_names.append(candidate) |
| 150 | + print " [%d] %s - %s" % (index, candidate, source) |
| 151 | + custom_index = len(candidate_names) |
| 152 | + # In interactive mode, additionally provide "custom" option and await user response |
| 153 | + if INTERACTIVE_MODE: |
| 154 | + print " [%d] Custom" % custom_index |
| 155 | + response = raw_input(" Your choice: ") |
| 156 | + while not response.isdigit() or int(response) > custom_index: |
| 157 | + response = raw_input(" Please enter an integer between 0 and %d: " % custom_index) |
| 158 | + response = int(response) |
| 159 | + if response == custom_index: |
| 160 | + new_author = raw_input(" Please type a custom name for this author: ") |
| 161 | + else: |
| 162 | + new_author = candidate_names[response] |
| 163 | + # In non-interactive mode, just pick the first candidate |
| 164 | + else: |
| 165 | + valid_candidate_names = [name for name, _ in candidates\ |
| 166 | + if is_valid_author(name) and name != NOT_FOUND] |
| 167 | + if valid_candidate_names: |
| 168 | + new_author = valid_candidate_names[0] |
| 169 | + # Finally, capitalize the author and replace the original one with it |
| 170 | + # If the final replacement is still invalid, log a warning |
| 171 | + if is_valid_author(new_author): |
| 172 | + new_author = capitalize_author(new_author) |
| 173 | + else: |
| 174 | + warnings.append("Unable to find a valid name %s for author %s" % (new_author, author)) |
| 175 | + print " * Replacing %s with %s" % (author, new_author) |
| 176 | + line = line.replace(author, new_author) |
| 177 | + new_contributors_file.write(line) |
| 178 | +print "==================================================================================\n" |
| 179 | +contributors_file.close() |
| 180 | +new_contributors_file.close() |
| 181 | + |
| 182 | +print "Translated contributors list successfully written to %s!" % new_contributors_file_name |
| 183 | + |
| 184 | +# Log any warnings encountered in the process |
| 185 | +if warnings: |
| 186 | + print "\n========== Warnings encountered while translating the contributor list ===========" |
| 187 | + for w in warnings: print w |
| 188 | + print "Please manually correct these in the final contributors list at %s." % new_contributors_file_name |
| 189 | + print "==================================================================================\n" |
| 190 | + |
0 commit comments