Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
greboid committed Sep 19, 2020
0 parents commit 9b94746
Show file tree
Hide file tree
Showing 44 changed files with 4,678 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
*.exe
*.exe~
*.dll
*.so
*.dylib
*.test
*.out
.idea
*.iml
data
config.yaml
.cache
17 changes: 17 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.5.0
hooks:
- id: check-merge-conflict
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/Lucas-C/pre-commit-hooks
rev: v1.1.7
hooks:
- id: remove-crlf
- repo: https://github.com/dnephin/pre-commit-golang
rev: 0a943e8cbbdfd4a4b33f15e77c542715b4964f65
hooks:
- id: go-fmt
- id: go-build
- id: go-imports
29 changes: 29 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
FROM golang:latest as builder

ENV USER=appuser
ENV UID=10001

RUN adduser \
--disabled-password \
--gecos "" \
--home "/nonexistent" \
--shell "/sbin/nologin" \
--no-create-home \
--uid "${UID}" \
"${USER}"

WORKDIR /app
COPY . /app
RUN CGO_ENABLED=0 GOOS=linux go build -a -ldflags '-extldflags "-static"' -trimpath -ldflags=-buildid= -o main github.com/greboid/irc/cmd/github

FROM scratch

COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /etc/passwd /etc/passwd
COPY --from=builder /etc/group /etc/group
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo

COPY --from=builder /app/main /irc-github
EXPOSE 8080
USER appuser:appuser
CMD ["/irc-github"]
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Greg Holmes

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
## Github IRC notifier plugin

Plugin for [IRC-Bot](https://github.com/greboid/irc-bot)

Receives notifications from a [Github](https://github.com/) instance and outputs them to a channel.

- go build go build github.com/greboid/irc-github/v2/cmd/github
- docker run greboid/irc-github

#### Configuration

At a bare minimum you also need to give it a channel, a github secret to verify received notifications
on and an RPC token. You'll like also want to specify the bot host.

Optionally you can hide any notifications about private channels, or send them to a different channel.

Once configured the URL to configure in github would be <Bot URL>/github

#### Example running

```
---
version: "3.5"
service:
goplum:
image: greboid/irc-github
environment:
RPC_HOST: bot
RPC_TOKEN: <as configured on the bot>
CHANNEL: #spam
GITHUB_SECRET: cUCrb7HJ
```

```
github -rpc-host bot -rpc-token <as configured on the bot> -channel #spam -github-secret cUCrb7HJ
```
81 changes: 81 additions & 0 deletions cmd/github/github.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package main

//ping events
type pinghook struct {
}

//Push events
type pushhook struct {
Refspec string `json:"ref"`
Repository Repository `json:"repository"`
Pusher Pusher `json:"pusher"`
Sender Sender `json:"sender"`
Forced bool `json:"forced"`
Deleted bool `json:"deleted"`
Created bool `json:"created"`
CompareLink string `json:"compare"`
Commits []Commit `json:"commits"`
Baserefspec string `json:"base_ref"`
}

type Repository struct {
FullName string `json:"full_name"`
IsPrivate bool `json:"private"`
}

type Pusher struct {
Name string `json:"name"`
}

type Commit struct {
ID string `json:"id"`
Message string `json:"message"`
URL string `json:"url"`
Author Author `json:"author"`
Committer Author `json:"committer"`
}

type Author struct {
User string `json:"username"`
}

type Sender struct {
Login string `json:"login"`
}

//Pull Request events
type prhook struct {
Action string `json:"action"`
PullRequest PullRequest `json:"pull_request"`
Repository Repository `json:"repository"`
}

type PullRequest struct {
HtmlURL string `json:"html_url"`
Url string `json:"url"`
State string `json:"state"`
Title string `json:"title"`
Body string `json:"body"`
User User `json:"user"`
Merged string `json:"merged_at"`
MergedBy User `json:"merged_by"`
}

type User struct {
Login string `json:"login"`
}

//Issue events
type issuehook struct {
Action string `json:"action"`
Issue Issue `json:"issue"`
Repository Repository `json:"repository"`
User User `json:"sender"`
}

type Issue struct {
HtmlURL string `json:"html_url"`
Title string `json:"title"`
State string `json:"state"`
User User `json:"user"`
}
26 changes: 26 additions & 0 deletions cmd/github/issueComment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"fmt"
)

type githubIssueCommenthandler struct{}

func (g *githubIssueCommenthandler) handleIssueCommentEvent(data issuehook) (messages []string) {
switch data.Action {
case "created":
return g.handleIssueCommentCreated(data)
}
return []string{}
}

func (g *githubIssueCommenthandler) handleIssueCommentCreated(data issuehook) (messages []string) {
messages = append(messages, fmt.Sprintf(
"[%s] %s commented on issue %s - %s",
data.Repository.FullName,
data.User.Login,
data.Issue.Title,
data.Issue.HtmlURL,
))
return
}
48 changes: 48 additions & 0 deletions cmd/github/issueComment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"github.com/sebdah/goldie/v2"
"strings"
"testing"
)

