Skip to content

Commit

Permalink
Merge pull request #92 from AlayaCare/unmanaged
Browse files Browse the repository at this point in the history
add an unmanaged tab
  • Loading branch information
nzin-alayacare authored Oct 26, 2024
2 parents 15b2c4f + 4824250 commit 1292ecf
Show file tree
Hide file tree
Showing 16 changed files with 830 additions and 44 deletions.
72 changes: 67 additions & 5 deletions browser/goliac-ui/src/components/DashboardApp.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<el-row>
<el-col :span="20" :offset="2">
<el-row>
<el-tabs class="full-width-tabs" v-model="activeName">
<el-tab-pane label="Status" name="first">
<el-tabs class="full-width-tabs" v-model="activeTabName">
<el-tab-pane label="Status" name="status">
<el-table
:data="statusTable"
:stripe="true"
Expand All @@ -20,7 +20,7 @@
<el-table-column prop="value" align="left" label="Value" />
</el-table>
</el-tab-pane>
<el-tab-pane label="Statistics" name="second">
<el-tab-pane label="Statistics" name="statistics">
<el-table
:data="statisticsTable"
:stripe="true"
Expand All @@ -31,6 +31,18 @@
<el-table-column prop="value" align="left" label="Value" />
</el-table>
</el-tab-pane>
<el-tab-pane label="Unmanaged" name="unmanaged">
<el-table
:data="unmanagedTable"
:stripe="true"
:highlight-current-row="false"
:default-sort="{ prop: 'title', order: 'descending' }"
>
<el-table-column width="150" prop="key" align="left" label="Key" sortable />
<el-table-column width="100" prop="nb" align="left" label="Nb" />
<el-table-column prop="values" align="left" label="Values" />
</el-table>
</el-tab-pane>
</el-tabs>
</el-row>
<el-row v-if="detailedErrors.length > 0 || detailedWarnings.length > 0">
Expand Down Expand Up @@ -115,17 +127,67 @@
flushcacheVisible: false,
statusTable: [],
statisticsTable: [],
unmanagedTable: [],
detailedErrors: [],
detailedWarnings: [],
version: "",
activeName: "first",
activeTabName: "status",
};
},
created() {
mounted() {
this.getStatus()
this.getStatistics()
this.getUnmanaged()
setInterval(() => {
this.getStatus()
this.getStatistics()
this.getUnmanaged()
}, 60000);
},
beforeUnmount() {
clearInterval(this.interval);
},
methods: {
getUnmanaged() {
Axios.get(`${API_URL}/unmanaged`).then(response => {
let unmanaged = response.data;
this.unmanagedTable = [
{
key: "Users",
nb: unmanaged.users ? unmanaged.users.length : "unknown",
values: unmanaged.users ? unmanaged.users.slice(0, 20).join(",") : "unknown",
},
{
key: "Teams",
nb: unmanaged.teams ? unmanaged.teams.length : "unknown",
values: unmanaged.teams ? unmanaged.teams.slice(0, 20).join(",") : "unknown",
},
{
key: "Repositories",
nb: unmanaged.repos ? unmanaged.repos.length : "unknown",
values: unmanaged.repos ? unmanaged.repos.slice(0, 20).join(",") : "unknown",
},
{
key: "Rulesets",
nb: unmanaged.rulesets ? unmanaged.rulesets.length : "unknown",
values: unmanaged.rulesets ? unmanaged.rulesets.slice(0, 20).join(",") : "unknown",
},
]
if (unmanaged.users && unmanaged.users.length > 20) {
this.unmanagedTable.users += ", ...";
}
if (unmanaged.teams && unmanaged.teams.length > 20) {
this.unmanagedTable.teams += ", ...";
}
if (unmanaged.repos && unmanaged.repos.length > 20) {
this.unmanagedTable.repos += ", ...";
}
if (unmanaged.rulesets && unmanaged.rulesets.length > 20) {
this.unmanagedTable.rulesets += ", ...";
}
}, handleErr.bind(this));
},
getStatistics() {
Axios.get(`${API_URL}/statistics`).then(response => {
let statistics = response.data;
Expand Down
4 changes: 2 additions & 2 deletions cmd/goliac/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ branch can be passed by parameter or by defining GOLIAC_SERVER_GIT_BRANCH env va
logrus.Fatalf("failed to create goliac: %s", err)
}
ctx := context.Background()
err, _, _ = goliac.Apply(ctx, true, repo, branch, true)
err, _, _, _ = goliac.Apply(ctx, true, repo, branch, true)
if err != nil {
logrus.Errorf("Failed to plan: %v", err)
}
Expand Down Expand Up @@ -97,7 +97,7 @@ branch can be passed by parameter or by defining GOLIAC_SERVER_GIT_BRANCH env va
}

ctx := context.Background()
err, _, _ = goliac.Apply(ctx, false, repo, branch, true)
err, _, _, _ = goliac.Apply(ctx, false, repo, branch, true)
if err != nil {
logrus.Errorf("Failed to apply: %v", err)
}
Expand Down
37 changes: 37 additions & 0 deletions docs/api_docs/bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,21 @@ paths:
description: generic error response
schema:
$ref: '#/definitions/error'
/unmanaged:
get:
tags:
- app
operationId: getUnmanaged
description: Get unmanaged resources metrics
responses:
'200':
description: get Goliac unmanaged resources metrics
schema:
$ref: '#/definitions/unmanaged'
default:
description: generic error response
schema:
$ref: '#/definitions/error'
definitions:
health:
type: object
Expand Down Expand Up @@ -488,6 +503,28 @@ definitions:
maxGithubThrottled:
type: integer
x-omitempty: false
unmanaged:
properties:
users:
type: array
items:
type: string
minLength: 1
teams:
type: array
items:
type: string
minLength: 1
repos:
type: array
items:
type: string
minLength: 1
rulesets:
type: array
items:
type: string
minLength: 1
error:
type: object
required:
Expand Down
53 changes: 40 additions & 13 deletions internal/engine/goliac_reconciliator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,61 +18,78 @@ const (
KeyAuthor key = "author"
)

type UnmanagedResources struct {
Users map[string]bool
Teams map[string]bool
Repositories map[string]bool
RuleSets map[int]bool
}

/*
* GoliacReconciliator is here to sync the local state to the remote state
*/
type GoliacReconciliator interface {
Reconciliate(ctx context.Context, local GoliacLocal, remote GoliacRemote, teamreponame string, dryrun bool, reposToArchive map[string]*GithubRepoComparable) error
Reconciliate(ctx context.Context, local GoliacLocal, remote GoliacRemote, teamreponame string, dryrun bool, reposToArchive map[string]*GithubRepoComparable) (*UnmanagedResources, error)
}

type GoliacReconciliatorImpl struct {
executor ReconciliatorExecutor
repoconfig *config.RepositoryConfig
unmanaged *UnmanagedResources
}

func NewGoliacReconciliatorImpl(executor ReconciliatorExecutor, repoconfig *config.RepositoryConfig) GoliacReconciliator {
return &GoliacReconciliatorImpl{
executor: executor,
repoconfig: repoconfig,
unmanaged: nil,
}
}

func (r *GoliacReconciliatorImpl) Reconciliate(ctx context.Context, local GoliacLocal, remote GoliacRemote, teamsreponame string, dryrun bool, reposToArchive map[string]*GithubRepoComparable) error {
func (r *GoliacReconciliatorImpl) Reconciliate(ctx context.Context, local GoliacLocal, remote GoliacRemote, teamsreponame string, dryrun bool, reposToArchive map[string]*GithubRepoComparable) (*UnmanagedResources, error) {
rremote := NewMutableGoliacRemoteImpl(ctx, remote)
r.Begin(ctx, dryrun)
err := r.reconciliateUsers(ctx, local, rremote, dryrun)
unmanaged := &UnmanagedResources{
Users: make(map[string]bool),
Teams: make(map[string]bool),
Repositories: make(map[string]bool),
RuleSets: make(map[int]bool),
}
r.unmanaged = unmanaged

err := r.reconciliateUsers(ctx, local, rremote, dryrun, unmanaged)
if err != nil {
r.Rollback(ctx, dryrun, err)
return err
return nil, err
}

err = r.reconciliateTeams(ctx, local, rremote, dryrun)
if err != nil {
r.Rollback(ctx, dryrun, err)
return err
return nil, err
}

err = r.reconciliateRepositories(ctx, local, rremote, teamsreponame, dryrun, reposToArchive)
if err != nil {
r.Rollback(ctx, dryrun, err)
return err
return nil, err
}

if remote.IsEnterprise() {
err = r.reconciliateRulesets(ctx, local, rremote, r.repoconfig, dryrun)
if err != nil {
r.Rollback(ctx, dryrun, err)
return err
return nil, err
}
}

return r.Commit(ctx, dryrun)
return r.unmanaged, r.Commit(ctx, dryrun)
}

/*
* This function sync teams and team's members
*/
func (r *GoliacReconciliatorImpl) reconciliateUsers(ctx context.Context, local GoliacLocal, remote *MutableGoliacRemoteImpl, dryrun bool) error {
func (r *GoliacReconciliatorImpl) reconciliateUsers(ctx context.Context, local GoliacLocal, remote *MutableGoliacRemoteImpl, dryrun bool, unmanaged *UnmanagedResources) error {
ghUsers := remote.Users()

rUsers := make(map[string]string)
Expand Down Expand Up @@ -422,6 +439,8 @@ func (r *GoliacReconciliatorImpl) reconciliateRepositories(ctx context.Context,
if r.repoconfig.DestructiveOperations.AllowDestructiveRepositories {
r.UpdateRepositoryUpdateBoolProperty(ctx, dryrun, remote, reponame, "archived", true)
toArchive[reponame] = rRepo
} else {
r.unmanaged.Repositories[reponame] = true
}
} else {
r.DeleteRepository(ctx, dryrun, remote, reponame)
Expand Down Expand Up @@ -547,12 +566,14 @@ func (r *GoliacReconciliatorImpl) RemoveUserFromOrg(ctx context.Context, dryrun
if a := ctx.Value(KeyAuthor); a != nil {
author = a.(string)
}
logrus.WithFields(map[string]interface{}{"dryrun": dryrun, "author": author, "command": "remove_user_from_org"}).Infof("ghusername: %s", ghuserid)
remote.RemoveUserFromOrg(ghuserid)
if r.executor != nil {
if r.repoconfig.DestructiveOperations.AllowDestructiveUsers {
if r.repoconfig.DestructiveOperations.AllowDestructiveUsers {
logrus.WithFields(map[string]interface{}{"dryrun": dryrun, "author": author, "command": "remove_user_from_org"}).Infof("ghusername: %s", ghuserid)
remote.RemoveUserFromOrg(ghuserid)
if r.executor != nil {
r.executor.RemoveUserFromOrg(ctx, dryrun, ghuserid)
}
} else {
r.unmanaged.Users[ghuserid] = true
}
}

Expand Down Expand Up @@ -600,6 +621,8 @@ func (r *GoliacReconciliatorImpl) DeleteTeam(ctx context.Context, dryrun bool, r
if r.executor != nil {
r.executor.DeleteTeam(ctx, dryrun, teamslug)
}
} else {
r.unmanaged.Teams[teamslug] = true
}
}
func (r *GoliacReconciliatorImpl) CreateRepository(ctx context.Context, dryrun bool, remote *MutableGoliacRemoteImpl, reponame string, descrition string, writers []string, readers []string, boolProperties map[string]bool) {
Expand Down Expand Up @@ -659,6 +682,8 @@ func (r *GoliacReconciliatorImpl) DeleteRepository(ctx context.Context, dryrun b
if r.executor != nil {
r.executor.DeleteRepository(ctx, dryrun, reponame)
}
} else {
r.unmanaged.Repositories[reponame] = true
}
}
func (r *GoliacReconciliatorImpl) UpdateRepositoryUpdateBoolProperty(ctx context.Context, dryrun bool, remote *MutableGoliacRemoteImpl, reponame string, propertyName string, propertyValue bool) {
Expand Down Expand Up @@ -702,6 +727,8 @@ func (r *GoliacReconciliatorImpl) DeleteRuleset(ctx context.Context, dryrun bool
if r.executor != nil {
r.executor.DeleteRuleset(ctx, dryrun, rulesetid)
}
} else {
r.unmanaged.RuleSets[rulesetid] = true
}
}
func (r *GoliacReconciliatorImpl) UpdateRepositorySetExternalUser(ctx context.Context, dryrun bool, remote *MutableGoliacRemoteImpl, reponame string, collaboatorGithubId string, permission string) {
Expand Down
Loading

0 comments on commit 1292ecf

Please sign in to comment.