diff --git a/cmd/server/openapi/docs.go b/cmd/server/openapi/docs.go index 45f2a8d74e1..4d5dfa1a566 100644 --- a/cmd/server/openapi/docs.go +++ b/cmd/server/openapi/docs.go @@ -5004,6 +5004,9 @@ const docTemplate = `{ "id": { "type": "integer" }, + "name": { + "type": "string" + }, "oauth_host": { "description": "public url for oauth if different from url", "type": "string" @@ -5039,6 +5042,9 @@ const docTemplate = `{ "id": { "type": "integer" }, + "name": { + "type": "string" + }, "oauth_client_secret": { "type": "string" }, diff --git a/server/api/forge.go b/server/api/forge.go index 67639a03988..8a2606ebc70 100644 --- a/server/api/forge.go +++ b/server/api/forge.go @@ -119,6 +119,7 @@ func PatchForge(c *gin.Context) { } forge.URL = in.URL forge.Type = in.Type + forge.Name = in.Name forge.OAuthClientID = in.OAuthClientID forge.OAuthHost = in.OAuthHost forge.SkipVerify = in.SkipVerify @@ -158,6 +159,7 @@ func PostForge(c *gin.Context) { forge := &model.Forge{ URL: in.URL, Type: in.Type, + Name: in.Name, OAuthClientID: in.OAuthClientID, OAuthClientSecret: in.OAuthClientSecret, OAuthHost: in.OAuthHost, diff --git a/server/api/forge_test.go b/server/api/forge_test.go index 3c30d28dee3..11a4a9f9069 100644 --- a/server/api/forge_test.go +++ b/server/api/forge_test.go @@ -64,9 +64,56 @@ func TestPostForge(t *testing.T) { require.NotNil(t, created) assert.Equal(t, []string{"org1", "org2"}, created.Orgs) }) + + t.Run("should store the name of the new forge", func(t *testing.T) { + c, rec, _store := newForgeCtx(t, http.MethodPost, &model.ForgeWithOAuthClientSecret{ + Type: model.ForgeTypeGithub, + URL: "https://github.com", + Name: "My Org GitHub", + OAuthClientSecret: "client-secret", + }) + + var created *model.Forge + _store.On("ForgeCreate", mock.Anything).Run(func(args mock.Arguments) { + created, _ = args.Get(0).(*model.Forge) + }).Return(nil) + + PostForge(c) + + assert.Equal(t, http.StatusOK, rec.Code) + require.NotNil(t, created) + assert.Equal(t, "My Org GitHub", created.Name) + }) } func TestPatchForge(t *testing.T) { + t.Run("should update the name of the forge", func(t *testing.T) { + c, rec, _store := newForgeCtx(t, http.MethodPatch, &model.ForgeWithOAuthClientSecret{ + Type: model.ForgeTypeGithub, + URL: "https://github.com", + Name: "Renamed forge", + }) + c.Params = gin.Params{{Key: "forge_id", Value: "1"}} + + _store.On("ForgeGet", int64(1)).Return(&model.Forge{ + ID: 1, + Type: model.ForgeTypeGithub, + URL: "https://github.com", + Name: "Old name", + }, nil) + + var updated *model.Forge + _store.On("ForgeUpdate", mock.Anything).Run(func(args mock.Arguments) { + updated, _ = args.Get(0).(*model.Forge) + }).Return(nil) + + PatchForge(c) + + assert.Equal(t, http.StatusOK, rec.Code) + require.NotNil(t, updated) + assert.Equal(t, "Renamed forge", updated.Name) + }) + t.Run("should update the allowed orgs of the forge", func(t *testing.T) { c, rec, _store := newForgeCtx(t, http.MethodPatch, &model.ForgeWithOAuthClientSecret{ Type: model.ForgeTypeGithub, diff --git a/server/model/forge.go b/server/model/forge.go index 04f6a001549..71f7f1182c4 100644 --- a/server/model/forge.go +++ b/server/model/forge.go @@ -29,6 +29,7 @@ const ( type Forge struct { ID int64 `json:"id" xorm:"pk autoincr 'id'"` Type ForgeType `json:"type" xorm:"VARCHAR(250)"` + Name string `json:"name,omitempty" xorm:"VARCHAR(250) 'name'"` URL string `json:"url" xorm:"VARCHAR(500) 'url'"` OAuthClientID string `json:"client,omitempty" xorm:"VARCHAR(250) 'oauth_client_id'"` OAuthClientSecret string `json:"-" xorm:"VARCHAR(250) 'oauth_client_secret'"` // do not expose client secret @@ -48,6 +49,7 @@ func (f *Forge) PublicCopy() *Forge { forge := &Forge{ ID: f.ID, Type: f.Type, + Name: f.Name, URL: f.URL, } diff --git a/web/src/assets/locales/en.json b/web/src/assets/locales/en.json index 67b4b8f85dc..876e1bf9840 100644 --- a/web/src/assets/locales/en.json +++ b/web/src/assets/locales/en.json @@ -628,6 +628,8 @@ "forgejo": "Forgejo", "addon": "Addon", "forge_type": "Forge type", + "forge_name": "Display name", + "forge_name_placeholder": "Optional label shown on the login page", "oauth_client_id": "OAuth Client ID", "oauth_client_secret": "OAuth Client Secret", "oauth_host": "OAuth host", diff --git a/web/src/components/admin/settings/forges/AdminForgeForm.vue b/web/src/components/admin/settings/forges/AdminForgeForm.vue index 8af8a8710b9..dfb6df2fe65 100644 --- a/web/src/components/admin/settings/forges/AdminForgeForm.vue +++ b/web/src/components/admin/settings/forges/AdminForgeForm.vue @@ -6,6 +6,10 @@ + + + + diff --git a/web/src/lib/api/types/forge.ts b/web/src/lib/api/types/forge.ts index c64743f4c91..2c1d5d59eb6 100644 --- a/web/src/lib/api/types/forge.ts +++ b/web/src/lib/api/types/forge.ts @@ -3,6 +3,7 @@ export type ForgeType = 'github' | 'gitlab' | 'gitea' | 'bitbucket' | 'bitbucket export interface Forge { id: number; type: ForgeType; + name?: string; url: string; client?: string; oauth_client_secret?: string; diff --git a/web/src/lib/forgeDisplayName.test.ts b/web/src/lib/forgeDisplayName.test.ts new file mode 100644 index 00000000000..2ed4dbe0390 --- /dev/null +++ b/web/src/lib/forgeDisplayName.test.ts @@ -0,0 +1,25 @@ +import { describe, expect, it } from 'vitest'; + +import type { Forge } from '~/lib/api/types'; +import { forgeDisplayName } from '~/lib/forgeDisplayName'; + +describe('forgeDisplayName', () => { + it('uses the custom name when set', () => { + const forge: Pick = { + type: 'github', + url: 'https://github.com', + name: 'Acme GitHub', + }; + + expect(forgeDisplayName(forge)).toBe('Acme GitHub'); + }); + + it('falls back to the hostname from the forge url', () => { + const forge: Pick = { + type: 'github', + url: 'https://github.com', + }; + + expect(forgeDisplayName(forge)).toBe('github.com'); + }); +}); diff --git a/web/src/lib/forgeDisplayName.ts b/web/src/lib/forgeDisplayName.ts new file mode 100644 index 00000000000..911b52cec3b --- /dev/null +++ b/web/src/lib/forgeDisplayName.ts @@ -0,0 +1,17 @@ +import type { Forge } from '~/lib/api/types'; + +export function forgeDisplayName(forge: Pick): string { + if (forge.name !== undefined && forge.name !== '') { + return forge.name; + } + + let name = forge.type.charAt(0).toUpperCase() + forge.type.slice(1); + + const forgeUrl = forge.oauth_host ?? forge.url; + if (forgeUrl !== undefined && forgeUrl !== '') { + const url = new URL(forgeUrl); + name = url.hostname; + } + + return name; +} diff --git a/web/src/views/Login.vue b/web/src/views/Login.vue index cb1d65fad5e..b2b46f42250 100644 --- a/web/src/views/Login.vue +++ b/web/src/views/Login.vue @@ -59,6 +59,7 @@ import Icon from '~/components/atomic/Icon.vue'; import useApiClient from '~/compositions/useApiClient'; import useAuthentication from '~/compositions/useAuthentication'; import { useWPTitle } from '~/compositions/useWPTitle'; +import { forgeDisplayName } from '~/lib/forgeDisplayName'; import type { Forge } from '~/lib/api/types'; const route = useRoute(); @@ -100,18 +101,16 @@ const failedForgeFavicons = ref(new Set()); // Track which favicons fail const forgesWithNameAndFavicon = computed(() => forges.value.map((forge) => { - let name = forge.type.charAt(0).toUpperCase() + forge.type.slice(1); let favicon: null | string = null; - if (forge.url || forge.oauth_host) { - const url = new URL(forge.oauth_host || forge.url); - name = url.hostname; + if ((forge.url !== undefined && forge.url !== '') || (forge.oauth_host !== undefined && forge.oauth_host !== '')) { + const url = new URL(forge.oauth_host ?? forge.url); favicon = `${url.origin}/favicon.ico`; } return { ...forge, - name, + name: forgeDisplayName(forge), favicon, }; }), diff --git a/web/src/views/admin/AdminUsers.vue b/web/src/views/admin/AdminUsers.vue index 56e0f4a3d6d..fdb377f966d 100644 --- a/web/src/views/admin/AdminUsers.vue +++ b/web/src/views/admin/AdminUsers.vue @@ -154,6 +154,7 @@ import { usePagination } from '~/compositions/usePaginate'; import { useWPTitle } from '~/compositions/useWPTitle'; import { DEFAULT_FORGE_ID } from '~/lib/api'; import type { User } from '~/lib/api/types'; +import { forgeDisplayName } from '~/lib/forgeDisplayName'; import { deepClone } from '~/lib/utils'; const apiClient = useApiClient(); @@ -178,18 +179,7 @@ const selectedForgeId = computed({ async function loadForges() { const forges = await apiClient.getForges({ page: 1 }); if (forges) { - forgesMap.value = new Map( - forges.map((forge) => { - let name = forge.type.charAt(0).toUpperCase() + forge.type.slice(1); - - if (forge.url || forge.oauth_host) { - const url = new URL(forge.oauth_host || forge.url); - name = url.hostname; - } - - return [forge.id, name]; - }), - ); + forgesMap.value = new Map(forges.map((forge) => [forge.id, forgeDisplayName(forge)])); } } diff --git a/web/src/views/admin/forges/AdminForges.vue b/web/src/views/admin/forges/AdminForges.vue index c8005ca3095..34529e899ee 100644 --- a/web/src/views/admin/forges/AdminForges.vue +++ b/web/src/views/admin/forges/AdminForges.vue @@ -10,7 +10,7 @@ :key="forge.id" class="bg-wp-background-200! dark:bg-wp-background-200! items-center gap-2" > - {{ forge.url.replace(/http(s):\/\//, '') }} + {{ forgeDisplayName(forge) }}