Skip to content

Commit

Permalink
Merge pull request #27 from PRODYNA/feature/26-mark-foreign-domains
Browse files Browse the repository at this point in the history
feature/26 mark foreign domains
  • Loading branch information
dkrizic authored Mar 22, 2024
2 parents 62a024d + 70915ae commit 6b8b876
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 18 deletions.
7 changes: 6 additions & 1 deletion action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@ 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 }}
GITHUB_TOKEN: ${{ inputs.github-token }}
TEMPLATE_FILE: ${{ inputs.template-file }}
MARKDOWN_FILE: ${{ inputs.markdown-file }}
VERBOSE: ${{ inputs.verbose }}
OWN_DOMAINS: ${{ inputs.own-domains }}
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (
keyTemplateFile = "TEMPLATE_FILE"
keyVerbose = "VERBOSE"
keyMarkdownFile = "MARKDOWN_FILE"
keyOwnDomains = "OWN_DOMAINS"
)

type Config struct {
Expand All @@ -23,6 +24,7 @@ type Config struct {
GithubToken string
TemplateFile string
MarkdownFile string
OwnDomains string
}

func New() (*Config, error) {
Expand All @@ -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
Expand Down
15 changes: 1 addition & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion template/members.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
8 changes: 8 additions & 0 deletions userlist/constructor.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package userlist

import "strings"

func New(options ...func(*UserListConfig)) *UserListConfig {
config := &UserListConfig{
validated: false,
Expand Down Expand Up @@ -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, ",")
}
}
16 changes: 15 additions & 1 deletion userlist/members.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/shurcooL/githubv4"
"golang.org/x/oauth2"
"log/slog"
"strings"
"time"
)

Expand Down Expand Up @@ -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),
}
Expand All @@ -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)
Expand All @@ -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
}
5 changes: 4 additions & 1 deletion userlist/userlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type UserListConfig struct {
validated bool
loaded bool
userList UserList
ownDomains []string
}

type UserList struct {
Expand All @@ -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
}
Expand Down Expand Up @@ -80,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
}

Expand Down

0 comments on commit 6b8b876

Please sign in to comment.