Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/30 json output #31

Merged
merged 11 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.idea/
.env
USERS.md
github-users
output.json
output.md
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
FROM golang:1.22.1-alpine3.19 as build
FROM golang:1.23.1-alpine3.20 as build

WORKDIR /app
COPY . /app
RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build --ldflags '-extldflags=-static' -o github-users main.go

FROM alpine:3.19.1
FROM alpine:3.20
COPY --from=build /app/github-users /app/
COPY /template /template
ENTRYPOINT ["/app/github-users"]
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ jobs:
# The template file to use for rendering the result
template-file: template/members.tpl
# The markdown file to write the result to
markdown-file: MEMBERS.md
output-file: MEMBERS.md
# Verbosity level, 0=info, 1=debug
verbose: 1

Expand All @@ -69,7 +69,7 @@ jobs:
# The template file to use for rendering the result
template-file: template/collaborators.tpl
# The markdown file to write the result to
markdown-file: COLLABORATORS.md
output-file: COLLABORATORS.md
# Verbosity level, 0=info, 1=debug
verbose: 1

Expand Down
16 changes: 8 additions & 8 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ inputs:
github-token:
description: 'The GitHub Token to use for authentication'
required: true
template-file:
template-files:
description: 'The template file to use for rendering the result'
required: false
default: '/template/members.tpl'
markdown-file:
description: 'The markdown file to write the result to'
default: '/template/markdown/members.tpl,/template/json/members.tpl'
output-files:
description: 'The output files to write the result to'
required: false
default: 'USERS.md'
default: 'MEMBERS.md,members.json'
verbose:
description: 'The verbosity level'
required: false
Expand All @@ -29,12 +29,12 @@ inputs:
default: ''
runs:
using: 'docker'
image: 'docker://ghcr.io/prodyna/github-users:v1.9'
image: 'docker://ghcr.io/prodyna/github-users:v2.0'
env:
ACTION: ${{ inputs.action }}
ENTERPRISE: ${{ inputs.enterprise }}
GITHUB_TOKEN: ${{ inputs.github-token }}
TEMPLATE_FILE: ${{ inputs.template-file }}
MARKDOWN_FILE: ${{ inputs.markdown-file }}
TEMPLATE_FILES: ${{ inputs.template-files }}
OUTPUT_FILES: ${{ inputs.markdown-files }}
VERBOSE: ${{ inputs.verbose }}
OWN_DOMAINS: ${{ inputs.own-domains }}
47 changes: 27 additions & 20 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,40 @@ import (
)

const (
keyAction = "ACTION"
keyEnterprise = "ENTERPRISE"
keyGithubToken = "GITHUB_TOKEN"
keyTemplateFile = "TEMPLATE_FILE"
keyVerbose = "VERBOSE"
keyMarkdownFile = "MARKDOWN_FILE"
keyOwnDomains = "OWN_DOMAINS"
keyAction = "action"
kkeyActionEnvironment = "ACTION"
keyEnterprise = "enterprise"
keyEnterpriseEnvironment = "ENTERPRISE"
keyGithubToken = "githubToken"
keyGithubTokenEnvironment = "GITHUB_TOKEN"
keyTemplateFiles = "template-files"
keyTemplateFilesEnvironment = "TEMPLATE_FILES"
keyOutputFiles = "output-files"
keyOutputFilesEnvironment = "OUTPUT_FILES"
keyVerbose = "verbose"
keyVerboseEnvironment = "VERBOSE"
keyOwnDomains = "own-domains"
keyOwnDomainsEnvironment = "OWN_DOMAINS"
)

type Config struct {
Action string
Enterprise string
GithubToken string
TemplateFile string
MarkdownFile string
OwnDomains string
Action string
Enterprise string
GithubToken string
TemplateFiles string
OutputFiles string
OwnDomains string
}

