|
| 1 | +// Copyright (c) 2017 Paul Tötterman <[email protected]>. All rights reserved. |
| 2 | + |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "os" |
| 9 | + |
| 10 | + "github.com/matrix-org/gomatrix" |
| 11 | +) |
| 12 | + |
| 13 | +func main() { |
| 14 | + homeServer := os.Getenv("PLUGIN_HOMESERVER") |
| 15 | + userName := os.Getenv("PLUGIN_USERNAME") |
| 16 | + password := os.Getenv("PLUGIN_PASSWORD") |
| 17 | + |
| 18 | + userID := os.Getenv("PLUGIN_USERID") |
| 19 | + accessToken := os.Getenv("PLUGIN_ACCESSTOKEN") |
| 20 | + |
| 21 | + roomID := os.Getenv("PLUGIN_ROOMID") |
| 22 | + message := os.Getenv("PLUGIN_MESSAGE") |
| 23 | + |
| 24 | + repoOwner := os.Getenv("DRONE_REPO_OWNER") |
| 25 | + repoName := os.Getenv("DRONE_REPO_NAME") |
| 26 | + |
| 27 | + buildStatus := os.Getenv("DRONE_BUILD_STATUS") |
| 28 | + buildLink := os.Getenv("DRONE_BUILD_LINK") |
| 29 | + buildBranch := os.Getenv("DRONE_BRANCH") |
| 30 | + buildAuthor := os.Getenv("DRONE_COMMIT_AUTHOR") |
| 31 | + buildCommit := os.Getenv("DRONE_COMMIT") |
| 32 | + |
| 33 | + m, err := gomatrix.NewClient(homeServer, userID, accessToken) |
| 34 | + if err != nil { |
| 35 | + log.Fatal(err) |
| 36 | + } |
| 37 | + |
| 38 | + if userID == "" || accessToken == "" { |
| 39 | + r, err := m.Login(&gomatrix.ReqLogin{ |
| 40 | + Type: "m.login.password", |
| 41 | + User: userName, |
| 42 | + Password: password, |
| 43 | + InitialDeviceDisplayName: "Drone", |
| 44 | + }) |
| 45 | + if err != nil { |
| 46 | + log.Fatal(err) |
| 47 | + } |
| 48 | + m.SetCredentials(r.UserID, r.AccessToken) |
| 49 | + } |
| 50 | + |
| 51 | + if message == "" { |
| 52 | + message = fmt.Sprintf("Build %s <%s> %s/%s#%s (%s) by %s", |
| 53 | + buildStatus, |
| 54 | + buildLink, |
| 55 | + repoOwner, |
| 56 | + repoName, |
| 57 | + buildCommit[:8], |
| 58 | + buildBranch, |
| 59 | + buildAuthor) |
| 60 | + } |
| 61 | + |
| 62 | + if _, err := m.SendNotice(roomID, message); err != nil { |
| 63 | + log.Fatal(err) |
| 64 | + } |
| 65 | +} |
0 commit comments