Home Home > GIT Browse
summaryrefslogtreecommitdiff
blob: e8cc22c210c10c89ad6ae0c1d1d328b9dedca24c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/bin/bash

URL="https://apibugzilla.suse.com/xmlrpc.cgi"
COMPONENT="Kernel"
COMMENT="This is an automated report for a proactive fix, documented below."

handle_one_patch() {
    FILE="$1"
    SUBJ=$(formail -x Subject < "${FILE}")
    BODY="$(formail -I "" < "${FILE}" | awk '/^---/ { exit; }; { print; } ')"

    # Use the last available version
    ${BUGZILLA} info -v "${PRODUCT}" > ${tmpdir}/version

    if [ $? -ne 0 ]; then
        echo "Could not get versions for ${PRODUCT}." >&2
        echo "This sometimes produces weird connection aborted errors." >&2
        cat ${tmpdir}/version >&2
        exit 1
    fi

    VERSION=$(cat ${tmpdir}/version | tail -1)

    ${BUGZILLA} new -p "${PRODUCT}" -c "${COMPONENT}" -a "${EMAIL}" \
        ${QA_EMAIL} -t "${SUBJ}" -v "${VERSION}" \
        --comment "${COMMENT}" -s "CONFIRMED" \
	--no-refresh --ids > ${tmpdir}/bugid

    if [ $? -ne 0 ]; then
        echo "Bug creation failed for "${FILE}"." >&2
	cat ${tmpdir}/bugid >&2
        exit 1
    fi

    read BUGID < ${tmpdir}/bugid

    # If we didn't get just a bug number, we got an error
    if [ -z "${BUGID}" -o "${BUGID}" != "${BUGID%[^0-9]}" ]; then
        echo "Bug creation failed for ${FILE}; Errors follow." >&2
        cat ${tmpdir}/bugid >&2
        exit 1
    fi

    OLDREFS=$(grep "^References:" "${FILE}"|sed -e 's#References: *##')
    if [ -n "${OLDREFS}" ]; then
        OLDREFS="${OLDREFS} "
    fi

    REFERENCE="References: ${OLDREFS}bsc#${BUGID}"
    if [ -n "${MORE_REFERENCES}" ]; then
        REFERENCE="${REFERENCE} ${MORE_REFERENCES}"
    fi

    # Update the references header before uploading
    FILENAME="$(basename "${FILE}")"
    grep -v "^References:" "${FILE}" | formail -f -I "${REFERENCE}" \
        > "${tmpdir}/${FILENAME}"
    if [ $? -eq 0 ]; then
        mv "${tmpdir}/${FILENAME}" "${FILE}"
    fi

    if [ "${SUBJ}" = "${SUBJ/\[PATCH}" ]; then
            SUBJ="[PATCH] ${SUBJ}"
    fi

    ${BUGZILLA} attach --file="${FILE}" -d "${SUBJ}" -t "text/x-patch" \
          --comment "${BODY}" ${BUGID}

    echo "Filed report ${BUGID} for ${FILE}"
}

usage () {
cat <<END
usage: $(basename $0) [options...] BUGID [BUGID ...]
Options:
    -e | --email <address>
        * Email address to which this report will be assigned
    -p | --product <product>
        * Bugzilla product to fail this report against
    -d | --debug
        * Enable debugging output (also assigns QA contact as you)
    -r | --reference
        * Any additional references to assign to this report,
          e.g. FATE#123456
    -h | --help
    	* Print this message

Notes:
  * If no email address specified, the username component of git-config
    user.email will be used with @suse.com appended.
  * If no product is specified, the \$BUGZILLA_PRODUCT variable in
    rpm/config.sh will be used, if present.
  * If the patch already contains references, they will be preserved with
    the bugzilla ID created and any additional references specified with
    --reference appended.  Otherwise, a new References tag will be created.
  * The patch will be updated with new references prior to upload.
  * Use of this script requires a ~/.bugzillarc with the following contents:
    [apibugzilla.suse.com]
    user = <bugzilla username>
    password = <bugzilla password>
    authtype = basic
END
    exit $1
}

# Defaults
EMAIL="$(git config user.email)"
if [ "${EMAIL}" != "${EMAIL%%*@suse.com}" ]; then
    EMAIL="${EMAIL%%@*}@suse.com"
fi

. $(dirname $0)/../rpm/config.sh
PRODUCT="${BUGZILLA_PRODUCT}"

OPTS="$(getopt -o e:p:dr:h --long email:,product:,debug,reference: -n 'bugzilla-upload' -- "$@")"

if [ $? -ne 0 ]; then
    echo "" >&2
    usage 1
fi

eval set -- "${OPTS}"

while true; do
    case "$1" in
    -e | --email)
        EMAIL="$2" ; shift 2 ;;
    -p | --product)
        PRODUCT="$2" ; shift 2 ;;
    -d | --debug)
        DEBUG=true; shift ;;
    -r | --reference)
        MORE_REFERENCES="${MORE_REFERENCES} $2" ; shift 2 ;;
    -h | --help)
        usage 0 ;;
    --)
        shift ; break ;;
    *)
        break ;;
    esac
done

if [ "$#" -eq 0 ]; then
	echo "" >&2
	usage 1
fi

DEBUG=true

if [ -z "${PRODUCT}" ]; then
    cat <<-END >&2
	ERROR: No product specified.

	Product may be specified via -p or rpm/config.sh:\$BUGZILLA_PRODUCT
	END
    exit 1
fi

if [ -z "${EMAIL}" ]; then
    cat <<-END >&2
	ERROR: No assignee email address specified.

	Email may be specified via -e or git-config user.email
	END
fi

# Don't spam QA while testing
if [ -n "${DEBUG}" ]; then
    QA_EMAIL="-q ${EMAIL}"
fi

DIR="$(realpath "$(dirname "$0")")"
if [ ! -e "${DIR}/bugzilla-cli" ]; then
    echo "Missing ${DIR}/bugzilla-cli" >&2
    exit 1
fi

BUGZILLA="${DIR}/bugzilla-cli --bugzilla ${URL} --ensure-logged-in"

cleanup () {
    rm -rf ${tmpdir}
}

trap cleanup EXIT
tmpdir=$(mktemp -d /tmp/bugzilla.XXXXXX)

for patch in "$@"; do
    handle_one_patch "${patch}"
done