Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adaptive username #536

Merged
merged 2 commits into from
Jun 28, 2023
Merged

feat: adaptive username #536

merged 2 commits into from
Jun 28, 2023

Conversation

elrrrrrrr
Copy link
Member

When executing npm cli owner add command, ref, it causes errors due to duplicate additions,when the selfRegistry configuration has the userPrefix option enabled.

  1. 🧶 Modify the user query api to prioritize returning users from selfRegistry
  2. 🧶 Ensure that the query api uniformly returns displayName

npm cli 执行 owner add 时 ref,依赖查询结果做去重,selfRegistry 配置 userPrefix 时会导致重复添加报错

  1. 🧶 修改用户查询接口,优先返回 selfRegistry 内的用户
  2. 🧶 查询接口统一返回 displayName

@elrrrrrrr elrrrrrrr added the enhancement New feature or request label Jun 28, 2023
export function getPrefixedName(prefix: string, username: string): string {
return prefix ? `${prefix}${username}` : username;
}

export async function calculateIntegrity(contentOrFile: Uint8Array | string) {
let integrityObj;
if (typeof contentOrFile === 'string') {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在给定的代码补丁中,有一个新的函数getPrefixedName被添加到代码中。这个函数将一个前缀字符串和一个用户名字符串作为参数,并返回一个带有前缀的用户名字符串。

代码的修改看起来是正确的,没有明显的错误或缺陷风险。然而,以下是一些建议改进:

  1. getPrefixedName函数中,可以添加输入参数的类型检查器,确保传递正确的参数类型。例如,为prefixusername添加类型注解string
  2. calculateIntegrity函数中,你可以添加返回类型注解(可能是Promise<void>)来指示异步函数的返回值类型。
  3. 如有可能,对函数和变量使用更具描述性的命名,以提高代码的可读性。

如果代码的上下文中还有其他内容,或者有其他方面需要注意的限制,请提供更多信息,以便我能够作出更准确的建议。

export function getPrefixedName(prefix: string, username: string): string {
return prefix ? `${prefix}${username}` : username;
}

export async function calculateIntegrity(contentOrFile: Uint8Array | string) {
let integrityObj;
if (typeof contentOrFile === 'string') {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

代码看起来没有明显的错误风险,但以下是一些建议:

  1. 在代码中添加适当的注释,以解释每个函数和条件的用途和意图。这将提高可读性并帮助其他开发人员理解代码。

  2. 在导出的函数上添加适当的类型注解。这将使代码更具可读性,并允许编辑器捕获潜在的类型错误。

  3. 对于 calculateIntegrity 函数,请确保在可能的情况下处理错误和异常情况。例如,如果传入的参数既不是 Uint8Array 类型也不是字符串类型,可能需要抛出一个错误或返回一个默认值。

  4. 针对每个函数编写一些单元测试,以确保其正确性和预期行为。这有助于捕获潜在的错误和边界情况。

总体而言,代码看起来相当简单且功能齐全。建议继续进行更准确的测试以确保正确的行为。

async findInRegistry(registry:Registry, name: string): Promise<UserEntity | null> {
return await this.findUserByName(getPrefixedName(registry.userPrefix, name));
}

async findUserByName(name: string): Promise<UserEntity | null> {
return await this.userRepository.findUserByName(name);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这段代码对UserService进行了补丁。下面是一些代码审查的建议和改进建议:

  1. 在导入语句处,可以把所有的import语句按照一定规则进行分组,以提高可读性。

  2. 对于findUserByNameOrDisplayName方法:

    • 增加了在包含前缀的情况下直接调用findUserByName的逻辑。
    • 通过registryManagerService来获取默认的注册表(defaultRegistry)和当前注册表(selfRegistry)。
    • 使用getPrefixedName函数生成带前缀的用户名,并在默认注册表和当前注册表中进行用户查找。
  3. 添加了findInRegistry方法,该方法在给定的注册表中查找用户。

  4. 建议在代码中添加注释,解释每个方法的功能和参数。

  5. 确保在使用async/await时处理异常情况,可以使用try-catch块来捕获和处理可能发生的错误。

  6. 检查是否有必要对参数进行验证和边界条件检查。

这些建议都是基于代码片段本身,更全面的代码审查还需要结合整个项目上下文来进行。如果有其他具体问题或需求,请提供更多细节。

assert.equal(res.body.email, email);
});


it('should 404 when user not exists', async () => {
const { authorization, name } = await TestUtil.createUser();
let res = await app.httpRequest()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这段代码的主要目标是测试用户控制器(UserController)中的一个方法。以下是对代码进行的简要代码审查:

  1. 在第23行和第34行之间添加了两个新的测试用例。

    • 第一个测试用例验证了当请求带有特定前缀信息时是否正确清除该前缀,并返回相应的用户信息。
    • 第二个测试用例验证了当请求自身用户时是否返回正确的用户信息。
  2. 需要确保在每个测试用例中都提供有效的授权头部(authorization)。

  3. 没有立即发现明显的错误或潜在的风险。

针对改进的建议:

  • 在测试用例中提供更详细的注释,以解释每个测试的目的和预期结果。
  • 添加边界测试用例,例如尝试使用无效的授权头部或无效的请求路径来测试系统的鲁棒性。
  • 确保在删除前缀信息的操作上进行适当的错误处理,例如在前缀信息为空或不存在时返回合适的错误响应。

请注意,这只是对给定代码片段的初步审查,可能还有其他方面需要检查。最佳实践是进行全面而彻底的代码审查,包括阅读和理解相关的功能实现和错误处理逻辑。

@codecov
Copy link

codecov bot commented Jun 28, 2023

Codecov Report

Merging #536 (ffb6baa) into master (bb5d993) will decrease coverage by 0.07%.
The diff coverage is 94.59%.

@@            Coverage Diff             @@
##           master     #536      +/-   ##
==========================================
- Coverage   97.03%   96.97%   -0.07%     
==========================================
  Files         174      174              
  Lines       16560    16591      +31     
  Branches     2170     2174       +4     
==========================================
+ Hits        16069    16089      +20     
- Misses        491      502      +11     
Impacted Files Coverage Δ
app/core/service/UserService.ts 95.55% <92.59%> (-0.41%) ⬇️
app/common/PackageUtil.ts 99.03% <100.00%> (+0.03%) ⬆️
app/port/controller/UserController.ts 99.14% <100.00%> (ø)

... and 2 files with indirect coverage changes

@elrrrrrrr elrrrrrrr merged commit dd69606 into master Jun 28, 2023
11 of 13 checks passed
@elrrrrrrr elrrrrrrr deleted the adaptive-username branch June 28, 2023 12:49
fengmk2 pushed a commit that referenced this pull request Jun 28, 2023
[skip ci]

## [3.35.0](v3.34.10...v3.35.0) (2023-06-28)

### Features

* adaptive username ([#536](#536)) ([dd69606](dd69606)), closes [/github.com/npm/cli/blob/latest/lib/commands/owner.js#L151](https://github.com/cnpm//github.com/npm/cli/blob/latest/lib/commands/owner.js/issues/L151)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants