Skip to content

Commit b78b78d

Browse files
committed
Finish template with right syntax
1 parent 762a4c2 commit b78b78d

File tree

3 files changed

+67
-19
lines changed

3 files changed

+67
-19
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/prodyna/github-users
22

3-
go 1.22.1
3+
go 1.23.1
44

55
require (
66
github.com/shurcooL/githubv4 v0.0.0-20240727222349-48295856cce7

template/json/collaborators.tpl

+32-15
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
1-
# GitHub Enterprise collaborators for {{ .Enterprise.Name }}
2-
3-
Last updated: {{ .Updated }}
4-
5-
| Number | User | Contributions | Organization | Repository |
6-
| ------ | ---- | ------------- | ------------ | ---------- |
7-
{{ range $user := .Users }}{{ range $org := $user.Organizations }}{{ range $repo := $org.Repositories }}| {{ $user.Number }} | [{{ $user.Login }}](https://github.com/{{ $user.Login }}) | {{if $user.Contributions}}:green_square:{{else}}:red_square:{{end}} {{ $user.Contributions }} | [{{ $org.Name }}](https://github.com/{{ $org.Login }}) | [{{ $repo.Name }}](https://github.com/{{ $org.Login }}/{{ $repo.Name }}) |
8-
{{ end }}{{ end }}{{ end }}
9-
10-
{{ if .Warnings }}
11-
## Warnings
12-
{{ range .Warnings }}* {{ . }}
13-
{{ end }}{{ end }}
14-
---
15-
Generated with :heart: by [github-users](https://github.com/prodyna/github-users)
1+
{
2+
"updated": "{{ .Updated }}",
3+
"enterprise": {
4+
"name": "{{ .Enterprise.Name }}",
5+
"slug": "{{ .Enterprise.Slug }}"
6+
},
7+
"users": [{{ range $user := .Users }}
8+
{
9+
"number": {{ $user.Number }},
10+
"login": "{{ $user.Login }}",
11+
"contributions": {{ $user.Contributions }},
12+
"organizations": [{{ range $org := $user.Organizations }}
13+
{
14+
"name": "{{ $org.Name }}",
15+
"login": "{{ $org.Login }}",
16+
"repositories": [{{ range $repo := $org.Repositories }}
17+
{
18+
"name": "{{ $repo.Name }}"
19+
}{{ if not $repo.Last }},{{ end }}{{ end }}
20+
]
21+
}{{ if not $org.Last }},{{ end }}{{ end }}
22+
]
23+
}{{ if not $user.Last }},{{ end }}{{ end }}
24+
],
25+
"warnings": [{{ range .Warnings }}
26+
"{{ .Message }}"{{ if not .Last }},{{ end }}{{ end }}
27+
],
28+
"generated": {
29+
"by": "github-users",
30+
"with": ":heart:"
31+
}
32+
}

userlist/userlist.go

+34-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ type UserList struct {
3232
Updated string `json:"updated"`
3333
Enterprise Enterprise `json:"enterprise"`
3434
Users []*User `json:"users"`
35-
Warnings []string `json:"warnings"`
35+
Warnings []*Warning `json:"warnings"`
36+
}
37+
38+
type Warning struct {
39+
Message string `json:"message"`
40+
Last bool `json:"last"`
3641
}
3742

3843
type Enterprise struct {
@@ -55,10 +60,12 @@ type Organization struct {
5560
Login string `json:"login"`
5661
Name string `json:"name"`
5762
Repositories *[]Repository `json:"repositories"`
63+
Last bool `json:"last"`
5864
}
5965

6066
type Repository struct {
6167
Name string `json:"name"`
68+
Last bool `json:"last"`
6269
}
6370

6471
func (c *UserListConfig) Validate() error {
@@ -174,6 +181,12 @@ func (ul *UserList) upsertUser(user User) {
174181
}
175182
}
176183
slog.Info("Upserting user", "login", user.Login)
184+
// mark all eixsting users as last = false
185+
for i, _ := range ul.Users {
186+
ul.Users[i].Last = false
187+
}
188+
// mark the new user as last = true
189+
user.Last = true
177190
ul.Users = append(ul.Users, &user)
178191
}
179192

@@ -209,6 +222,13 @@ func (u *User) upsertOrganization(org Organization) {
209222
return
210223
}
211224
}
225+
slog.Debug("Upserting organization", "name", org.Name)
226+
// mark all existing organizations as last = false
227+
for i, _ := range *u.Organizations {
228+
(*u.Organizations)[i].Last = false
229+
}
230+
// mark the new organization as last = true
231+
org.Last = true
212232
*u.Organizations = append(*u.Organizations, org)
213233
}
214234

@@ -220,6 +240,12 @@ func (o *Organization) upsertRepository(repo Repository) {
220240
}
221241
}
222242
slog.Debug("Upserting repository", "name", repo.Name, "organization", o.Name)
243+
// mark all existing repositories as last = false
244+
for i := range *o.Repositories {
245+
(*o.Repositories)[i].Last = false
246+
}
247+
// mark the new repository as last = true
248+
repo.Last = true
223249
*o.Repositories = append(*o.Repositories, repo)
224250
}
225251

@@ -237,6 +263,7 @@ func (u *User) createOrganization(login string, name string) *Organization {
237263
Login: login,
238264
Name: name,
239265
Repositories: new([]Repository),
266+
Last: false,
240267
}
241268
u.upsertOrganization(*org)
242269
return org
@@ -261,7 +288,11 @@ func (o *Organization) createRepository(name string) *Repository {
261288

262289
func (c *UserList) addWarning(warning string) {
263290
if c.Warnings == nil {
264-
c.Warnings = make([]string, 0)
291+
c.Warnings = make([]*Warning, 0)
292+
}
293+
// mark all exisint warnings as last = false
294+
for _, w := range c.Warnings {
295+
w.Last = false
265296
}
266-
c.Warnings = append(c.Warnings, warning)
297+
c.Warnings = append(c.Warnings, &Warning{Message: warning, Last: true})
267298
}

0 commit comments

Comments
 (0)