Skip to content

Commit

Permalink
rewriting...
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed Dec 16, 2017
1 parent d39261a commit 5fac727
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 61 deletions.
52 changes: 41 additions & 11 deletions Gopkg.lock

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

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@
[[dependencies]]
branch = "master"
name = "golang.org/x/oauth2"

[[constraint]]
name = "github.com/boltdb/bolt"
version = "1.3.1"
117 changes: 77 additions & 40 deletions cmd/github-vacations/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,92 @@ package main
import (
"fmt"
"os"
"strings"

githubvacations "github.com/caarlos0/github-vacations"
"github.com/caarlos0/spin"
"github.com/urfave/cli"
"github.com/alecthomas/kingpin"
"github.com/apex/log"
"github.com/apex/log/handlers/cli"
"github.com/boltdb/bolt"
lib "github.com/caarlos0/github-vacations"
)

var version = "master"
func init() {
log.SetHandler(cli.Default)
}

const (
version = "master"
)

var (
app = kingpin.New("github-vacations", "Automagically ignore all notifications related to work when you are on vacations")
dbPath = app.Flag("db", "Path to the database").Default("$HOME/.vacations.db").String()
check = app.Command("check", "Check for work notifications and mark them as read").Default()
org = check.Flag("org", "Organization name to ignore").Required().Short('o').String()
token = check.Flag("token", "Your GitHub token").Required().Short('t').Envar("GITHUB_TOKEN").String()
read = app.Command("read", "Read notifications that were previously marked as read")
)

func main() {
app := cli.NewApp()
app.Name = "github-vacations"
app.Usage = "Automagically ignore all notifications related to work when you are on vacations"
app.Version = version
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "org, o",
Usage: "Organization name to ignore",
},
cli.StringFlag{
Name: "token, t",
EnvVar: "GITHUB_TOKEN",
Usage: "GitHub token",
},
fmt.Printf("\n")
defer fmt.Printf("\n")
app.Version(version)
app.VersionFlag.Short('v')
app.HelpFlag.Short('h')
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
case check.FullCommand():
checkNotifications(*dbPath, *org, *token)
case read.FullCommand():
readNotification(*dbPath)
}
app.Action = func(c *cli.Context) error {
var token = c.String("token")
var org = strings.ToLower(c.String("org"))
if token == "" {
return cli.NewExitError("missing GITHUB_TOKEN", 1)
}
if org == "" {
return cli.NewExitError("missing organization to ignore", 1)
}

func readNotification(path string) {
db, err := bolt.Open(os.ExpandEnv(path), 0600, nil)
kingpin.FatalIfError(err, "%v")
if err := db.View(func(tx *bolt.Tx) error {
var bucket = tx.Bucket([]byte("notifications"))
if bucket == nil {
log.Info("0 notifications to read")
return nil
}
var spin = spin.New("%s Helping you to not work...")
spin.Start()
count, err := githubvacations.MarkWorkNotificationsAsRead(token, org)
spin.Stop()

bucket.ForEach(func(url, title []byte) error {
printLink(title, url)
return nil
})
return nil
}); err != nil {
kingpin.FatalIfError(err, "%v")
}
}

func printLink(title, url []byte) {
// TODO: this is a hack because printf won't accept `\e`
var e = string(0x1b)
log.Infof("%s]8;;%s\a%s%s]8;;\a", e, url, title, e)
}

func checkNotifications(path, org, token string) {
log.Info("helping you not to work...")
db, err := bolt.Open(os.ExpandEnv(path), 0600, nil)
kingpin.FatalIfError(err, "%v")
defer db.Close()

notifications, err := lib.MarkWorkNotificationsAsRead(token, org)
kingpin.FatalIfError(err, "%v")
log.Infof("%d %s notifications mark as read", len(notifications), org)

if err := db.Update(func(tx *bolt.Tx) error {
bucket, err := tx.CreateBucketIfNotExists([]byte("notifications"))
if err != nil {
var msg = "failed to mark notifications as read"
if count > 0 {
msg = fmt.Sprintf("%v notifications marked as read, others failed", count)
}
return cli.NewExitError(msg, 1)
return err
}
for _, notification := range notifications {
bucket.Put([]byte(notification.URL), []byte(notification.Title))
}
fmt.Printf("%v notifications marked as read", count)
return nil
}); err != nil {
kingpin.FatalIfError(err, "%v")
}
if err := app.Run(os.Args); err != nil {
panic(err)
}
log.Infof("notifications stored on %s", path)
}
39 changes: 29 additions & 10 deletions github-vacations.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ import (
"golang.org/x/oauth2"
)

// Notification from github
type Notification struct {
URL, Title, Repo, Reason string
}

// MarkWorkNotificationsAsRead checks your notifications from work and mark
// them as read
func MarkWorkNotificationsAsRead(token, org string) (count int, err error) {
func MarkWorkNotificationsAsRead(token, org string) ([]Notification, error) {
var result []Notification
var ctx = context.Background()
var client = github.NewClient(oauth2.NewClient(
ctx,
Expand All @@ -23,19 +29,32 @@ func MarkWorkNotificationsAsRead(token, org string) (count int, err error) {
&github.NotificationListOptions{},
)
if err != nil {
return
return result, err
}
for _, notification := range notifications {
var owner = *notification.Repository.Owner.Login
if strings.ToLower(owner) == org {
if _, err = client.Activity.DeleteThreadSubscription(ctx, *notification.ID); err != nil {
return
var owner = notification.GetRepository().GetOwner().GetLogin()
if strings.ToLower(owner) == strings.ToLower(org) {
if _, err = client.Activity.DeleteThreadSubscription(ctx, notification.GetID()); err != nil {
return result, err
}
if _, err = client.Activity.MarkThreadRead(ctx, *notification.ID); err != nil {
return
// if _, err = client.Activity.MarkThreadRead(ctx, notification.GetID()); err != nil {
// return result, err
// }
var url = notification.GetSubject().GetURL()
for old, new := range map[string]string{
"api.github.com": "github.com",
"/repos/": "/",
"/pulls/": "/pull/",
} {
url = strings.Replace(url, old, new, 1)
}
count++
result = append(result, Notification{
URL: url,
Title: notification.GetSubject().GetTitle(),
Repo: notification.GetRepository().GetFullName(),
Reason: notification.GetReason(),
})
}
}
return
return result, err
}

0 comments on commit 5fac727

Please sign in to comment.