Skip to content

Commit 28742bd

Browse files
cuixqmichaelkedar
andauthored
feat(tools/datafix): allow updating all bugs in a source with confirmation (#3996)
This change enhances the `request_worker_update_record.py` script by adding the ability to process all bug records from a specified source. Previously, the script required at least one bug ID to be provided, so making it cumbersome to re-process an entire source. Now, if no bug IDs are supplied: 1. The script queries all bugs for the given source. 2. It displays the total number of bugs found. 3. It prompts the user for confirmation before proceeding to publish the update messages. This makes the tool more flexible for bulk updates while maintaining safety with an explicit confirmation step. --------- Co-authored-by: Michael Kedar <[email protected]>
1 parent 8bc725e commit 28742bd

File tree

1 file changed

+33
-12
lines changed

1 file changed

+33
-12
lines changed

tools/datafix/request_worker_update_record.py

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,35 +103,56 @@ def main():
103103
default=False,
104104
help="Delete bugs if not found in source (GIT only)")
105105
parser.add_argument(
106-
"bugs", action="append", nargs="+", help="The bug IDs to operate on")
106+
"bugs",
107+
action="append",
108+
nargs="*",
109+
help="The bug IDs to operate on. If not specified, all bugs from the "
110+
"source will be processed.")
107111

108112
args = parser.parse_args()
109113

110114
datastore_client = ndb.Client(args.project_id)
111115

112116
with datastore_client.context():
113-
source = osv.SourceRepository.get_by_id(args.source)
114-
115-
if source.type == osv.SourceRepositoryType.REST_ENDPOINT:
116-
for bug in args.bugs[0]:
117-
record_url = f'{source.link}{bug}{source.extension}'
118-
path = f'{bug}{source.extension}'
117+
source_repo = osv.SourceRepository.get_by_id(args.source)
118+
if not source_repo:
119+
raise ValueError(f"Source repository '{args.source}' not found.")
120+
121+
bugs_to_process = []
122+
if args.bugs and args.bugs[0]:
123+
bugs_to_process = args.bugs[0]
124+
else:
125+
print(
126+
f'No bug IDs provided. Querying all bugs for source {args.source}...')
127+
query = osv.Bug.query(osv.Bug.source == args.source)
128+
bugs_to_process = [b.id() for b in query.iter(keys_only=True)]
129+
print(f'Found {len(bugs_to_process)} bugs to update.')
130+
confirm = input('Are you sure you want to proceed? (y/N) ')
131+
if confirm.lower() not in ('y', 'yes'):
132+
print('Aborting.')
133+
return
134+
135+
if source_repo.type == osv.SourceRepositoryType.REST_ENDPOINT:
136+
for bug in bugs_to_process:
137+
record_url = f'{source_repo.link}{bug}{source_repo.extension}'
138+
path = f'{bug}{source_repo.extension}'
119139
request_url_update(record_url, args.project_id, args.source, path,
120140
args.timeout, False)
121141

122-
if source.type == osv.SourceRepositoryType.GIT:
123-
for bug in args.bugs[0]:
142+
if source_repo.type == osv.SourceRepositoryType.GIT:
143+
for bug in bugs_to_process:
124144
entity = osv.Bug.get_by_id(bug)
125145
if not entity:
126-
raise ValueError(f'{bug} does not exist in Datastore')
146+
print(f'Warning: {bug} does not exist in Datastore, skipping.')
147+
continue
127148

128149
path = entity.source_id.split(':')[1]
129150

130-
record_url = github_raw_url(source.repo_url, path)
151+
record_url = github_raw_url(source_repo.repo_url, path)
131152
request_url_update(record_url, args.project_id, args.source, path,
132153
args.timeout, args.allow_delete)
133154

134-
if source.type == osv.SourceRepositoryType.BUCKET:
155+
if source_repo.type == osv.SourceRepositoryType.BUCKET:
135156
raise NotImplementedError("Use reimport_gcs_record.py for now")
136157

137158

0 commit comments

Comments
 (0)