func New() (*Config, error) {
c := Config{}
flag.StringVar(&c.Action, keyAction, lookupEnvOrString("ACTION", ""), "The action to perform.")
flag.StringVar(&c.Enterprise, keyEnterprise, lookupEnvOrString("ENTERPRISE", ""), "The GitHub Enterprise to query for repositories.")
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.")
flag.StringVar(&c.Action, keyAction, lookupEnvOrString(kkeyActionEnvironment, ""), "The action to perform.")
flag.StringVar(&c.Enterprise, keyEnterprise, lookupEnvOrString(keyEnterpriseEnvironment, ""), "The GitHub Enterprise to query for repositories.")
flag.StringVar(&c.GithubToken, keyGithubToken, lookupEnvOrString(keyGithubTokenEnvironment, ""), "The GitHub Token to use for authentication.")
flag.StringVar(&c.TemplateFiles, keyTemplateFiles, lookupEnvOrString(keyTemplateFilesEnvironment, "template/members.tpl"), "The template file to use for rendering the result.")
flag.StringVar(&c.OutputFiles, keyOutputFiles, lookupEnvOrString(keyOutputFilesEnvironment, ""), "The output file to write the result to.")
flag.StringVar(&c.OwnDomains, keyOwnDomains, lookupEnvOrString(keyOwnDomainsEnvironment, ""), "The comma separated list of domains to consider as own domains.")
verbose := flag.Int(keyVerbose, lookupEnvOrInt(keyVerboseEnvironment, 0), "Verbosity level, 0=info, 1=debug. Overrides the environment variable VERBOSE.")

level := slog.LevelInfo
if *verbose > 0 {
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
module github.com/prodyna/github-users

go 1.22.1
go 1.23.1

require (
github.com/shurcooL/githubv4 v0.0.0-20240120211514-18a1ae0e79dc
golang.org/x/oauth2 v0.18.0
github.com/shurcooL/githubv4 v0.0.0-20240727222349-48295856cce7
golang.org/x/oauth2 v0.23.0
)

require (
github.com/golang/protobuf v1.5.4 // indirect
github.com/shurcooL/graphql v0.0.0-20230722043721-ed46e5a46466 // indirect
golang.org/x/net v0.22.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/protobuf v1.33.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/shurcooL/githubv4 v0.0.0-20240120211514-18a1ae0e79dc h1:vH0NQbIDk+mJLvBliNGfcQgUmhlniWBDXC79oRxfZA0=
github.com/shurcooL/githubv4 v0.0.0-20240120211514-18a1ae0e79dc/go.mod h1:zqMwyHmnN/eDOZOdiTohqIUKUrTFX62PNlu7IJdu0q8=
github.com/shurcooL/githubv4 v0.0.0-20240727222349-48295856cce7 h1:cYCy18SHPKRkvclm+pWm1Lk4YrREb4IOIb/YdFO0p2M=
github.com/shurcooL/githubv4 v0.0.0-20240727222349-48295856cce7/go.mod h1:zqMwyHmnN/eDOZOdiTohqIUKUrTFX62PNlu7IJdu0q8=
github.com/shurcooL/graphql v0.0.0-20230722043721-ed46e5a46466 h1:17JxqqJY66GmZVHkmAsGEkcIu0oCe3AM420QDgGwZx0=
github.com/shurcooL/graphql v0.0.0-20230722043721-ed46e5a46466/go.mod h1:9dIRpgIY7hVhoqfe0/FcYp0bpInZaT7dc3BYOprrIUE=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
Expand All @@ -24,6 +26,8 @@ golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/oauth2 v0.18.0 h1:09qnuIAgzdx1XplqJvW6CQqMCtGZykZWcXzPMPUusvI=
golang.org/x/oauth2 v0.18.0/go.mod h1:Wf7knwG0MPoWIMMBgFlEaSUDaKskp0dCfrlJRJXbBi8=
golang.org/x/oauth2 v0.23.0 h1:PbgcYx2W7i4LvjJWEbf0ngHV6qJYr86PkAV3bXdLEbs=
golang.org/x/oauth2 v0.23.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down Expand Up @@ -53,3 +57,5 @@ google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ func main() {
userlist.WithAction(c.Action),
userlist.WithEnterprise(c.Enterprise),
userlist.WithGithubToken(c.GithubToken),
userlist.WithTemplateFile(c.TemplateFile),
userlist.WithMarkdownFile(c.MarkdownFile),
userlist.WithTemplateFiles(c.TemplateFiles),
userlist.WithOutputFiles(c.OutputFiles),
userlist.WithOwnDomains(c.OwnDomains),
)

Expand Down
32 changes: 32 additions & 0 deletions template/json/collaborators.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"updated": "{{ .Updated }}",
"enterprise": {
"name": "{{ .Enterprise.Name }}",
"slug": "{{ .Enterprise.Slug }}"
},
"users": [{{ range $user := .Users }}
{
"number": {{ $user.Number }},
"login": "{{ $user.Login }}",
"contributions": {{ $user.Contributions }},
"organizations": [{{ range $org := $user.Organizations }}
{
"name": "{{ $org.Name }}",
"login": "{{ $org.Login }}",
"repositories": [{{ range $repo := $org.Repositories }}
{
"name": "{{ $repo.Name }}"
}{{ if not $repo.Last }},{{ end }}{{ end }}
]
}{{ if not $org.Last }},{{ end }}{{ end }}
]
}{{ if not $user.Last }},{{ end }}{{ end }}
],
"warnings": [{{ range .Warnings }}
"{{ .Message }}"{{ if not .Last }},{{ end }}{{ end }}
],
"generated": {
"by": "github-users",
"with": ":heart:"
}
}
26 changes: 26 additions & 0 deletions template/json/members.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"enterprise": {
"name": "{{ .Enterprise.Name }}",
"slug": "{{ .Enterprise.Slug }}",
"users": [{{ range .Users }}
{
"number": {{ .Number }},
"login": "{{ .Login }}",
"login_url": "https://github.com/enterprises/{{ $.Enterprise.Slug }}/people/{{ .Login }}/sso",
"name": "{{ .Name }}",
"email": "{{ .Email }}",
"contributions": {{ .Contributions }},
"is_own_domain": {{ .IsOwnDomain }}
}{{ if not .Last }},{{ end }}{{ end }}
]
},
"warnings": [{{ range .Warnings }}
"{{ . }}"{{ if not .Last }},{{ end }}
{{ end }}
],
"generated": {
"at": "{{ .Updated }}",
"by": "github-users",
"with": ":heart:"
}
}
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions userlist/constructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ func WithAction(action string) func(*UserListConfig) {
}
}

