Exemplo n.º 1
0
func oldWebhookHandler(c *integram.Context, wc *integram.WebhookContext) (err error) {

	wh := oldWebhook{}

	payload := wc.FormValue("payload")

	if payload == "" {
		return errors.New("X-Event-Key header missed and old-style webhook data not found")
	}

	json.Unmarshal([]byte(payload), &wh)

	if wh.CanonURL == "" {
		return errors.New("Error decoding payload for old webhook")
	}

	msg := c.NewMessage()
	commits := 0
	text := ""
	wp := ""

	if len(wh.Commits) == 0 {
		return nil
	}

	headCommit := wh.Commits[len(wh.Commits)-1]

	if len(wh.Commits) > 1 {

		wp = c.WebPreview(fmt.Sprintf("%d commits", len(wh.Commits)), "@"+wh.Commits[0].Node[0:10]+" ... @"+headCommit.Node[0:10], "", wh.CanonURL+wh.Repository.AbsoluteURL+"/compare/"+headCommit.Node+".."+wh.Commits[0].Parents[0], "")

		anyOherPersonCommits := false
		for _, commit := range wh.Commits {
			if commit.Author != wh.User {
				anyOherPersonCommits = true
				break
			}
		}
		for _, commit := range wh.Commits {
			commits++
			if anyOherPersonCommits {
				text += m.Bold(commit.Author) + ": "
			}
			text += m.URL(commit.Message, wh.CanonURL+wh.Repository.AbsoluteURL+"commits/"+headCommit.Node[0:10]) + "\n"
		}

	} else if len(wh.Commits) == 1 {
		wp = c.WebPreview("Commit", "@"+headCommit.Node[0:10], "", wh.CanonURL+wh.Repository.AbsoluteURL+"commits/"+headCommit.Node[0:10], "")

		commit := &wh.Commits[0]
		if commit.Author != wh.User {
			text += m.Bold(commit.Author) + ": "
		}

		text += commit.Message + "\n"
	}

	if len(wh.Commits) > 0 {

		return msg.SetTextFmt("%s %s to %s/%s\n%s",
			m.Bold(wh.User),
			m.URL("pushed", wp),
			m.URL(wh.Repository.Name, wh.CanonURL+wh.Repository.AbsoluteURL),
			m.URL(headCommit.Branch, wh.CanonURL+wh.Repository.AbsoluteURL+"branch/"+headCommit.Branch),
			text).
			EnableHTML().
			Send()
	}
	return nil

}