Browse Source

Initial Commit

master
bn4t 5 months ago
commit
187769796f
8 changed files with 210 additions and 0 deletions
  1. +34
    -0
      .drone.yml
  2. +3
    -0
      .env.sample
  3. +2
    -0
      .gitignore
  4. +20
    -0
      Dockerfile
  5. +12
    -0
      go.mod
  6. +41
    -0
      go.sum
  7. +67
    -0
      main.go
  8. +31
    -0
      utils.go

+ 34
- 0
.drone.yml View File

@@ -0,0 +1,34 @@
kind: pipeline
name: default


- name: build
image: docker:dind
environment:
USERNAME:
from_secret: registry_username
PASSWORD:
from_secret: registry_password
volumes:
- name: dockersock
path: /var/run
commands:
- sleep 5 # give docker enough time to start
- docker login -u $USERNAME -p $PASSWORD registry.bn4t.me
- docker build --pull -t registry.bn4t.me/bn4t/pleroma-release-bot:latest .
- docker push registry.bn4t.me/bn4t/pleroma-release-bot:latest
when:
branch:
- master

services:
- name: docker
image: docker:dind
privileged: true
volumes:
- name: dockersock
path: /var/run

volumes:
- name: dockersock
temp: {}

+ 3
- 0
.env.sample View File

@@ -0,0 +1,3 @@
SERVER_URL=https://social.bn4t.me
USERNAME=bot
PASSWORD=P4ssw0rd

+ 2
- 0
.gitignore View File

@@ -0,0 +1,2 @@
.idea/*
.env

+ 20
- 0
Dockerfile View File

@@ -0,0 +1,20 @@
FROM golang:alpine as builder

RUN mkdir /build /out

WORKDIR /build
COPY . .
RUN go get && go build -o /out/pleroma-release-bot





FROM alpine:latest

RUN mkdir /app && addgroup -S pleroma-bot && adduser -S pleroma-bot -G pleroma-bot
COPY --from=builder /out/pleroma-release-bot /app/pleroma-release-bot
USER pleroma-bot

ENTRYPOINT /app/pleroma-release-bot


+ 12
- 0
go.mod View File

@@ -0,0 +1,12 @@
module git.bn4t.me/bn4t/pleroma-release-bot

go 1.12

require (
github.com/joho/godotenv v1.3.0
github.com/mattn/go-mastodon v0.0.4
github.com/mmcdole/gofeed v1.0.0-beta2
github.com/mmcdole/goxpp v0.0.0-20181012175147-0068e33feabf // indirect
github.com/robfig/cron v1.2.0
github.com/stretchr/testify v1.3.0 // indirect
)

+ 41
- 0
go.sum View File

@@ -0,0 +1,41 @@
github.com/PuerkitoBio/goquery v1.5.0 h1:uGvmFXOA73IKluu/F84Xd1tt/z07GYm8X49XKHP7EJk=
github.com/PuerkitoBio/goquery v1.5.0/go.mod h1:qD2PgZ9lccMbQlc7eEOjaeRlFQON7xY8kdmcsrnKqMg=
github.com/andybalholm/cascadia v1.0.0 h1:hOCXnnZ5A+3eVDX8pvgl4kofXv2ELss0bKcqRySc45o=
github.com/andybalholm/cascadia v1.0.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ=
github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-mastodon v0.0.4 h1:+F2RbXbHkiBfx6SXMJEvEwZ0i8pI9nMnZhKkvjxq9Rs=
github.com/mattn/go-mastodon v0.0.4/go.mod h1:ZBkemyyYYhNAN5JJ0H/ZSW8HfPCW45rHFHyWNwSfpTA=
github.com/mattn/go-tty v0.0.0-20190424173100-523744f04859/go.mod h1:XPvLUNfbS4fJH25nqRHfWLMa1ONC8Amw+mIA639KxkE=
github.com/mmcdole/gofeed v1.0.0-beta2 h1:CjQ0ADhAwNSb08zknAkGOEYqr8zfZKfrzgk9BxpWP2E=
github.com/mmcdole/gofeed v1.0.0-beta2/go.mod h1:/BF9JneEL2/flujm8XHoxUcghdTV6vvb3xx/vKyChFU=
github.com/mmcdole/goxpp v0.0.0-20181012175147-0068e33feabf h1:sWGE2v+hO0Nd4yFU/S/mDBM5plIU8v/Qhfz41hkDIAI=
github.com/mmcdole/goxpp v0.0.0-20181012175147-0068e33feabf/go.mod h1:pasqhqstspkosTneA62Nc+2p9SOBBYAPbnmRRWPQ0V8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ=
github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80 h1:nrZ3ySNYwJbSpD6ce9duiP+QkD3JuLCcWkdaehUS/3Y=
github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80/go.mod h1:iFyPdL66DjUD96XmzVL3ZntbzcflLnznH0fr99w5VqE=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190509222800-a4d6f7feada5 h1:6M3SDHlHHDCx2PcQw3S4KsR170vGqDhJDOmpVd4Hjak=
golang.org/x/net v0.0.0-20190509222800-a4d6f7feada5/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190509141414-a5b02f93d862/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

+ 67
- 0
main.go View File

@@ -0,0 +1,67 @@
package main

import (
"context"
"github.com/joho/godotenv"
"github.com/mattn/go-mastodon"
"github.com/mmcdole/gofeed"
"github.com/robfig/cron"
"log"
"os"
"time"
)

var MastoClient *mastodon.Client

func main() {
if FileExists(".env") {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
}


clientId, clientSecret := initMastoApp()

MastoClient = mastodon.NewClient(&mastodon.Config{
Server: os.Getenv("SERVER_URL"),
ClientID: clientId,
ClientSecret: clientSecret,
})


err := MastoClient.Authenticate(context.Background(), os.Getenv("USERNAME"), os.Getenv("PASSWORD"))
if err != nil {
log.Fatal(err)
}


c := cron.New()
// check for new releases every 3 minutes
c.AddFunc("0 */3 * * * *", checkForNewReleases )
c.Run()
}

func checkForNewReleases() {
fp := gofeed.NewParser()
feed, err := fp.ParseURL("https://git.pleroma.social/pleroma/pleroma/-/tags?format=atom")
if err != nil{
log.Print(err.Error())
}

// check if new release happened in the last 3 mins (cron runs all 3 mins)
// if not don't post it
// Need to use updatedParsed since Gitlab doesn't set a published value
if feed.Items[0].UpdatedParsed.Unix() + 180 > time.Now().Unix(){
ctx := context.Background()
toot := mastodon.Toot{
Status: "A new Pleroma version got released: " + feed.Items[0].Title + "\n\n" + feed.Items[0].Link,
}

_, err := MastoClient.PostStatus(ctx, &toot)
if err != nil{
log.Print(err.Error())
}
}
}

+ 31
- 0
utils.go View File

@@ -0,0 +1,31 @@
package main

import (
"context"
"github.com/mattn/go-mastodon"
"log"
"os"
)

func FileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}


func initMastoApp() (clientId string, clientSecret string) {
app, err := mastodon.RegisterApp(context.Background(), &mastodon.AppConfig{
Server: os.Getenv("SERVER_URL"),
ClientName: "pleroma-release-bot",
Scopes: "read write follow",
Website: "https://git.bn4t.me/bn4t/pleroma-release-bot",
})
if err != nil {
log.Fatal(err)
}

return app.ClientID, app.ClientSecret
}

Loading…
Cancel
Save