func WithTemplateFile(templateFile string) func(*UserListConfig) {
func WithTemplateFiles(templateFiles string) func(*UserListConfig) {
return func(config *UserListConfig) {
config.templateFile = templateFile
config.templateFiles = strings.Split(templateFiles, ",")
}
}

Expand All @@ -37,9 +37,9 @@ func WithGithubToken(githubToken string) func(*UserListConfig) {
}
}

func WithMarkdownFile(markdownFile string) func(*UserListConfig) {
func WithOutputFiles(outputFiles string) func(*UserListConfig) {
return func(config *UserListConfig) {
config.markdownFile = markdownFile
config.outputFiles = strings.Split(outputFiles, ",")
}
}

Expand Down
9 changes: 9 additions & 0 deletions userlist/members.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ func (c *UserListConfig) loadMembers() error {
variables["after"] = githubv4.NewString(query.Enterprise.OwnerInfo.SamlIdentityProvider.ExternalIdentities.PageInfo.EndCursor)
}

// iterate over all users and mark the last one as last
for i, u := range c.userList.Users {
if i == len(c.userList.Users)-1 {
u.Last = true
} else {
u.Last = false
}
}

slog.InfoContext(ctx, "Loaded userlist", "users", len(c.userList.Users))
c.loaded = true
return nil
Expand Down
Loading