1#!/usr/bin/env bash
2#===-- merge-request.sh  ---------------------------------------------------===#
3#
4# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5# See https://llvm.org/LICENSE.txt for license information.
6# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7#
8#===------------------------------------------------------------------------===#
9#
10# Submit a merge request to bugzilla.
11#
12#===------------------------------------------------------------------------===#
13
14dryrun=""
15stable_version=""
16revisions=""
17BUGZILLA_BIN=""
18BUGZILLA_CMD=""
19release_metabug=""
20bugzilla_product="new-bugs"
21bugzilla_component="new bugs"
22bugzilla_assigned_to=""
23bugzilla_user=""
24bugzilla_version=""
25bugzilla_url="https://bugs.llvm.org/xmlrpc.cgi"
26
27function usage() {
28  echo "usage: `basename $0` -user EMAIL -stable-version X.Y -r NUM"
29  echo ""
30  echo " -user EMAIL             Your email address for logging into bugzilla."
31  echo " -stable-version X.Y     The stable release version (e.g. 4.0, 5.0)."
32  echo " -r NUM                  Revision number to merge (e.g. 1234567)."
33  echo "                         This option can be specified multiple times."
34  echo " -bugzilla-bin PATH      Path to bugzilla binary (optional)."
35  echo " -assign-to EMAIL        Assign bug to user with EMAIL (optional)."
36  echo " -dry-run                Print commands instead of executing them."
37}
38
39while [ $# -gt 0 ]; do
40  case $1 in
41    -user)
42      shift
43      bugzilla_user="$1"
44      ;;
45    -stable-version)
46      shift
47      stable_version="$1"
48      ;;
49    -r)
50      shift
51      revisions="$revisions $1"
52      ;;
53    -project)
54      shift
55      project="$1"
56      ;;
57    -component)
58      shift
59      bugzilla_component="$1"
60      ;;
61    -bugzilla-bin)
62      shift
63      BUGZILLA_BIN="$1"
64      ;;
65    -assign-to)
66      shift
67      bugzilla_assigned_to="--assigned_to=$1"
68      ;;
69    -dry-run)
70      dryrun="echo"
71      ;;
72    -help | --help | -h | --h | -\? )
73      usage
74      exit 0
75      ;;
76    * )
77      echo "unknown option: $1"
78      usage
79      exit 1
80      ;;
81  esac
82  shift
83done
84
85if [ -z "$stable_version" ]; then
86  echo "error: no stable version specified"
87  exit 1
88fi
89
90case $stable_version in
91  4.0)
92    release_metabug="32061"
93    ;;
94  5.0)
95    release_metabug="34492"
96    ;;
97  6.0)
98    release_metabug="36649"
99    ;;
100  7.0)
101    release_metabug="39106"
102    ;;
103  8.0)
104    release_metabug="41221"
105    ;;
106  9.0)
107    release_metabug="43360"
108    ;;
109  *)
110    echo "error: invalid stable version"
111    exit 1
112esac
113bugzilla_version=$stable_version
114
115if [ -z "$revisions" ]; then
116  echo "error: no revisions specified"
117  exit 1
118fi
119
120if [ -z "$bugzilla_user" ]; then
121  echo "error: bugzilla username not specified."
122  exit 1
123fi
124
125if [ -z "$BUGZILLA_BIN" ]; then
126  BUGZILLA_BIN=`which bugzilla`
127  if [ $? -ne 0 ]; then
128    echo "error: could not find bugzilla executable."
129    echo "Make sure the bugzilla cli tool is installed on your system: "
130    echo "pip install python-bugzilla (recommended)"
131    echo ""
132    echo "Fedora: dnf install python-bugzilla"
133    echo "Ubuntu/Debian: apt-get install bugzilla-cli"
134    exit 1
135  fi
136fi
137
138BUGZILLA_MAJOR_VERSION=`$BUGZILLA_BIN --version 2>&1 | cut -d . -f 1`
139
140if [ $BUGZILLA_MAJOR_VERSION -eq 1 ]; then
141
142  echo "***************************** Error ** ********************************"
143  echo "You are using an older version of the bugzilla cli tool, which is not "
144  echo "supported.  You need to use bugzilla cli version 2.0.0 or higher:"
145  echo "***********************************************************************"
146  exit 1
147fi
148
149BUGZILLA_CMD="$BUGZILLA_BIN --bugzilla=$bugzilla_url"
150
151rev_string=""
152for r in $revisions; do
153  rev_string="$rev_string r$r"
154done
155
156echo "Checking for duplicate bugs..."
157
158check_duplicates=`$BUGZILLA_CMD query --blocked=$release_metabug --field="cf_fixed_by_commits=$rev_string"`
159
160if [ -n "$check_duplicates" ]; then
161  echo "Duplicate bug found:"
162  echo $check_duplicates
163  exit 1
164fi
165
166echo "Done"
167
168# Get short commit summary.  To avoid having a huge summary, we just
169# use the commit message for the first commit.
170commit_summary=''
171for r in $revisions; do
172  commit_msg=`svn log -r $r https://llvm.org/svn/llvm-project/`
173  if [ $? -ne 0 ]; then
174    echo "warning: failed to get commit message."
175    commit_msg=""
176  fi
177  break
178done
179
180if [ -n "$commit_msg" ]; then
181  commit_summary=`echo "$commit_msg" | sed '4q;d' | cut -c1-80`
182  commit_summary=" : ${commit_summary}"
183fi
184
185bug_summary="Merge${rev_string} into the $stable_version branch${commit_summary}"
186
187set -x
188
189# Login to bugzilla
190$BUGZILLA_CMD login $bugzilla_user
191
192bug_id=`${dryrun} $BUGZILLA_CMD --ensure-logged-in new \
193  -p "$bugzilla_product" \
194  -c "$bugzilla_component" --blocked=$release_metabug \
195  -o All --priority=P --arch All -v $bugzilla_version \
196  --field="cf_fixed_by_commits=$rev_string" \
197  --summary "${bug_summary}" \
198  -l "Is it OK to merge the following revision(s) to the $stable_version branch?" \
199  $bugzilla_assigned_to \
200  -i`
201
202if [ -n "$dryrun" ]; then
203  exit 0
204fi
205
206set +x
207
208if [ -z "$bug_id" ]; then
209  echo "Failed to create bug."
210  exit 1
211fi
212
213echo " Created new bug:"
214echo https://llvm.org/PR$bug_id
215
216# Add links to revisions
217for r in $revisions; do
218  $BUGZILLA_CMD --ensure-logged-in modify -l "https://reviews.llvm.org/rL$r" $bug_id
219done
220