func mustBeAuthed(c *integram.Context) (bool, error) { provider := c.OAuthProvider() if !provider.IsSetup() { return false, c.NewMessage().SetText(fmt.Sprintf("To be able to use interactive replies in Telegram, first you need to add oauth application on your hosted Bitbucket instance (admin priveleges required): %s\nAdd application with any name(f.e. Telegram) and specify this *Redirect URI*: \n%s\n\nAfter you press *Submit* you will receive app info. First, send me the *Application Id*", c.ServiceBaseURL.String()+"/account/", provider.RedirectURL())). SetChat(c.User.ID). SetBackupChat(c.Chat.ID). EnableHTML(). EnableForceReply(). DisableWebPreview(). SetReplyAction(hostedAppIDEntered, c.ServiceBaseURL.String()).Send() } if !c.User.OAuthValid() { if c.Callback != nil { c.AnswerCallbackQuery("You need to authorize me\nUse the \"Tap me to auth\" button", true) } if !c.User.IsPrivateStarted() && c.Callback != nil { kb := c.Callback.Message.InlineKeyboardMarkup kb.AddPMSwitchButton(c.Bot(), "👉 Tap me to auth", "auth") c.EditPressedInlineKeyboard(kb) } else { return false, c.NewMessage().SetTextFmt("You need to authorize me to use interactive replies: %s", c.User.OauthInitURL()). DisableWebPreview(). SetChat(c.User.ID).SetBackupChat(c.Chat.ID).Send() } } return true, nil }
/*func me(c *integram.Context) (*gitlab.User, error) { api:=Api(c).Repositories.Commits.GetCommit() user := &gitlab.User{} c.User.Cache("me", user) if user.ID > 0 { return user, nil } user, _, err := Api(c).Users.CurrentUser() if err != nil { return nil, err } c.User.SetCache("me", user, time.Hour*24*30) return user, nil } func cacheNickMap(c *integram.Context) error { me, err := me(c) if err != nil { return err } c.SetServiceCache("nick_map_"+me.Username, c.User.UserName, time.Hour*24*365) err = c.SetServiceCache("nick_map_"+me.Email, c.User.UserName, time.Hour*24*365) return err } */ func hostedAppSecretEntered(c *integram.Context, baseURL string, appID string) error { c.SetServiceBaseURL(baseURL) appSecret := strings.TrimSpace(c.Message.Text) if len(appSecret) != 64 { c.NewMessage().SetText("Looks like this *Application Secret* is incorrect. Must be a 64 HEX symbols. Please try again").EnableHTML().DisableWebPreview().SetReplyAction(hostedAppSecretEntered, baseURL).Send() return errors.New("Application Secret '" + appSecret + "' is incorrect") } conf := integram.OAuthProvider{BaseURL: c.ServiceBaseURL, ID: appID, Secret: appSecret} token, err := conf.OAuth2Client(c).Exchange(oauth2.NoContext, "-") if strings.Contains(err.Error(), `"error":"invalid_grant"`) { // means the app is exists c.SaveOAuthProvider(c.ServiceBaseURL, appID, appSecret) _, err := mustBeAuthed(c) return err } c.NewMessage().SetText("Application ID or Secret is incorrect. Please try again. Enter *Application Id*"). EnableHTML(). SetReplyAction(hostedAppIDEntered, baseURL).Send() fmt.Printf("Exchange: token: %+v, err:%v\n", token, err) return nil }
func webhookHandler(c *integram.Context, wc *integram.WebhookContext) (err error) { wh := webhook{Mrkdwn: true} err = wc.JSON(&wh) if err != nil { return } if len(wh.Attachments) > 0 { if wh.Text != "" { wh.Text += "\n" } wp := c.WebPreview(wh.Attachments[0].Title, wh.Attachments[0].AuthorName, wh.Attachments[0].Pretext, wh.Attachments[0].TitleLink, wh.Attachments[0].ThumbURL) text := m.URL(" ", wp) + " " + wh.Text for i, attachment := range wh.Attachments { if i > 0 { text += "\n" } text += m.URL(attachment.Title, attachment.TitleLink) + " " + attachment.Pretext } return c.NewMessage().SetText(text).EnableAntiFlood().EnableHTML().Send() } if wh.Text != "" { m := c.NewMessage().SetText(wh.Text + " " + wh.Channel).EnableAntiFlood() if wh.Mrkdwn { m.EnableMarkdown() } return m.Send() } return errors.New("Text and Attachments not found") }
func commitsReplied(c *integram.Context, baseURL string, projectID int, commits []commit) error { authorized, err := mustBeAuthed(c) if err != nil { return err } if !authorized { //todo:bug message lost return c.User.SetAfterAuthAction(commitsReplied, baseURL, projectID, commits) } buttons := integram.Buttons{} for _, commit := range commits { buttons.Append(commit.ID, trim(commit.Message, 40)) } return c.NewMessage(). SetText(c.User.Mention()+" please specify commit to comment"). SetKeyboard(buttons.Markup(1), true). EnableForceReply(). SetReplyAction(commitToReplySelected, baseURL, projectID, c.Message). Send() }
func settings(c *integram.Context) error { btns := integram.InlineButtons{} btns.Append("ci", "CI") btns.Append("mr", "Merge requests") return c.NewMessage().SetText("Tune the notifications").SetInlineKeyboard(btns.Markup(1, "categories")).SetCallbackAction(settingsKeyboardPressed).Send() }
func hostedAppIDEntered(c *integram.Context, baseURL string) error { c.SetServiceBaseURL(baseURL) appID := strings.TrimSpace(c.Message.Text) if len(appID) != 64 { c.NewMessage().SetText("Looks like this *Application Id* is incorrect. Must be a 64 HEX symbols. Please try again"). EnableHTML(). SetReplyAction(hostedAppIDEntered, baseURL).Send() return errors.New("Application Id '" + appID + "' is incorrect") } return c.NewMessage().SetText("Great! Now write me the *Secret* for this application"). EnableHTML(). SetReplyAction(hostedAppSecretEntered, baseURL, appID).Send() }
func update(c *integram.Context) error { command, param := c.Message.GetCommand() if c.Message.IsEventBotAddedToGroup() { command = "start" } if param == "silent" { command = "" } switch command { case "start": return c.NewMessage().EnableAntiFlood().EnableHTML(). SetText("Hi here! You can send " + m.URL("Slack-compatible", "https://api.slack.com/docs/message-formatting#message_formatting") + " simple webhooks to " + m.Bold("this chat") + " using this URL: \n" + m.Fixed(c.Chat.ServiceHookURL()) + "\n\nExample (JSON payload):\n" + m.Pre("{\"text\":\"So _advanced_\\nMuch *innovations* 🙀\"}")).Send() } return nil }
func mustBeAuthed(c *integram.Context) (bool, error) { provider := c.OAuthProvider() if !provider.IsSetup() { return false, c.NewMessage().SetText(fmt.Sprintf("To be able to use interactive replies in Telegram, first you need to add oauth application on your hosted GitLab instance (admin priveleges required): %s\nAdd application with any name(f.e. Telegram) and specify this *Redirect URI*: \n%s\n\nAfter you press *Submit* you will receive app info. First, send me the *Application ID*", c.ServiceBaseURL.String()+"/admin/applications/new", provider.RedirectURL())). SetChat(c.User.ID). SetBackupChat(c.Chat.ID). EnableHTML(). EnableForceReply(). DisableWebPreview(). SetReplyAction(hostedAppIDEntered, c.ServiceBaseURL.String()).Send() } if !c.User.OAuthValid() { return false, c.NewMessage().SetTextFmt("You need to authorize me to use interactive replies: %s", c.User.OauthInitURL()). DisableWebPreview(). SetChat(c.User.ID).SetBackupChat(c.Chat.ID).Send() } return true, nil }
func update(c *integram.Context) error { command, param := c.Message.GetCommand() if c.Message.IsEventBotAddedToGroup() { command = "start" } if param == "silent" { command = "" } switch command { case "start": return c.NewMessage().EnableAntiFlood().EnableHTML(). SetTextFmt("Hi here! To setup notifications for %s for your BitBucket repo, open Settings -> Webhooks and add this URL:\n%s\n%s", m.Bold("this chat"), m.Fixed(c.Chat.ServiceHookURL()), m.Bold("⚠️ Don't forget to add all triggers inside the \"Choose from a full list of triggers\" radio button!")).Send() case "cancel", "clean", "reset": return c.NewMessage().SetText("Clean").HideKeyboard().Send() } return nil }
func update(c *integram.Context) error { command, param := c.Message.GetCommand() if c.Message.IsEventBotAddedToGroup() { command = "start" } if param == "silent" { command = "" } switch command { case "start": return c.NewMessage().EnableAntiFlood().EnableHTML(). SetText("Hi here! To setup notifications " + m.Bold("for this chat") + " your GitLab project(repo), open Settings -> Web Hooks and add this URL:\n" + m.Fixed(c.Chat.ServiceHookURL())).EnableHTML().Send() case "cancel", "clean", "reset": return c.NewMessage().SetText("Clean").HideKeyboard().Send() case "settings": return settings(c) } return nil }
func webhookHandler(c *integram.Context, wc *integram.WebhookContext) (err error) { eventKey := wc.Header("X-Event-Key") if eventKey == "" { // try the old one Bitbucket POST service return oldWebhookHandler(c, wc) } if _, ok := eventTypeMap[eventKey]; !ok { return errors.New("Bad X-Event-Key: " + eventKey) } c.Log().Debugf("eventKey=%v", eventKey) if err != nil { return errors.New("JSON deserialization error: " + err.Error()) } switch eventKey { case "repo:push": event := api.RepoPushEvent{} wc.JSON(&event) for _, change := range event.Push.Changes { msg := c.NewMessage() commits := 0 text := "" if len(change.Commits) > 1 { anyOherPersonCommits := false for _, commit := range change.Commits { if commit.Author.User.UUID != event.Actor.UUID { anyOherPersonCommits = true break } } for _, commit := range change.Commits { commits++ if anyOherPersonCommits { text += mention(c, &commit.Author.User) + ": " } text += m.URL(commit.Message, commit.Links.HTML.Href) + "\n" } if change.Truncated { text += m.URL("... See all", change.Links.Commits.Href) + "\n" } } else if len(change.Commits) == 1 { commits++ commit := &change.Commits[0] if commit.Author.User.UUID != event.Actor.UUID { text += mention(c, &commit.Author.User) + ": " } text += commit.Message + "\n" } change.New.Target.Author.User.Links.Avatar.Href = strings.Replace(change.New.Target.Author.User.Links.Avatar.Href, "/32/", "/128/", 1) wp := "" if change.Truncated { wp = c.WebPreview("> 5 commits", "@"+commitShort(change.Old.Target.Hash)+" ... @"+commitShort(change.New.Target.Hash), "", change.Links.HTML.Href, change.New.Target.Author.User.Links.Avatar.Href) } else if commits > 1 { wp = c.WebPreview(fmt.Sprintf("%d commits", commits), "@"+commitShort(change.Old.Target.Hash)+" ... @"+commitShort(change.New.Target.Hash), "", change.Links.HTML.Href, change.New.Target.Author.User.Links.Avatar.Href) } else if commits == 1 { wp = c.WebPreview("Commit", "@"+commitShort(change.New.Target.Hash), "", change.Commits[0].Links.HTML.Href, change.New.Target.Author.User.Links.Avatar.Href) } if commits > 0 { pushedText := "" if change.Forced { pushedText = m.URL("❗️ forcibly pushed", wp) } else { pushedText = m.URL("pushed", wp) } err = msg.SetTextFmt("%s %s to %s/%s\n%s", mention(c, &change.New.Target.Author.User), pushedText, m.URL(event.Repository.Name, event.Repository.Links.HTML.Href), m.URL(change.New.Name, change.New.Links.HTML.Href), text). AddEventID(commitUniqueID(change.Commits[0].Hash)). EnableHTML(). Send() } } case "issue:created": event := api.IssueCreatedEvent{} err := wc.JSON(&event) if err != nil { return err } event.Issue.Repository = &event.Repository c.SetServiceCache(issueUniqueID(event.Repository.FullName, event.Issue.ID), event.Issue, time.Hour*24*365) return c.NewMessage().AddEventID(issueUniqueID(event.Repository.FullName, event.Issue.ID)). SetInlineKeyboard(issueInlineKeyboard(&event.Issue)). SetText(issueText(c, &event.Issue)). SetCallbackAction(issueInlineButtonPressed, event.Repository.FullName, event.Issue.ID). EnableHTML().Send() case "issue:comment_created": event := api.IssueCommentCreatedEvent{} err := wc.JSON(&event) if err != nil { return err } var rm *integram.Message if event.Comment.Parent.ID > 0 { // actually bitbucket doesn't provide parent id for issue comments for now rm, _ = c.FindMessageByEventID(issueCommentUniqueID(event.Repository.FullName, event.Issue.ID, event.Comment.Parent.ID)) } if rm == nil { rm, _ = c.FindMessageByEventID(issueUniqueID(event.Repository.FullName, event.Issue.ID)) } msg := c.NewMessage().AddEventID(issueCommentUniqueID(event.Repository.FullName, event.Issue.ID, event.Comment.ID)).EnableHTML() if rm != nil { return msg.SetReplyToMsgID(rm.MsgID).SetText(fmt.Sprintf("%s: %s", mention(c, &event.Actor), event.Comment.Content.Raw)).Send() } wp := c.WebPreview("Issue", event.Issue.Title, event.Repository.FullName, event.Comment.Links.HTML.Href, "") return msg.SetText(fmt.Sprintf("%s %s: %s", m.URL("💬", wp), mention(c, &event.Actor), event.Comment.Content.Raw)).Send() case "repo:commit_comment_created": event := api.RepoCommitCommentCreatedEvent{} err := wc.JSON(&event) if err != nil { return err } var rm *integram.Message if event.Comment.Parent.ID > 0 { // actually bitbucket doesn't provide parent id for issue comments for now rm, _ = c.FindMessageByEventID(commitCommentUniqueID(event.Commit.Hash, event.Comment.Parent.ID)) } if rm == nil { rm, _ = c.FindMessageByEventID(commitUniqueID(event.Commit.Hash)) } msg := c.NewMessage().AddEventID(commitCommentUniqueID(event.Commit.Hash, event.Comment.Parent.ID)).EnableHTML() if rm != nil { return msg.SetReplyToMsgID(rm.MsgID).SetText(fmt.Sprintf("%s: %s", mention(c, &event.Actor), event.Comment.Content.Raw)).Send() } wp := c.WebPreview("Commit", "@"+event.Commit.Hash[0:10], event.Repository.FullName, event.Comment.Links.HTML.Href, "") return msg.SetText(fmt.Sprintf("%s %s: %s", m.URL("💬", wp), mention(c, &event.Actor), event.Comment.Content.Raw)).Send() case "issue:updated": event := api.IssueUpdatedEvent{} err := wc.JSON(&event) if err != nil { return err } eventID := issueUniqueID(event.Repository.FullName, event.Issue.ID) rm, _ := c.FindMessageByEventID(eventID) msg := c.NewMessage().AddEventID(issueCommentUniqueID(event.Repository.FullName, event.Issue.ID, event.Comment.ID)).EnableHTML() if event.Comment.Content.Raw != "" { event.Comment.Content.Raw = ": " + event.Comment.Content.Raw } if rm != nil { c.EditMessagesTextWithEventID(eventID, issueText(c, &event.Issue)) // if last Issue message just posted if err == nil && time.Now().Sub(rm.Date).Seconds() < 60 { return nil } return msg.SetReplyToMsgID(rm.MsgID).SetText(fmt.Sprintf("%s update the issue%s", mention(c, &event.Actor), event.Comment.Content.Raw)).Send() } return msg.SetText(fmt.Sprintf("%s updated an issue%s\n%s", mention(c, &event.Actor), event.Comment.Content.Raw, issueText(c, &event.Issue))).Send() case "pullrequest:updated": event := api.PullRequestCreatedEvent{} err := wc.JSON(&event) if err != nil { return err } prText := prText(c, &event.PullRequest) eventID := prUniqueID(event.Repository.FullName, event.PullRequest.ID) rm, _ := c.FindMessageByEventID(eventID) msg := c.NewMessage() if rm != nil { c.EditMessagesTextWithEventID(eventID, prText) // if last PR message just posted if err == nil && time.Now().Sub(rm.Date).Seconds() < 60 { return nil } msg.SetReplyToMsgID(rm.MsgID) } else { msg.AddEventID(prUniqueID(event.Repository.FullName, event.PullRequest.ID)) } return msg. SetText("✏️ " + prText). EnableHTML().Send() case "pullrequest:created": event := api.PullRequestCreatedEvent{} err := wc.JSON(&event) if err != nil { return err } c.SetServiceCache(prUniqueID(event.Repository.FullName, event.PullRequest.ID), event.PullRequest, time.Hour*24*365) return c.NewMessage().AddEventID(prUniqueID(event.Repository.FullName, event.PullRequest.ID)). SetText(prText(c, &event.PullRequest)). EnableHTML().Send() case "pullrequest:approved": event := api.PullRequestApprovedEvent{} err := wc.JSON(&event) if err != nil { return err } rm, _ := c.FindMessageByEventID(prUniqueID(event.Repository.FullName, event.PullRequest.ID)) msg := c.NewMessage().EnableHTML() if rm != nil { return msg.SetReplyToMsgID(rm.MsgID).SetText(fmt.Sprintf("✅ Approved by %s", mention(c, &event.Actor))).Send() } wp := c.WebPreview("Pull Request", event.PullRequest.Title, "by "+event.PullRequest.Author.DisplayName+" in "+event.Repository.FullName, event.PullRequest.Links.HTML.Href, "") return msg.SetText(fmt.Sprintf("✅ %s by %s", m.URL("Approved", wp), mention(c, &event.Actor))).Send() case "pullrequest:unapproved": event := api.PullRequestApprovalRemovedEvent{} err := wc.JSON(&event) if err != nil { return err } rm, _ := c.FindMessageByEventID(prUniqueID(event.Repository.FullName, event.PullRequest.ID)) msg := c.NewMessage().EnableHTML() if rm != nil { return msg.SetReplyToMsgID(rm.MsgID).SetText(fmt.Sprintf("❌ %s removed approval", mention(c, &event.Actor))).Send() } wp := c.WebPreview("Pull Request", event.PullRequest.Title, "by "+event.PullRequest.Author.DisplayName+" in "+event.Repository.FullName, event.PullRequest.Links.HTML.Href, "") return msg.SetText(fmt.Sprintf("❌ %s %s", mention(c, &event.Actor), m.URL("removed approval", wp))).Send() case "pullrequest:fulfilled": event := api.PullRequestMergedEvent{} err := wc.JSON(&event) if err != nil { return err } rm, _ := c.FindMessageByEventID(prUniqueID(event.Repository.FullName, event.PullRequest.ID)) msg := c.NewMessage().EnableHTML() if rm != nil { return msg.SetReplyToMsgID(rm.MsgID).SetText(fmt.Sprintf("✅ Merged by %s", mention(c, &event.Actor))).Send() } wp := c.WebPreview("Pull Request", event.PullRequest.Title, "by "+event.PullRequest.Author.DisplayName+" in "+event.Repository.FullName, event.PullRequest.Links.HTML.Href, "") return msg.SetText(fmt.Sprintf("✅ %s by %s", m.URL("Merged", wp), mention(c, &event.Actor))).Send() case "pullrequest:rejected": event := api.PullRequestDeclinedEvent{} err := wc.JSON(&event) if err != nil { return err } rm, _ := c.FindMessageByEventID(prUniqueID(event.Repository.FullName, event.PullRequest.ID)) msg := c.NewMessage().EnableHTML() if rm != nil { return msg.SetReplyToMsgID(rm.MsgID).SetText(fmt.Sprintf("❌ Declined by %s: %s", mention(c, &event.Actor), event.PullRequest.Reason)).Send() } wp := c.WebPreview("Pull Request", event.PullRequest.Title, "by "+event.PullRequest.Author.DisplayName+" in "+event.Repository.FullName, event.PullRequest.Links.HTML.Href, "") return msg.SetText(fmt.Sprintf("❌ %s by %s", m.URL("Declined", wp), mention(c, &event.Actor))).Send() case "pullrequest:comment_created": event := api.PullRequestCommentCreatedEvent{} err := wc.JSON(&event) if err != nil { return err } rm, _ := c.FindMessageByEventID(prUniqueID(event.Repository.FullName, event.PullRequest.ID)) msg := c.NewMessage().AddEventID(prCommentUniqueID(event.Repository.FullName, event.PullRequest.ID, event.Comment.ID)).EnableHTML() if rm != nil { return msg.SetReplyToMsgID(rm.MsgID).SetText(fmt.Sprintf("%s: %s", mention(c, &event.Actor), event.Comment.Content.Raw)).Send() } wp := c.WebPreview("Pull Request", event.PullRequest.Title, event.Repository.FullName, event.PullRequest.Links.HTML.Href, "") return msg.SetText(fmt.Sprintf("%s %s: %s", m.URL("💬", wp), mention(c, &event.Actor), event.Comment.Content.Raw)).Send() } return err }
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 }
func oAuthSuccessful(c *integram.Context) error { return c.NewMessage().SetText("Great! Now you can reply issues, commits, merge requests and snippets").Send() }
func webhookHandler(c *integram.Context, request *integram.WebhookContext) (err error) { wh := &webhook{} err = request.JSON(wh) if err != nil { return } msg := c.NewMessage() if wh.Repository.Homepage != "" { c.SetServiceBaseURL(wh.Repository.Homepage) } else if wh.ObjectAttributes != nil { if wh.ObjectAttributes.URL == "" { c.Log().WithField("wh", wh).Error("gitlab webhook empty url") } c.SetServiceBaseURL(wh.ObjectAttributes.URL) } switch wh.ObjectKind { case "push": s := strings.Split(wh.Ref, "/") branch := s[len(s)-1] text := "" added := 0 removed := 0 modified := 0 anyOherPersonCommits := false for _, commit := range wh.Commits { if commit.Author.Email != wh.UserEmail && commit.Author.Name != wh.UserName { anyOherPersonCommits = true } } for _, commit := range wh.Commits { commit.Message = strings.TrimSuffix(commit.Message, "\n") if anyOherPersonCommits { text += mention(c, commit.Author.Name, commit.Author.Email) + ": " } text += m.URL(commit.Message, commit.URL) + "\n" added += len(commit.Added) removed += len(commit.Removed) modified += len(commit.Modified) } f := "" if modified > 0 { f += strconv.Itoa(modified) + " files modified" } if added > 0 { if f == "" { f += strconv.Itoa(added) + " files added" } else { f += " " + strconv.Itoa(added) + " added" } } if removed > 0 { if f == "" { f += strconv.Itoa(removed) + " files removed" } else { f += " " + strconv.Itoa(removed) + " removed" } } wp := "" if len(wh.Commits) > 1 { wp = c.WebPreview(fmt.Sprintf("%d commits", len(wh.Commits)), "@"+wh.Before[0:10]+" ... @"+wh.After[0:10], f, compareURL(wh.Repository.Homepage, wh.Before, wh.After), "") } else if len(wh.Commits) == 1 { wp = c.WebPreview("Commit", "@"+wh.After[0:10], f, wh.Commits[0].URL, "") } var err error if len(wh.Commits) > 0 { if len(wh.Commits) == 1 { msg.SetReplyAction(commitReplied, c.ServiceBaseURL.String(), wh.ProjectID, wh.Commits[0].ID) } else { msg.SetReplyAction(commitsReplied, c.ServiceBaseURL.String(), wh.ProjectID, wh.Commits) } text := fmt.Sprintf("%s %s to %s\n%s", mention(c, wh.UserName, wh.UserEmail), m.URL("pushed", wp), m.URL(wh.Repository.Name+"/"+branch, wh.Repository.Homepage+"/tree/"+url.QueryEscape(branch)), text) c.Chat.SetCache("commit_"+wh.Commits[len(wh.Commits)-1].ID, text, time.Hour*24*30) err = msg.AddEventID("commit_" + wh.Commits[len(wh.Commits)-1].ID).SetText(text). EnableHTML(). Send() } else { if wh.After != "0000000000000000000000000000000000000000" && wh.After != "" { err = msg.SetText(fmt.Sprintf("%s created branch %s\n%s", mention(c, wh.UserName, wh.UserEmail), m.URL(wh.Repository.Name+"/"+branch, wh.Repository.Homepage+"/tree/"+url.QueryEscape(branch)), text)). EnableHTML(). Send() } else { err = msg.SetText(fmt.Sprintf("%s deleted branch %s\n%s", mention(c, wh.UserName, wh.UserEmail), m.Bold(wh.Repository.Name+"/"+branch), text)). EnableHTML(). Send() } } return err case "tag_push": s := strings.Split(wh.Ref, "/") itemType := s[len(s)-2] if itemType == "tags" { itemType = "tag" } else if itemType == "heads" { itemType = "branch" } return msg.SetText(fmt.Sprintf("%s pushed new %s at %s", mention(c, wh.UserName, wh.UserEmail), m.URL(itemType+" "+s[len(s)-1], wh.Repository.Homepage+"/tree/"+s[len(s)-1]), m.URL(wh.UserName+" / "+wh.Repository.Name, wh.Repository.Homepage))). EnableHTML().DisableWebPreview().Send() case "issue": if wh.ObjectAttributes.MilestoneID > 0 { // Todo: need an API access to fetch milestones } msg.SetReplyAction(issueReplied, c.ServiceBaseURL.String(), wh.ObjectAttributes.ProjectID, wh.ObjectAttributes.ID) if wh.ObjectAttributes.Action == "open" { return msg.AddEventID("issue_" + strconv.Itoa(wh.ObjectAttributes.ID)).SetText(fmt.Sprintf("%s %s %s at %s:\n%s\n%s", mention(c, wh.User.Username, wh.UserEmail), wh.ObjectAttributes.State, m.URL("issue", wh.ObjectAttributes.URL), m.URL(wh.User.Username+" / "+wh.Repository.Name, wh.Repository.Homepage), m.Bold(wh.ObjectAttributes.Title), wh.ObjectAttributes.Description)). EnableHTML().DisableWebPreview().Send() } action := "updated" if wh.ObjectAttributes.Action == "reopen" { action = "reopened" } else if wh.ObjectAttributes.Action == "close" { action = "closed" } id := issueMessageID(c, wh.ObjectAttributes.ID) if id > 0 { // reply to existing message return msg.SetText(fmt.Sprintf("%s by %s", m.Bold(action), mention(c, wh.User.Username, ""))). EnableHTML().DisableWebPreview().SetReplyToMsgID(id).Send() } // original message not found. Send WebPreview wp := c.WebPreview("Issue", wh.ObjectAttributes.Title, wh.User.Username+" / "+wh.Repository.Name, wh.ObjectAttributes.URL, "") return msg.SetText(fmt.Sprintf("%s by %s", m.URL(action, wp), mention(c, wh.User.Username, ""))).EnableHTML().Send() case "note": wp := "" noteType := "" originMsg := &integram.Message{} noteID := strconv.Itoa(wh.ObjectAttributes.ID) if wh.ObjectAttributes.Note == "Commit" { // collisions by date are unlikely here noteID = wh.ObjectAttributes.CreatedAt } if msg, _ := c.FindMessageByEventID(noteUniqueID(wh.ObjectAttributes.ProjectID, noteID)); msg != nil { return nil } switch wh.ObjectAttributes.NoteableType { case "Commit": noteType = "commit" originMsg, _ = c.FindMessageByEventID(fmt.Sprintf("commit_%s", wh.ObjectAttributes.CommitID)) if originMsg != nil { break } wp = c.WebPreview("Commit", "@"+wh.ObjectAttributes.CommitID[0:10], wh.User.Username+" / "+wh.Repository.Name, wh.ObjectAttributes.URL, "") case "MergeRequest": noteType = "merge request" originMsg, _ = c.FindMessageByEventID(fmt.Sprintf("mr_%d", wh.MergeRequest.ID)) if originMsg != nil { break } wp = c.WebPreview("Merge Request", wh.MergeRequest.Title, wh.User.Username+" / "+wh.Repository.Name, wh.ObjectAttributes.URL, "") case "Issue": noteType = "issue" originMsg, _ = c.FindMessageByEventID(fmt.Sprintf("issue_%d", wh.Issue.ID)) if originMsg != nil { break } wp = c.WebPreview("Issue", wh.Issue.Title, wh.User.Username+" / "+wh.Repository.Name, wh.ObjectAttributes.URL, "") case "Snippet": noteType = "snippet" originMsg, _ = c.FindMessageByEventID(fmt.Sprintf("snippet_%d", wh.Snippet.ID)) if originMsg != nil { break } wp = c.WebPreview("Snippet", wh.Snippet.Title, wh.User.Username+" / "+wh.Repository.Name, wh.ObjectAttributes.URL, "") } if originMsg == nil { if wp == "" { wp = wh.ObjectAttributes.URL } if noteType == "" { noteType = strings.ToLower(wh.ObjectAttributes.NoteableType) } return msg.SetTextFmt("%s commented on %s: %s", mention(c, wh.User.Username, ""), m.URL(noteType, wp), wh.ObjectAttributes.Note). EnableHTML(). Send() } return msg.SetText(fmt.Sprintf("%s: %s", mention(c, wh.User.Username, ""), wh.ObjectAttributes.Note)). DisableWebPreview().EnableHTML().SetReplyToMsgID(originMsg.MsgID).Send() case "merge_request": cs := chatSettings(c) if !cs.MR.Open && (wh.ObjectAttributes.Action == "open" || wh.ObjectAttributes.Action == "reopen") { return nil } if !cs.MR.Close && (wh.ObjectAttributes.Action == "close") { return nil } if !cs.MR.Update && (wh.ObjectAttributes.Action == "update") { return nil } if !cs.MR.Merge && (wh.ObjectAttributes.Action == "merge") { return nil } if wh.ObjectAttributes.Action == "open" { if wh.ObjectAttributes.Description != "" { wh.ObjectAttributes.Description = "\n" + wh.ObjectAttributes.Description } err := msg.AddEventID("mr_" + strconv.Itoa(wh.ObjectAttributes.ID)).SetText(fmt.Sprintf("%s %s %s at %s:\n%s%s", mention(c, wh.User.Username, wh.UserEmail), wh.ObjectAttributes.State, m.URL("merge request", wh.ObjectAttributes.URL), m.URL(wh.UserName+" / "+wh.Repository.Name, wh.Repository.Homepage), m.Bold(wh.ObjectAttributes.Title), wh.ObjectAttributes.Description)). EnableHTML().DisableWebPreview().Send() return err } originMsg, _ := c.FindMessageByEventID(fmt.Sprintf("mr_%d", wh.ObjectAttributes.ID)) if originMsg != nil { return msg.SetText(fmt.Sprintf("%s %s by %s", m.URL("merge request", wh.ObjectAttributes.URL), wh.ObjectAttributes.State, mention(c, wh.User.Username, wh.UserEmail))). EnableHTML().SetReplyToMsgID(originMsg.MsgID).DisableWebPreview().Send() } wp := c.WebPreview("Merge Request", wh.ObjectAttributes.Title, wh.ObjectAttributes.Description, wh.ObjectAttributes.URL, "") return msg.SetText(fmt.Sprintf("%s %s by %s", m.URL("Merge request", wp), wh.ObjectAttributes.State, mention(c, wh.User.Username, wh.UserEmail))). EnableHTML().Send() case "build": time.Sleep(time.Second) // workaround for simultaneously push/build webhooks // todo: replace with job? commitMsg, _ := c.FindMessageByEventID(fmt.Sprintf("commit_%s", wh.SHA)) text := "" commit := "" build := m.URL(strings.ToUpper(wh.BuildStage[0:1])+wh.BuildStage[1:], fmt.Sprintf("%s/builds/%d", wh.Repository.Homepage, wh.BuildID)) if commitMsg == nil { hpURL := strings.Split(wh.Repository.Homepage, "/") username := hpURL[len(hpURL)-2] commit = m.URL("Commit", c.WebPreview("Commit", "@"+wh.SHA[0:10], username+" / "+wh.Repository.Name, wh.Repository.URL+"/commit/"+wh.SHA, "")) + " " build = m.URL(wh.BuildStage, fmt.Sprintf("%s/builds/%d", wh.Repository.Homepage, wh.BuildID)) } else { msg.SetReplyToMsgID(commitMsg.MsgID).DisableWebPreview() } if strings.ToLower(wh.BuildStage) != strings.ToLower(wh.BuildName) { build += " #" + wh.BuildName } if wh.BuildStatus == "pending" { text = "⏳ CI: " + commit + build + " is pending" } else if wh.BuildStatus == "running" { text = "⚙ CI: " + commit + build + " is running" } else if wh.BuildStatus == "success" { text = fmt.Sprintf("✅ CI: "+commit+build+" succeeded after %.1f sec", wh.BuildDuration) } else if wh.BuildStatus == "failed" { text = fmt.Sprintf("‼️ CI: "+commit+build+" failed after %.1f sec", wh.BuildDuration) } else if wh.BuildStatus == "canceled" { text = fmt.Sprintf("🔚 CI: "+commit+build+" canceled by %s after %.1f sec", mention(c, wh.User.Name, ""), wh.BuildDuration) } if commitMsg != nil { var commitMsgText string c.Chat.Cache("commit_"+wh.Commit.SHA, &commitMsgText) if commitMsgText != "" { _, err = c.EditMessagesTextWithEventID(commitMsg.EventID[0], commitMsgText+"\n"+text) } } cs := chatSettings(c) if cs.CI.Success && (wh.BuildStatus == "success") || cs.CI.Cancel && (wh.BuildStatus == "canceled") || cs.CI.Fail && (wh.BuildStatus == "failed") { return msg.SetText(text). EnableHTML().Send() } } return }
func oAuthSuccessful(c *integram.Context) error { c.Service().SheduleJob(cacheNickMap, 0, time.Now().Add(time.Second*5), c) return c.NewMessage().SetText("Great! Now you can reply issues, commits, merge requests and snippets").Send() }