Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | /** |
| 6 | * @fileoverview Issue actions, selectors, and reducers organized into |
| 7 | * a single Redux "Duck" that manages updating and retrieving issue state |
| 8 | * on the frontend. |
| 9 | * |
| 10 | * Reference: https://github.com/erikras/ducks-modular-redux |
| 11 | */ |
| 12 | |
| 13 | import {combineReducers} from 'redux'; |
| 14 | import {createSelector} from 'reselect'; |
| 15 | import {autolink} from 'autolink.js'; |
| 16 | import {fieldTypes, extractTypeForIssue, |
| 17 | fieldValuesToMap} from 'shared/issue-fields.js'; |
| 18 | import {removePrefix, objectToMap} from 'shared/helpers.js'; |
| 19 | import {issueRefToString, issueToIssueRefString, |
| 20 | issueStringToRef, issueNameToRefString} from 'shared/convertersV0.js'; |
| 21 | import {fromShortlink} from 'shared/federated.js'; |
| 22 | import {createReducer, createRequestReducer, |
| 23 | createKeyedRequestReducer} from './redux-helpers.js'; |
| 24 | import * as projectV0 from './projectV0.js'; |
| 25 | import * as userV0 from './userV0.js'; |
| 26 | import {fieldValueMapKey} from 'shared/metadata-helpers.js'; |
| 27 | import {prpcClient} from 'prpc-client-instance.js'; |
| 28 | import loadGapi, {fetchGapiEmail} from 'shared/gapi-loader.js'; |
| 29 | import 'shared/typedef.js'; |
| 30 | |
| 31 | /** @typedef {import('redux').AnyAction} AnyAction */ |
| 32 | |
| 33 | // Actions |
| 34 | export const VIEW_ISSUE = 'VIEW_ISSUE'; |
| 35 | |
| 36 | export const FETCH_START = 'issueV0/FETCH_START'; |
| 37 | export const FETCH_SUCCESS = 'issueV0/FETCH_SUCCESS'; |
| 38 | export const FETCH_FAILURE = 'issueV0/FETCH_FAILURE'; |
| 39 | |
| 40 | export const FETCH_ISSUES_START = 'issueV0/FETCH_ISSUES_START'; |
| 41 | export const FETCH_ISSUES_SUCCESS = 'issueV0/FETCH_ISSUES_SUCCESS'; |
| 42 | export const FETCH_ISSUES_FAILURE = 'issueV0/FETCH_ISSUES_FAILURE'; |
| 43 | |
| 44 | const FETCH_HOTLISTS_START = 'FETCH_HOTLISTS_START'; |
| 45 | export const FETCH_HOTLISTS_SUCCESS = 'FETCH_HOTLISTS_SUCCESS'; |
| 46 | const FETCH_HOTLISTS_FAILURE = 'FETCH_HOTLISTS_FAILURE'; |
| 47 | |
| 48 | const FETCH_ISSUE_LIST_START = 'FETCH_ISSUE_LIST_START'; |
| 49 | export const FETCH_ISSUE_LIST_UPDATE = 'FETCH_ISSUE_LIST_UPDATE'; |
| 50 | const FETCH_ISSUE_LIST_SUCCESS = 'FETCH_ISSUE_LIST_SUCCESS'; |
| 51 | const FETCH_ISSUE_LIST_FAILURE = 'FETCH_ISSUE_LIST_FAILURE'; |
| 52 | |
| 53 | const FETCH_PERMISSIONS_START = 'FETCH_PERMISSIONS_START'; |
| 54 | const FETCH_PERMISSIONS_SUCCESS = 'FETCH_PERMISSIONS_SUCCESS'; |
| 55 | const FETCH_PERMISSIONS_FAILURE = 'FETCH_PERMISSIONS_FAILURE'; |
| 56 | |
| 57 | export const STAR_START = 'STAR_START'; |
| 58 | export const STAR_SUCCESS = 'STAR_SUCCESS'; |
| 59 | const STAR_FAILURE = 'STAR_FAILURE'; |
| 60 | |
| 61 | const PRESUBMIT_START = 'PRESUBMIT_START'; |
| 62 | const PRESUBMIT_SUCCESS = 'PRESUBMIT_SUCCESS'; |
| 63 | const PRESUBMIT_FAILURE = 'PRESUBMIT_FAILURE'; |
| 64 | |
| 65 | export const FETCH_IS_STARRED_START = 'FETCH_IS_STARRED_START'; |
| 66 | export const FETCH_IS_STARRED_SUCCESS = 'FETCH_IS_STARRED_SUCCESS'; |
| 67 | const FETCH_IS_STARRED_FAILURE = 'FETCH_IS_STARRED_FAILURE'; |
| 68 | |
| 69 | const FETCH_ISSUES_STARRED_START = 'FETCH_ISSUES_STARRED_START'; |
| 70 | export const FETCH_ISSUES_STARRED_SUCCESS = 'FETCH_ISSUES_STARRED_SUCCESS'; |
| 71 | const FETCH_ISSUES_STARRED_FAILURE = 'FETCH_ISSUES_STARRED_FAILURE'; |
| 72 | |
| 73 | const FETCH_COMMENTS_START = 'FETCH_COMMENTS_START'; |
| 74 | export const FETCH_COMMENTS_SUCCESS = 'FETCH_COMMENTS_SUCCESS'; |
| 75 | const FETCH_COMMENTS_FAILURE = 'FETCH_COMMENTS_FAILURE'; |
| 76 | |
| 77 | const FETCH_COMMENT_REFERENCES_START = 'FETCH_COMMENT_REFERENCES_START'; |
| 78 | const FETCH_COMMENT_REFERENCES_SUCCESS = 'FETCH_COMMENT_REFERENCES_SUCCESS'; |
| 79 | const FETCH_COMMENT_REFERENCES_FAILURE = 'FETCH_COMMENT_REFERENCES_FAILURE'; |
| 80 | |
| 81 | const FETCH_REFERENCED_USERS_START = 'FETCH_REFERENCED_USERS_START'; |
| 82 | const FETCH_REFERENCED_USERS_SUCCESS = 'FETCH_REFERENCED_USERS_SUCCESS'; |
| 83 | const FETCH_REFERENCED_USERS_FAILURE = 'FETCH_REFERENCED_USERS_FAILURE'; |
| 84 | |
| 85 | const FETCH_RELATED_ISSUES_START = 'FETCH_RELATED_ISSUES_START'; |
| 86 | const FETCH_RELATED_ISSUES_SUCCESS = 'FETCH_RELATED_ISSUES_SUCCESS'; |
| 87 | const FETCH_RELATED_ISSUES_FAILURE = 'FETCH_RELATED_ISSUES_FAILURE'; |
| 88 | |
| 89 | const FETCH_FEDERATED_REFERENCES_START = 'FETCH_FEDERATED_REFERENCES_START'; |
| 90 | const FETCH_FEDERATED_REFERENCES_SUCCESS = 'FETCH_FEDERATED_REFERENCES_SUCCESS'; |
| 91 | const FETCH_FEDERATED_REFERENCES_FAILURE = 'FETCH_FEDERATED_REFERENCES_FAILURE'; |
| 92 | |
| 93 | const CONVERT_START = 'CONVERT_START'; |
| 94 | const CONVERT_SUCCESS = 'CONVERT_SUCCESS'; |
| 95 | const CONVERT_FAILURE = 'CONVERT_FAILURE'; |
| 96 | |
| 97 | const UPDATE_START = 'UPDATE_START'; |
| 98 | const UPDATE_SUCCESS = 'UPDATE_SUCCESS'; |
| 99 | const UPDATE_FAILURE = 'UPDATE_FAILURE'; |
| 100 | |
| 101 | const UPDATE_APPROVAL_START = 'UPDATE_APPROVAL_START'; |
| 102 | const UPDATE_APPROVAL_SUCCESS = 'UPDATE_APPROVAL_SUCCESS'; |
| 103 | const UPDATE_APPROVAL_FAILURE = 'UPDATE_APPROVAL_FAILURE'; |
| 104 | |
| 105 | /* State Shape |
| 106 | { |
| 107 | issuesByRefString: Object<IssueRefString, Issue>, |
| 108 | |
| 109 | viewedIssueRef: IssueRefString, |
| 110 | |
| 111 | hotlists: Array<HotlistV0>, |
| 112 | issueList: { |
| 113 | issueRefs: Array<IssueRefString>, |
| 114 | progress: number, |
| 115 | totalResults: number, |
| 116 | } |
| 117 | comments: Array<IssueComment>, |
| 118 | commentReferences: Object, |
| 119 | relatedIssues: Object, |
| 120 | referencedUsers: Array<UserV0>, |
| 121 | starredIssues: Object<IssueRefString, Boolean>, |
| 122 | permissions: Array<string>, |
| 123 | presubmitResponse: Object, |
| 124 | |
| 125 | requests: { |
| 126 | fetch: ReduxRequestState, |
| 127 | fetchHotlists: ReduxRequestState, |
| 128 | fetchIssueList: ReduxRequestState, |
| 129 | fetchPermissions: ReduxRequestState, |
| 130 | starringIssues: Object<string, ReduxRequestState>, |
| 131 | presubmit: ReduxRequestState, |
| 132 | fetchComments: ReduxRequestState, |
| 133 | fetchCommentReferences: ReduxRequestState, |
| 134 | fetchFederatedReferences: ReduxRequestState, |
| 135 | fetchRelatedIssues: ReduxRequestState, |
| 136 | fetchStarredIssues: ReduxRequestState, |
| 137 | fetchStarredIssues: ReduxRequestState, |
| 138 | convert: ReduxRequestState, |
| 139 | update: ReduxRequestState, |
| 140 | updateApproval: ReduxRequestState, |
| 141 | }, |
| 142 | } |
| 143 | */ |
| 144 | |
| 145 | // Helpers for the reducers. |
| 146 | |
| 147 | /** |
| 148 | * Overrides local data for single approval on an Issue object with fresh data. |
| 149 | * Note that while an Issue can have multiple approvals, this function only |
| 150 | * refreshes data for a single approval. |
| 151 | * @param {Issue} issue Issue Object being updated. |
| 152 | * @param {ApprovalDef} approval A single approval to override in the issue. |
| 153 | * @return {Issue} Issue with updated approval data. |
| 154 | */ |
| 155 | const updateApprovalValues = (issue, approval) => { |
| 156 | if (!issue.approvalValues) return issue; |
| 157 | const newApprovals = issue.approvalValues.map((item) => { |
| 158 | if (item.fieldRef.fieldName === approval.fieldRef.fieldName) { |
| 159 | // PhaseRef isn't populated on the response so we want to make sure |
| 160 | // it doesn't overwrite the original phaseRef with {}. |
| 161 | return {...approval, phaseRef: item.phaseRef}; |
| 162 | } |
| 163 | return item; |
| 164 | }); |
| 165 | return {...issue, approvalValues: newApprovals}; |
| 166 | }; |
| 167 | |
| 168 | // Reducers |
| 169 | |
| 170 | /** |
| 171 | * Creates a new issuesByRefString Object with a single issue's data |
| 172 | * edited. |
| 173 | * @param {Object<IssueRefString, Issue>} issuesByRefString |
| 174 | * @param {Issue} issue The new issue data to add to the state. |
| 175 | * @return {Object<IssueRefString, Issue>} |
| 176 | */ |
| 177 | const updateSingleIssueInState = (issuesByRefString, issue) => { |
| 178 | return { |
| 179 | ...issuesByRefString, |
| 180 | [issueToIssueRefString(issue)]: issue, |
| 181 | }; |
| 182 | }; |
| 183 | |
| 184 | // TODO(crbug.com/monorail/6882): Finish converting all other issue |
| 185 | // actions to use this format. |
| 186 | /** |
| 187 | * Adds issues fetched by a ListIssues request to the Redux store in a |
| 188 | * normalized format. |
| 189 | * @param {Object<IssueRefString, Issue>} state Redux state. |
| 190 | * @param {AnyAction} action |
| 191 | * @param {Array<Issue>} action.issues The list of issues that was fetched. |
| 192 | * @param {Issue=} action.issue The issue being updated. |
| 193 | * @param {number=} action.starCount Number of stars the issue has. This changes |
| 194 | * when a user stars an issue and needs to be updated. |
| 195 | * @param {ApprovalDef=} action.approval A new approval to update the issue |
| 196 | * with. |
| 197 | * @param {IssueRef=} action.issueRef A specific IssueRef to update. |
| 198 | */ |
| 199 | export const issuesByRefStringReducer = createReducer({}, { |
| 200 | [FETCH_ISSUE_LIST_UPDATE]: (state, {issues}) => { |
| 201 | const newState = {...state}; |
| 202 | |
| 203 | issues.forEach((issue) => { |
| 204 | const refString = issueToIssueRefString(issue); |
| 205 | newState[refString] = {...newState[refString], ...issue}; |
| 206 | }); |
| 207 | |
| 208 | return newState; |
| 209 | }, |
| 210 | [FETCH_SUCCESS]: (state, {issue}) => updateSingleIssueInState(state, issue), |
| 211 | [FETCH_ISSUES_SUCCESS]: (state, {issues}) => { |
| 212 | const newState = {...state}; |
| 213 | |
| 214 | issues.forEach((issue) => { |
| 215 | const refString = issueToIssueRefString(issue); |
| 216 | newState[refString] = {...newState[refString], ...issue}; |
| 217 | }); |
| 218 | |
| 219 | return newState; |
| 220 | }, |
| 221 | [CONVERT_SUCCESS]: (state, {issue}) => updateSingleIssueInState(state, issue), |
| 222 | [UPDATE_SUCCESS]: (state, {issue}) => updateSingleIssueInState(state, issue), |
| 223 | [UPDATE_APPROVAL_SUCCESS]: (state, {issueRef, approval}) => { |
| 224 | const issueRefString = issueToIssueRefString(issueRef); |
| 225 | const originalIssue = state[issueRefString] || {}; |
| 226 | const newIssue = updateApprovalValues(originalIssue, approval); |
| 227 | return { |
| 228 | ...state, |
| 229 | [issueRefString]: { |
| 230 | ...newIssue, |
| 231 | }, |
| 232 | }; |
| 233 | }, |
| 234 | [STAR_SUCCESS]: (state, {issueRef, starCount}) => { |
| 235 | const issueRefString = issueToIssueRefString(issueRef); |
| 236 | const originalIssue = state[issueRefString] || {}; |
| 237 | return { |
| 238 | ...state, |
| 239 | [issueRefString]: { |
| 240 | ...originalIssue, |
| 241 | starCount, |
| 242 | }, |
| 243 | }; |
| 244 | }, |
| 245 | }); |
| 246 | |
| 247 | /** |
| 248 | * Sets a reference for the issue that the user is currently viewing. |
| 249 | * @param {IssueRefString} state Currently viewed issue. |
| 250 | * @param {AnyAction} action |
| 251 | * @param {IssueRef} action.issueRef The updated localId to view. |
| 252 | * @return {IssueRefString} |
| 253 | */ |
| 254 | const viewedIssueRefReducer = createReducer('', { |
| 255 | [VIEW_ISSUE]: (state, {issueRef}) => issueRefToString(issueRef) || state, |
| 256 | }); |
| 257 | |
| 258 | /** |
| 259 | * Reducer to manage updating the list of hotlists attached to an Issue. |
| 260 | * @param {Array<HotlistV0>} state List of issue hotlists. |
| 261 | * @param {AnyAction} action |
| 262 | * @param {Array<HotlistV0>} action.hotlists New list of hotlists. |
| 263 | * @return {Array<HotlistV0>} |
| 264 | */ |
| 265 | const hotlistsReducer = createReducer([], { |
| 266 | [FETCH_HOTLISTS_SUCCESS]: (_, {hotlists}) => hotlists, |
| 267 | }); |
| 268 | |
| 269 | /** |
| 270 | * @typedef {Object} IssueListState |
| 271 | * @property {Array<IssueRefString>} issues The list of issues being viewed, |
| 272 | * in a normalized form. |
| 273 | * @property {number} progress The percentage of issues loaded. Used for |
| 274 | * incremental loading of issues in the grid view. |
| 275 | * @property {number} totalResults The total number of issues matching the |
| 276 | * query. |
| 277 | */ |
| 278 | |
| 279 | /** |
| 280 | * Handles the state of the currently viewed issue list. This reducer |
| 281 | * stores this data in normalized form. |
| 282 | * @param {IssueListState} state |
| 283 | * @param {AnyAction} action |
| 284 | * @param {Array<Issue>} action.issues Issues that were fetched. |
| 285 | * @param {number} state.progress New percentage of issues have been loaded. |
| 286 | * @param {number} state.totalResults The total number of issues matching the |
| 287 | * query. |
| 288 | * @return {IssueListState} |
| 289 | */ |
| 290 | export const issueListReducer = createReducer({}, { |
| 291 | [FETCH_ISSUE_LIST_UPDATE]: (_state, {issues, progress, totalResults}) => ({ |
| 292 | issueRefs: issues.map(issueToIssueRefString), progress, totalResults, |
| 293 | }), |
| 294 | }); |
| 295 | |
| 296 | /** |
| 297 | * Updates the comments attached to the currently viewed issue. |
| 298 | * @param {Array<IssueComment>} state The list of comments in an issue. |
| 299 | * @param {AnyAction} action |
| 300 | * @param {Array<IssueComment>} action.comments Fetched comments. |
| 301 | * @return {Array<IssueComment>} |
| 302 | */ |
| 303 | const commentsReducer = createReducer([], { |
| 304 | [FETCH_COMMENTS_SUCCESS]: (_state, {comments}) => comments, |
| 305 | }); |
| 306 | |
| 307 | // TODO(crbug.com/monorail/5953): Come up with some way to refactor |
| 308 | // autolink.js's reference code to allow avoiding duplicate lookups |
| 309 | // with data already in Redux state. |
| 310 | /** |
| 311 | * For autolinking, this reducer stores the dereferenced data for bits |
| 312 | * of data that were referenced in comments. For example, comments might |
| 313 | * include user emails or IDs for other issues, and this state slice would |
| 314 | * store the full Objects for that data. |
| 315 | * @param {Array<CommentReference>} state |
| 316 | * @param {AnyAction} action |
| 317 | * @param {Array<CommentReference>} action.commentReferences New references |
| 318 | * to store. |
| 319 | * @return {Array<CommentReference>} |
| 320 | */ |
| 321 | const commentReferencesReducer = createReducer({}, { |
| 322 | [FETCH_COMMENTS_START]: (_state, _action) => ({}), |
| 323 | [FETCH_COMMENT_REFERENCES_SUCCESS]: (_state, {commentReferences}) => { |
| 324 | return commentReferences; |
| 325 | }, |
| 326 | }); |
| 327 | |
| 328 | /** |
| 329 | * Handles state for related issues such as blocking and blocked on issues, |
| 330 | * including federated references that could reference external issues outside |
| 331 | * Monorail. |
| 332 | * @param {Object<IssueRefString, Issue>} state |
| 333 | * @param {AnyAction} action |
| 334 | * @param {Object<IssueRefString, Issue>=} action.relatedIssues New related |
| 335 | * issues. |
| 336 | * @param {Array<IssueRef>=} action.fedRefIssueRefs List of fetched federated |
| 337 | * issue references. |
| 338 | * @return {Object<IssueRefString, Issue>} |
| 339 | */ |
| 340 | export const relatedIssuesReducer = createReducer({}, { |
| 341 | [FETCH_RELATED_ISSUES_SUCCESS]: (_state, {relatedIssues}) => relatedIssues, |
| 342 | [FETCH_FEDERATED_REFERENCES_SUCCESS]: (state, {fedRefIssueRefs}) => { |
| 343 | if (!fedRefIssueRefs) { |
| 344 | return state; |
| 345 | } |
| 346 | |
| 347 | const fedRefStates = {}; |
| 348 | fedRefIssueRefs.forEach((ref) => { |
| 349 | fedRefStates[ref.extIdentifier] = ref; |
| 350 | }); |
| 351 | |
| 352 | // Return a new object, in Redux fashion. |
| 353 | return Object.assign(fedRefStates, state); |
| 354 | }, |
| 355 | }); |
| 356 | |
| 357 | /** |
| 358 | * Stores data for users referenced by issue. ie: Owner, CC, etc. |
| 359 | * @param {Object<string, UserV0>} state |
| 360 | * @param {AnyAction} action |
| 361 | * @param {Object<string, UserV0>} action.referencedUsers |
| 362 | * @return {Object<string, UserV0>} |
| 363 | */ |
| 364 | const referencedUsersReducer = createReducer({}, { |
| 365 | [FETCH_REFERENCED_USERS_SUCCESS]: (_state, {referencedUsers}) => |
| 366 | referencedUsers, |
| 367 | }); |
| 368 | |
| 369 | /** |
| 370 | * Handles updating state of all starred issues. |
| 371 | * @param {Object<IssueRefString, boolean>} state Set of starred issues, |
| 372 | * stored in a serializeable Object form. |
| 373 | * @param {AnyAction} action |
| 374 | * @param {IssueRef=} action.issueRef An issue with a star state being updated. |
| 375 | * @param {boolean=} action.starred Whether the issue is starred or unstarred. |
| 376 | * @param {Array<IssueRef>=} action.starredIssueRefs A list of starred issues. |
| 377 | * @return {Object<IssueRefString, boolean>} |
| 378 | */ |
| 379 | export const starredIssuesReducer = createReducer({}, { |
| 380 | [STAR_SUCCESS]: (state, {issueRef, starred}) => { |
| 381 | return {...state, [issueRefToString(issueRef)]: starred}; |
| 382 | }, |
| 383 | [FETCH_ISSUES_STARRED_SUCCESS]: (_state, {starredIssueRefs}) => { |
| 384 | const normalizedStars = {}; |
| 385 | starredIssueRefs.forEach((issueRef) => { |
| 386 | normalizedStars[issueRefToString(issueRef)] = true; |
| 387 | }); |
| 388 | return normalizedStars; |
| 389 | }, |
| 390 | [FETCH_IS_STARRED_SUCCESS]: (state, {issueRef, starred}) => { |
| 391 | const refString = issueRefToString(issueRef); |
| 392 | return {...state, [refString]: starred}; |
| 393 | }, |
| 394 | }); |
| 395 | |
| 396 | /** |
| 397 | * Adds the result of an IssuePresubmit response to the Redux store. |
| 398 | * @param {Object} state Initial Redux state. |
| 399 | * @param {AnyAction} action |
| 400 | * @param {Object} action.presubmitResponse The issue |
| 401 | * presubmit response Object. |
| 402 | * @return {Object} |
| 403 | */ |
| 404 | const presubmitResponseReducer = createReducer({}, { |
| 405 | [PRESUBMIT_SUCCESS]: (_state, {presubmitResponse}) => presubmitResponse, |
| 406 | }); |
| 407 | |
| 408 | /** |
| 409 | * Stores the user's permissions for a given issue. |
| 410 | * @param {Array<string>} state Permission list. Each permission is a string |
| 411 | * with the name of the permission. |
| 412 | * @param {AnyAction} action |
| 413 | * @param {Array<string>} action.permissions The fetched permission data. |
| 414 | * @return {Array<string>} |
| 415 | */ |
| 416 | const permissionsReducer = createReducer([], { |
| 417 | [FETCH_PERMISSIONS_SUCCESS]: (_state, {permissions}) => permissions, |
| 418 | }); |
| 419 | |
| 420 | const requestsReducer = combineReducers({ |
| 421 | fetch: createRequestReducer( |
| 422 | FETCH_START, FETCH_SUCCESS, FETCH_FAILURE), |
| 423 | fetchIssues: createRequestReducer( |
| 424 | FETCH_ISSUES_START, FETCH_ISSUES_SUCCESS, FETCH_ISSUES_FAILURE), |
| 425 | fetchHotlists: createRequestReducer( |
| 426 | FETCH_HOTLISTS_START, FETCH_HOTLISTS_SUCCESS, FETCH_HOTLISTS_FAILURE), |
| 427 | fetchIssueList: createRequestReducer( |
| 428 | FETCH_ISSUE_LIST_START, |
| 429 | FETCH_ISSUE_LIST_SUCCESS, |
| 430 | FETCH_ISSUE_LIST_FAILURE), |
| 431 | fetchPermissions: createRequestReducer( |
| 432 | FETCH_PERMISSIONS_START, |
| 433 | FETCH_PERMISSIONS_SUCCESS, |
| 434 | FETCH_PERMISSIONS_FAILURE), |
| 435 | starringIssues: createKeyedRequestReducer( |
| 436 | STAR_START, STAR_SUCCESS, STAR_FAILURE), |
| 437 | presubmit: createRequestReducer( |
| 438 | PRESUBMIT_START, PRESUBMIT_SUCCESS, PRESUBMIT_FAILURE), |
| 439 | fetchComments: createRequestReducer( |
| 440 | FETCH_COMMENTS_START, FETCH_COMMENTS_SUCCESS, FETCH_COMMENTS_FAILURE), |
| 441 | fetchCommentReferences: createRequestReducer( |
| 442 | FETCH_COMMENT_REFERENCES_START, |
| 443 | FETCH_COMMENT_REFERENCES_SUCCESS, |
| 444 | FETCH_COMMENT_REFERENCES_FAILURE), |
| 445 | fetchFederatedReferences: createRequestReducer( |
| 446 | FETCH_FEDERATED_REFERENCES_START, |
| 447 | FETCH_FEDERATED_REFERENCES_SUCCESS, |
| 448 | FETCH_FEDERATED_REFERENCES_FAILURE), |
| 449 | fetchRelatedIssues: createRequestReducer( |
| 450 | FETCH_RELATED_ISSUES_START, |
| 451 | FETCH_RELATED_ISSUES_SUCCESS, |
| 452 | FETCH_RELATED_ISSUES_FAILURE), |
| 453 | fetchReferencedUsers: createRequestReducer( |
| 454 | FETCH_REFERENCED_USERS_START, |
| 455 | FETCH_REFERENCED_USERS_SUCCESS, |
| 456 | FETCH_REFERENCED_USERS_FAILURE), |
| 457 | fetchIsStarred: createRequestReducer( |
| 458 | FETCH_IS_STARRED_START, FETCH_IS_STARRED_SUCCESS, |
| 459 | FETCH_IS_STARRED_FAILURE), |
| 460 | fetchStarredIssues: createRequestReducer( |
| 461 | FETCH_ISSUES_STARRED_START, FETCH_ISSUES_STARRED_SUCCESS, |
| 462 | FETCH_ISSUES_STARRED_FAILURE, |
| 463 | ), |
| 464 | convert: createRequestReducer( |
| 465 | CONVERT_START, CONVERT_SUCCESS, CONVERT_FAILURE), |
| 466 | update: createRequestReducer( |
| 467 | UPDATE_START, UPDATE_SUCCESS, UPDATE_FAILURE), |
| 468 | // TODO(zhangtiff): Update this to use createKeyedRequestReducer() instead, so |
| 469 | // users can update multiple approvals at once. |
| 470 | updateApproval: createRequestReducer( |
| 471 | UPDATE_APPROVAL_START, UPDATE_APPROVAL_SUCCESS, UPDATE_APPROVAL_FAILURE), |
| 472 | }); |
| 473 | |
| 474 | export const reducer = combineReducers({ |
| 475 | viewedIssueRef: viewedIssueRefReducer, |
| 476 | |
| 477 | issuesByRefString: issuesByRefStringReducer, |
| 478 | |
| 479 | hotlists: hotlistsReducer, |
| 480 | issueList: issueListReducer, |
| 481 | comments: commentsReducer, |
| 482 | commentReferences: commentReferencesReducer, |
| 483 | relatedIssues: relatedIssuesReducer, |
| 484 | referencedUsers: referencedUsersReducer, |
| 485 | starredIssues: starredIssuesReducer, |
| 486 | permissions: permissionsReducer, |
| 487 | presubmitResponse: presubmitResponseReducer, |
| 488 | |
| 489 | requests: requestsReducer, |
| 490 | }); |
| 491 | |
| 492 | // Selectors |
| 493 | const RESTRICT_VIEW_PREFIX = 'restrict-view-'; |
| 494 | const RESTRICT_EDIT_PREFIX = 'restrict-editissue-'; |
| 495 | const RESTRICT_COMMENT_PREFIX = 'restrict-addissuecomment-'; |
| 496 | |
| 497 | /** |
| 498 | * Selector to retrieve all normalized Issue data in the Redux store, |
| 499 | * keyed by IssueRefString. |
| 500 | * @param {any} state |
| 501 | * @return {Object<IssueRefString, Issue>} |
| 502 | */ |
| 503 | const issuesByRefString = (state) => state.issue.issuesByRefString; |
| 504 | |
| 505 | /** |
| 506 | * Selector to return a function to retrieve an Issue from the Redux store. |
| 507 | * @param {any} state |
| 508 | * @return {function(string): ?Issue} |
| 509 | */ |
| 510 | export const issue = createSelector(issuesByRefString, (issuesByRefString) => |
| 511 | (name) => issuesByRefString[issueNameToRefString(name)]); |
| 512 | |
| 513 | /** |
| 514 | * Selector to return a function to retrieve a given Issue Object from |
| 515 | * the Redux store. |
| 516 | * @param {any} state |
| 517 | * @return {function(IssueRefString, string): Issue} |
| 518 | */ |
| 519 | export const issueForRefString = createSelector(issuesByRefString, |
| 520 | (issuesByRefString) => (issueRefString, projectName = undefined) => { |
| 521 | // In some contexts, an issue ref string will omit a project name, |
| 522 | // assuming the default project to be the project name. We never |
| 523 | // omit the project name in strings used as keys, so we have to |
| 524 | // make sure issue ref strings contain the project name. |
| 525 | const ref = issueStringToRef(issueRefString, projectName); |
| 526 | const refString = issueRefToString(ref); |
| 527 | if (issuesByRefString.hasOwnProperty(refString)) { |
| 528 | return issuesByRefString[refString]; |
| 529 | } |
| 530 | return issueStringToRef(refString, projectName); |
| 531 | }); |
| 532 | |
| 533 | /** |
| 534 | * Selector to get a reference to the currently viewed issue, in string form. |
| 535 | * @param {any} state |
| 536 | * @return {IssueRefString} |
| 537 | */ |
| 538 | const viewedIssueRefString = (state) => state.issue.viewedIssueRef; |
| 539 | |
| 540 | /** |
| 541 | * Selector to get a reference to the currently viewed issue. |
| 542 | * @param {any} state |
| 543 | * @return {IssueRef} |
| 544 | */ |
| 545 | export const viewedIssueRef = createSelector(viewedIssueRefString, |
| 546 | (viewedIssueRefString) => issueStringToRef(viewedIssueRefString)); |
| 547 | |
| 548 | /** |
| 549 | * Selector to get the full Issue data for the currently viewed issue. |
| 550 | * @param {any} state |
| 551 | * @return {Issue} |
| 552 | */ |
| 553 | export const viewedIssue = createSelector(issuesByRefString, |
| 554 | viewedIssueRefString, |
| 555 | (issuesByRefString, viewedIssueRefString) => |
| 556 | issuesByRefString[viewedIssueRefString] || {}); |
| 557 | |
| 558 | export const comments = (state) => state.issue.comments; |
| 559 | export const commentsLoaded = (state) => state.issue.commentsLoaded; |
| 560 | |
| 561 | const _commentReferences = (state) => state.issue.commentReferences; |
| 562 | export const commentReferences = createSelector(_commentReferences, |
| 563 | (commentReferences) => objectToMap(commentReferences)); |
| 564 | |
| 565 | export const hotlists = (state) => state.issue.hotlists; |
| 566 | |
| 567 | const stateIssueList = (state) => state.issue.issueList; |
| 568 | export const issueList = createSelector( |
| 569 | issuesByRefString, |
| 570 | stateIssueList, |
| 571 | (issuesByRefString, stateIssueList) => { |
| 572 | return (stateIssueList.issueRefs || []).map((issueRef) => { |
| 573 | return issuesByRefString[issueRef]; |
| 574 | }); |
| 575 | }, |
| 576 | ); |
| 577 | export const totalIssues = (state) => state.issue.issueList.totalResults; |
| 578 | export const issueListProgress = (state) => state.issue.issueList.progress; |
| 579 | export const issueListPhaseNames = createSelector(issueList, (issueList) => { |
| 580 | const phaseNamesSet = new Set(); |
| 581 | if (issueList) { |
| 582 | issueList.forEach(({phases}) => { |
| 583 | if (phases) { |
| 584 | phases.forEach(({phaseRef: {phaseName}}) => { |
| 585 | phaseNamesSet.add(phaseName.toLowerCase()); |
| 586 | }); |
| 587 | } |
| 588 | }); |
| 589 | } |
| 590 | return Array.from(phaseNamesSet); |
| 591 | }); |
| 592 | |
| 593 | /** |
| 594 | * @param {any} state |
| 595 | * @return {boolean} Whether the currently viewed issue list |
| 596 | * has loaded. |
| 597 | */ |
| 598 | export const issueListLoaded = createSelector( |
| 599 | stateIssueList, |
| 600 | (stateIssueList) => stateIssueList.issueRefs !== undefined); |
| 601 | |
| 602 | export const permissions = (state) => state.issue.permissions; |
| 603 | export const presubmitResponse = (state) => state.issue.presubmitResponse; |
| 604 | |
| 605 | const _relatedIssues = (state) => state.issue.relatedIssues || {}; |
| 606 | export const relatedIssues = createSelector(_relatedIssues, |
| 607 | (relatedIssues) => objectToMap(relatedIssues)); |
| 608 | |
| 609 | const _referencedUsers = (state) => state.issue.referencedUsers || {}; |
| 610 | export const referencedUsers = createSelector(_referencedUsers, |
| 611 | (referencedUsers) => objectToMap(referencedUsers)); |
| 612 | |
| 613 | export const isStarred = (state) => state.issue.isStarred; |
| 614 | export const _starredIssues = (state) => state.issue.starredIssues; |
| 615 | |
| 616 | export const requests = (state) => state.issue.requests; |
| 617 | |
| 618 | // Returns a Map of in flight StarIssues requests, keyed by issueRef. |
| 619 | export const starringIssues = createSelector(requests, (requests) => |
| 620 | objectToMap(requests.starringIssues)); |
| 621 | |
| 622 | export const starredIssues = createSelector( |
| 623 | _starredIssues, |
| 624 | (starredIssues) => { |
| 625 | const stars = new Set(); |
| 626 | for (const [ref, starred] of Object.entries(starredIssues)) { |
| 627 | if (starred) stars.add(ref); |
| 628 | } |
| 629 | return stars; |
| 630 | }, |
| 631 | ); |
| 632 | |
| 633 | // TODO(zhangtiff): Split up either comments or approvals into their own "duck". |
| 634 | export const commentsByApprovalName = createSelector( |
| 635 | comments, |
| 636 | (comments) => { |
| 637 | const map = new Map(); |
| 638 | comments.forEach((comment) => { |
| 639 | const key = (comment.approvalRef && comment.approvalRef.fieldName) || |
| 640 | ''; |
| 641 | if (map.has(key)) { |
| 642 | map.get(key).push(comment); |
| 643 | } else { |
| 644 | map.set(key, [comment]); |
| 645 | } |
| 646 | }); |
| 647 | return map; |
| 648 | }, |
| 649 | ); |
| 650 | |
| 651 | export const fieldValues = createSelector( |
| 652 | viewedIssue, |
| 653 | (issue) => issue && issue.fieldValues, |
| 654 | ); |
| 655 | |
| 656 | export const labelRefs = createSelector( |
| 657 | viewedIssue, |
| 658 | (issue) => issue && issue.labelRefs, |
| 659 | ); |
| 660 | |
| 661 | export const type = createSelector( |
| 662 | fieldValues, |
| 663 | labelRefs, |
| 664 | (fieldValues, labelRefs) => extractTypeForIssue(fieldValues, labelRefs), |
| 665 | ); |
| 666 | |
| 667 | export const restrictions = createSelector( |
| 668 | labelRefs, |
| 669 | (labelRefs) => { |
| 670 | if (!labelRefs) return {}; |
| 671 | |
| 672 | const restrictions = {}; |
| 673 | |
| 674 | labelRefs.forEach((labelRef) => { |
| 675 | const label = labelRef.label; |
| 676 | const lowerCaseLabel = label.toLowerCase(); |
| 677 | |
| 678 | if (lowerCaseLabel.startsWith(RESTRICT_VIEW_PREFIX)) { |
| 679 | const permissionType = removePrefix(label, RESTRICT_VIEW_PREFIX); |
| 680 | if (!('view' in restrictions)) { |
| 681 | restrictions['view'] = [permissionType]; |
| 682 | } else { |
| 683 | restrictions['view'].push(permissionType); |
| 684 | } |
| 685 | } else if (lowerCaseLabel.startsWith(RESTRICT_EDIT_PREFIX)) { |
| 686 | const permissionType = removePrefix(label, RESTRICT_EDIT_PREFIX); |
| 687 | if (!('edit' in restrictions)) { |
| 688 | restrictions['edit'] = [permissionType]; |
| 689 | } else { |
| 690 | restrictions['edit'].push(permissionType); |
| 691 | } |
| 692 | } else if (lowerCaseLabel.startsWith(RESTRICT_COMMENT_PREFIX)) { |
| 693 | const permissionType = removePrefix(label, RESTRICT_COMMENT_PREFIX); |
| 694 | if (!('comment' in restrictions)) { |
| 695 | restrictions['comment'] = [permissionType]; |
| 696 | } else { |
| 697 | restrictions['comment'].push(permissionType); |
| 698 | } |
| 699 | } |
| 700 | }); |
| 701 | |
| 702 | return restrictions; |
| 703 | }, |
| 704 | ); |
| 705 | |
| 706 | export const isOpen = createSelector( |
| 707 | viewedIssue, |
| 708 | (issue) => issue && issue.statusRef && issue.statusRef.meansOpen || false); |
| 709 | |
| 710 | // Returns a function that, given an issue and its related issues, |
| 711 | // returns a combined list of issue ref strings including related issues, |
| 712 | // blocking or blocked on issues, and federated references. |
| 713 | const mapRefsWithRelated = (blocking) => { |
| 714 | return (issue, relatedIssues) => { |
| 715 | let refs = []; |
| 716 | if (blocking) { |
| 717 | if (issue.blockingIssueRefs) { |
| 718 | refs = refs.concat(issue.blockingIssueRefs); |
| 719 | } |
| 720 | if (issue.danglingBlockingRefs) { |
| 721 | refs = refs.concat(issue.danglingBlockingRefs); |
| 722 | } |
| 723 | } else { |
| 724 | if (issue.blockedOnIssueRefs) { |
| 725 | refs = refs.concat(issue.blockedOnIssueRefs); |
| 726 | } |
| 727 | if (issue.danglingBlockedOnRefs) { |
| 728 | refs = refs.concat(issue.danglingBlockedOnRefs); |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | // Note: relatedIssues is a Redux generated key for issues, not part of the |
| 733 | // pRPC Issue object. |
| 734 | if (issue.relatedIssues) { |
| 735 | refs = refs.concat(issue.relatedIssues); |
| 736 | } |
| 737 | return refs.map((ref) => { |
| 738 | const key = issueRefToString(ref); |
| 739 | if (relatedIssues.has(key)) { |
| 740 | return relatedIssues.get(key); |
| 741 | } |
| 742 | return ref; |
| 743 | }); |
| 744 | }; |
| 745 | }; |
| 746 | |
| 747 | export const blockingIssues = createSelector( |
| 748 | viewedIssue, relatedIssues, |
| 749 | mapRefsWithRelated(true), |
| 750 | ); |
| 751 | |
| 752 | export const blockedOnIssues = createSelector( |
| 753 | viewedIssue, relatedIssues, |
| 754 | mapRefsWithRelated(false), |
| 755 | ); |
| 756 | |
| 757 | export const mergedInto = createSelector( |
| 758 | viewedIssue, relatedIssues, |
| 759 | (issue, relatedIssues) => { |
| 760 | if (!issue || !issue.mergedIntoIssueRef) return {}; |
| 761 | const key = issueRefToString(issue.mergedIntoIssueRef); |
| 762 | if (relatedIssues && relatedIssues.has(key)) { |
| 763 | return relatedIssues.get(key); |
| 764 | } |
| 765 | return issue.mergedIntoIssueRef; |
| 766 | }, |
| 767 | ); |
| 768 | |
| 769 | export const sortedBlockedOn = createSelector( |
| 770 | blockedOnIssues, |
| 771 | (blockedOn) => blockedOn.sort((a, b) => { |
| 772 | const aIsOpen = a.statusRef && a.statusRef.meansOpen ? 1 : 0; |
| 773 | const bIsOpen = b.statusRef && b.statusRef.meansOpen ? 1 : 0; |
| 774 | return bIsOpen - aIsOpen; |
| 775 | }), |
| 776 | ); |
| 777 | |
| 778 | // values (from issue.fieldValues) is an array with one entry per value. |
| 779 | // We want to turn this into a map of fieldNames -> values. |
| 780 | export const fieldValueMap = createSelector( |
| 781 | fieldValues, |
| 782 | (fieldValues) => fieldValuesToMap(fieldValues), |
| 783 | ); |
| 784 | |
| 785 | // Get the list of full componentDefs for the viewed issue. |
| 786 | export const components = createSelector( |
| 787 | viewedIssue, |
| 788 | projectV0.componentsMap, |
| 789 | (issue, components) => { |
| 790 | if (!issue || !issue.componentRefs) return []; |
| 791 | return issue.componentRefs.map( |
| 792 | (comp) => components.get(comp.path) || comp); |
| 793 | }, |
| 794 | ); |
| 795 | |
| 796 | // Get custom fields that apply to a specific issue. |
| 797 | export const fieldDefs = createSelector( |
| 798 | projectV0.fieldDefs, |
| 799 | type, |
| 800 | fieldValueMap, |
| 801 | (fieldDefs, type, fieldValues) => { |
| 802 | if (!fieldDefs) return []; |
| 803 | type = type || ''; |
| 804 | return fieldDefs.filter((f) => { |
| 805 | const fieldValueKey = fieldValueMapKey(f.fieldRef.fieldName, |
| 806 | f.phaseRef && f.phaseRef.phaseName); |
| 807 | if (fieldValues && fieldValues.has(fieldValueKey)) { |
| 808 | // Regardless of other checks, include a particular field def if the |
| 809 | // issue has a value defined. |
| 810 | return true; |
| 811 | } |
| 812 | // Skip approval type and phase fields here. |
| 813 | if (f.fieldRef.approvalName || |
| 814 | f.fieldRef.type === fieldTypes.APPROVAL_TYPE || |
| 815 | f.isPhaseField) { |
| 816 | return false; |
| 817 | } |
| 818 | |
| 819 | // If this fieldDef belongs to only one type, filter out the field if |
| 820 | // that type isn't the specified type. |
| 821 | if (f.applicableType && type.toLowerCase() !== |
| 822 | f.applicableType.toLowerCase()) { |
| 823 | return false; |
| 824 | } |
| 825 | |
| 826 | return true; |
| 827 | }); |
| 828 | }, |
| 829 | ); |
| 830 | |
| 831 | // Action Creators |
| 832 | /** |
| 833 | * Tells Redux that the user has navigated to an issue page and is now |
| 834 | * viewing a new issue. |
| 835 | * @param {IssueRef} issueRef The issue that the user is viewing. |
| 836 | * @return {AnyAction} |
| 837 | */ |
| 838 | export const viewIssue = (issueRef) => ({type: VIEW_ISSUE, issueRef}); |
| 839 | |
| 840 | export const fetchCommentReferences = (comments, projectName) => { |
| 841 | return async (dispatch) => { |
| 842 | dispatch({type: FETCH_COMMENT_REFERENCES_START}); |
| 843 | |
| 844 | try { |
| 845 | const refs = await autolink.getReferencedArtifacts(comments, projectName); |
| 846 | const commentRefs = {}; |
| 847 | refs.forEach(({componentName, existingRefs}) => { |
| 848 | commentRefs[componentName] = existingRefs; |
| 849 | }); |
| 850 | dispatch({ |
| 851 | type: FETCH_COMMENT_REFERENCES_SUCCESS, |
| 852 | commentReferences: commentRefs, |
| 853 | }); |
| 854 | } catch (error) { |
| 855 | dispatch({type: FETCH_COMMENT_REFERENCES_FAILURE, error}); |
| 856 | } |
| 857 | }; |
| 858 | }; |
| 859 | |
| 860 | export const fetchReferencedUsers = (issue) => async (dispatch) => { |
| 861 | if (!issue) return; |
| 862 | dispatch({type: FETCH_REFERENCED_USERS_START}); |
| 863 | |
| 864 | // TODO(zhangtiff): Make this function account for custom fields |
| 865 | // of type user. |
| 866 | const userRefs = [...(issue.ccRefs || [])]; |
| 867 | if (issue.ownerRef) { |
| 868 | userRefs.push(issue.ownerRef); |
| 869 | } |
| 870 | (issue.approvalValues || []).forEach((approval) => { |
| 871 | userRefs.push(...(approval.approverRefs || [])); |
| 872 | if (approval.setterRef) { |
| 873 | userRefs.push(approval.setterRef); |
| 874 | } |
| 875 | }); |
| 876 | |
| 877 | try { |
| 878 | const resp = await prpcClient.call( |
| 879 | 'monorail.Users', 'ListReferencedUsers', {userRefs}); |
| 880 | |
| 881 | const referencedUsers = {}; |
| 882 | (resp.users || []).forEach((user) => { |
| 883 | referencedUsers[user.displayName] = user; |
| 884 | }); |
| 885 | dispatch({type: FETCH_REFERENCED_USERS_SUCCESS, referencedUsers}); |
| 886 | } catch (error) { |
| 887 | dispatch({type: FETCH_REFERENCED_USERS_FAILURE, error}); |
| 888 | } |
| 889 | }; |
| 890 | |
| 891 | export const fetchFederatedReferences = (issue) => async (dispatch) => { |
| 892 | dispatch({type: FETCH_FEDERATED_REFERENCES_START}); |
| 893 | |
| 894 | // Concat all potential fedrefs together, convert from shortlink to classes, |
| 895 | // then fire off a request to fetch the status of each. |
| 896 | const fedRefs = [] |
| 897 | .concat(issue.danglingBlockingRefs || []) |
| 898 | .concat(issue.danglingBlockedOnRefs || []) |
| 899 | .concat(issue.mergedIntoIssueRef ? [issue.mergedIntoIssueRef] : []) |
| 900 | .filter((ref) => ref && ref.extIdentifier) |
| 901 | .map((ref) => fromShortlink(ref.extIdentifier)) |
| 902 | .filter((fedRef) => fedRef); |
| 903 | |
| 904 | // If no FedRefs, return empty Map. |
| 905 | if (fedRefs.length === 0) { |
| 906 | return; |
| 907 | } |
| 908 | |
| 909 | try { |
| 910 | // Load email separately since it might have changed. |
| 911 | await loadGapi(); |
| 912 | const email = await fetchGapiEmail(); |
| 913 | |
| 914 | // If already logged in, dispatch login success event. |
| 915 | dispatch({ |
| 916 | type: userV0.GAPI_LOGIN_SUCCESS, |
| 917 | email: email, |
| 918 | }); |
| 919 | |
| 920 | await Promise.all(fedRefs.map((fedRef) => fedRef.getFederatedDetails())); |
| 921 | const fedRefIssueRefs = fedRefs.map((fedRef) => fedRef.toIssueRef()); |
| 922 | |
| 923 | dispatch({ |
| 924 | type: FETCH_FEDERATED_REFERENCES_SUCCESS, |
| 925 | fedRefIssueRefs: fedRefIssueRefs, |
| 926 | }); |
| 927 | } catch (error) { |
| 928 | dispatch({type: FETCH_FEDERATED_REFERENCES_FAILURE, error}); |
| 929 | } |
| 930 | }; |
| 931 | |
| 932 | // TODO(zhangtiff): Figure out if we can reduce request/response sizes by |
| 933 | // diffing issues to fetch against issues we already know about to avoid |
| 934 | // fetching duplicate info. |
| 935 | export const fetchRelatedIssues = (issue) => async (dispatch) => { |
| 936 | if (!issue) return; |
| 937 | dispatch({type: FETCH_RELATED_ISSUES_START}); |
| 938 | |
| 939 | const refsToFetch = (issue.blockedOnIssueRefs || []).concat( |
| 940 | issue.blockingIssueRefs || []); |
| 941 | // Add mergedinto ref, exclude FedRefs which are fetched separately. |
| 942 | if (issue.mergedIntoIssueRef && !issue.mergedIntoIssueRef.extIdentifier) { |
| 943 | refsToFetch.push(issue.mergedIntoIssueRef); |
| 944 | } |
| 945 | |
| 946 | const message = { |
| 947 | issueRefs: refsToFetch, |
| 948 | }; |
| 949 | try { |
| 950 | // Fire off call to fetch FedRefs. Since it might take longer it is |
| 951 | // handled by a separate reducer. |
| 952 | dispatch(fetchFederatedReferences(issue)); |
| 953 | |
| 954 | const resp = await prpcClient.call( |
| 955 | 'monorail.Issues', 'ListReferencedIssues', message); |
| 956 | |
| 957 | const relatedIssues = {}; |
| 958 | |
| 959 | const openIssues = resp.openRefs || []; |
| 960 | const closedIssues = resp.closedRefs || []; |
| 961 | openIssues.forEach((issue) => { |
| 962 | issue.statusRef.meansOpen = true; |
| 963 | relatedIssues[issueRefToString(issue)] = issue; |
| 964 | }); |
| 965 | closedIssues.forEach((issue) => { |
| 966 | issue.statusRef.meansOpen = false; |
| 967 | relatedIssues[issueRefToString(issue)] = issue; |
| 968 | }); |
| 969 | dispatch({ |
| 970 | type: FETCH_RELATED_ISSUES_SUCCESS, |
| 971 | relatedIssues: relatedIssues, |
| 972 | }); |
| 973 | } catch (error) { |
| 974 | dispatch({type: FETCH_RELATED_ISSUES_FAILURE, error}); |
| 975 | }; |
| 976 | }; |
| 977 | |
| 978 | /** |
| 979 | * Fetches issue data needed to display a detailed view of a single |
| 980 | * issue. This function dispatches many actions to handle the fetching |
| 981 | * of issue comments, permissions, star state, and more. |
| 982 | * @param {IssueRef} issueRef The issue that the user is viewing. |
| 983 | * @return {function(function): Promise<void>} |
| 984 | */ |
| 985 | export const fetchIssuePageData = (issueRef) => async (dispatch) => { |
| 986 | dispatch(fetchComments(issueRef)); |
| 987 | dispatch(fetch(issueRef)); |
| 988 | dispatch(fetchPermissions(issueRef)); |
| 989 | dispatch(fetchIsStarred(issueRef)); |
| 990 | }; |
| 991 | |
| 992 | /** |
| 993 | * @param {IssueRef} issueRef Which issue to fetch. |
| 994 | * @return {function(function): Promise<void>} |
| 995 | */ |
| 996 | export const fetch = (issueRef) => async (dispatch) => { |
| 997 | dispatch({type: FETCH_START}); |
| 998 | |
| 999 | try { |
| 1000 | const resp = await prpcClient.call( |
| 1001 | 'monorail.Issues', 'GetIssue', {issueRef}, |
| 1002 | ); |
| 1003 | |
| 1004 | const movedToRef = resp.movedToRef; |
| 1005 | |
| 1006 | // The API can return deleted issue objects that don't have issueRef data |
| 1007 | // specified. For this case, we want to make sure a projectName and localId |
| 1008 | // are still provided to the frontend to ensure that keying issues still |
| 1009 | // works. |
| 1010 | const issue = {...issueRef, ...resp.issue}; |
| 1011 | if (movedToRef) { |
| 1012 | issue.movedToRef = movedToRef; |
| 1013 | } |
| 1014 | |
| 1015 | dispatch({type: FETCH_SUCCESS, issue}); |
| 1016 | |
| 1017 | if (!issue.isDeleted && !movedToRef) { |
| 1018 | dispatch(fetchRelatedIssues(issue)); |
| 1019 | dispatch(fetchHotlists(issueRef)); |
| 1020 | dispatch(fetchReferencedUsers(issue)); |
| 1021 | dispatch(userV0.fetchProjects([issue.reporterRef])); |
| 1022 | } |
| 1023 | } catch (error) { |
| 1024 | dispatch({type: FETCH_FAILURE, error}); |
| 1025 | } |
| 1026 | }; |
| 1027 | |
| 1028 | /** |
| 1029 | * Action creator to fetch multiple Issues. |
| 1030 | * @param {Array<IssueRef>} issueRefs An Array of Issue references to fetch. |
| 1031 | * @return {function(function): Promise<void>} |
| 1032 | */ |
| 1033 | export const fetchIssues = (issueRefs) => async (dispatch) => { |
| 1034 | dispatch({type: FETCH_ISSUES_START}); |
| 1035 | |
| 1036 | try { |
| 1037 | const {openRefs, closedRefs} = await prpcClient.call( |
| 1038 | 'monorail.Issues', 'ListReferencedIssues', {issueRefs}); |
| 1039 | const issues = [...openRefs || [], ...closedRefs || []]; |
| 1040 | |
| 1041 | dispatch({type: FETCH_ISSUES_SUCCESS, issues}); |
| 1042 | } catch (error) { |
| 1043 | dispatch({type: FETCH_ISSUES_FAILURE, error}); |
| 1044 | } |
| 1045 | }; |
| 1046 | |
| 1047 | /** |
| 1048 | * Gets the hotlists that a given issue is in. |
| 1049 | * @param {IssueRef} issueRef |
| 1050 | * @return {function(function): Promise<void>} |
| 1051 | */ |
| 1052 | export const fetchHotlists = (issueRef) => async (dispatch) => { |
| 1053 | dispatch({type: FETCH_HOTLISTS_START}); |
| 1054 | |
| 1055 | try { |
| 1056 | const resp = await prpcClient.call( |
| 1057 | 'monorail.Features', 'ListHotlistsByIssue', {issue: issueRef}); |
| 1058 | |
| 1059 | const hotlists = (resp.hotlists || []); |
| 1060 | hotlists.sort((hotlistA, hotlistB) => { |
| 1061 | return hotlistA.name.localeCompare(hotlistB.name); |
| 1062 | }); |
| 1063 | dispatch({type: FETCH_HOTLISTS_SUCCESS, hotlists}); |
| 1064 | } catch (error) { |
| 1065 | dispatch({type: FETCH_HOTLISTS_FAILURE, error}); |
| 1066 | }; |
| 1067 | }; |
| 1068 | |
| 1069 | /** |
| 1070 | * Async action creator to fetch issues in the issue list and grid pages. This |
| 1071 | * action creator supports batching multiple async requests to support the grid |
| 1072 | * view's ability to load up to 6,000 issues in one page load. |
| 1073 | * |
| 1074 | * @param {string} projectName The project to fetch issues from. |
| 1075 | * @param {Object} params Options for which issues to fetch. |
| 1076 | * @param {string=} params.q The query string for the search. |
| 1077 | * @param {string=} params.can The ID of the canned query for the search. |
| 1078 | * @param {string=} params.groupby The spec of which fields to group by. |
| 1079 | * @param {string=} params.sort The spec of which fields to sort by. |
| 1080 | * @param {number=} params.start What cursor index to start at. |
| 1081 | * @param {number=} params.maxItems How many items to fetch per page. |
| 1082 | * @param {number=} params.maxCalls The maximum number of API calls to make. |
| 1083 | * Combined with pagination.maxItems, this defines the maximum number of |
| 1084 | * issues this method can fetch. |
| 1085 | * @return {function(function): Promise<void>} |
| 1086 | */ |
| 1087 | export const fetchIssueList = |
| 1088 | (projectName, {q = undefined, can = undefined, groupby = undefined, |
| 1089 | sort = undefined, start = undefined, maxItems = undefined, |
| 1090 | maxCalls = 1, |
| 1091 | }) => async (dispatch) => { |
| 1092 | let updateData = {}; |
| 1093 | const promises = []; |
| 1094 | const issuesByRequest = []; |
| 1095 | let issueLimit; |
| 1096 | let totalIssues; |
| 1097 | let totalCalls; |
| 1098 | const itemsPerCall = maxItems || 1000; |
| 1099 | |
| 1100 | const cannedQuery = Number.parseInt(can) || undefined; |
| 1101 | |
| 1102 | const pagination = { |
| 1103 | ...(start && {start}), |
| 1104 | ...(maxItems && {maxItems}), |
| 1105 | }; |
| 1106 | |
| 1107 | const message = { |
| 1108 | projectNames: [projectName], |
| 1109 | query: q, |
| 1110 | cannedQuery, |
| 1111 | groupBySpec: groupby, |
| 1112 | sortSpec: sort, |
| 1113 | pagination, |
| 1114 | }; |
| 1115 | |
| 1116 | dispatch({type: FETCH_ISSUE_LIST_START}); |
| 1117 | |
| 1118 | // initial api call made to determine total number of issues matching |
| 1119 | // the query. |
| 1120 | try { |
| 1121 | // TODO(zhangtiff): Refactor this action creator when adding issue |
| 1122 | // list pagination. |
| 1123 | const resp = await prpcClient.call( |
| 1124 | 'monorail.Issues', 'ListIssues', message); |
| 1125 | |
| 1126 | // prpcClient is not actually a protobuf client and therefore not |
| 1127 | // hydrating default values. See crbug.com/monorail/6641 |
| 1128 | // Until that is fixed, we have to explicitly define it. |
| 1129 | const defaultFetchListResponse = {totalResults: 0, issues: []}; |
| 1130 | |
| 1131 | updateData = |
| 1132 | Object.entries(resp).length === 0 ? |
| 1133 | defaultFetchListResponse : |
| 1134 | resp; |
| 1135 | issuesByRequest[0] = updateData.issues; |
| 1136 | issueLimit = updateData.totalResults; |
| 1137 | |
| 1138 | // determine correct issues to load and number of calls to be made. |
| 1139 | if (issueLimit > (itemsPerCall * maxCalls)) { |
| 1140 | totalIssues = itemsPerCall * maxCalls; |
| 1141 | totalCalls = maxCalls - 1; |
| 1142 | } else { |
| 1143 | totalIssues = issueLimit; |
| 1144 | totalCalls = Math.ceil(issueLimit / itemsPerCall) - 1; |
| 1145 | } |
| 1146 | |
| 1147 | if (totalIssues) { |
| 1148 | updateData.progress = updateData.issues.length / totalIssues; |
| 1149 | } else { |
| 1150 | updateData.progress = 1; |
| 1151 | } |
| 1152 | |
| 1153 | dispatch({type: FETCH_ISSUE_LIST_UPDATE, ...updateData}); |
| 1154 | |
| 1155 | // remaining api calls are made. |
| 1156 | for (let i = 1; i <= totalCalls; i++) { |
| 1157 | promises[i - 1] = (async () => { |
| 1158 | const resp = await prpcClient.call( |
| 1159 | 'monorail.Issues', 'ListIssues', { |
| 1160 | ...message, |
| 1161 | pagination: {start: i * itemsPerCall, maxItems: itemsPerCall}, |
| 1162 | }); |
| 1163 | issuesByRequest[i] = (resp.issues || []); |
| 1164 | // sort the issues in the correct order. |
| 1165 | updateData.issues = []; |
| 1166 | issuesByRequest.forEach((issue) => { |
| 1167 | updateData.issues = updateData.issues.concat(issue); |
| 1168 | }); |
| 1169 | updateData.progress = updateData.issues.length / totalIssues; |
| 1170 | dispatch({type: FETCH_ISSUE_LIST_UPDATE, ...updateData}); |
| 1171 | })(); |
| 1172 | } |
| 1173 | |
| 1174 | await Promise.all(promises); |
| 1175 | |
| 1176 | // TODO(zhangtiff): Try to delete FETCH_ISSUE_LIST_SUCCESS in favor of |
| 1177 | // just FETCH_ISSUE_LIST_UPDATE. |
| 1178 | dispatch({type: FETCH_ISSUE_LIST_SUCCESS}); |
| 1179 | } catch (error) { |
| 1180 | dispatch({type: FETCH_ISSUE_LIST_FAILURE, error}); |
| 1181 | }; |
| 1182 | }; |
| 1183 | |
| 1184 | /** |
| 1185 | * Fetches the currently logged in user's permissions for a given issue. |
| 1186 | * @param {Issue} issueRef |
| 1187 | * @return {function(function): Promise<void>} |
| 1188 | */ |
| 1189 | export const fetchPermissions = (issueRef) => async (dispatch) => { |
| 1190 | dispatch({type: FETCH_PERMISSIONS_START}); |
| 1191 | |
| 1192 | try { |
| 1193 | const resp = await prpcClient.call( |
| 1194 | 'monorail.Issues', 'ListIssuePermissions', {issueRef}, |
| 1195 | ); |
| 1196 | |
| 1197 | dispatch({type: FETCH_PERMISSIONS_SUCCESS, permissions: resp.permissions}); |
| 1198 | } catch (error) { |
| 1199 | dispatch({type: FETCH_PERMISSIONS_FAILURE, error}); |
| 1200 | }; |
| 1201 | }; |
| 1202 | |
| 1203 | /** |
| 1204 | * Fetches comments for an issue. Note that issue descriptions are also |
| 1205 | * comments. |
| 1206 | * @param {IssueRef} issueRef |
| 1207 | * @return {function(function): Promise<void>} |
| 1208 | */ |
| 1209 | export const fetchComments = (issueRef) => async (dispatch) => { |
| 1210 | dispatch({type: FETCH_COMMENTS_START}); |
| 1211 | |
| 1212 | try { |
| 1213 | const resp = await prpcClient.call( |
| 1214 | 'monorail.Issues', 'ListComments', {issueRef}); |
| 1215 | |
| 1216 | dispatch({type: FETCH_COMMENTS_SUCCESS, comments: resp.comments}); |
| 1217 | dispatch(fetchCommentReferences( |
| 1218 | resp.comments, issueRef.projectName)); |
| 1219 | |
| 1220 | const commenterRefs = (resp.comments || []).map( |
| 1221 | (comment) => comment.commenter); |
| 1222 | dispatch(userV0.fetchProjects(commenterRefs)); |
| 1223 | } catch (error) { |
| 1224 | dispatch({type: FETCH_COMMENTS_FAILURE, error}); |
| 1225 | }; |
| 1226 | }; |
| 1227 | |
| 1228 | /** |
| 1229 | * Gets whether the logged in user has starred a given issue. |
| 1230 | * @param {IssueRef} issueRef |
| 1231 | * @return {function(function): Promise<void>} |
| 1232 | */ |
| 1233 | export const fetchIsStarred = (issueRef) => async (dispatch) => { |
| 1234 | dispatch({type: FETCH_IS_STARRED_START}); |
| 1235 | try { |
| 1236 | const resp = await prpcClient.call( |
| 1237 | 'monorail.Issues', 'IsIssueStarred', {issueRef}, |
| 1238 | ); |
| 1239 | |
| 1240 | dispatch({ |
| 1241 | type: FETCH_IS_STARRED_SUCCESS, |
| 1242 | starred: resp.isStarred, |
| 1243 | issueRef: issueRef, |
| 1244 | }); |
| 1245 | } catch (error) { |
| 1246 | dispatch({type: FETCH_IS_STARRED_FAILURE, error}); |
| 1247 | }; |
| 1248 | }; |
| 1249 | |
| 1250 | /** |
| 1251 | * Fetch all of a logged in user's starred issues. |
| 1252 | * @return {function(function): Promise<void>} |
| 1253 | */ |
| 1254 | export const fetchStarredIssues = () => async (dispatch) => { |
| 1255 | dispatch({type: FETCH_ISSUES_STARRED_START}); |
| 1256 | |
| 1257 | try { |
| 1258 | const resp = await prpcClient.call( |
| 1259 | 'monorail.Issues', 'ListStarredIssues', {}, |
| 1260 | ); |
| 1261 | dispatch({type: FETCH_ISSUES_STARRED_SUCCESS, |
| 1262 | starredIssueRefs: resp.starredIssueRefs}); |
| 1263 | } catch (error) { |
| 1264 | dispatch({type: FETCH_ISSUES_STARRED_FAILURE, error}); |
| 1265 | }; |
| 1266 | }; |
| 1267 | |
| 1268 | /** |
| 1269 | * Stars or unstars an issue. |
| 1270 | * @param {IssueRef} issueRef The issue to star. |
| 1271 | * @param {boolean} starred Whether to star or unstar. |
| 1272 | * @return {function(function): Promise<void>} |
| 1273 | */ |
| 1274 | export const star = (issueRef, starred) => async (dispatch) => { |
| 1275 | const requestKey = issueRefToString(issueRef); |
| 1276 | |
| 1277 | dispatch({type: STAR_START, requestKey}); |
| 1278 | const message = {issueRef, starred}; |
| 1279 | |
| 1280 | try { |
| 1281 | const resp = await prpcClient.call( |
| 1282 | 'monorail.Issues', 'StarIssue', message, |
| 1283 | ); |
| 1284 | |
| 1285 | dispatch({ |
| 1286 | type: STAR_SUCCESS, |
| 1287 | starCount: resp.starCount, |
| 1288 | issueRef, |
| 1289 | starred, |
| 1290 | requestKey, |
| 1291 | }); |
| 1292 | } catch (error) { |
| 1293 | dispatch({type: STAR_FAILURE, error, requestKey}); |
| 1294 | } |
| 1295 | }; |
| 1296 | |
| 1297 | /** |
| 1298 | * Runs a presubmit request to find warnings to show the user before an issue |
| 1299 | * edit is saved. |
| 1300 | * @param {IssueRef} issueRef The issue being edited. |
| 1301 | * @param {IssueDelta} issueDelta The user's in flight changes to the issue. |
| 1302 | * @return {function(function): Promise<void>} |
| 1303 | */ |
| 1304 | export const presubmit = (issueRef, issueDelta) => async (dispatch) => { |
| 1305 | dispatch({type: PRESUBMIT_START}); |
| 1306 | |
| 1307 | try { |
| 1308 | const resp = await prpcClient.call( |
| 1309 | 'monorail.Issues', 'PresubmitIssue', {issueRef, issueDelta}); |
| 1310 | |
| 1311 | dispatch({type: PRESUBMIT_SUCCESS, presubmitResponse: resp}); |
| 1312 | } catch (error) { |
| 1313 | dispatch({type: PRESUBMIT_FAILURE, error: error}); |
| 1314 | } |
| 1315 | }; |
| 1316 | |
| 1317 | /** |
| 1318 | * Async action creator to update an issue's approval. |
| 1319 | * |
| 1320 | * @param {Object} params Options for the approval update. |
| 1321 | * @param {IssueRef} params.issueRef |
| 1322 | * @param {FieldRef} params.fieldRef |
| 1323 | * @param {ApprovalDelta=} params.approvalDelta |
| 1324 | * @param {string=} params.commentContent |
| 1325 | * @param {boolean=} params.sendEmail |
| 1326 | * @param {boolean=} params.isDescription |
| 1327 | * @param {AttachmentUpload=} params.uploads |
| 1328 | * @return {function(function): Promise<void>} |
| 1329 | */ |
| 1330 | export const updateApproval = ({issueRef, fieldRef, approvalDelta, |
| 1331 | commentContent, sendEmail, isDescription, uploads}) => async (dispatch) => { |
| 1332 | dispatch({type: UPDATE_APPROVAL_START}); |
| 1333 | try { |
| 1334 | const {approval} = await prpcClient.call( |
| 1335 | 'monorail.Issues', 'UpdateApproval', { |
| 1336 | ...(issueRef && {issueRef}), |
| 1337 | ...(fieldRef && {fieldRef}), |
| 1338 | ...(approvalDelta && {approvalDelta}), |
| 1339 | ...(commentContent && {commentContent}), |
| 1340 | ...(sendEmail && {sendEmail}), |
| 1341 | ...(isDescription && {isDescription}), |
| 1342 | ...(uploads && {uploads}), |
| 1343 | }); |
| 1344 | |
| 1345 | dispatch({type: UPDATE_APPROVAL_SUCCESS, approval, issueRef}); |
| 1346 | dispatch(fetch(issueRef)); |
| 1347 | dispatch(fetchComments(issueRef)); |
| 1348 | } catch (error) { |
| 1349 | dispatch({type: UPDATE_APPROVAL_FAILURE, error: error}); |
| 1350 | }; |
| 1351 | }; |
| 1352 | |
| 1353 | /** |
| 1354 | * Async action creator to update an issue. |
| 1355 | * |
| 1356 | * @param {Object} params Options for the issue update. |
| 1357 | * @param {IssueRef} params.issueRef |
| 1358 | * @param {IssueDelta=} params.delta |
| 1359 | * @param {string=} params.commentContent |
| 1360 | * @param {boolean=} params.sendEmail |
| 1361 | * @param {boolean=} params.isDescription |
| 1362 | * @param {AttachmentUpload=} params.uploads |
| 1363 | * @param {Array<number>=} params.keptAttachments |
| 1364 | * @return {function(function): Promise<void>} |
| 1365 | */ |
| 1366 | export const update = ({issueRef, delta, commentContent, sendEmail, |
| 1367 | isDescription, uploads, keptAttachments}) => async (dispatch) => { |
| 1368 | dispatch({type: UPDATE_START}); |
| 1369 | |
| 1370 | try { |
| 1371 | const {issue} = await prpcClient.call( |
| 1372 | 'monorail.Issues', 'UpdateIssue', {issueRef, delta, |
| 1373 | commentContent, sendEmail, isDescription, uploads, |
| 1374 | keptAttachments}); |
| 1375 | |
| 1376 | dispatch({type: UPDATE_SUCCESS, issue}); |
| 1377 | dispatch(fetchComments(issueRef)); |
| 1378 | dispatch(fetchRelatedIssues(issue)); |
| 1379 | dispatch(fetchReferencedUsers(issue)); |
| 1380 | } catch (error) { |
| 1381 | dispatch({type: UPDATE_FAILURE, error: error}); |
| 1382 | }; |
| 1383 | }; |
| 1384 | |
| 1385 | /** |
| 1386 | * Converts an issue from one template to another. This is used for changing |
| 1387 | * launch issues. |
| 1388 | * @param {IssueRef} issueRef |
| 1389 | * @param {Object} options |
| 1390 | * @param {string=} options.templateName |
| 1391 | * @param {string=} options.commentContent |
| 1392 | * @param {boolean=} options.sendEmail |
| 1393 | * @return {function(function): Promise<void>} |
| 1394 | */ |
| 1395 | export const convert = (issueRef, {templateName = '', |
| 1396 | commentContent = '', sendEmail = true}, |
| 1397 | ) => async (dispatch) => { |
| 1398 | dispatch({type: CONVERT_START}); |
| 1399 | |
| 1400 | try { |
| 1401 | const resp = await prpcClient.call( |
| 1402 | 'monorail.Issues', 'ConvertIssueApprovalsTemplate', |
| 1403 | {issueRef, templateName, commentContent, sendEmail}); |
| 1404 | |
| 1405 | dispatch({type: CONVERT_SUCCESS, issue: resp.issue}); |
| 1406 | const fetchCommentsMessage = {issueRef}; |
| 1407 | dispatch(fetchComments(fetchCommentsMessage)); |
| 1408 | } catch (error) { |
| 1409 | dispatch({type: CONVERT_FAILURE, error: error}); |
| 1410 | }; |
| 1411 | }; |