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

Add org list #2338

Merged
merged 7 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
84 changes: 84 additions & 0 deletions cmd/server/docs/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 72 additions & 0 deletions server/api/orgs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright 2023 Woodpecker Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package api

import (
"net/http"
"strconv"

"github.com/gin-gonic/gin"

"github.com/woodpecker-ci/woodpecker/server/router/middleware/session"
"github.com/woodpecker-ci/woodpecker/server/store"
)

// GetOrgs
//
// @Summary Get all orgs
// @Description Returns all registered orgs in the system. Requires admin rights.
// @Router /orgs [get]
// @Produce json
// @Success 200 {array} Org
// @Tags Orgs
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
// @Param page query int false "for response pagination, page offset number" default(1)
// @Param perPage query int false "for response pagination, max items per page" default(50)
func GetOrgs(c *gin.Context) {
orgs, err := store.FromContext(c).GetOrgList(session.Pagination(c))
if err != nil {
c.String(500, "Error getting user list. %s", err)
return
}
c.JSON(200, orgs)
}

// DeleteOrg
//
// @Summary Delete an org
// @Description Deletes the given org. Requires admin rights.
// @Router /orgs/{id} [delete]
// @Produce json
// @Success 204 {object} Org
// @Tags Orgs
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
// @Param id path string true "the org's id"
func DeleteOrg(c *gin.Context) {
_store := store.FromContext(c)

orgID, err := strconv.ParseInt(c.Param("org_id"), 10, 64)
if err != nil {
c.String(http.StatusBadRequest, "Error parsing org id. %s", err)
return
}

err = _store.OrgDelete(orgID)
if err != nil {
c.String(http.StatusInternalServerError, "Error deleting org %d. %s", orgID, err)
}

c.String(http.StatusNoContent, "")
}
2 changes: 1 addition & 1 deletion server/forge/mocks/forge.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 17 additions & 12 deletions server/router/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,25 @@ func apiRoutes(e *gin.RouterGroup) {
users.DELETE("/:login", api.DeleteUser)
}

apiBase.GET("/orgs/lookup/*org_full_name", api.LookupOrg)
orgBase := apiBase.Group("/orgs/:org_id")
orgs := apiBase.Group("/orgs")
{
orgBase.GET("/permissions", api.GetOrgPermissions)

org := orgBase.Group("")
orgs.GET("", session.MustAdmin(), api.GetOrgs)
orgs.GET("/lookup/*org_full_name", api.LookupOrg)
orgBase := orgs.Group("/:org_id")
{
org.Use(session.MustOrgMember(true))
org.GET("", api.GetOrg)
org.GET("/secrets", api.GetOrgSecretList)
org.POST("/secrets", api.PostOrgSecret)
org.GET("/secrets/:secret", api.GetOrgSecret)
org.PATCH("/secrets/:secret", api.PatchOrgSecret)
org.DELETE("/secrets/:secret", api.DeleteOrgSecret)
orgBase.GET("/permissions", api.GetOrgPermissions)

org := orgBase.Group("")
{
org.Use(session.MustOrgMember(true))
org.DELETE("", session.MustAdmin(), api.DeleteOrg)
org.GET("", api.GetOrg)
org.GET("/secrets", api.GetOrgSecretList)
org.POST("/secrets", api.PostOrgSecret)
org.GET("/secrets/:secret", api.GetOrgSecret)
org.PATCH("/secrets/:secret", api.PatchOrgSecret)
org.DELETE("/secrets/:secret", api.DeleteOrgSecret)
}
}
}

Expand Down
8 changes: 7 additions & 1 deletion server/store/datastore/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ package datastore
import (
"strings"

"github.com/woodpecker-ci/woodpecker/server/model"
"xorm.io/xorm"

"github.com/woodpecker-ci/woodpecker/server/model"
)

