Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cmd/server/openapi/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -5039,6 +5042,9 @@ const docTemplate = `{
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"oauth_client_secret": {
"type": "string"
},
Expand Down
2 changes: 2 additions & 0 deletions server/api/forge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
47 changes: 47 additions & 0 deletions server/api/forge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions server/model/forge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -48,6 +49,7 @@ func (f *Forge) PublicCopy() *Forge {
forge := &Forge{
ID: f.ID,
Type: f.Type,
Name: f.Name,
URL: f.URL,
}

Expand Down
2 changes: 2 additions & 0 deletions web/src/assets/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions web/src/components/admin/settings/forges/AdminForgeForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
<SelectField :id="id" v-model="forgeType" :options="forgeTypeOptions" required />
</InputField>

<InputField v-slot="{ id }" :label="$t('forge_name')">
<TextField :id="id" v-model="forge.name" :placeholder="$t('forge_name_placeholder')" />
</InputField>

<InputField v-if="forge.type !== 'bitbucket'" v-slot="{ id }" :label="$t('url')">
<TextField :id="id" v-model="forge.url" :placeholder="$t('url')" required />
</InputField>
Expand Down
1 change: 1 addition & 0 deletions web/src/lib/api/types/forge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
25 changes: 25 additions & 0 deletions web/src/lib/forgeDisplayName.test.ts
Original file line number Diff line number Diff line change
@@ -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<Forge, 'type' | 'url' | 'oauth_host' | 'name'> = {
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<Forge, 'type' | 'url' | 'oauth_host' | 'name'> = {
type: 'github',
url: 'https://github.com',
};

expect(forgeDisplayName(forge)).toBe('github.com');
});
});
17 changes: 17 additions & 0 deletions web/src/lib/forgeDisplayName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { Forge } from '~/lib/api/types';

export function forgeDisplayName(forge: Pick<Forge, 'type' | 'url' | 'oauth_host' | 'name'>): 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;
}
9 changes: 4 additions & 5 deletions web/src/views/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -100,18 +101,16 @@ const failedForgeFavicons = ref(new Set<number>()); // 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,
};
}),
Expand Down
14 changes: 2 additions & 12 deletions web/src/views/admin/AdminUsers.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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)]));
}
}

Expand Down
3 changes: 2 additions & 1 deletion web/src/views/admin/forges/AdminForges.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
:key="forge.id"
class="bg-wp-background-200! dark:bg-wp-background-200! items-center gap-2"
>
<span>{{ forge.url.replace(/http(s):\/\//, '') }}</span>
<span>{{ forgeDisplayName(forge) }}</span>
<Badge class="md:display-unset ml-auto hidden" :value="forge.type" />
<div class="flex items-center gap-2">
<IconButton
Expand Down Expand Up @@ -50,6 +50,7 @@ import useApiClient from '~/compositions/useApiClient';
import { useAsyncAction } from '~/compositions/useAsyncAction';
import useNotifications from '~/compositions/useNotifications';
import { usePagination } from '~/compositions/usePaginate';
import { forgeDisplayName } from '~/lib/forgeDisplayName';
import type { Forge } from '~/lib/api/types';

const apiClient = useApiClient();
Expand Down