diff options
author | Max Magorsch <arzano@gentoo.org> | 2020-12-08 01:21:04 +0000 |
---|---|---|
committer | Max Magorsch <arzano@gentoo.org> | 2020-12-08 01:21:04 +0000 |
commit | ba76c05ceca6a7879678873f360cdaf575f0f493 (patch) | |
tree | 597f2ee142ca4e57f1daffd29286e533c10a397a /pkg/models/user.go | |
download | go-gentoo-ba76c05ceca6a7879678873f360cdaf575f0f493.tar.gz go-gentoo-ba76c05ceca6a7879678873f360cdaf575f0f493.tar.bz2 go-gentoo-ba76c05ceca6a7879678873f360cdaf575f0f493.zip |
Initial version
Signed-off-by: Max Magorsch <arzano@gentoo.org>
Diffstat (limited to 'pkg/models/user.go')
-rw-r--r-- | pkg/models/user.go | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/pkg/models/user.go b/pkg/models/user.go new file mode 100644 index 0000000..09dc247 --- /dev/null +++ b/pkg/models/user.go @@ -0,0 +1,61 @@ +// Contains the model of the application data + +package models + +import ( + "encoding/xml" + "io/ioutil" + "net/http" + "strings" +) + +type User struct { + Email string `pg:",pk"` + RealName string + UserName string + Projects []string +} + +func (u *User) IsAdmin() bool { + for _, project := range u.Projects { + if project == "infra" { + return true + } + } + return false +} + +func (u *User) ComputeProjects() error { + projects, err := parseProjectList() + + if err != nil { + return err + } + + for _, project := range projects.Projects { + for _, member := range project.Members { + if member.Email == u.Email { + abbreviation := strings.ReplaceAll(project.Email, "@gentoo.org", "") + u.Projects = append(u.Projects, abbreviation) + } + } + } + + return nil +} + +// parseQAReport gets the xml from qa-reports.gentoo.org and parses it +func parseProjectList() (ProjectList, error) { + resp, err := http.Get("https://api.gentoo.org/metastructure/projects.xml") + if err != nil { + return ProjectList{}, err + } + defer resp.Body.Close() + xmlData, err := ioutil.ReadAll(resp.Body) + if err != nil { + return ProjectList{}, err + } + var projectList ProjectList + xml.Unmarshal(xmlData, &projectList) + return projectList, err +} |