go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/milo/ui/src/monitoring/util/bug_annotations.ts (about) 1 // Copyright 2024 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 import { AlertJson, BugId, TreeJson } from '@/monitoring/util/server_json'; 16 17 const monorailProjectId = (tree: TreeJson): string => { 18 return tree.default_monorail_project_name || 'chromium'; 19 }; 20 21 export const linkBug = async ( 22 tree: TreeJson, 23 alerts: AlertJson[], 24 bugId: string, 25 ): Promise<void> => { 26 const data = { 27 bugs: [ 28 { 29 id: bugId, 30 projectId: monorailProjectId(tree), 31 }, 32 ], 33 }; 34 35 // TODO: This API doesn't support CORS, so this doesn't currently work. Create an RPC to do the same thing. 36 const promises = alerts.map((alert) => 37 post('/api/v1/annotations/' + tree.name + '/add', { 38 ...data, 39 key: alert.key, 40 }), 41 ); 42 await Promise.all(promises); 43 }; 44 45 export const unlinkBug = async ( 46 tree: TreeJson, 47 alert: AlertJson, 48 bugs: BugId[], 49 ): Promise<void> => { 50 // TODO: This API doesn't support CORS, so this doesn't currently work. Create an RPC to do the same thing. 51 await post('/api/v1/annotations/' + tree.name + '/remove', { 52 bugs, 53 key: alert.key, 54 }); 55 }; 56 57 const post = async <DataType, ResponseType>( 58 path: string, 59 data: DataType, 60 ): Promise<ResponseType> => { 61 const response = await fetch(path, { 62 method: 'POST', 63 credentials: 'include', 64 body: JSON.stringify({ 65 data, 66 }), 67 }); 68 if (!response.ok) { 69 throw new Error(await response.text()); 70 } 71 return response.json(); 72 };