Skip to content

Commit 0fa17aa

Browse files
author
thisisaaronland
committed
update wof-create-hooks to use common list repos code; update vendor deps
1 parent 334f072 commit 0fa17aa

File tree

386 files changed

+60199
-89966
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

386 files changed

+60199
-89966
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2016, Mapzen
1+
Copyright (c) 2020, Aaron Straup Cope
22
All rights reserved.
33

44
Redistribution and use in source and binary forms, with or without

README.md

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,44 @@ _All of the webhook tools need some documentation loving..._
2020

2121
#### wof-create-hook
2222

23-
_Please write me_
24-
2523
```
26-
./bin/wof-create-hook -token {TOKEN} -hook-url {URL} -hook-secret {SECRET} -org whosonfirst-data -repo whosonfirst-data-venue-us-il
24+
> ./bin/wof-create-hook -h
25+
Usage of ./bin/wof-create-hook:
26+
-dryrun
27+
Go through the motions but don't create webhooks.
28+
-exclude value
29+
Exclude repositories with this prefix
30+
-hook-content-type string
31+
The content type for your webhook. (default "json")
32+
-hook-secret string
33+
The secret for your webhook.
34+
-hook-url string
35+
A valid webhook URL.
36+
-org string
37+
The GitHub organization to create webhookd in.
38+
-prefix value
39+
Limit repositories to only those with this prefix
40+
-token string
41+
A valid GitHub API access token.
2742
```
2843

29-
You can also create webhooks for all of the repositories in an organization by passing the `-repo '*'` flag. You can still filter the list of repos by setting the `-prefix` flag.
44+
For example:
3045

3146
```
32-
./bin/wof-create-hook -token {TOKEN} -hook-url {URL} -hook-secret {SECRET} -org whosonfirst-data -repo '*' -prefix whosonfirst-data
33-
fetching repo list...🕓
34-
2017/04/05 15:42:24 webhook already configured for whosonfirst-data, skipping
35-
2017/04/05 15:42:24 created webhook for whosonfirst-data-venue-us-wv
36-
2017/04/05 15:42:25 created webhook for whosonfirst-data-venue-us-ne
37-
2017/04/05 15:42:25 created webhook for whosonfirst-data-venue-us-wi
38-
2017/04/05 15:42:25 created webhook for whosonfirst-data-venue-us-nv
39-
2017/04/05 15:42:25 created webhook for whosonfirst-data-venue-us-ar
40-
2017/04/05 15:42:25 created webhook for whosonfirst-data-venue-us-ms
41-
42-
...and so on
47+
$> ./bin/wof-create-hook -org sfomuseum-data -hook-secret {SECRET} -hook-url {WEBHOOK_URL} -prefix sfomuseum-data -exclude sfomuseum-data-flights -exclude sfomuseum-data-faa -token {GITHUB_TOKEN}
48+
2020/09/08 16:46:17 created webhook for sfomuseum-data-whosonfirst
49+
2020/09/08 16:46:18 created webhook for sfomuseum-data-publicart
50+
2020/09/08 16:46:19 created webhook for sfomuseum-data-architecture
51+
2020/09/08 16:46:20 created webhook for sfomuseum-data-maps
52+
2020/09/08 16:46:21 created webhook for sfomuseum-data-exhibition
53+
2020/09/08 16:46:22 created webhook for sfomuseum-data-enterprise
54+
2020/09/08 16:46:23 created webhook for sfomuseum-data-aircraft
55+
2020/09/08 16:46:24 created webhook for sfomuseum-data-media
56+
2020/09/08 16:46:25 created webhook for sfomuseum-data-media-flickr
57+
2020/09/08 16:46:26 created webhook for sfomuseum-data-testing
58+
2020/09/08 16:46:28 created webhook for sfomuseum-data-collection-classifications
59+
2020/09/08 16:46:29 created webhook for sfomuseum-data-media-collection
60+
2020/09/08 16:46:29 created webhook for sfomuseum-data-collection
4361
```
4462

4563
#### wof-list-hooks

cmd/wof-create-hook/main.go

Lines changed: 54 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,29 @@ package main
99
import (
1010
"flag"
1111
"fmt"
12-
"github.com/briandowns/spinner"
1312
"github.com/google/go-github/v27/github"
13+
"github.com/sfomuseum/go-flags/multi"
14+
"github.com/whosonfirst/go-whosonfirst-github/organizations"
1415
"github.com/whosonfirst/go-whosonfirst-github/util"
1516
"log"
16-
"strings"
17-
"time"
1817
)
1918

