Created
December 3, 2015 21:03
-
-
Save wind0r/d3ff26bed2deb70841d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"fmt" | |
"log" | |
"net/http" | |
"net/url" | |
"text/template" | |
"github.com/applikatoni/applikatoni/deploy" | |
"github.com/applikatoni/applikatoni/models" | |
"database/sql" | |
) | |
const slackSummaryTmplStr = `New {{.GitHubRepo}} Deployment: | |
{{.Username}} deployed {{.Branch}} on {{.Target}} :pizza: | |
> {{.Comment}} | |
<{{.GitHubUrl}}|View latest commit on GitHub> | |
<{{.DeploymentURL}}|Open deployment in Applikatoni> | |
` | |
var slackSummaryTemplate = template.Must(template.New("slackSummary").Parse(slackSummaryTmplStr)) | |
func NotifySlack(deploymentId int) { | |
deployment, err := getDeployment(db, deploymentId) | |
if err != nil { | |
log.Printf("Could not find deployment with id %v, %s\n", deploymentId, err) | |
return | |
} | |
application, err := findApplication(deployment.ApplicationName) | |
if err != nil { | |
log.Printf("Could not find application with name %v, %s\n", deployment.ApplicationName, err) | |
return | |
} | |
target, err := findTarget(application, deployment.TargetName) | |
if err != nil { | |
log.Printf("Could not find target with name %v, %s\n", deployment.TargetName, err) | |
return | |
} | |
user, err := getUser(db, deployment.UserId) | |
if err != nil { | |
log.Printf("Could not find User with id %v, %s\n", deployment.UserId, err) | |
return | |
} | |
if target.SlackEndpoint == "" { | |
return | |
} | |
summary, err := generateSlackSummary(application, deployment, user) | |
if err != nil { | |
log.Printf("Could not generate deployment summary, %s\n", err) | |
} | |
payload := fmt.Sprintf("{\"text\":\"%s\"}", summary) | |
fmt.Println(payload) | |
resp, err := http.PostForm(target.SlackEndpoint, url.Values{"payload": {payload}}) | |
if err != nil || resp.StatusCode != 200 { | |
log.Printf("Error while notifying Slack about deployment of %v on %v, %v! err: %s, resp: %s\n", deployment.ApplicationName, | |
deployment.TargetName, | |
deployment.CommitSha, | |
err, | |
resp.Status) | |
} else { | |
log.Printf("Successfully notified Slack about deployment of %v on %v, %v!\n", deployment.ApplicationName, deployment.TargetName, deployment.CommitSha) | |
} | |
} | |
func generateSlackSummary(a *models.Application, d *models.Deployment, u *models.User) (string, error) { | |
var summary bytes.Buffer | |
var scheme string | |
if config.SSLEnabled { | |
scheme = "https" | |
} else { | |
scheme = "http" | |
} | |
deploymentUrl := fmt.Sprintf("%s://%s/%v/deployments/%v", scheme, config.Host, | |
a.GitHubRepo, d.Id) | |
gitHubUrl := fmt.Sprintf("https://github.com/%v/%v/commit/%v", | |
a.GitHubOwner, a.GitHubRepo, d.CommitSha) | |
err := slackSummaryTemplate.Execute(&summary, map[string]interface{}{ | |
"GitHubRepo": a.GitHubRepo, | |
"Branch": d.Branch, | |
"Target": d.TargetName, | |
"Username": u.Name, | |
"Comment": d.Comment, | |
"GitHubUrl": gitHubUrl, | |
"DeploymentURL": deploymentUrl, | |
}) | |
return summary.String(), err | |
} | |
func newSlackNotifier(db *sql.DB) deploy.Listener { | |
fn := func(logs <-chan deploy.LogEntry) { | |
for entry := range logs { | |
if entry.EntryType == deploy.DEPLOYMENT_SUCCESS { | |
go NotifySlack(entry.DeploymentId) | |
} | |
} | |
} | |
return fn | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment