-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[server] For GitLab projects without an owner avatar, fall back to the namespace avatar, or generate the default GitLab avatar #8824
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,21 @@ import { inject, injectable } from "inversify"; | |
import { TokenProvider } from "../../../src/user/token-provider"; | ||
import { UserDB } from "@gitpod/gitpod-db/lib"; | ||
import { Gitlab } from "@gitbeaker/node"; | ||
import { ProjectSchemaDefault, NamespaceInfoSchemaDefault } from "@gitbeaker/core/dist/types/services/Projects"; | ||
|
||
// Add missing fields to Gitbeaker's ProjectSchema type | ||
type ProjectSchema = ProjectSchemaDefault & { | ||
last_activity_at: string; | ||
namespace: NamespaceInfoSchemaDefault & { | ||
avatar_url: string | null; | ||
parent_id: number | null; | ||
}; | ||
owner?: { | ||
id: number; | ||
name: string; | ||
avatar_url: string | null; | ||
}; | ||
}; | ||
|
||
@injectable() | ||
export class GitLabAppSupport { | ||
|
@@ -38,12 +53,12 @@ export class GitLabAppSupport { | |
// | ||
const projectsWithAccess = await api.Projects.all({ min_access_level: "40", perPage: 100 }); | ||
for (const project of projectsWithAccess) { | ||
const anyProject = project as any; | ||
const path = anyProject.path as string; | ||
const fullPath = anyProject.path_with_namespace as string; | ||
const cloneUrl = anyProject.http_url_to_repo as string; | ||
const updatedAt = anyProject.last_activity_at as string; | ||
const accountAvatarUrl = anyProject.owner?.avatar_url as string; | ||
const aProject = project as ProjectSchema; | ||
const path = aProject.path as string; | ||
const fullPath = aProject.path_with_namespace as string; | ||
const cloneUrl = aProject.http_url_to_repo as string; | ||
const updatedAt = aProject.last_activity_at as string; | ||
const accountAvatarUrl = await this.getAccountAvatarUrl(aProject, params.provider.host); | ||
const account = fullPath.split("/")[0]; | ||
|
||
(account === usersGitLabAccount ? ownersRepos : result).push({ | ||
|
@@ -61,4 +76,35 @@ export class GitLabAppSupport { | |
result.unshift(...ownersRepos); | ||
return result; | ||
} | ||
|
||
protected async getAccountAvatarUrl(project: ProjectSchema, providerHost: string): Promise<string> { | ||
let owner = project.owner; | ||
if (!owner && project.namespace && !project.namespace.parent_id) { | ||
// Fall back to "root namespace" / "top-level group" | ||
owner = project.namespace; | ||
} | ||
if (!owner) { | ||
// Could not determine account avatar | ||
return ""; | ||
} | ||
if (owner.avatar_url) { | ||
const url = owner.avatar_url; | ||
// Sometimes GitLab avatar URLs are relative -- ensure we always use the correct host | ||
return url[0] === "/" ? `https://${providerHost}${url}` : url; | ||
} | ||
// If there is no avatar, generate the same default avatar that GitLab uses. Based on: | ||
// - https://gitlab.com/gitlab-org/gitlab/-/blob/b2a22b6e85200ce55ab09b5c765043441b086c96/app/helpers/avatars_helper.rb#L151-161 | ||
// - https://gitlab.com/gitlab-org/gitlab/-/blob/861f52858a1db07bdb122fe947dec9b0a09ce807/app/assets/stylesheets/startup/startup-general.scss#L1611-1631 | ||
// - https://gitlab.com/gitlab-org/gitlab/-/blob/861f52858a1db07bdb122fe947dec9b0a09ce807/app/assets/stylesheets/startup/startup-general.scss#L420-422 | ||
const backgroundColors = ["#fcf1ef", "#f4f0ff", "#f1f1ff", "#e9f3fc", "#ecf4ee", "#fdf1dd", "#f0f0f0"]; | ||
const backgroundColor = backgroundColors[owner.id % backgroundColors.length]; | ||
// Uppercase first character of the name, support emojis, default to whitespace. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. praise: Lovely comment! 💯 |
||
const text = String.fromCodePoint(owner.name.codePointAt(0) || 32 /* space */).toUpperCase(); | ||
const svg = `<svg viewBox="0 0 32 32" height="32" width="32" style="background-color: ${backgroundColor}" xmlns="http://www.w3.org/2000/svg"> | ||
<text x="50%" y="50%" dominant-baseline="central" text-anchor="middle" style='font-size: 0.875rem; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans", Ubuntu, Cantarell, "Helvetica Neue", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"'> | ||
${text} | ||
</text> | ||
</svg>`; | ||
return `data:image/svg+xml,${encodeURIComponent(svg.replace(/\s+/g, " "))}`; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Much nicer on the eyes