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

Commit

Permalink
Deduplicate github repos and gitlab projects
Browse files Browse the repository at this point in the history
  • Loading branch information
Stig Lindqvist committed Dec 6, 2017
1 parent 2f20c34 commit a99e9c5
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
add further global filters
- enable and disable filters via the config
- output the number of filtered pull requests
- deduplicate github and gitlab repositories

## [0.6.0] - 2017-10-13

Expand Down
14 changes: 14 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,23 @@ func newConfig(filePath string) (*Config, error) {
config.Filters.Add(filters.Review)
config.Filters.Add(filters.WIP)

config.GitHubRepos = deduplicate(config.GitHubRepos)
config.GitLabRepos = deduplicate(config.GitLabRepos)
return config, nil
}

func deduplicate(s []string) []string {
m := make(map[string]bool)
for _, v := range s {
if _, seen := m[v]; !seen {
s[len(m)] = v
m[v] = true
}
}
s = s[:len(m)]
return s
}

func (c *Config) validate() []error {
var errors []error
if c.SlackToken == "" {
Expand Down
30 changes: 28 additions & 2 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import "testing"
import (
"testing"
)

func TestNewConfig(t *testing.T) {
config, err := newConfig("testdata/test_config.json")
Expand Down Expand Up @@ -105,7 +107,31 @@ func TestNewConfig_NoFilters(t *testing.T) {
}

if len(config.Filters.filters) != 3 {
t.Errorf("Expected 3 filters, got got '%d'", len(config.Filters.filters))
t.Errorf("Expected 3 filters, got '%d'", len(config.Filters.filters))
return
}
}

func TestNewConfig_Deduplication(t *testing.T) {
config, err := newConfig("testdata/test_config_duplicate_repos.json")
if err != nil {
t.Error(err)
return
}

validationErrors := config.validate()
if len(validationErrors) != 0 {
for _, err := range validationErrors {
t.Errorf("Did not expect validation error: %+v", err)
}
return
}

if len(config.GitHubRepos) != 2 {
t.Errorf("Expected 2 github repos, got '%d'", len(config.GitHubRepos))
}

if len(config.GitLabRepos) != 2 {
t.Errorf("Expected 2 gitlab repos, got '%d'", len(config.GitLabRepos))
}
}
23 changes: 23 additions & 0 deletions testdata/test_config_duplicate_repos.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"github_token": "secret_github_token",
"github_organisations": [
"facebook"
],
"github_users": [
"stojg"
],
"github_repos": [
"user1/repo1",
"user2/repo1",
"user2/repo1"
],
"gitlab_token": "secret_gitlab_token",
"gitlab_repos": [
"project1/repo1",
"project2/repo1",
"project2/repo1"
],
"gitlab_url": "https://www.example.com",
"slack_token": "secret_slack_token",
"slack_channel": "myteamchat"
}

0 comments on commit a99e9c5

Please sign in to comment.