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