Skip to content

Commit 4313b5f

Browse files
Merge pull request #14 from m-lab/sandbox-soltesz-noop
Add an "in memory" option so non-production deployments can still run
2 parents 72bc343 + d58b2ed commit 4313b5f

File tree

3 files changed

+150
-2
lines changed

3 files changed

+150
-2
lines changed

cmd/github_receiver/main.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
"github.com/m-lab/alertmanager-github-receiver/alerts"
2828
"github.com/m-lab/alertmanager-github-receiver/issues"
29+
"github.com/m-lab/alertmanager-github-receiver/issues/local"
2930
"github.com/prometheus/client_golang/prometheus"
3031
"github.com/prometheus/client_golang/prometheus/promhttp"
3132
)
@@ -35,6 +36,7 @@ var (
3536
githubOrg = flag.String("org", "", "The github user or organization name where all repos are found.")
3637
githubRepo = flag.String("repo", "", "The default repository for creating issues when alerts do not include a repo label.")
3738
enableAutoClose = flag.Bool("enable-auto-close", false, "Once an alert stops firing, automatically close open issues.")
39+
enableInMemory = flag.Bool("enable-inmemory", false, "Perform all operations in memory, without using github API.")
3840
receiverPort = flag.String("port", "9393", "The port for accepting alertmanager webhook messages.")
3941
)
4042

@@ -66,7 +68,7 @@ func init() {
6668
}
6769
}
6870

69-
func serveReceiverHandler(client *issues.Client) {
71+
func serveReceiverHandler(client alerts.ReceiverClient) {
7072
receiverHandler := &alerts.ReceiverHandler{
7173
Client: client,
7274
DefaultRepo: *githubRepo,
@@ -84,6 +86,11 @@ func main() {
8486
flag.Usage()
8587
os.Exit(1)
8688
}
87-
client := issues.NewClient(*githubOrg, *authtoken)
89+
var client alerts.ReceiverClient
90+
if *enableInMemory {
91+
client = local.NewClient()
92+
} else {
93+
client = issues.NewClient(*githubOrg, *authtoken)
94+
}
8895
serveReceiverHandler(client)
8996
}

issues/local/local.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2017 alertmanager-github-receiver Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//////////////////////////////////////////////////////////////////////////////
15+
16+
// Package memory provides in memory operations on GitHub issues.
17+
package local
18+
19+
import (
20+
"fmt"
21+
"log"
22+
23+
"github.com/google/go-github/github"
24+
)
25+
26+
// Client manages operations on the in memory store.
27+
type Client struct {
28+
issues map[string]*github.Issue
29+
}
30+
31+
// NewClient creates a Client.
32+
func NewClient() *Client {
33+
return &Client{
34+
issues: make(map[string]*github.Issue),
35+
}
36+
}
37+
38+
// CreateIssue adds a new issue to the in memory store.
39+
func (c *Client) CreateIssue(repo, title, body string) (*github.Issue, error) {
40+
c.issues[title] = &github.Issue{
41+
Title: &title,
42+
Body: &body,
43+
}
44+
return c.issues[title], nil
45+
}
46+
47+
// ListOpenIssues returns all issues in the memory store.
48+
func (c *Client) ListOpenIssues() ([]*github.Issue, error) {
49+
var allIssues []*github.Issue
50+
for title := range c.issues {
51+
log.Println("ListOpenIssues:", title)
52+
allIssues = append(allIssues, c.issues[title])
53+
}
54+
return allIssues, nil
55+
}
56+
57+
// CloseIssue removes the issue from the in memory store.
58+
func (c *Client) CloseIssue(issue *github.Issue) (*github.Issue, error) {
59+
if _, ok := c.issues[issue.GetTitle()]; !ok {
60+
return nil, fmt.Errorf("Unknown issue:%s", issue.GetTitle())
61+
}
62+
delete(c.issues, issue.GetTitle())
63+
return issue, nil
64+
}

issues/local/local_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright 2017 alertmanager-github-receiver Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//////////////////////////////////////////////////////////////////////////////
15+
16+
package local
17+
18+
import (
19+
"reflect"
20+
"testing"
21+
22+
"github.com/google/go-github/github"
23+
)
24+
25+
func TestMemoryClient(t *testing.T) {
26+
tests := []struct {
27+
name string
28+
title string
29+
body string
30+
wantErr bool
31+
}{
32+
{
33+
name: "success",
34+
title: "alert name",
35+
body: "foobar",
36+
},
37+
}
38+
for _, tt := range tests {
39+
t.Run(tt.name, func(t *testing.T) {
40+
c := NewClient()
41+
got, err := c.CreateIssue("fake-repo", tt.title, tt.body)
42+
if (err != nil) != tt.wantErr {
43+
t.Errorf("Client.CreateIssue() error = %v, wantErr %v", err, tt.wantErr)
44+
return
45+
}
46+
wantIssue := &github.Issue{
47+
Title: &tt.title,
48+
Body: &tt.body,
49+
}
50+
if !reflect.DeepEqual(got, wantIssue) {
51+
t.Errorf("Client.CreateIssue() = %v, want %v", got, wantIssue)
52+
}
53+
list, err := c.ListOpenIssues()
54+
if (err != nil) != tt.wantErr {
55+
t.Errorf("Client.ListOpenIssues() error = %v, wantErr %v", err, tt.wantErr)
56+
return
57+
}
58+
wantList := []*github.Issue{
59+
&github.Issue{
60+
Title: &tt.title,
61+
Body: &tt.body,
62+
},
63+
}
64+
if !reflect.DeepEqual(list, wantList) {
65+
t.Errorf("Client.ListOpenIssues() = %v, want %v", list, wantList)
66+
}
67+
closed, err := c.CloseIssue(got)
68+
if (err != nil) != tt.wantErr {
69+
t.Errorf("Client.CloseIssue() error = %v, wantErr %v", err, tt.wantErr)
70+
return
71+
}
72+
if !reflect.DeepEqual(closed, got) {
73+
t.Errorf("Client.CloseIssue() = %v, want %v", closed, got)
74+
}
75+
})
76+
}
77+
}

0 commit comments

Comments
 (0)