From cb1c390141d2995733f46a502238a69c6fe79642 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20T=C3=B6tterman?= Date: Tue, 31 Oct 2017 20:58:23 +0200 Subject: [PATCH] Initial import --- .gitignore | 4 ++++ Dockerfile | 8 +++++++ Gopkg.lock | 15 +++++++++++++ Gopkg.toml | 26 ++++++++++++++++++++++ LICENSE | 13 +++++++++++ Makefile | 13 +++++++++++ README.md | 25 +++++++++++++++++++++ main.go | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 169 insertions(+) create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 Gopkg.lock create mode 100644 Gopkg.toml create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5b8f9f0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.swp +*~ +/drone-plugin-matrix +/vendor diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..927d18e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,8 @@ +# vim: set ft=dockerfile: +FROM alpine:3.6 +# Author with no obligation to maintain +MAINTAINER Paul Tötterman + +RUN apk --no-cache add ca-certificates +ADD drone-plugin-matrix / +ENTRYPOINT /drone-plugin-matrix diff --git a/Gopkg.lock b/Gopkg.lock new file mode 100644 index 0000000..77d6019 --- /dev/null +++ b/Gopkg.lock @@ -0,0 +1,15 @@ +# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. + + +[[projects]] + branch = "master" + name = "github.com/matrix-org/gomatrix" + packages = ["."] + revision = "a7fc80c8060c2544fe5d4dae465b584f8e9b4e27" + +[solve-meta] + analyzer-name = "dep" + analyzer-version = 1 + inputs-digest = "d631b7f46070377e77e160dda36075f4421695f6149e974427eafc8458012b3c" + solver-name = "gps-cdcl" + solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml new file mode 100644 index 0000000..36e6b1d --- /dev/null +++ b/Gopkg.toml @@ -0,0 +1,26 @@ + +# Gopkg.toml example +# +# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md +# 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" + + +[[constraint]] + branch = "master" + name = "github.com/matrix-org/gomatrix" diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c245a7b --- /dev/null +++ b/LICENSE @@ -0,0 +1,13 @@ +Copyright (c) 2017, Paul Tötterman + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d82484f --- /dev/null +++ b/Makefile @@ -0,0 +1,13 @@ +.PHONY: build +build: drone-plugin-matrix + +drone-plugin-matrix: main.go + CGO_ENABLED=0 go build -ldflags '-s -w' + +.PHONY: docker +docker: drone-plugin-matrix + docker build -t drone-plugin-matrix . + +.PHONY: clean +clean: + rm -f drone-plugin-matrix diff --git a/README.md b/README.md new file mode 100644 index 0000000..5794912 --- /dev/null +++ b/README.md @@ -0,0 +1,25 @@ +# drone-plugin-matrix + +[Drone](https://drone.io/) notifications to [Matrix](https://matrix.org/) + +Usage: + +```yaml + matrix: + image: ptman/drone-plugin-matrix + homeserver: https://matrix.org + roomid: '!0123456789abcdef:matrix.org' # room has to already be joined + username: ourbot # either username + password: *account-password* # and password + userid: @ourbot:matrix.org # or userid + accesstoken: 0123456789abcdef # and accesstoken + secrets: # and a better idea + - source: matrix_username # is to not store + target: plugin_username # credentials in the git repo + - source: matrix_password # but instead use drone + target: plugin_password # secret management +``` + +## License + +ISC diff --git a/main.go b/main.go new file mode 100644 index 0000000..c5b29c7 --- /dev/null +++ b/main.go @@ -0,0 +1,65 @@ +// Copyright (c) 2017 Paul Tötterman . All rights reserved. + +package main + +import ( + "fmt" + "log" + "os" + + "github.com/matrix-org/gomatrix" +) + +func main() { + homeServer := os.Getenv("PLUGIN_HOMESERVER") + userName := os.Getenv("PLUGIN_USERNAME") + password := os.Getenv("PLUGIN_PASSWORD") + + userID := os.Getenv("PLUGIN_USERID") + accessToken := os.Getenv("PLUGIN_ACCESSTOKEN") + + roomID := os.Getenv("PLUGIN_ROOMID") + message := os.Getenv("PLUGIN_MESSAGE") + + repoOwner := os.Getenv("DRONE_REPO_OWNER") + repoName := os.Getenv("DRONE_REPO_NAME") + + buildStatus := os.Getenv("DRONE_BUILD_STATUS") + buildLink := os.Getenv("DRONE_BUILD_LINK") + buildBranch := os.Getenv("DRONE_BRANCH") + buildAuthor := os.Getenv("DRONE_COMMIT_AUTHOR") + buildCommit := os.Getenv("DRONE_COMMIT") + + m, err := gomatrix.NewClient(homeServer, userID, accessToken) + if err != nil { + log.Fatal(err) + } + + if userID == "" || accessToken == "" { + r, err := m.Login(&gomatrix.ReqLogin{ + Type: "m.login.password", + User: userName, + Password: password, + InitialDeviceDisplayName: "Drone", + }) + if err != nil { + log.Fatal(err) + } + m.SetCredentials(r.UserID, r.AccessToken) + } + + if message == "" { + message = fmt.Sprintf("Build %s <%s> %s/%s#%s (%s) by %s", + buildStatus, + buildLink, + repoOwner, + repoName, + buildCommit[:8], + buildBranch, + buildAuthor) + } + + if _, err := m.SendNotice(roomID, message); err != nil { + log.Fatal(err) + } +}