blob: 3a47ed3f52e7aac01e18622a5bec537317f9f411 [file] [log] [blame]
Dmitrii Filippov587086b2019-09-03 16:10:06 +02001/**
2 * @license
3 * Copyright (C) 2019 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 Filippov3592f652020-03-19 16:00:26 +010017import {GrDisplayNameUtils} from '../gr-display-name-utils/gr-display-name-utils.js';
18
Dmitrii Filippov44f47c02020-03-24 13:53:22 +010019/**
20 * @enum {string}
21 */
Dmitrii Filippovd85c72c2020-04-07 13:39:25 +020022export const SUGGESTIONS_PROVIDERS_USERS_TYPES = {
Dmitrii Filippov44f47c02020-03-24 13:53:22 +010023 REVIEWER: 'reviewers',
24 CC: 'ccs',
25 ANY: 'any',
26};
Dmitrii Filippov587086b2019-09-03 16:10:06 +020027
Dmitrii Filippov44f47c02020-03-24 13:53:22 +010028export class GrReviewerSuggestionsProvider {
29 static create(restApi, changeNumber, usersType) {
30 switch (usersType) {
Dmitrii Filippovd85c72c2020-04-07 13:39:25 +020031 case SUGGESTIONS_PROVIDERS_USERS_TYPES.REVIEWER:
Dmitrii Filippov44f47c02020-03-24 13:53:22 +010032 return new GrReviewerSuggestionsProvider(restApi, changeNumber,
33 input => restApi.getChangeSuggestedReviewers(changeNumber,
34 input));
Dmitrii Filippovd85c72c2020-04-07 13:39:25 +020035 case SUGGESTIONS_PROVIDERS_USERS_TYPES.CC:
Dmitrii Filippov44f47c02020-03-24 13:53:22 +010036 return new GrReviewerSuggestionsProvider(restApi, changeNumber,
37 input => restApi.getChangeSuggestedCCs(changeNumber, input));
Dmitrii Filippovd85c72c2020-04-07 13:39:25 +020038 case SUGGESTIONS_PROVIDERS_USERS_TYPES.ANY:
Dmitrii Filippov44f47c02020-03-24 13:53:22 +010039 return new GrReviewerSuggestionsProvider(restApi, changeNumber,
40 input => restApi.getSuggestedAccounts(
41 `cansee:${changeNumber} ${input}`));
42 default:
43 throw new Error(`Unknown users type: ${usersType}`);
44 }
Dmitrii Filippov587086b2019-09-03 16:10:06 +020045 }
46
Dmitrii Filippov44f47c02020-03-24 13:53:22 +010047 constructor(restAPI, changeNumber, apiCall) {
48 this._changeNumber = changeNumber;
49 this._apiCall = apiCall;
50 this._restAPI = restAPI;
51 }
Milutin Kristoficade40e072019-09-15 09:23:03 +020052
Dmitrii Filippov44f47c02020-03-24 13:53:22 +010053 init() {
54 if (this._initPromise) {
Dmitrii Filippov587086b2019-09-03 16:10:06 +020055 return this._initPromise;
56 }
Dmitrii Filippov44f47c02020-03-24 13:53:22 +010057 const getConfigPromise = this._restAPI.getConfig().then(cfg => {
58 this._config = cfg;
59 });
60 const getLoggedInPromise = this._restAPI.getLoggedIn().then(loggedIn => {
61 this._loggedIn = loggedIn;
62 });
63 this._initPromise = Promise.all([getConfigPromise, getLoggedInPromise])
64 .then(() => {
65 this._initialized = true;
66 });
67 return this._initPromise;
Dmitrii Filippov587086b2019-09-03 16:10:06 +020068 }
69
Dmitrii Filippov44f47c02020-03-24 13:53:22 +010070 getSuggestions(input) {
71 if (!this._initialized || !this._loggedIn) {
72 return Promise.resolve([]);
73 }
74
75 return this._apiCall(input)
76 .then(reviewers => (reviewers || []));
77 }
78
79 makeSuggestionItem(suggestion) {
80 if (suggestion.account) {
81 // Reviewer is an account suggestion from getChangeSuggestedReviewers.
82 return {
83 name: GrDisplayNameUtils.getAccountDisplayName(this._config,
84 suggestion.account),
85 value: suggestion,
86 };
87 }
88
89 if (suggestion.group) {
90 // Reviewer is a group suggestion from getChangeSuggestedReviewers.
91 return {
92 name: GrDisplayNameUtils.getGroupDisplayName(suggestion.group),
93 value: suggestion,
94 };
95 }
96
97 if (suggestion._account_id) {
98 // Reviewer is an account suggestion from getSuggestedAccounts.
99 return {
100 name: GrDisplayNameUtils.getAccountDisplayName(this._config,
101 suggestion),
102 value: {account: suggestion, count: 1},
103 };
104 }
105 }
106}