Hirthanan Subenderan | a43fd4d | 2020-03-30 13:01:45 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # -*- coding: utf-8 -*-" |
| 3 | # |
| 4 | # Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 5 | # Use of this source code is governed by a BSD-style license that can be |
| 6 | # found in the LICENSE file. |
| 7 | |
| 8 | """Find missing stable and backported mainline fix patches in chromeos.""" |
| 9 | |
| 10 | from __future__ import print_function |
| 11 | |
Guenter Roeck | a0d63a5 | 2020-06-18 09:17:49 -0700 | [diff] [blame] | 12 | import MySQLdb # pylint: disable=import-error |
Hirthanan Subenderan | a43fd4d | 2020-03-30 13:01:45 -0700 | [diff] [blame] | 13 | |
| 14 | import common |
| 15 | |
Hirthanan Subenderan | d9d1b84 | 2020-04-09 12:15:14 -0700 | [diff] [blame] | 16 | DEFAULT_MERGED_REASON = 'Fix merged into linux chrome' |
Hirthanan Subenderan | a43fd4d | 2020-03-30 13:01:45 -0700 | [diff] [blame] | 17 | |
Curtis Malainey | c2f7124 | 2020-05-27 14:43:32 -0700 | [diff] [blame] | 18 | |
| 19 | def upstream_fixes_for_shas(db, upstream_shas): |
| 20 | """Returns list of fixer sha's for a given upstream sha. |
| 21 | |
| 22 | TODO(*): remove this after build_ordered_fixes_table_map moved to SQL CTE |
| 23 | Note: above todo is blocked by migration to MySQL 5.7, once upgraded then we can switch |
| 24 | """ |
| 25 | upstream_shas = ["\'" + sha + "\'" for sha in upstream_shas] |
| 26 | c = db.cursor() |
| 27 | |
| 28 | # format string here since we are inserting n elements |
| 29 | q = """SELECT fixedby_upstream_sha |
| 30 | FROM upstream_fixes |
| 31 | WHERE upstream_sha IN ({})""".format(', '.join(upstream_shas)) |
| 32 | c.execute(q) |
| 33 | |
| 34 | return [a[0] for a in c.fetchall()] |
| 35 | |
| 36 | |
Hirthanan Subenderan | 30be90b | 2020-04-02 10:03:25 -0700 | [diff] [blame] | 37 | def get_fixes_table_primary_key(db, fixes_table, fix_change_id): |
| 38 | """Retrieves the primary keys from a fixes table using changeid.""" |
| 39 | c = db.cursor(MySQLdb.cursors.DictCursor) |
| 40 | |
| 41 | q = """SELECT kernel_sha, fixedby_upstream_sha |
| 42 | FROM {fixes_table} |
| 43 | WHERE fix_change_id = %s""".format(fixes_table=fixes_table) |
| 44 | |
| 45 | c.execute(q, [fix_change_id]) |
| 46 | row = c.fetchone() |
| 47 | return (row['kernel_sha'], row['fixedby_upstream_sha']) |
| 48 | |
| 49 | |
Guenter Roeck | 89bd6a1 | 2020-07-26 07:23:26 -0700 | [diff] [blame] | 50 | def get_fix_status_and_changeid(db, fixes_tables, sha_list, strict): |
Guenter Roeck | a0d63a5 | 2020-06-18 09:17:49 -0700 | [diff] [blame] | 51 | """Get branch, fix_change_id, initial_status and status for one or more rows in fixes table.""" |
Hirthanan Subenderan | a43fd4d | 2020-03-30 13:01:45 -0700 | [diff] [blame] | 52 | c = db.cursor(MySQLdb.cursors.DictCursor) |
| 53 | |
Guenter Roeck | 89bd6a1 | 2020-07-26 07:23:26 -0700 | [diff] [blame] | 54 | # If sha_list has only one entry, there will be only one database match. |
| 55 | # Use OR in the query if this is the case. |
| 56 | if len(sha_list) < 2: |
| 57 | strict = False |
| 58 | |
Guenter Roeck | 6d53439 | 2020-07-24 14:42:52 -0700 | [diff] [blame] | 59 | pre_q = """SELECT '{fixes_table}' AS 'table', branch, kernel_sha, fixedby_upstream_sha, |
| 60 | fix_change_id, initial_status, status |
| 61 | FROM {fixes_table} |
| 62 | WHERE """ |
Hirthanan Subenderan | a43fd4d | 2020-03-30 13:01:45 -0700 | [diff] [blame] | 63 | |
Guenter Roeck | 6fb9506 | 2020-07-27 20:29:15 -0700 | [diff] [blame] | 64 | joined_list = '("'+'","'.join(sha_list)+'")' |
Guenter Roeck | 89bd6a1 | 2020-07-26 07:23:26 -0700 | [diff] [blame] | 65 | |
| 66 | pre_q += ' kernel_sha IN %s' % joined_list |
| 67 | pre_q += ' AND' if strict else ' OR' |
| 68 | pre_q += ' fixedby_upstream_sha IN %s' % joined_list |
Guenter Roeck | a0d63a5 | 2020-06-18 09:17:49 -0700 | [diff] [blame] | 69 | |
Guenter Roeck | 6d53439 | 2020-07-24 14:42:52 -0700 | [diff] [blame] | 70 | q = pre_q.format(fixes_table=fixes_tables.pop(0)) |
| 71 | while fixes_tables: |
| 72 | q += ' UNION ' |
| 73 | q += pre_q.format(fixes_table=fixes_tables.pop(0)) |
| 74 | |
| 75 | c.execute(q) |
Guenter Roeck | a0d63a5 | 2020-06-18 09:17:49 -0700 | [diff] [blame] | 76 | return c.fetchall() |
| 77 | |
| 78 | |
Hirthanan Subenderan | c5e6c40 | 2020-04-10 14:31:08 -0700 | [diff] [blame] | 79 | def update_change_abandoned(db, fixes_table, kernel_sha, fixedby_upstream_sha, reason=None): |
Hirthanan Subenderan | a43fd4d | 2020-03-30 13:01:45 -0700 | [diff] [blame] | 80 | """Updates fixes_table unique fix row to indicate fix cl has been abandoned. |
| 81 | |
| 82 | Function will only abandon rows in the table which have status OPEN or CONFLICT. |
| 83 | """ |
| 84 | c = db.cursor() |
| 85 | q = """UPDATE {fixes_table} |
Hirthanan Subenderan | c5e6c40 | 2020-04-10 14:31:08 -0700 | [diff] [blame] | 86 | SET status = 'ABANDONED', close_time = %s, reason = %s |
Hirthanan Subenderan | a43fd4d | 2020-03-30 13:01:45 -0700 | [diff] [blame] | 87 | WHERE kernel_sha = %s |
| 88 | AND fixedby_upstream_sha = %s |
| 89 | AND (status = 'OPEN' OR status = 'CONFLICT')""".format(fixes_table=fixes_table) |
| 90 | close_time = common.get_current_time() |
Hirthanan Subenderan | c5e6c40 | 2020-04-10 14:31:08 -0700 | [diff] [blame] | 91 | c.execute(q, [close_time, reason, kernel_sha, fixedby_upstream_sha]) |
Hirthanan Subenderan | a43fd4d | 2020-03-30 13:01:45 -0700 | [diff] [blame] | 92 | db.commit() |
| 93 | |
| 94 | |
Hirthanan Subenderan | c5e6c40 | 2020-04-10 14:31:08 -0700 | [diff] [blame] | 95 | def update_change_restored(db, fixes_table, kernel_sha, fixedby_upstream_sha, reason=None): |
Hirthanan Subenderan | a43fd4d | 2020-03-30 13:01:45 -0700 | [diff] [blame] | 96 | """Updates fixes_table unique fix row to indicate fix cl has been reopened.""" |
Guenter Roeck | 89bd6a1 | 2020-07-26 07:23:26 -0700 | [diff] [blame] | 97 | rows = get_fix_status_and_changeid(db, [fixes_table], [kernel_sha, fixedby_upstream_sha], True) |
Guenter Roeck | a0d63a5 | 2020-06-18 09:17:49 -0700 | [diff] [blame] | 98 | row = rows[0] |
Hirthanan Subenderan | a8113f0 | 2020-04-10 14:04:34 -0700 | [diff] [blame] | 99 | status = 'OPEN' if row['fix_change_id'] else row['initial_status'] |
Hirthanan Subenderan | a43fd4d | 2020-03-30 13:01:45 -0700 | [diff] [blame] | 100 | |
| 101 | c = db.cursor() |
| 102 | q = """UPDATE {fixes_table} |
Hirthanan Subenderan | c5e6c40 | 2020-04-10 14:31:08 -0700 | [diff] [blame] | 103 | SET status = %s, close_time = %s, reason = %s |
Hirthanan Subenderan | a43fd4d | 2020-03-30 13:01:45 -0700 | [diff] [blame] | 104 | WHERE kernel_sha = %s |
| 105 | AND fixedby_upstream_sha = %s |
| 106 | AND status = 'ABANDONED'""".format(fixes_table=fixes_table) |
Guenter Roeck | a0d63a5 | 2020-06-18 09:17:49 -0700 | [diff] [blame] | 107 | c.execute(q, [status, None, reason, kernel_sha, fixedby_upstream_sha]) |
Hirthanan Subenderan | a43fd4d | 2020-03-30 13:01:45 -0700 | [diff] [blame] | 108 | db.commit() |
Hirthanan Subenderan | 30be90b | 2020-04-02 10:03:25 -0700 | [diff] [blame] | 109 | |
| 110 | |
Hirthanan Subenderan | d9d1b84 | 2020-04-09 12:15:14 -0700 | [diff] [blame] | 111 | def update_change_merged(db, fixes_table, kernel_sha, fixedby_upstream_sha, |
| 112 | reason=DEFAULT_MERGED_REASON): |
Hirthanan Subenderan | 30be90b | 2020-04-02 10:03:25 -0700 | [diff] [blame] | 113 | """Updates fixes_table unique fix row to indicate fix cl has been merged.""" |
| 114 | c = db.cursor() |
| 115 | q = """UPDATE {fixes_table} |
Hirthanan Subenderan | d9d1b84 | 2020-04-09 12:15:14 -0700 | [diff] [blame] | 116 | SET status = 'MERGED', close_time = %s, reason = %s |
Hirthanan Subenderan | 30be90b | 2020-04-02 10:03:25 -0700 | [diff] [blame] | 117 | WHERE kernel_sha = %s |
| 118 | AND fixedby_upstream_sha = %s""".format(fixes_table=fixes_table) |
| 119 | close_time = common.get_current_time() |
Hirthanan Subenderan | d9d1b84 | 2020-04-09 12:15:14 -0700 | [diff] [blame] | 120 | c.execute(q, [close_time, reason, kernel_sha, fixedby_upstream_sha]) |
Hirthanan Subenderan | 30be90b | 2020-04-02 10:03:25 -0700 | [diff] [blame] | 121 | db.commit() |
| 122 | |
| 123 | |
| 124 | def update_change_status(db, fixes_table, fix_change_id, status): |
| 125 | """Updates fixes_table with the latest status from Gerrit API. |
| 126 | |
| 127 | This is done to synchronize CL's that are |
| 128 | abandoned/restored on Gerrit with our database state |
| 129 | """ |
| 130 | kernel_sha, fixedby_upstream_sha = get_fixes_table_primary_key(db, fixes_table, fix_change_id) |
| 131 | if status == common.Status.OPEN: |
| 132 | update_change_restored(db, fixes_table, kernel_sha, fixedby_upstream_sha) |
| 133 | elif status == common.Status.ABANDONED: |
| 134 | update_change_abandoned(db, fixes_table, kernel_sha, fixedby_upstream_sha) |
| 135 | elif status == common.Status.MERGED: |
| 136 | update_change_merged(db, fixes_table, kernel_sha, fixedby_upstream_sha) |
| 137 | else: |
| 138 | raise ValueError('Change should be either OPEN, ABANDONED, or MERGED') |
Hirthanan Subenderan | d9d1b84 | 2020-04-09 12:15:14 -0700 | [diff] [blame] | 139 | |
| 140 | |
| 141 | def update_conflict_to_open(db, fixes_table, kernel_sha, fixedby_upstream_sha, fix_change_id): |
| 142 | """Updates fixes_table to represent an open change that previously resulted in conflict.""" |
| 143 | c = db.cursor() |
| 144 | reason = 'Patch applies cleanly after originally conflicting.' |
| 145 | q = """UPDATE {fixes_table} |
| 146 | SET status = 'OPEN', fix_change_id = %s, reason = %s |
| 147 | WHERE kernel_sha = %s |
| 148 | AND fixedby_upstream_sha = %s""".format(fixes_table=fixes_table) |
| 149 | c.execute(q, [fix_change_id, reason, kernel_sha, fixedby_upstream_sha]) |
| 150 | db.commit() |