summaryrefslogtreecommitdiff
blob: 09dc24775071a8f1002d73ac8a995c7042525fd8 (plain)
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
// 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
}