Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dpb587 committed Sep 12, 2018
0 parents commit 3266dce
Show file tree
Hide file tree
Showing 103 changed files with 12,084 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Gopkg.toml example
#
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true


[[constraint]]
branch = "master"
name = "github.com/dpb587/go-pairist"

[[constraint]]
name = "github.com/nlopes/slack"
version = "0.3.0"

[[constraint]]
name = "github.com/pkg/errors"
version = "0.8.0"

[prune]
go-tests = true
unused-packages = true
18 changes: 18 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Copyright (c) 2017 Danny Berger <https://dpb587.me>

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.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# go-slack-topic-bot

Some utilities for making a [Slack](https://slack.com/) bot that manages a channel's topic.


## License

[MIT License](LICENSE)
60 changes: 60 additions & 0 deletions main/cfbosh.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main

import (
"log"
"os"

"github.com/dpb587/go-slack-topic-bot/message"
"github.com/dpb587/go-slack-topic-bot/message/boshio"
"github.com/dpb587/go-slack-topic-bot/message/github"
"github.com/dpb587/go-slack-topic-bot/message/pairist"
"github.com/dpb587/go-slack-topic-bot/slack"
)

func main() {
msg, err := message.Join(
" || ",
message.Prefix(
"_interrupt_ ",
message.Join(
" ",
pairist.Interrupt{
Team: "sf-bosh",
Interruptible: pairist.InterruptibleHours("12:00", "18:00", "America/Los_Angeles"),
People: map[string]string{
"Luan": "luan",
"Josh R": "jrussett",
"Josh": "jaresty",
"Danny": "dberger",
"Mike": "mxu",
"Jim": "jfmyers9",
"Morgan": "mfine",
"Belinda": "belinda_liu",
"Max": "mpetersen",
},
},
),
),
message.Literal("_docs_ <https://bosh.io|bosh.io>"),
message.Prefix(
"_latest_ ",
message.Join(
" ",
boshio.Release{Alias: "bosh", Repository: "github.com/cloudfoundry/bosh"},
github.Release{Alias: "bosh-cli", Owner: "cloudfoundry", Name: "bosh-cli"},
boshio.Stemcell{Alias: "ubuntu-xenial", Name: "bosh-aws-xen-hvm-ubuntu-xenial-go_agent"},
boshio.Stemcell{Alias: "ubuntu-trusty", Name: "bosh-aws-xen-hvm-ubuntu-trusty-go_agent"},
),
),
).Message()
if err != nil {
log.Panicf("ERROR: %v", err)
}

log.Printf("DEBUG: expected message: %s", msg)

err = slack.UpdateChannelTopic(os.Getenv("SLACK_CHANNEL"), msg)
if err != nil {
log.Panicf("ERROR: %v", err)
}
}
47 changes: 47 additions & 0 deletions message/boshio/release.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package boshio

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"

"github.com/pkg/errors"
"github.com/dpb587/go-slack-topic-bot/message"
)

type Release struct {
Alias string
Repository string
}

var _ message.Messager = &Release{}

type boshioReleaseApiV1 []struct {
Version string `json:"version"`
}

func (m Release) Message() (string, error) {
res, err := http.DefaultClient.Get(fmt.Sprintf("https://bosh.io/api/v1/releases/%s", m.Repository))
if err != nil {
return "", err
}

resBodyBytes, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", errors.Wrap(err, "reading response")
}

var data boshioReleaseApiV1

err = json.Unmarshal(resBodyBytes, &data)
if err != nil {
return "", errors.Wrap(err, "unmarshalling")
}

if len(data) == 0 {
return "", nil
}

return fmt.Sprintf("%s/%s", m.Alias, data[0].Version), nil
}
47 changes: 47 additions & 0 deletions message/boshio/stemcell.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package boshio

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"

"github.com/pkg/errors"
"github.com/dpb587/go-slack-topic-bot/message"
)

type Stemcell struct {
Alias string
Name string
}

var _ message.Messager = &Stemcell{}

type boshioStemcellApiV1 []struct {
Version string `json:"version"`
}

func (m Stemcell) Message() (string, error) {
res, err := http.DefaultClient.Get(fmt.Sprintf("https://bosh.io/api/v1/stemcells/%s", m.Name))
if err != nil {
return "", err
}

resBodyBytes, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", errors.Wrap(err, "reading response")
}

var data boshioStemcellApiV1

err = json.Unmarshal(resBodyBytes, &data)
if err != nil {
return "", errors.Wrap(err, "unmarshalling")
}

if len(data) == 0 {
return "", nil
}

return fmt.Sprintf("%s/%s", m.Alias, data[0].Version), nil
}
48 changes: 48 additions & 0 deletions message/github/release.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package github

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"

"github.com/pkg/errors"
"github.com/dpb587/go-slack-topic-bot/message"
)

type Release struct {
Alias string
Owner string
Name string
}

var _ message.Messager = &Release{}

type gitHubReleaseApiV2 []struct {
Name string `json:"name"`
}

func (m Release) Message() (string, error) {
res, err := http.DefaultClient.Get(fmt.Sprintf("https://api.github.com/repos/%s/%s/releases", m.Owner, m.Name))
if err != nil {
return "", err
}

resBodyBytes, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", errors.Wrap(err, "reading response")
}

var data gitHubReleaseApiV2

err = json.Unmarshal(resBodyBytes, &data)
if err != nil {
return "", errors.Wrap(err, "unmarshalling")
}

if len(data) == 0 {
return "", nil
}

return fmt.Sprintf("%s/%s", m.Alias, data[0].Name), nil
}
40 changes: 40 additions & 0 deletions message/joiner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package message

import (
"strings"

"github.com/pkg/errors"
)

type Joiner struct {
delimiter string
messages []Messager
}

var _ Messager = &Joiner{}

func Join(delimiter string, templates ...Messager) Messager {
return Joiner{
delimiter: delimiter,
messages: templates,
}
}

func (m Joiner) Message() (string, error) {
var msgs []string

for tplIdx, tpl := range m.messages {
msg, err := tpl.Message()
if err != nil {
return "", errors.Wrapf(err, "template %d", tplIdx)
}

if msg == "" {
continue
}

msgs = append(msgs, msg)
}

return strings.Join(msgs, m.delimiter), nil
}
17 changes: 17 additions & 0 deletions message/literal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package message

type literal struct {
message string
}

func Literal(message string) Messager {
return &literal{
message: message,
}
}

var _ Messager = &literal{}

func (m literal) Message() (string, error) {
return m.message, nil
}
5 changes: 5 additions & 0 deletions message/messager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package message

type Messager interface {
Message() (string, error)
}
Loading

0 comments on commit 3266dce

Please sign in to comment.