feat(qaops): 增加个人自然月用量与零记录人员明细

This commit is contained in:
姚章浩
2026-10-17 19:59:33 +08:00
parent 56b8cabed9
commit 9556db1ef0
18 changed files with 1248 additions and 10 deletions
+19 -5
View File
@@ -6,10 +6,12 @@ import (
)
type PersonAttribution struct {
State string `json:"state"`
PersonID string `json:"-"`
Departments []string `json:"departments"`
Region string `json:"region"`
Name string `json:"name"`
DepartmentIDs []string `json:"-"`
State string `json:"state"`
PersonID string `json:"-"`
Departments []string `json:"departments"`
Region string `json:"region"`
}
type Attribution struct {
Metadata State
@@ -17,6 +19,18 @@ type Attribution struct {
unions map[string]string
}
func (p *Attribution) Members() []PersonAttribution {
result := []PersonAttribution{}
if p == nil {
return result
}
for _, person := range p.people {
result = append(result, person)
}
sort.Slice(result, func(i, j int) bool { return result[i].PersonID < result[j].PersonID })
return result
}
func BuildAttribution(state State, directory Directory) (*Attribution, error) {
p := &Attribution{Metadata: state, people: map[string]PersonAttribution{}, unions: map[string]string{}}
if state.State == "empty" {
@@ -43,7 +57,7 @@ func BuildAttribution(state State, directory Directory) (*Attribution, error) {
regions[d.Region] = true
}
}
item := PersonAttribution{State: "unassigned", PersonID: person.ID, Departments: []string{}}
item := PersonAttribution{Name: person.Name, DepartmentIDs: append([]string{}, person.DepartmentIDs...), State: "unassigned", PersonID: person.ID, Departments: []string{}}
for path := range paths {
item.Departments = append(item.Departments, path)
}
+30
View File
@@ -0,0 +1,30 @@
package qaopsorg
import (
"encoding/json"
"strings"
"testing"
)
func TestMembersKeepNamedAndUnnamedRosterPeopleWithoutRawIdentifiers(t *testing.T) {
d := Directory{Departments: []Department{{ID: "root", Name: "fixture"}}, People: []Person{{ID: "private-a", Name: "<源端姓名>", UnionID: "private-union", DepartmentIDs: []string{"root"}}, {ID: "private-zero", DepartmentIDs: []string{"root"}}}}
p, err := BuildAttribution(State{State: "ready", DirectoryID: "directory"}, d)
if err != nil {
t.Fatal(err)
}
members := p.Members()
if len(members) != 2 {
t.Fatal("zero-use roster member omitted")
}
if p.Resolve("private-a", "").Name != "<源端姓名>" || p.Resolve("private-zero", "").Name != "" {
t.Fatal("display name lost or guessed")
}
raw, _ := json.Marshal(members)
if strings.Contains(string(raw), "private-") || strings.Contains(string(raw), "department_ids") {
t.Fatal("internal identifiers serialized")
}
d.People[0].Name = "bad\x00name"
if _, err = BuildAttribution(State{State: "ready", DirectoryID: "directory"}, d); err == nil {
t.Fatal("invalid display name accepted")
}
}
+2 -1
View File
@@ -15,6 +15,7 @@ type Department struct {
Name string `json:"name"`
}
type Person struct {
Name string `json:"name,omitempty"`
ID string `json:"id"`
UnionID string `json:"union_id,omitempty"`
DepartmentIDs []string `json:"department_ids"`
@@ -107,7 +108,7 @@ func Preview(directory Directory, rules []Rule) (PreviewResult, error) {
}
people := map[string]bool{}
for _, person := range directory.People {
if !validID(person.ID) || people[person.ID] || len(person.DepartmentIDs) > 1000 {
if !validID(person.ID) || people[person.ID] || len(person.DepartmentIDs) > 1000 || len(person.Name) > 1000 || strings.ContainsRune(person.Name, 0) {
return result, ErrInvalid
}
people[person.ID] = true