From 7feb9075da5c7d8cd2ac7880ca7a1b5dd4de6461 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Darko=20Kri=C5=BEi=C4=87?= Date: Fri, 22 Mar 2024 12:09:16 +0100 Subject: [PATCH 1/2] Marking foreign domains --- action.yaml | 7 ++++++- config/config.go | 3 +++ template/members.tpl | 2 +- userlist/constructor.go | 8 ++++++++ userlist/members.go | 16 +++++++++++++++- userlist/userlist.go | 2 ++ 6 files changed, 35 insertions(+), 3 deletions(-) diff --git a/action.yaml b/action.yaml index bd94883..a238dd1 100644 --- a/action.yaml +++ b/action.yaml @@ -23,9 +23,13 @@ inputs: description: 'The verbosity level' required: false default: 1 + own-domains: + description: 'Own domains to filter users by email domain' + required: false + default: '' runs: using: 'docker' - image: 'docker://ghcr.io/prodyna/github-users:v1.8' + image: 'docker://ghcr.io/prodyna/github-users:v1.9' env: ACTION: ${{ inputs.action }} ENTERPRISE: ${{ inputs.enterprise }} @@ -33,3 +37,4 @@ runs: TEMPLATE_FILE: ${{ inputs.template-file }} MARKDOWN_FILE: ${{ inputs.markdown-file }} VERBOSE: ${{ inputs.verbose }} + OWN_DOMAINS: ${{ inputs.own-domains }} diff --git a/config/config.go b/config/config.go index 3592313..422750f 100644 --- a/config/config.go +++ b/config/config.go @@ -15,6 +15,7 @@ const ( keyTemplateFile = "TEMPLATE_FILE" keyVerbose = "VERBOSE" keyMarkdownFile = "MARKDOWN_FILE" + keyOwnDomains = "OWN_DOMAINS" ) type Config struct { @@ -23,6 +24,7 @@ type Config struct { GithubToken string TemplateFile string MarkdownFile string + OwnDomains string } func New() (*Config, error) { @@ -32,6 +34,7 @@ func New() (*Config, error) { flag.StringVar(&c.GithubToken, keyGithubToken, lookupEnvOrString("GITHUB_TOKEN", ""), "The GitHub Token to use for authentication.") flag.StringVar(&c.TemplateFile, keyTemplateFile, lookupEnvOrString("TEMPLATE_FILE", "template/members.tpl"), "The template file to use for rendering the result.") flag.StringVar(&c.MarkdownFile, keyMarkdownFile, lookupEnvOrString("MARKDOWN_FILE", "USERS.md"), "The markdown file to write the result to.") + flag.StringVar(&c.OwnDomains, keyOwnDomains, lookupEnvOrString("OWN_DOMAINS", ""), "The comma separated list of domains to consider as own domains.") verbose := flag.Int("verbose", lookupEnvOrInt(keyVerbose, 0), "Verbosity level, 0=info, 1=debug. Overrides the environment variable VERBOSE.") level := slog.LevelInfo diff --git a/template/members.tpl b/template/members.tpl index 0b013b7..cd8f853 100644 --- a/template/members.tpl +++ b/template/members.tpl @@ -4,7 +4,7 @@ Last updated: {{ .Updated }} | # | GitHub Login | GitHub name | E-Mail | Contributions | | --- | --- | --- | --- | --- | -{{ range .Users }} | {{ .Number }} | [{{ .Login }}](https://github.com/enterprises/{{ $.Enterprise.Slug }}/people/{{ .Login }}/sso) | {{ .Name }} | {{ .Email }} | {{if .Contributions}}:green_square:{{else}}:red_square:{{end}} [{{.Contributions }}](https://github.com/{{ .Login }}) | +{{ range .Users }} | {{ .Number }} | [{{ .Login }}](https://github.com/enterprises/{{ $.Enterprise.Slug }}/people/{{ .Login }}/sso) | {{ .Name }} | {{ if .IsOwnDomain }}:green_square:{{else}}:red_square:{{end}} {{ .Email }} | {{if .Contributions}}:green_square:{{else}}:red_square:{{end}} [{{.Contributions }}](https://github.com/{{ .Login }}) | {{ end }} {{ if .Users }}_{{ len .Users }} users_{{ else }}No users found.{{ end }} diff --git a/userlist/constructor.go b/userlist/constructor.go index 3b4e5e3..5813bb7 100644 --- a/userlist/constructor.go +++ b/userlist/constructor.go @@ -1,5 +1,7 @@ package userlist +import "strings" + func New(options ...func(*UserListConfig)) *UserListConfig { config := &UserListConfig{ validated: false, @@ -40,3 +42,9 @@ func WithMarkdownFile(markdownFile string) func(*UserListConfig) { config.markdownFile = markdownFile } } + +func WithOwnDomains(ownDomains string) func(*UserListConfig) { + return func(config *UserListConfig) { + config.ownDomains = strings.Split(ownDomains, ",") + } +} diff --git a/userlist/members.go b/userlist/members.go index 09e50a6..91f64de 100644 --- a/userlist/members.go +++ b/userlist/members.go @@ -5,6 +5,7 @@ import ( "github.com/shurcooL/githubv4" "golang.org/x/oauth2" "log/slog" + "strings" "time" ) @@ -57,7 +58,7 @@ func (c *UserListConfig) loadMembers() error { window := 25 variables := map[string]interface{}{ - "slug": githubv4.String("prodyna"), + "slug": githubv4.String(c.enterprise), "first": githubv4.Int(window), "after": (*githubv4.String)(nil), } @@ -81,6 +82,7 @@ func (c *UserListConfig) loadMembers() error { Login: e.Node.User.Login, Name: e.Node.User.Name, Email: e.Node.SamlIdentity.NameId, + IsOwnDomain: IsOwnDomain(e.Node.SamlIdentity.NameId, c.ownDomains), Contributions: e.Node.User.ContributionsCollection.ContributionCalendar.TotalContributions, } c.userList.upsertUser(u) @@ -97,3 +99,15 @@ func (c *UserListConfig) loadMembers() error { c.loaded = true return nil } + +func IsOwnDomain(email string, ownDomains []string) bool { + if len(ownDomains) == 0 { + return true + } + for _, domain := range ownDomains { + if strings.HasSuffix(email, domain) { + return true + } + } + return false +} diff --git a/userlist/userlist.go b/userlist/userlist.go index cd709c0..cb4ec5c 100644 --- a/userlist/userlist.go +++ b/userlist/userlist.go @@ -25,6 +25,7 @@ type UserListConfig struct { validated bool loaded bool userList UserList + ownDomains []string } type UserList struct { @@ -44,6 +45,7 @@ type User struct { Login string `json:"Login"` Name string `json:"Name"` Email string `json:"Email"` + IsOwnDomain bool `json:"IsOwnDomain"` Contributions int `json:"Contributions"` Organizations *[]Organization } From 70915aeb2487e108ea89170554bf8be8b149d916 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Darko=20Kri=C5=BEi=C4=87?= Date: Fri, 22 Mar 2024 12:19:35 +0100 Subject: [PATCH 2/2] Implementation done --- main.go | 15 +-------------- userlist/userlist.go | 3 ++- 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/main.go b/main.go index dc90d7e..4d3af92 100644 --- a/main.go +++ b/main.go @@ -7,20 +7,6 @@ import ( "os" ) -const ( - keyAction = "ACTION" - keyOrganization = "ORGANIZATION" - keyGithubToken = "GITHUB_TOKEN" - keyTemplateFile = "TEMPLATE_FILE" -) - -type Config struct { - Action string - Enterprise string - GithubToken string - TemplateFile string -} - func main() { c, err := config.New() if err != nil { @@ -34,6 +20,7 @@ func main() { userlist.WithGithubToken(c.GithubToken), userlist.WithTemplateFile(c.TemplateFile), userlist.WithMarkdownFile(c.MarkdownFile), + userlist.WithOwnDomains(c.OwnDomains), ) err = ulc.Validate() diff --git a/userlist/userlist.go b/userlist/userlist.go index cb4ec5c..fdb919f 100644 --- a/userlist/userlist.go +++ b/userlist/userlist.go @@ -82,7 +82,8 @@ func (c *UserListConfig) Validate() error { "enterprise", c.enterprise, "template", c.templateFile, "githubToken", "***", - "markdownFile", c.markdownFile) + "markdownFile", c.markdownFile, + slog.Any("ownDomains", c.ownDomains)) return nil }