Dave Borowitz | 8cdc76b | 2018-03-26 10:04:27 -0400 | [diff] [blame] | 1 | /** |
| 2 | * @license |
| 3 | * Copyright (C) 2017 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 17 | import '../../../styles/gr-menu-page-styles'; |
| 18 | import '../../../styles/gr-subpage-styles'; |
| 19 | import '../../../styles/shared-styles'; |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 20 | import '../gr-access-section/gr-access-section'; |
| 21 | import {flush} from '@polymer/polymer/lib/legacy/polymer.dom'; |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 22 | import {PolymerElement} from '@polymer/polymer/polymer-element'; |
| 23 | import {htmlTemplate} from './gr-repo-access_html'; |
| 24 | import {encodeURL, getBaseUrl, singleDecodeURL} from '../../../utils/url-util'; |
| 25 | import {GerritNav} from '../../core/gr-navigation/gr-navigation'; |
| 26 | import {toSortedPermissionsArray} from '../../../utils/access-util'; |
| 27 | import {customElement, property} from '@polymer/decorators'; |
Dmitrii Filippov | e3c09ae | 2020-07-10 11:39:50 +0200 | [diff] [blame] | 28 | import { |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 29 | RepoName, |
| 30 | ProjectInfo, |
| 31 | CapabilityInfoMap, |
| 32 | LabelNameToLabelTypeInfoMap, |
| 33 | ProjectAccessInput, |
| 34 | GitRef, |
| 35 | UrlEncodedRepoName, |
| 36 | ProjectAccessGroups, |
| 37 | } from '../../../types/common'; |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 38 | import {GrButton} from '../../shared/gr-button/gr-button'; |
| 39 | import {GrAccessSection} from '../gr-access-section/gr-access-section'; |
| 40 | import { |
| 41 | AutocompleteQuery, |
| 42 | AutocompleteSuggestion, |
| 43 | } from '../../shared/gr-autocomplete/gr-autocomplete'; |
| 44 | import { |
| 45 | EditableLocalAccessSectionInfo, |
| 46 | PermissionAccessSection, |
| 47 | PropertyTreeNode, |
| 48 | PrimitiveValue, |
| 49 | } from './gr-repo-access-interfaces'; |
Milutin Kristofic | 860fe4d | 2020-11-23 16:13:45 +0100 | [diff] [blame] | 50 | import {firePageError, fireAlert} from '../../../utils/event-util'; |
Ben Rohlfs | 43935a4 | 2020-12-01 19:14:09 +0100 | [diff] [blame] | 51 | import {appContext} from '../../../services/app-context'; |
Dhruv Srivastava | d4880e3 | 2021-01-29 13:42:58 +0100 | [diff] [blame] | 52 | import {WebLinkInfo} from '../../../types/diff'; |
Becky Siegel | 148c7b2 | 2018-01-16 15:01:58 -0800 | [diff] [blame] | 53 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 54 | const NOTHING_TO_SAVE = 'No changes to save.'; |
Becky Siegel | 8d7b627 | 2018-03-28 09:38:29 -0700 | [diff] [blame] | 55 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 56 | const MAX_AUTOCOMPLETE_RESULTS = 50; |
Becky Siegel | 148c7b2 | 2018-01-16 15:01:58 -0800 | [diff] [blame] | 57 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 58 | /** |
| 59 | * Fired when save is a no-op |
| 60 | * |
| 61 | * @event show-alert |
| 62 | */ |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 63 | @customElement('gr-repo-access') |
Ben Rohlfs | 27c856da | 2021-03-12 14:09:10 +0100 | [diff] [blame] | 64 | export class GrRepoAccess extends PolymerElement { |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 65 | static get template() { |
| 66 | return htmlTemplate; |
| 67 | } |
Becky Siegel | 9640eb2 | 2017-12-11 15:58:57 -0800 | [diff] [blame] | 68 | |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 69 | @property({type: String, observer: '_repoChanged'}) |
| 70 | repo?: RepoName; |
Becky Siegel | 9640eb2 | 2017-12-11 15:58:57 -0800 | [diff] [blame] | 71 | |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 72 | @property({type: String}) |
| 73 | path?: string; |
Becky Siegel | 9640eb2 | 2017-12-11 15:58:57 -0800 | [diff] [blame] | 74 | |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 75 | @property({type: Boolean}) |
| 76 | _canUpload?: boolean = false; // restAPI can return undefined |
Becky Siegel | 9640eb2 | 2017-12-11 15:58:57 -0800 | [diff] [blame] | 77 | |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 78 | @property({type: String}) |
| 79 | _inheritFromFilter?: RepoName; |
Becky Siegel | 9640eb2 | 2017-12-11 15:58:57 -0800 | [diff] [blame] | 80 | |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 81 | @property({type: Object}) |
| 82 | _query: AutocompleteQuery; |
Becky Siegel | 6db432f | 2017-08-25 09:17:42 -0700 | [diff] [blame] | 83 | |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 84 | @property({type: Array}) |
| 85 | _ownerOf?: GitRef[]; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 86 | |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 87 | @property({type: Object}) |
| 88 | _capabilities?: CapabilityInfoMap; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 89 | |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 90 | @property({type: Object}) |
| 91 | _groups?: ProjectAccessGroups; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 92 | |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 93 | @property({type: Object}) |
| 94 | _inheritsFrom?: ProjectInfo | null | {}; |
| 95 | |
| 96 | @property({type: Object}) |
| 97 | _labels?: LabelNameToLabelTypeInfoMap; |
| 98 | |
| 99 | @property({type: Object}) |
| 100 | _local?: EditableLocalAccessSectionInfo; |
| 101 | |
| 102 | @property({type: Boolean, observer: '_handleEditingChanged'}) |
| 103 | _editing = false; |
| 104 | |
| 105 | @property({type: Boolean}) |
| 106 | _modified = false; |
| 107 | |
| 108 | @property({type: Array}) |
| 109 | _sections?: PermissionAccessSection[]; |
| 110 | |
| 111 | @property({type: Array}) |
Dhruv Srivastava | d4880e3 | 2021-01-29 13:42:58 +0100 | [diff] [blame] | 112 | _weblinks?: WebLinkInfo[]; |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 113 | |
| 114 | @property({type: Boolean}) |
| 115 | _loading = true; |
| 116 | |
Dhruv Srivastava | 7e454cd | 2021-02-22 15:06:58 +0100 | [diff] [blame] | 117 | private originalInheritsFrom?: ProjectInfo | null; |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 118 | |
Dhruv Srivastava | 9f65d91 | 2021-02-22 10:36:19 +0100 | [diff] [blame] | 119 | private readonly restApiService = appContext.restApiService; |
Ben Rohlfs | 43935a4 | 2020-12-01 19:14:09 +0100 | [diff] [blame] | 120 | |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 121 | constructor() { |
| 122 | super(); |
| 123 | this._query = () => this._getInheritFromSuggestions(); |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 124 | this.addEventListener('access-modified', () => |
| 125 | this._handleAccessModified() |
| 126 | ); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 127 | } |
Becky Siegel | 6db432f | 2017-08-25 09:17:42 -0700 | [diff] [blame] | 128 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 129 | _handleAccessModified() { |
| 130 | this._modified = true; |
| 131 | } |
Becky Siegel | 9640eb2 | 2017-12-11 15:58:57 -0800 | [diff] [blame] | 132 | |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 133 | _repoChanged(repo: RepoName) { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 134 | this._loading = true; |
Becky Siegel | 9640eb2 | 2017-12-11 15:58:57 -0800 | [diff] [blame] | 135 | |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 136 | if (!repo) { |
| 137 | return Promise.resolve(); |
| 138 | } |
Paladox none | 68511792 | 2018-03-17 20:05:21 +0000 | [diff] [blame] | 139 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 140 | return this._reload(repo); |
| 141 | } |
Paladox none | 70cb10c | 2018-02-17 19:12:09 +0000 | [diff] [blame] | 142 | |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 143 | _reload(repo: RepoName) { |
Dhruv Srivastava | b0131f9 | 2020-11-24 09:31:54 +0100 | [diff] [blame] | 144 | const errFn = (response?: Response | null) => { |
Ben Rohlfs | a76c82f | 2021-01-22 22:22:32 +0100 | [diff] [blame] | 145 | firePageError(response); |
Dhruv Srivastava | b0131f9 | 2020-11-24 09:31:54 +0100 | [diff] [blame] | 146 | }; |
Paladox none | 70cb10c | 2018-02-17 19:12:09 +0000 | [diff] [blame] | 147 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 148 | this._editing = false; |
Paladox none | 70cb10c | 2018-02-17 19:12:09 +0000 | [diff] [blame] | 149 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 150 | // Always reset sections when a project changes. |
| 151 | this._sections = []; |
Ben Rohlfs | 43935a4 | 2020-12-01 19:14:09 +0100 | [diff] [blame] | 152 | const sectionsPromises = this.restApiService |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 153 | .getRepoAccessRights(repo, errFn) |
| 154 | .then(res => { |
| 155 | if (!res) { |
| 156 | return Promise.resolve(undefined); |
| 157 | } |
Becky Siegel | 6af6325 | 2018-04-26 14:02:36 -0700 | [diff] [blame] | 158 | |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 159 | // Keep a copy of the original inherit from values separate from |
| 160 | // the ones data bound to gr-autocomplete, so the original value |
| 161 | // can be restored if the user cancels. |
| 162 | this._inheritsFrom = res.inherits_from |
| 163 | ? { |
| 164 | ...res.inherits_from, |
| 165 | } |
| 166 | : null; |
Dhruv Srivastava | 7e454cd | 2021-02-22 15:06:58 +0100 | [diff] [blame] | 167 | this.originalInheritsFrom = res.inherits_from |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 168 | ? { |
| 169 | ...res.inherits_from, |
| 170 | } |
| 171 | : null; |
| 172 | // Initialize the filter value so when the user clicks edit, the |
| 173 | // current value appears. If there is no parent repo, it is |
| 174 | // initialized as an empty string. |
| 175 | this._inheritFromFilter = res.inherits_from |
| 176 | ? res.inherits_from.name |
| 177 | : ('' as RepoName); |
| 178 | // 'as EditableLocalAccessSectionInfo' is required because res.local |
| 179 | // type doesn't have index signature |
| 180 | this._local = res.local as EditableLocalAccessSectionInfo; |
| 181 | this._groups = res.groups; |
| 182 | this._weblinks = res.config_web_links || []; |
| 183 | this._canUpload = res.can_upload; |
| 184 | this._ownerOf = res.owner_of || []; |
| 185 | return toSortedPermissionsArray(this._local); |
| 186 | }); |
Becky Siegel | 6af6325 | 2018-04-26 14:02:36 -0700 | [diff] [blame] | 187 | |
Ben Rohlfs | 43935a4 | 2020-12-01 19:14:09 +0100 | [diff] [blame] | 188 | const capabilitiesPromises = this.restApiService |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 189 | .getCapabilities(errFn) |
| 190 | .then(res => { |
| 191 | if (!res) { |
| 192 | return Promise.resolve(undefined); |
| 193 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 194 | |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 195 | return res; |
| 196 | }); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 197 | |
Ben Rohlfs | 43935a4 | 2020-12-01 19:14:09 +0100 | [diff] [blame] | 198 | const labelsPromises = this.restApiService |
| 199 | .getRepo(repo, errFn) |
| 200 | .then(res => { |
| 201 | if (!res) { |
| 202 | return Promise.resolve(undefined); |
| 203 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 204 | |
Ben Rohlfs | 43935a4 | 2020-12-01 19:14:09 +0100 | [diff] [blame] | 205 | return res.labels; |
| 206 | }); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 207 | |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 208 | return Promise.all([ |
| 209 | sectionsPromises, |
| 210 | capabilitiesPromises, |
| 211 | labelsPromises, |
| 212 | ]).then(([sections, capabilities, labels]) => { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 213 | this._capabilities = capabilities; |
| 214 | this._labels = labels; |
| 215 | this._sections = sections; |
| 216 | this._loading = false; |
| 217 | }); |
| 218 | } |
| 219 | |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 220 | _handleUpdateInheritFrom(e: CustomEvent<{value: string}>) { |
| 221 | const parentProject: ProjectInfo = { |
| 222 | id: e.detail.value as UrlEncodedRepoName, |
| 223 | name: this._inheritFromFilter, |
| 224 | }; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 225 | if (!this._inheritsFrom) { |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 226 | this._inheritsFrom = parentProject; |
| 227 | } else { |
| 228 | // TODO(TS): replace with |
| 229 | // this._inheritsFrom = {...this._inheritsFrom, ...parentProject}; |
| 230 | const projectInfo = this._inheritsFrom as ProjectInfo; |
| 231 | projectInfo.id = parentProject.id; |
| 232 | projectInfo.name = parentProject.name; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 233 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 234 | this._handleAccessModified(); |
| 235 | } |
| 236 | |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 237 | _getInheritFromSuggestions(): Promise<AutocompleteSuggestion[]> { |
Ben Rohlfs | 43935a4 | 2020-12-01 19:14:09 +0100 | [diff] [blame] | 238 | return this.restApiService |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 239 | .getRepos(this._inheritFromFilter, MAX_AUTOCOMPLETE_RESULTS) |
| 240 | .then(response => { |
| 241 | const projects: AutocompleteSuggestion[] = []; |
| 242 | if (!response) { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 243 | return projects; |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 244 | } |
| 245 | for (const item of response) { |
| 246 | projects.push({ |
| 247 | name: item.name, |
| 248 | value: item.id, |
| 249 | }); |
| 250 | } |
| 251 | return projects; |
| 252 | }); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 253 | } |
| 254 | |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 255 | _computeLoadingClass(loading: boolean) { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 256 | return loading ? 'loading' : ''; |
| 257 | } |
| 258 | |
| 259 | _handleEdit() { |
| 260 | this._editing = !this._editing; |
| 261 | } |
| 262 | |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 263 | _editOrCancel(editing: boolean) { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 264 | return editing ? 'Cancel' : 'Edit'; |
| 265 | } |
| 266 | |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 267 | _computeWebLinkClass(weblinks?: string[]) { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 268 | return weblinks && weblinks.length ? 'show' : ''; |
| 269 | } |
| 270 | |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 271 | _computeShowInherit(inheritsFrom?: RepoName) { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 272 | return inheritsFrom ? 'show' : ''; |
| 273 | } |
| 274 | |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 275 | // TODO(TS): Unclear what is model here, provide a better explanation |
| 276 | _handleAddedSectionRemoved(e: CustomEvent & {model: {index: string}}) { |
| 277 | if (!this._sections) { |
| 278 | return; |
| 279 | } |
| 280 | const index = Number(e.model.index); |
| 281 | if (isNaN(index)) { |
| 282 | return; |
| 283 | } |
| 284 | this._sections = this._sections |
| 285 | .slice(0, index) |
| 286 | .concat(this._sections.slice(index + 1, this._sections.length)); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 287 | } |
| 288 | |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 289 | _handleEditingChanged(editing: boolean, editingOld: boolean) { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 290 | // Ignore when editing gets set initially. |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 291 | if (!editingOld || editing) { |
| 292 | return; |
| 293 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 294 | // Remove any unsaved but added refs. |
| 295 | if (this._sections) { |
| 296 | this._sections = this._sections.filter(p => !p.value.added); |
| 297 | } |
| 298 | // Restore inheritFrom. |
| 299 | if (this._inheritsFrom) { |
Dmitrii Filippov | aab9825 | 2021-04-06 19:04:11 +0200 | [diff] [blame^] | 300 | // Can't assign this._inheritsFrom = {...this.originalInheritsFrom} |
| 301 | // directly, because this._inheritsFrom is declared as |
| 302 | // '...|null|undefined` and typescript reports error when trying |
| 303 | // to access .name property (because 'name' in null and 'name' in undefined |
| 304 | // lead to runtime error) |
| 305 | // After migrating to Typescript v4.2 the code below can be rewritten as |
| 306 | // const copy = {...this.originalInheritsFrom}; |
| 307 | const copy: ProjectInfo | {} = this.originalInheritsFrom |
| 308 | ? {...this.originalInheritsFrom} |
| 309 | : {}; |
| 310 | this._inheritsFrom = copy; |
| 311 | this._inheritFromFilter = 'name' in copy ? copy.name : undefined; |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 312 | } |
| 313 | if (!this._local) { |
| 314 | return; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 315 | } |
| 316 | for (const key of Object.keys(this._local)) { |
| 317 | if (this._local[key].added) { |
| 318 | delete this._local[key]; |
Tao Zhou | 704cfe73 | 2020-03-12 08:32:46 +0100 | [diff] [blame] | 319 | } |
Dmitrii Filippov | 3fd2b10 | 2019-11-15 16:16:46 +0100 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 323 | _updateRemoveObj(addRemoveObj: {remove: PropertyTreeNode}, path: string[]) { |
| 324 | let curPos: PropertyTreeNode = addRemoveObj.remove; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 325 | for (const item of path) { |
| 326 | if (!curPos[item]) { |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 327 | if (item === path[path.length - 1]) { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 328 | if (path[path.length - 2] === 'permissions') { |
| 329 | curPos[item] = {rules: {}}; |
| 330 | } else if (path.length === 1) { |
| 331 | curPos[item] = {permissions: {}}; |
| 332 | } else { |
| 333 | curPos[item] = {}; |
| 334 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 335 | } else { |
| 336 | curPos[item] = {}; |
| 337 | } |
| 338 | } |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 339 | // The last item can be a PrimitiveValue, but we don't use it |
| 340 | // All intermediate items are PropertyTreeNode |
| 341 | // TODO(TS): rewrite this loop and process the last item explicitly |
| 342 | curPos = curPos[item] as PropertyTreeNode; |
| 343 | } |
| 344 | return addRemoveObj; |
| 345 | } |
| 346 | |
| 347 | _updateAddObj( |
| 348 | addRemoveObj: {add: PropertyTreeNode}, |
| 349 | path: string[], |
| 350 | value: PropertyTreeNode | PrimitiveValue |
| 351 | ) { |
| 352 | let curPos: PropertyTreeNode = addRemoveObj.add; |
| 353 | for (const item of path) { |
| 354 | if (!curPos[item]) { |
| 355 | if (item === path[path.length - 1]) { |
| 356 | curPos[item] = value; |
| 357 | } else { |
| 358 | curPos[item] = {}; |
| 359 | } |
| 360 | } |
| 361 | // The last item can be a PrimitiveValue, but we don't use it |
| 362 | // All intermediate items are PropertyTreeNode |
| 363 | // TODO(TS): rewrite this loop and process the last item explicitly |
| 364 | curPos = curPos[item] as PropertyTreeNode; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 365 | } |
| 366 | return addRemoveObj; |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Used to recursively remove any objects with a 'deleted' bit. |
| 371 | */ |
Ben Rohlfs | 7b71b11 | 2021-02-12 10:36:08 +0100 | [diff] [blame] | 372 | _recursivelyRemoveDeleted(obj?: PropertyTreeNode) { |
| 373 | if (!obj) return; |
| 374 | for (const k of Object.keys(obj)) { |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 375 | const node = obj[k]; |
| 376 | if (typeof node === 'object') { |
| 377 | if (node.deleted) { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 378 | delete obj[k]; |
| 379 | return; |
| 380 | } |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 381 | this._recursivelyRemoveDeleted(node); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 386 | _recursivelyUpdateAddRemoveObj( |
Ben Rohlfs | 7b71b11 | 2021-02-12 10:36:08 +0100 | [diff] [blame] | 387 | obj: PropertyTreeNode | undefined, |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 388 | addRemoveObj: { |
| 389 | add: PropertyTreeNode; |
| 390 | remove: PropertyTreeNode; |
| 391 | }, |
| 392 | path: string[] = [] |
| 393 | ) { |
Ben Rohlfs | 7b71b11 | 2021-02-12 10:36:08 +0100 | [diff] [blame] | 394 | if (!obj) return; |
| 395 | for (const k of Object.keys(obj)) { |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 396 | const node = obj[k]; |
| 397 | if (typeof node === 'object') { |
| 398 | const updatedId = node.updatedId; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 399 | const ref = updatedId ? updatedId : k; |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 400 | if (node.deleted) { |
| 401 | this._updateRemoveObj(addRemoveObj, path.concat(k)); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 402 | continue; |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 403 | } else if (node.modified) { |
| 404 | this._updateRemoveObj(addRemoveObj, path.concat(k)); |
| 405 | this._updateAddObj(addRemoveObj, path.concat(ref), node); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 406 | /* Special case for ref changes because they need to be added and |
| 407 | removed in a different way. The new ref needs to include all |
| 408 | changes but also the initial state. To do this, instead of |
| 409 | continuing with the same recursion, just remove anything that is |
| 410 | deleted in the current state. */ |
| 411 | if (updatedId && updatedId !== k) { |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 412 | this._recursivelyRemoveDeleted( |
| 413 | addRemoveObj.add[updatedId] as PropertyTreeNode |
| 414 | ); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 415 | } |
| 416 | continue; |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 417 | } else if (node.added) { |
| 418 | this._updateAddObj(addRemoveObj, path.concat(ref), node); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 419 | /** |
| 420 | * As add / delete both can happen in the new section, |
| 421 | * so here to make sure it will remove the deleted ones. |
| 422 | * |
| 423 | * @see Issue 11339 |
| 424 | */ |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 425 | this._recursivelyRemoveDeleted( |
| 426 | addRemoveObj.add[k] as PropertyTreeNode |
| 427 | ); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 428 | continue; |
| 429 | } |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 430 | this._recursivelyUpdateAddRemoveObj(node, addRemoveObj, path.concat(k)); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * Returns an object formatted for saving or submitting access changes for |
| 437 | * review |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 438 | */ |
| 439 | _computeAddAndRemove() { |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 440 | const addRemoveObj: { |
| 441 | add: PropertyTreeNode; |
| 442 | remove: PropertyTreeNode; |
| 443 | parent?: string | null; |
| 444 | } = { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 445 | add: {}, |
| 446 | remove: {}, |
| 447 | }; |
| 448 | |
Dhruv Srivastava | 7e454cd | 2021-02-22 15:06:58 +0100 | [diff] [blame] | 449 | const originalInheritsFromId = this.originalInheritsFrom |
| 450 | ? singleDecodeURL(this.originalInheritsFrom.id) |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 451 | : null; |
| 452 | // TODO(TS): this._inheritsFrom as ProjectInfo might be a mistake. |
| 453 | // _inheritsFrom can be {} |
| 454 | const inheritsFromId = this._inheritsFrom |
| 455 | ? singleDecodeURL((this._inheritsFrom as ProjectInfo).id) |
| 456 | : null; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 457 | |
| 458 | const inheritFromChanged = |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 459 | // Inherit from changed |
| 460 | (originalInheritsFromId && originalInheritsFromId !== inheritsFromId) || |
| 461 | // Inherit from added (did not have one initially); |
| 462 | (!originalInheritsFromId && inheritsFromId); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 463 | |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 464 | if (!this._local) { |
| 465 | return addRemoveObj; |
| 466 | } |
| 467 | |
| 468 | this._recursivelyUpdateAddRemoveObj( |
| 469 | (this._local as unknown) as PropertyTreeNode, |
| 470 | addRemoveObj |
| 471 | ); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 472 | |
| 473 | if (inheritFromChanged) { |
| 474 | addRemoveObj.parent = inheritsFromId; |
| 475 | } |
| 476 | return addRemoveObj; |
| 477 | } |
| 478 | |
| 479 | _handleCreateSection() { |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 480 | if (!this._local) { |
| 481 | return; |
| 482 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 483 | let newRef = 'refs/for/*'; |
| 484 | // Avoid using an already used key for the placeholder, since it |
| 485 | // immediately gets added to an object. |
| 486 | while (this._local[newRef]) { |
| 487 | newRef = `${newRef}*`; |
| 488 | } |
| 489 | const section = {permissions: {}, added: true}; |
| 490 | this.push('_sections', {id: newRef, value: section}); |
| 491 | this.set(['_local', newRef], section); |
| 492 | flush(); |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 493 | // Template already instantiated at this point |
| 494 | (this.root!.querySelector( |
| 495 | 'gr-access-section:last-of-type' |
| 496 | ) as GrAccessSection).editReference(); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 497 | } |
| 498 | |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 499 | _getObjforSave(): ProjectAccessInput | undefined { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 500 | const addRemoveObj = this._computeAddAndRemove(); |
| 501 | // If there are no changes, don't actually save. |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 502 | if ( |
| 503 | !Object.keys(addRemoveObj.add).length && |
| 504 | !Object.keys(addRemoveObj.remove).length && |
| 505 | !addRemoveObj.parent |
| 506 | ) { |
Milutin Kristofic | 860fe4d | 2020-11-23 16:13:45 +0100 | [diff] [blame] | 507 | fireAlert(this, NOTHING_TO_SAVE); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 508 | return; |
| 509 | } |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 510 | const obj: ProjectAccessInput = ({ |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 511 | add: addRemoveObj.add, |
| 512 | remove: addRemoveObj.remove, |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 513 | } as unknown) as ProjectAccessInput; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 514 | if (addRemoveObj.parent) { |
| 515 | obj.parent = addRemoveObj.parent; |
| 516 | } |
| 517 | return obj; |
| 518 | } |
| 519 | |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 520 | _handleSave(e: Event) { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 521 | const obj = this._getObjforSave(); |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 522 | if (!obj) { |
| 523 | return; |
| 524 | } |
| 525 | const button = e && (e.target as GrButton); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 526 | if (button) { |
| 527 | button.loading = true; |
| 528 | } |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 529 | const repo = this.repo; |
| 530 | if (!repo) { |
| 531 | return Promise.resolve(); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 532 | } |
Ben Rohlfs | 43935a4 | 2020-12-01 19:14:09 +0100 | [diff] [blame] | 533 | return this.restApiService |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 534 | .setRepoAccessRights(repo, obj) |
| 535 | .then(() => { |
| 536 | this._reload(repo); |
| 537 | }) |
| 538 | .finally(() => { |
| 539 | this._modified = false; |
| 540 | if (button) { |
| 541 | button.loading = false; |
| 542 | } |
| 543 | }); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 544 | } |
| 545 | |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 546 | _handleSaveForReview(e: Event) { |
| 547 | const obj = this._getObjforSave(); |
| 548 | if (!obj) { |
| 549 | return; |
| 550 | } |
| 551 | const button = e && (e.target as GrButton); |
| 552 | if (button) { |
| 553 | button.loading = true; |
| 554 | } |
| 555 | if (!this.repo) { |
| 556 | return; |
| 557 | } |
Ben Rohlfs | 43935a4 | 2020-12-01 19:14:09 +0100 | [diff] [blame] | 558 | return this.restApiService |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 559 | .setRepoAccessRightsForReview(this.repo, obj) |
| 560 | .then(change => { |
| 561 | GerritNav.navigateToChange(change); |
| 562 | }) |
| 563 | .finally(() => { |
| 564 | this._modified = false; |
| 565 | if (button) { |
| 566 | button.loading = false; |
| 567 | } |
| 568 | }); |
| 569 | } |
| 570 | |
| 571 | _computeSaveReviewBtnClass(canUpload?: boolean) { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 572 | return !canUpload ? 'invisible' : ''; |
| 573 | } |
| 574 | |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 575 | _computeSaveBtnClass(ownerOf?: GitRef[]) { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 576 | return ownerOf && ownerOf.length === 0 ? 'invisible' : ''; |
| 577 | } |
| 578 | |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 579 | _computeMainClass( |
| 580 | ownerOf: GitRef[] | undefined, |
| 581 | canUpload: boolean, |
| 582 | editing: boolean |
| 583 | ) { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 584 | const classList = []; |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 585 | if ((ownerOf && ownerOf.length > 0) || canUpload) { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 586 | classList.push('admin'); |
| 587 | } |
| 588 | if (editing) { |
| 589 | classList.push('editing'); |
| 590 | } |
| 591 | return classList.join(' '); |
| 592 | } |
| 593 | |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 594 | _computeParentHref(repoName: RepoName) { |
| 595 | return getBaseUrl() + `/admin/repos/${encodeURL(repoName, true)},access`; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 596 | } |
| 597 | } |
| 598 | |
Dmitrii Filippov | 460685c2 | 2020-08-21 11:12:49 +0200 | [diff] [blame] | 599 | declare global { |
| 600 | interface HTMLElementTagNameMap { |
| 601 | 'gr-repo-access': GrRepoAccess; |
| 602 | } |
| 603 | } |