blob: 5c12ae873f190ce3dcfb916be4a25a7ca2d8e6f2 [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001// 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
5import {projectMemberToProjectName} from 'shared/converters.js';
6
7// TODO(crbug.com/monorail/7910): Dedupe this with the similar "projectRoles"
8// constant in <mr-header>.
9const 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 */
23export 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}