blob: 25a34d97baa1154d1e1b9ed24a7b71bf4c4295e5 [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001// Copyright 2020 The Chromium Authors
Copybara854996b2021-09-07 19:36:02 +00002// 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}