|
| 1 | +package jira |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/sirupsen/logrus" |
| 8 | + "github.com/slack-go/slack" |
| 9 | + |
| 10 | + "github.com/openshift/ci-tools/pkg/jira" |
| 11 | +) |
| 12 | + |
| 13 | +func handleCloseJira( |
| 14 | + client *slack.Client, |
| 15 | + issueFiler jira.IssueFiler, |
| 16 | + threadMapping *ThreadJiraMapping, |
| 17 | + channelID, threadTS, userID string, |
| 18 | + logger *logrus.Entry, |
| 19 | +) (bool, error) { |
| 20 | + log := logger.WithField("action", "close-jira") |
| 21 | + |
| 22 | + // Check if thread has associated Jira issue |
| 23 | + ctx := context.Background() |
| 24 | + jiraKey, exists := threadMapping.Get(ctx, threadTS) |
| 25 | + if !exists { |
| 26 | + log.Info("thread does not have associated Jira issue") |
| 27 | + _, _, err := client.PostMessage(channelID, |
| 28 | + slack.MsgOptionText("This thread does not have an associated Jira issue.", false), |
| 29 | + slack.MsgOptionTS(threadTS)) |
| 30 | + return true, err |
| 31 | + } |
| 32 | + |
| 33 | + // Get thread content for final summary |
| 34 | + messages, err := GetThreadContent(client, channelID, threadTS) |
| 35 | + if err != nil { |
| 36 | + log.WithError(err).Error("failed to get thread content") |
| 37 | + return false, err |
| 38 | + } |
| 39 | + |
| 40 | + // Generate final summary |
| 41 | + _, description := GetSummary(client, channelID, threadTS, messages) |
| 42 | + finalSummary := fmt.Sprintf("Thread resolved. Final summary:\n\n%s", description) |
| 43 | + |
| 44 | + // Cast to IssueUpdater to access AddComment and TransitionIssue methods |
| 45 | + updater, ok := issueFiler.(jira.IssueUpdater) |
| 46 | + if !ok { |
| 47 | + log.Error("issueFiler does not support AddComment and TransitionIssue methods") |
| 48 | + _, _, postErr := client.PostMessage(channelID, |
| 49 | + slack.MsgOptionText("Failed to close Jira issue: issueFiler does not support required methods", false), |
| 50 | + slack.MsgOptionTS(threadTS)) |
| 51 | + return true, postErr |
| 52 | + } |
| 53 | + |
| 54 | + // Add comment with final summary (private for Red Hat employees) |
| 55 | + if err := updater.AddComment(jiraKey, finalSummary, log); err != nil { |
| 56 | + log.WithError(err).Error("failed to add comment to Jira issue") |
| 57 | + _, _, postErr := client.PostMessage(channelID, |
| 58 | + slack.MsgOptionText(fmt.Sprintf("Failed to add comment to Jira issue: %v", err), false), |
| 59 | + slack.MsgOptionTS(threadTS)) |
| 60 | + return true, postErr |
| 61 | + } |
| 62 | + |
| 63 | + // Transition issue to "Done" |
| 64 | + if err := updater.TransitionIssue(jiraKey, "Done", log); err != nil { |
| 65 | + log.WithError(err).Error("failed to transition Jira issue") |
| 66 | + _, _, postErr := client.PostMessage(channelID, |
| 67 | + slack.MsgOptionText(fmt.Sprintf("Failed to transition Jira issue: %v", err), false), |
| 68 | + slack.MsgOptionTS(threadTS)) |
| 69 | + return true, postErr |
| 70 | + } |
| 71 | + |
| 72 | + // Post confirmation |
| 73 | + jiraURL := fmt.Sprintf("https://issues.redhat.com/browse/%s", jiraKey) |
| 74 | + message := fmt.Sprintf("✅ Closed Jira issue: <%s|%s>", jiraURL, jiraKey) |
| 75 | + _, _, err = client.PostMessage(channelID, |
| 76 | + slack.MsgOptionText(message, false), |
| 77 | + slack.MsgOptionTS(threadTS)) |
| 78 | + if err != nil { |
| 79 | + log.WithError(err).Warn("failed to post confirmation message") |
| 80 | + } |
| 81 | + |
| 82 | + log.Infof("Closed Jira issue %s for thread %s", jiraKey, threadTS) |
| 83 | + return true, nil |
| 84 | +} |
0 commit comments