Skip to content
This repository was archived by the owner on May 15, 2023. It is now read-only.

Commit a84a81f

Browse files
author
Stig Lindqvist
committed
support getting all of an organisations repos
1 parent 581494b commit a84a81f

File tree

4 files changed

+88
-40
lines changed

4 files changed

+88
-40
lines changed

CHANGELOG.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,31 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## [0.6.0] - 2017-10-13
8+
9+
### Changed
10+
11+
- Configuration to get private and public repositories for Organisations
12+
- Configuration to get users public repositories
13+
14+
### Removed
15+
16+
- Configuration for getting repositories with wildcard syntax
17+
18+
## [0.5.1] - 2017-10-11
19+
20+
### Added
21+
- use of glide as a dependency manager to lock down the gitlab client to v3 of the gitlab API
22+
23+
### Changed
24+
- escape &, > and < characters for slack
25+
- better Work-In-Progress (WIP) detection from the pull request titles
26+
27+
## [0.5.0] - 2017-05-09
28+
29+
### Changed
30+
- Exclude Github PR that are currently under review
31+
732
## [0.4.0] - 2016-09-03
833

934
### Added

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ Example JSON
5959
```
6060
{
6161
"github_token": "secret_token",
62+
"github_organisations": [
63+
"facebook"
64+
],
65+
"github_user": [
66+
"stojg"
67+
],
6268
"github_repos": [
6369
"user1/repo1",
6470
"user2/repo1"
@@ -78,18 +84,15 @@ Example JSON
7884
}
7985
```
8086

81-
To get all repos for a github user / organisation, replace the repository name with a star:
82-
83-
```
84-
"github_repos": [ "user1/*" ]
85-
86-
```
87-
87+
Note that `github_organisations` will get all public and private repos and that `github_user` will only get the public
88+
repos for a user due to how gitlab works.
8889

8990
The ENV variables are
9091

9192
```
9293
export GITHUB_TOKEN="<super_secret_github token>"
94+
export GITHUB_ORGANISATIONS - "facebook,twitter"
95+
export GITHUB_USERS - "stojg,KentBeck"
9396
export GITHUB_REPOS="user_org/repo1,user_org/repo2" # comma separated
9497
export GITLAB_TOKEN="<super_secret_github token>"
9598
export GITLAB_URL="http://example.com"
@@ -99,8 +102,6 @@ export SLACK_CHANNEL="my_slack_room"
99102
export USER_WHITELIST="user1,user2"
100103
```
101104

102-
GitLab configuration is optional.
103-
104105
## run it
105106

106107
`purr --config my_team.json`

