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

Commit

Permalink
Adding file config and user whitelist
Browse files Browse the repository at this point in the history
  • Loading branch information
Stig Lindqvist committed Sep 1, 2016
1 parent 9d13dbd commit 135cbb4
Show file tree
Hide file tree
Showing 4 changed files with 295 additions and 112 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [0.3.0] - 2016-09-02
### Added
- JSON file for configuration
- User Whitelist

## [0.2.0] - 2016-08-31
### Added
- Find pull requests from Gitlab
Expand Down
45 changes: 37 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,56 @@ If you want to cross-compile for other platform, see http://golangcookbook.com/c

## configuration

purr can be configured with a JSON file and ENV variables. The ENV variables takes
precedence over the file configuration.

Example JSON

```
{
"github_token": "secret_token",
"github_repos": [
"user1/repo1",
"user2/repo1"
],
"gitlab_token": "secret_token",
"gitlab_repos": [
"project1/repo1",
"project2/repo1"
],
"gitlab_url": "https://www.example.com",
"slack_token": "secret_token",
"slack_channel": "myteamchat",
"user_whitelist": [
"user1",
"user2"
]
}
```

The ENV variables are

```
export GITHUB_TOKEN="<super_secret_github token>"
export GITHUB_REPOS="user_org/repo1,user_org/repo2" # comma separated
export GITLAB_TOKEN="<super_secret_github token>"
export GITLAB_URL="http://example.com"
export GITLAB_REPOS="project1/repo1,project2/repo1"
export SLACK_TOKEN="<super_secret_slack_token>"
export SLACK_CHANNEL="my_slack_room"
export USER_WHITELIST="user1,user2"
```

Optional configuration for Gitlab is
GitLab configuration is optional.

```
export GITLAB_TOKEN="<super_secret_gitlab token>"
export GITLAB_URL="https://gitlab.example.com"
export GITLAB_REPOS="namespace/project1,namespace/project2"
```
User whitelist will only show PR created by or assigned to that user.

It will send the message as a bot user with the name `purr` and use the emoticon
`:purr:` as its avatar in slack.

## run it

`purr`
`purr --config my_team.json`

This is a one shot action, so you might want to put into a cron or a [systemd timer unit](https://wiki.archlinux.org/index.php/Systemd/Timers)

Expand All @@ -53,7 +82,7 @@ GITHUB_TOKEN="<super_secret_github token>"
SLACK_TOKEN="<super_secret_slack_token>"
GITHUB_REPOS="user_org/repo1,user_org/repo2" # comma separated
SLACK_CHANNEL="my_slack_room"
0 8 * * * username /usr/bin/purr
0 8 * * * username /usr/bin/purr --config /etc/purr/my_team.json
```

## End result:
Expand Down
113 changes: 113 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
)

// Config contains the settings from the user
type Config struct {
GitHubToken string `json:"github_token"`
GitHubRepos []string `json:"github_repos"`
GitLabToken string `json:"gitlab_token"`
GitLabRepos []string `json:"gitlab_repos"`
GitlabURL string `json:"gitlab_url"`
SlackToken string `json:"slack_token"`
SlackChannel string `json:"slack_channel"`
UserWhiteList []string `json:"user_whitelist"`
}

func newConfig() *Config {

c := &Config{}

if configFile != "" {
file, e := ioutil.ReadFile(configFile)
if e != nil {
usageAndExit(e.Error(), 1)
}

if err := json.Unmarshal(file, &c); err != nil {
usageAndExit(err.Error(), 1)
}
}

if os.Getenv("GITHUB_TOKEN") != "" {
c.GitHubToken = os.Getenv("GITHUB_TOKEN")
}
if os.Getenv("GITHUB_REPOS") != "" {
c.GitHubRepos = strings.Split(os.Getenv("GITHUB_REPOS"), ",")
}
if os.Getenv("GITLAB_TOKEN") != "" {
c.GitLabToken = os.Getenv("GITLAB_TOKEN")
}
if os.Getenv("GITLAB_REPOS") != "" {
c.GitLabRepos = strings.Split(os.Getenv("GITLAB_REPOS"), ",")
}
if os.Getenv("GITLAB_URL") != "" {
c.GitlabURL = os.Getenv("GITLAB_URL")
}
if os.Getenv("SLACK_TOKEN") != "" {
c.SlackToken = os.Getenv("SLACK_TOKEN")
}
if os.Getenv("SLACK_CHANNEL") != "" {
c.SlackChannel = os.Getenv("SLACK_CHANNEL")
}
if os.Getenv("USER_WHITELIST") != "" {
c.UserWhiteList = strings.Split(os.Getenv("USER_WHITELIST"), ",")
}
return c
}

func (c *Config) validate() []error {
var errors []error
if c.GitHubToken == "" {
errors = append(errors, fmt.Errorf("GitHub token cannot be empty"))
}
if len(c.GitHubRepos) == 0 {
errors = append(errors, fmt.Errorf("GitHub repos cannot be empty"))
}
if c.SlackToken == "" {
errors = append(errors, fmt.Errorf("Slack token cannot be empty"))
}
if c.SlackChannel == "" {
errors = append(errors, fmt.Errorf("Slack channel cannot be empty"))
}

return errors
}

func configHelp() {
fmt.Fprintln(os.Stderr, "\npurr requrires configuration to be either in a config file or set the ENV")

fmt.Fprintln(os.Stderr, "\nThe configuration file (--config) looks like this:")

exampleConfig := &Config{
GitHubToken: "secret_token",
GitHubRepos: []string{"user1/repo1", "user2/repo1"},
GitLabToken: "secret_token",
GitLabRepos: []string{"project1/repo1", "project2/repo1"},
GitlabURL: "https://www.example.com",
SlackToken: "secret_token",
SlackChannel: "myteamchat",
UserWhiteList: []string{"user1", "user2"},
}
b, err := json.MarshalIndent(exampleConfig, "", " ")
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s", err)
}
fmt.Fprintf(os.Stderr, "\n%s\n\n", b)

fmt.Fprint(os.Stderr, "The above configuration can be overridden with ENV variables:\n\n")
fmt.Fprintln(os.Stderr, " * GITHUB_TOKEN")
fmt.Fprintln(os.Stderr, " * GITHUB_REPOS - comma separated list")
fmt.Fprintln(os.Stderr, " * GITLAB_TOKEN")
fmt.Fprintln(os.Stderr, " * GITLAB_URL")
fmt.Fprintln(os.Stderr, " * GITLAB_REPOS - comma separated list")
fmt.Fprintln(os.Stderr, " * SLACK_TOKEN")
fmt.Fprintln(os.Stderr, " * SLACK_CHANNEL")
fmt.Fprintln(os.Stderr, " * USER_WHITELIST - comma separated list")
}
Loading

0 comments on commit 135cbb4

Please sign in to comment.