func Test_githubissuehandler_handIssueCommentEvent_comment(t *testing.T) {
g := &githubIssueCommenthandler{}
hook := issuehook{}
err := getTestData("issuecomments/commented_1.json", &hook)
if err != nil {
t.Fatal("Unable to parse example data")
}
messages := g.handleIssueCommentEvent(hook)
if len(messages) == 0 {
t.Fatal("Output expected, none provided")
}
}

func Test_githubissuehandler_handIssueCommentEvent_unknown(t *testing.T) {
g := &githubIssueCommenthandler{}
hook := issuehook{}
hook.Action = "ThisWillError"
messages := g.handleIssueCommentEvent(hook)
if len(messages) != 0 {
t.Fatal("Output provided, none expected")
}
}

func Test_githubissuehandler_handleIssueCommentCreated(t *testing.T) {
tests := []string{"issuecomments/commented_1.json"}
gold := goldie.New(t)
for index := range tests {
t.Run(tests[index], func(t *testing.T) {
g := &githubIssueCommenthandler{}
hook := issuehook{}
err := getTestData(tests[index], &hook)
if err != nil {
t.Fatal("Unable to parse example data")
}
got := []byte(strings.Join(g.handleIssueCommentCreated(hook), "\n"))
gold.Assert(t, tests[index], got)
})

}
}
37 changes: 37 additions & 0 deletions cmd/github/issues.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import "fmt"

type githubissuehandler struct{}

func (g *githubissuehandler) handleIssueEvent(data issuehook) (messages []string) {
switch data.Action {
case "opened":
return g.handleIssueOpened(data)
case "closed":
return g.handleIssueClosed(data)
}
return []string{}
}

func (g *githubissuehandler) handleIssueOpened(data issuehook) (messages []string) {
messages = append(messages, fmt.Sprintf(
"[%s] %s create issue: %s - %s",
data.Repository.FullName,
data.User.Login,
data.Issue.Title,
data.Issue.HtmlURL,
))
return
}

func (g *githubissuehandler) handleIssueClosed(data issuehook) (messages []string) {
messages = append(messages, fmt.Sprintf(
"[%s] %s closed issue %s - %s",
data.Repository.FullName,
data.User.Login,
data.Issue.Title,
data.Issue.HtmlURL,
))
return
}
79 changes: 79 additions & 0 deletions cmd/github/issues_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package main

import (
"github.com/sebdah/goldie/v2"
"strings"
"testing"
)

func Test_githubissuehandler_handIssueEvent_open(t *testing.T) {
g := &githubissuehandler{}
hook := issuehook{}
err := getTestData("issues/create_1.json", &hook)
if err != nil {
t.Fatal("Unable to parse example data")
}
messages := g.handleIssueEvent(hook)
if len(messages) == 0 {
t.Fatal("Output expected, none provided")
}
}

func Test_githubissuehandler_handIssueEvent_closed(t *testing.T) {
g := &githubissuehandler{}
hook := issuehook{}
err := getTestData("issues/closed_1.json", &hook)
if err != nil {
t.Fatal("Unable to parse example data")
}
messages := g.handleIssueEvent(hook)
if len(messages) == 0 {
t.Fatal("Output expected, none provided")
}
}

func Test_githubissuehandler_handIssueEvent_unknown(t *testing.T) {
g := &githubissuehandler{}
hook := issuehook{}
hook.Action = "ThisWillError"
messages := g.handleIssueEvent(hook)
if len(messages) != 0 {
t.Fatal("Output provided, none expected")
}
}

func Test_githubissuehandler_handleIssueOpened(t *testing.T) {
tests := []string{"issues/create_1.json"}
gold := goldie.New(t)
for index := range tests {
t.Run(tests[index], func(t *testing.T) {
g := &githubissuehandler{}
hook := issuehook{}
err := getTestData(tests[index], &hook)
if err != nil {
t.Fatal("Unable to parse example data")
}
got := []byte(strings.Join(g.handleIssueOpened(hook), "\n"))
gold.Assert(t, tests[index], got)
})

}
}

func Test_githubissuehandler_handleIssueClosed(t *testing.T) {
tests := []string{"issues/closed_1.json"}
gold := goldie.New(t)
for index := range tests {
t.Run(tests[index], func(t *testing.T) {
g := &githubissuehandler{}
hook := issuehook{}
err := getTestData(tests[index], &hook)
if err != nil {
t.Fatal("Unable to parse example data")
}
got := []byte(strings.Join(g.handleIssueClosed(hook), "\n"))
gold.Assert(t, tests[index], got)
})

}
}
Loading

0 comments on commit 9b94746

Please sign in to comment.