config.go

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ import (
1010

1111
// Config contains the settings from the user
1212
type Config struct {
13-
GitHubToken string `json:"github_token"`
14-
GitHubRepos []string `json:"github_repos"`
15-
GitLabToken string `json:"gitlab_token"`
16-
GitLabRepos []string `json:"gitlab_repos"`
17-
GitlabURL string `json:"gitlab_url"`
18-
SlackToken string `json:"slack_token"`
19-
SlackChannel string `json:"slack_channel"`
20-
UserWhiteList []string `json:"user_whitelist"`
13+
GitHubToken string `json:"github_token"`
14+
GitHubOrganisations []string `json:"github_organisations"`
15+
GitHubUsers []string `json:"github_user"`
16+
GitHubRepos []string `json:"github_repos"`
17+
GitLabToken string `json:"gitlab_token"`
18+
GitLabRepos []string `json:"gitlab_repos"`
19+
GitlabURL string `json:"gitlab_url"`
20+
SlackToken string `json:"slack_token"`
21+
SlackChannel string `json:"slack_channel"`
22+
UserWhiteList []string `json:"user_whitelist"`
2123
}
2224

2325
func newConfig(filePath string) (*Config, error) {
@@ -38,6 +40,12 @@ func newConfig(filePath string) (*Config, error) {
3840
if os.Getenv("GITHUB_TOKEN") != "" {
3941
c.GitHubToken = os.Getenv("GITHUB_TOKEN")
4042
}
43+
if os.Getenv("GITHUB_ORGANISATIONS") != "" {
44+
c.GitHubOrganisations = strings.Split(os.Getenv("GITHUB_ORGANISATIONS"), ",")
45+
}
46+
if os.Getenv("GITHUB_USERS") != "" {
47+
c.GitHubUsers = strings.Split(os.Getenv("GITHUB_USERS"), ",")
48+
}
4149
if os.Getenv("GITHUB_REPOS") != "" {
4250
c.GitHubRepos = strings.Split(os.Getenv("GITHUB_REPOS"), ",")
4351
}
@@ -67,9 +75,6 @@ func (c *Config) validate() []error {
6775
if c.GitHubToken == "" {
6876
errors = append(errors, fmt.Errorf("GitHub token cannot be empty"))
6977
}
70-
if len(c.GitHubRepos) == 0 {
71-
errors = append(errors, fmt.Errorf("GitHub repos cannot be empty"))
72-
}
7378
if c.SlackToken == "" {
7479
errors = append(errors, fmt.Errorf("Slack token cannot be empty"))
7580
}
@@ -86,14 +91,16 @@ func configHelp() {
8691
fmt.Fprintln(os.Stderr, "\nThe configuration file (--config) looks like this:")
8792

8893
exampleConfig := &Config{
89-
GitHubToken: "secret_token",
90-
GitHubRepos: []string{"user1/repo1", "user2/repo1"},
91-
GitLabToken: "secret_token",
92-
GitLabRepos: []string{"project1/repo1", "project2/repo1"},
93-
GitlabURL: "https://www.example.com",
94-
SlackToken: "secret_token",
95-
SlackChannel: "myteamchat",
96-
UserWhiteList: []string{"user1", "user2"},
94+
GitHubToken: "secret_token",
95+
GitHubOrganisations: []string{"facebook"},
96+
GitHubUsers: []string{"stojg"},
97+
GitHubRepos: []string{"user1/repo1", "user2/repo1"},
98+
GitLabToken: "secret_token",
99+
GitLabRepos: []string{"project1/repo1", "project2/repo1"},
100+
GitlabURL: "https://www.example.com",
101+
SlackToken: "secret_token",
102+
SlackChannel: "myteamchat",
103+
UserWhiteList: []string{"user1", "user2"},
97104
}
98105
b, err := json.MarshalIndent(exampleConfig, "", " ")
99106
if err != nil {
@@ -103,6 +110,8 @@ func configHelp() {
103110

104111
fmt.Fprint(os.Stderr, "The above configuration can be overridden with ENV variables:\n\n")
105112
fmt.Fprintln(os.Stderr, " * GITHUB_TOKEN")
113+
fmt.Fprintln(os.Stderr, " * GITHUB_ORGANISATIONS - comma separated list")
114+
fmt.Fprintln(os.Stderr, " * GITHUB_USERS - comma separated list")
106115
fmt.Fprintln(os.Stderr, " * GITHUB_REPOS - comma separated list")
107116
fmt.Fprintln(os.Stderr, " * GITLAB_TOKEN")
108117
fmt.Fprintln(os.Stderr, " * GITLAB_URL")

main.go

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,27 +108,40 @@ func trawlGitHub(conf *Config) <-chan *PullRequest {
108108
tc := oauth2.NewClient(oauth2.NoContext, ts)
109109
client := github.NewClient(tc)
110110

111-
// check for wildcards in the repo name and expand them into individual repos
112111
var repos []string
113-
for _, repoName := range conf.GitHubRepos {
114-
repoParts := strings.Split(repoName, "/")
115-
if len(repoParts) != 2 {
116-
logrus.Errorf("%s is not a valid GitHub repository\n", repoName)
112+
113+
// check for a organisation and all it's repositories
114+
for _, organisationName := range conf.GitHubOrganisations {
115+
// first try listing by organisation
116+
allRepos, _, err := client.Repositories.ListByOrg(context.Background(), organisationName, nil)
117+
if err != nil {
118+
logrus.Errorf("Failed getting repositories for organisation %s: %v", organisationName, err)
117119
continue
118120
}
119-
if repoParts[1] != "*" {
120-
repos = append(repos, repoName)
121-
continue
121+
for i := range allRepos {
122+
repos = append(repos, *allRepos[i].FullName)
122123
}
123-
logrus.Debugf("expanding wildcard on %s", repoName)
124-
allRepos, _, err := client.Repositories.List(context.Background(), repoParts[0], nil)
124+
}
125+
126+
for _, user := range conf.GitHubUsers {
127+
// first try listing by organisation
128+
allRepos, _, err := client.Repositories.List(context.Background(), user, nil)
125129
if err != nil {
126-
logrus.Error(err)
130+
logrus.Errorf("Failed getting repositories for organisation %s: %v", user, err)
127131
continue
128132
}
129133
for i := range allRepos {
130-
repos = append(repos, fmt.Sprintf("%s/%s", repoParts[0], *allRepos[i].Name))
134+
repos = append(repos, *allRepos[i].FullName)
135+
}
136+
}
137+
138+
for _, repoName := range conf.GitHubRepos {
139+
repoParts := strings.Split(repoName, "/")
140+
if len(repoParts) != 2 {
141+
logrus.Errorf("%s is not a valid GitHub repository\n", repoName)
142+
continue
131143
}
144+
repos = append(repos, repoName)
132145
}
133146

134147
// spin out each request to find PRs on a repo into a separate goroutine so we fetch them

0 commit comments

Comments
 (0)