go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/milo/ui/src/common/tools/url_utils/url_utils.ts (about) 1 // Copyright 2022 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 { Build, BuilderID } from '@/common/services/buildbucket'; 16 import { GerritChange } from '@/common/services/common'; 17 import { urlSetSearchQueryParam } from '@/generic_libs/tools/utils'; 18 import { Variant } from '@/proto/go.chromium.org/luci/resultdb/proto/v1/common.pb'; 19 import { TestLocation } from '@/proto/go.chromium.org/luci/resultdb/proto/v1/test_metadata.pb'; 20 21 export function getBuildURLPathFromBuildData( 22 build: Pick<Build, 'builder' | 'number' | 'id'>, 23 ): string { 24 return getBuildURLPath( 25 build.builder, 26 build.number ? build.number.toString() : `b${build.id}`, 27 ); 28 } 29 30 export function getBuildURLPathFromBuildId(buildId: string): string { 31 return `/ui/b/${buildId}`; 32 } 33 34 export function getBuildURLPath( 35 builder: BuilderID, 36 buildIdOrNum: string, 37 ): string { 38 return `${getBuilderURLPath(builder)}/${buildIdOrNum}`; 39 } 40 41 export function getBuilderURLPath(builder: BuilderID): string { 42 return `/ui/p/${builder.project}/builders/${ 43 builder.bucket 44 }/${encodeURIComponent(builder.builder)}`; 45 } 46 47 export function getOldConsoleURLPath(proj: string, consoleId: string) { 48 return `/p/${proj}/g/${encodeURIComponent(consoleId)}/console`; 49 } 50 51 export function getProjectURLPath(proj: string) { 52 return `/ui/p/${proj}`; 53 } 54 55 export function getLegacyBuildURLPath( 56 builder: BuilderID, 57 buildNumOrId: string, 58 ) { 59 return `/old/p/${builder.project}/builders/${ 60 builder.bucket 61 }/${encodeURIComponent(builder.builder)}/${buildNumOrId}`; 62 } 63 64 export function getGerritChangeURL(change: GerritChange): string { 65 return `https://${change.host}/c/${change.change}/${change.patchset}`; 66 } 67 68 export function getSwarmingTaskURL(hostname: string, taskId: string): string { 69 return `https://${hostname}/task?id=${taskId}&o=true&w=true`; 70 } 71 72 export function getSwarmingBotListURL( 73 hostname: string, 74 dimensions: readonly string[], 75 ): string { 76 return `https://${hostname}/botlist?${new URLSearchParams( 77 dimensions.map((d) => ['f', d]), 78 )}`; 79 } 80 81 export function getInvURLPath(invId: string): string { 82 return `/ui/inv/${invId}`; 83 } 84 85 export function getRawArtifactURLPath(artifactName: string): string { 86 return `/raw-artifact/${artifactName}`; 87 } 88 89 export function getImageDiffArtifactURLPath( 90 diffArtifactName: string, 91 expectedArtifactId: string, 92 actualArtifactId: string, 93 ) { 94 const search = new URLSearchParams(); 95 search.set('actual_artifact_id', actualArtifactId); 96 search.set('expected_artifact_id', expectedArtifactId); 97 return `/ui/artifact/image-diff/${diffArtifactName}?${search}`; 98 } 99 100 export function getTextDiffArtifactURLPath(artifactName: string) { 101 return `/ui/artifact/text-diff/${artifactName}`; 102 } 103 104 export function getTestHistoryURLPath(realm: string, testId: string) { 105 return `/ui/test/${realm}/${encodeURIComponent(testId)}`; 106 } 107 108 export function generateTestHistoryURLSearchParams(variant: Variant) { 109 return Object.entries(variant.def || {}) 110 .map( 111 ([dimension, value]) => 112 `V:${encodeURIComponent(dimension)}=${encodeURIComponent(value)}`, 113 ) 114 .join(' '); 115 } 116 117 export function getTestHistoryURLWithSearchParam( 118 project: string, 119 testId: string, 120 queryParam: string, 121 ) { 122 return urlSetSearchQueryParam( 123 getTestHistoryURLPath(project, testId), 124 'q', 125 queryParam, 126 ); 127 } 128 129 export const NOT_FOUND_URL = '/ui/not-found'; 130 131 export function getLoginUrl(redirectTo: string) { 132 return `/auth/openid/login?${new URLSearchParams({ r: redirectTo })}`; 133 } 134 135 export function getLogoutUrl(redirectTo: string) { 136 return `/auth/openid/logout?${new URLSearchParams({ r: redirectTo })}`; 137 } 138 139 export function getCodeSourceUrl(testLocation: TestLocation, branch = 'HEAD') { 140 return ( 141 testLocation.repo + 142 '/+/' + 143 branch + 144 testLocation.fileName.slice(1) + 145 (testLocation.line ? '#' + testLocation.line : '') 146 ); 147 } 148 149 /** 150 * Update a single query parameter and return the update query string. 151 * 152 * @param currentSearchString The current search parameters string. 153 * @param paramKey The parameter key to set. 154 * @param paramValue The value to set. 155 * @returns The updated parameter string. 156 */ 157 export function setSingleQueryParam( 158 currentSearchString: string, 159 paramKey: string, 160 paramValue: string, 161 ): URLSearchParams { 162 const updatedSearchParams = new URLSearchParams(currentSearchString); 163 updatedSearchParams.set(paramKey, paramValue); 164 return updatedSearchParams; 165 }