Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | // Copyright 2020 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 | import {projectMemberToProjectName} from 'shared/converters.js'; |
| 6 | |
| 7 | // TODO(crbug.com/monorail/7910): Dedupe this with the similar "projectRoles" |
| 8 | // constant in <mr-header>. |
| 9 | const projectRoles = Object.freeze({ |
| 10 | PROJECT_ROLE_UNSPECIFIED: '', |
| 11 | OWNER: 'Owner', |
| 12 | COMMITTER: 'Committer', |
| 13 | CONTRIBUTOR: 'Contributor', |
| 14 | }); |
| 15 | |
| 16 | /** |
| 17 | * Creates a mapping of project names to the user's role in that project. |
| 18 | * @param {Array<ProjectMember>} projectMembers Project memebrships |
| 19 | * for a given user. |
| 20 | * @return {Object<ProjectName, string>} Mapping of a user's roles, |
| 21 | * by project name. |
| 22 | */ |
| 23 | export function computeRoleByProjectName(projectMembers) { |
| 24 | const mapping = {}; |
| 25 | if (!projectMembers) return mapping; |
| 26 | projectMembers.forEach(({name, role}) => { |
| 27 | mapping[projectMemberToProjectName(name)] = projectRoles[role]; |
| 28 | }); |
| 29 | return mapping; |
| 30 | } |