2019
func main() {
2120

22-
org := flag.String("org", "", "")
23-
repo := flag.String("repo", "", "")
24-
token := flag.String("token", "", "...")
25-
prefix := flag.String("prefix", "", "Limit repositories to only those with this prefix")
21+
org := flag.String("org", "", "The GitHub organization to create webhookd in.")
22+
token := flag.String("token", "", "A valid GitHub API access token.")
2623

27-
// name := flag.String("hook-name", "web", "")
28-
url := flag.String("hook-url", "", "")
29-
content_type := flag.String("hook-content-type", "json", "")
30-
secret := flag.String("hook-secret", "", "")
24+
url := flag.String("hook-url", "", "A valid webhook URL.")
25+
content_type := flag.String("hook-content-type", "json", "The content type for your webhook.")
26+
secret := flag.String("hook-secret", "", "The secret for your webhook.")
27+
28+
var prefix multi.MultiString
29+
flag.Var(&prefix, "prefix", "Limit repositories to only those with this prefix")
30+
31+
var exclude multi.MultiString
32+
flag.Var(&exclude, "exclude", "Exclude repositories with this prefix")
33+
34+
dryrun := flag.Bool("dryrun", false, "Go through the motions but don't create webhooks.")
3135

3236
flag.Parse()
3337

@@ -41,119 +45,68 @@ func main() {
4145
log.Fatal(err)
4246
}
4347

44-
config := make(map[string]interface{})
48+
hook_config := make(map[string]interface{})
4549

46-
config["url"] = *url
47-
config["content_type"] = *content_type
48-
config["secret"] = *secret
50+
hook_config["url"] = *url
51+
hook_config["content_type"] = *content_type
52+
hook_config["secret"] = *secret
4953

5054
hook := github.Hook{
5155
// Name: name,
52-
Config: config,
56+
Config: hook_config,
5357
}
5458

55-
if *repo == "" {
56-
57-
_, _, err = client.Organizations.CreateHook(ctx, *org, &hook)
58-
59-
if err != nil {
60-
log.Fatal(err)
61-
}
62-
63-
} else {
64-
65-
has_hook := make(map[string]bool)
66-
67-
repos := make([]string, 0)
68-
69-
if *repo == "*" {
70-
71-
done := make(chan bool)
72-
73-
go func() {
74-
75-
sp := spinner.New(spinner.CharSets[38], 200*time.Millisecond)
76-
sp.Prefix = "fetching repo list..."
77-
sp.Start()
78-
79-
for {
80-
81-
select {
82-
case <-done:
83-
sp.Stop()
84-
return
85-
}
86-
}
87-
}()
88-
89-
repos_opts := &github.RepositoryListByOrgOptions{
90-
ListOptions: github.ListOptions{PerPage: 100},
91-
}
92-
93-
for {
59+
opts := organizations.NewDefaultListOptions()
9460

95-
repos_list, repos_rsp, err := client.Repositories.ListByOrg(ctx, *org, repos_opts)
61+
opts.Prefix = prefix
62+
opts.Exclude = exclude
63+
// opts.Forked = *forked
64+
// opts.NotForked = *not_forked
65+
opts.AccessToken = *token
9666

97-
if err != nil {
98-
log.Fatal(err)
99-
}
67+
repos, err := organizations.ListRepos(*org, opts)
10068

101-
for _, r := range repos_list {
102-
103-
if *prefix != "" && !strings.HasPrefix(*r.Name, *prefix) {
104-
continue
105-
}
106-
107-
repos = append(repos, *r.Name)
108-
109-
hooks_opts := github.ListOptions{PerPage: 100}
69+
if err != nil {
70+
log.Fatal(err)
71+
}
11072

111-
hooks, _, err := client.Repositories.ListHooks(ctx, *org, *r.Name, &hooks_opts)
73+
for _, r := range repos {
11274

113-
if err != nil {
114-
log.Fatal(err)
115-
}
75+
has_hook := false
11676

117-
for _, h := range hooks {
77+
hooks_opts := github.ListOptions{PerPage: 100}
11878

119-
if h.Config["url"] == *url {
120-
has_hook[*r.Name] = true
121-
break
122-
}
123-
}
79+
hooks, _, err := client.Repositories.ListHooks(ctx, *org, r, &hooks_opts)
12480

125-
}
81+
if err != nil {
82+
log.Fatal(err)
83+
}
12684

127-
if repos_rsp.NextPage == 0 {
128-
break
129-
}
85+
for _, h := range hooks {
13086

131-
repos_opts.ListOptions.Page = repos_rsp.NextPage
87+
if h.Config["url"] == *url {
88+
has_hook = true
89+
break
13290
}
133-
134-
done <- true
135-
136-
} else {
137-
repos = append(repos, *repo)
13891
}
13992

140-
for _, r := range repos {
141-
142-
_, ok := has_hook[r]
143-
144-
if ok {
145-
log.Println(fmt.Sprintf("webhook already configured for %s, skipping", r))
146-
continue
147-
}
93+
if has_hook {
94+
log.Println(fmt.Sprintf("webhook already configured for %s, skipping", r))
95+
continue
96+
}
14897

149-
_, _, err = client.Repositories.CreateHook(ctx, *org, r, &hook)
98+
if *dryrun {
99+
log.Printf("Create Webhook for %s\n", r)
100+
continue
101+
}
150102

151-
if err != nil {
152-
log.Fatal(err)
153-
}
103+
_, _, err = client.Repositories.CreateHook(ctx, *org, r, &hook)
154104

155-
log.Println(fmt.Sprintf("created webhook for %s", r))
105+
if err != nil {
106+
log.Fatal(err)
156107
}
108+
109+
log.Println(fmt.Sprintf("created webhook for %s", r))
157110
}
158111

159112
}

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.12
55
require (
66
github.com/briandowns/spinner v1.11.1
77
github.com/google/go-github/v27 v27.0.6
8-
github.com/sfomuseum/go-flags v0.3.1
8+
github.com/sfomuseum/go-flags v0.4.1
99
github.com/whosonfirst/iso8601duration v0.0.0-20150204201828-8da3af7a2a61
10-
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
10+
golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43
1111
)

0 commit comments

Comments
 (0)