func (s storage) OrgCreate(org *model.Org) error {
Expand Down Expand Up @@ -62,3 +63,8 @@ func (s storage) OrgRepoList(org *model.Org, p *model.ListOptions) ([]*model.Rep
var repos []*model.Repo
return repos, s.paginate(p).OrderBy("repo_id").Where("repo_org_id = ?", org.ID).Find(&repos)
}

func (s storage) GetOrgList(p *model.ListOptions) ([]*model.Org, error) {
var orgs []*model.Org
return orgs, s.paginate(p).Where("is_user = ?", false).OrderBy("id").Find(&orgs)
}
28 changes: 27 additions & 1 deletion server/store/mocks/store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ type Store interface {
OrgFindByName(string) (*model.Org, error)
OrgUpdate(*model.Org) error
OrgDelete(int64) error
GetOrgList(*model.ListOptions) ([]*model.Org, error)
qwerty287 marked this conversation as resolved.
Show resolved Hide resolved

// Org repos
OrgRepoList(*model.Org, *model.ListOptions) ([]*model.Repo, error)
Expand Down
1 change: 1 addition & 0 deletions web/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ declare module '@vue/runtime-core' {
ActionsTab: typeof import('./src/components/repo/settings/ActionsTab.vue')['default']
ActivePipelines: typeof import('./src/components/layout/header/ActivePipelines.vue')['default']
AdminAgentsTab: typeof import('./src/components/admin/settings/AdminAgentsTab.vue')['default']
AdminOrgsTab: typeof import('./src/components/admin/settings/AdminOrgsTab.vue')['default']
AdminQueueStats: typeof import('./src/components/admin/settings/queue/AdminQueueStats.vue')['default']
AdminQueueTab: typeof import('./src/components/admin/settings/AdminQueueTab.vue')['default']
AdminSecretsTab: typeof import('./src/components/admin/settings/AdminSecretsTab.vue')['default']
Expand Down
10 changes: 10 additions & 0 deletions web/src/assets/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,16 @@
},
"delete_user": "Delete user",
"edit_user": "Edit user"
},
"orgs": {
"orgs": "Organizations",
"desc": "Organizations owning repositories on this server",
"none": "There are no organizations yet.",
"org_settings": "Organization settings",
"delete_org": "Delete organization",
"deleted": "Organization deleted",
"delete_confirm": "Do you really want to delete this organization?",
"view": "View organization"
}
}
},
Expand Down
68 changes: 68 additions & 0 deletions web/src/components/admin/settings/AdminOrgsTab.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<template>
<Settings :title="$t('admin.settings.orgs.orgs')" :desc="$t('admin.settings.orgs.desc')">
<div class="space-y-4 text-wp-text-100">
<ListItem
v-for="org in orgs"
:key="org.id"
class="items-center gap-2 !bg-wp-background-200 !dark:bg-wp-background-100"
>
<span>{{ org.name }}</span>
<IconButton
icon="chevron-right"
:title="$t('admin.settings.orgs.view')"
class="ml-auto w-8 h-8"
:to="{ name: 'org', params: { orgId: org.id } }"
/>
<IconButton
icon="settings"
:title="$t('admin.settings.orgs.org_settings')"
class="w-8 h-8"
:to="{ name: 'org-settings', params: { orgId: org.id } }"
/>
<IconButton
icon="trash"
:title="$t('admin.settings.orgs.delete_org')"
class="ml-2 w-8 h-8 hover:text-wp-control-error-100"
:is-loading="isDeleting"
@click="deleteOrg(org)"
/>
</ListItem>

<div v-if="orgs?.length === 0" class="ml-2">{{ $t('admin.settings.orgs.none') }}</div>
</div>
</Settings>
</template>

<script lang="ts" setup>
import { useI18n } from 'vue-i18n';

import IconButton from '~/components/atomic/IconButton.vue';
import ListItem from '~/components/atomic/ListItem.vue';
import Settings from '~/components/layout/Settings.vue';
import useApiClient from '~/compositions/useApiClient';
import { useAsyncAction } from '~/compositions/useAsyncAction';
import useNotifications from '~/compositions/useNotifications';
import { usePagination } from '~/compositions/usePaginate';
import { Org } from '~/lib/api/types';

const apiClient = useApiClient();
const notifications = useNotifications();
const { t } = useI18n();

async function loadOrgs(page: number): Promise<Org[] | null> {
return apiClient.getOrgs(page);
}

const { resetPage, data: orgs } = usePagination(loadOrgs);

const { doSubmit: deleteOrg, isLoading: isDeleting } = useAsyncAction(async (_org: Org) => {
// eslint-disable-next-line no-restricted-globals, no-alert
if (!confirm(t('admin.settings.orgs.delete_confirm'))) {
return;
}

await apiClient.deleteOrg(_org);
notifications.notify({ title: t('admin.settings.orgs.deleted'), type: 'success' });
resetPage();
});
</script>
8 changes: 8 additions & 0 deletions web/src/lib/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,14 @@ export default class WoodpeckerClient extends ApiClient {
return this._delete('/api/user/token') as Promise<string>;
}

getOrgs(page: number): Promise<Org[] | null> {
return this._get(`/api/orgs?page=${page}`) as Promise<Org[] | null>;
}

deleteOrg(org: Org): Promise<unknown> {
return this._delete(`/api/orgs/${org.id}`);
}

// eslint-disable-next-line promise/prefer-await-to-callbacks
on(callback: (data: { pipeline?: Pipeline; repo?: Repo; step?: PipelineWorkflow }) => void): EventSource {
return this._subscribe('/api/stream/events', callback, {
Expand Down
Loading