/
count_team.go
67 lines (55 loc) · 1.83 KB
/
count_team.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// SPDX-License-Identifier: Apache-2.0
package secret
import (
"context"
"strings"
"github.com/go-vela/types/constants"
"github.com/sirupsen/logrus"
)
// CountSecretsForTeam gets the count of secrets by org and team name from the database.
func (e *engine) CountSecretsForTeam(ctx context.Context, org, team string, filters map[string]interface{}) (int64, error) {
e.logger.WithFields(logrus.Fields{
"org": org,
"team": team,
"type": constants.SecretShared,
}).Tracef("getting count of secrets for team %s/%s from the database", org, team)
// variable to store query results
var s int64
// send query to the database and store result in variable
err := e.client.
Table(constants.TableSecret).
Where("type = ?", constants.SecretShared).
Where("org = ?", org).
Where("team = ?", team).
Where(filters).
Count(&s).
Error
return s, err
}
// CountSecretsForTeams gets the count of secrets by teams within an org from the database.
func (e *engine) CountSecretsForTeams(ctx context.Context, org string, teams []string, filters map[string]interface{}) (int64, error) {
// lower case team names for not case-sensitive values from the SCM i.e. GitHub
//
// iterate through the list of teams provided
for index, team := range teams {
// ensure the team name is lower case
teams[index] = strings.ToLower(team)
}
e.logger.WithFields(logrus.Fields{
"org": org,
"teams": teams,
"type": constants.SecretShared,
}).Tracef("getting count of secrets for teams %s in org %s from the database", teams, org)
// variable to store query results
var s int64
// send query to the database and store result in variable
err := e.client.
Table(constants.TableSecret).
Where("type = ?", constants.SecretShared).
Where("org = ?", org).
Where("LOWER(team) IN (?)", teams).
Where(filters).
Count(&s).
Error
return s, err
}