github.com/mongodb/grip@v0.0.0-20240213223901-f906268d82b9/send/jira_comment.go (about) 1 package send 2 3 import ( 4 "context" 5 "fmt" 6 "log" 7 "os" 8 9 "github.com/mongodb/grip/level" 10 "github.com/mongodb/grip/message" 11 "github.com/pkg/errors" 12 ) 13 14 type jiraCommentJournal struct { 15 issueID string 16 opts *JiraOptions 17 *Base 18 } 19 20 // MakeJiraCommentLogger is the same as NewJiraCommentLogger but uses a warning 21 // level of Trace 22 func MakeJiraCommentLogger(ctx context.Context, id string, opts *JiraOptions) (Sender, error) { 23 return NewJiraCommentLogger(ctx, id, opts, LevelInfo{level.Trace, level.Trace}) 24 } 25 26 // NewJiraCommentLogger constructs a Sender that creates issues to jira, given 27 // options defined in a JiraOptions struct. id parameter is the ID of the issue. 28 // ctx is used as the request context in the OAuth HTTP client 29 func NewJiraCommentLogger(ctx context.Context, id string, opts *JiraOptions, l LevelInfo) (Sender, error) { 30 if err := opts.Validate(); err != nil { 31 return nil, err 32 } 33 34 j := &jiraCommentJournal{ 35 opts: opts, 36 issueID: id, 37 Base: NewBase(id), 38 } 39 40 if err := j.opts.client.CreateClient(opts.HTTPClient, opts.BaseURL); err != nil { 41 return nil, err 42 } 43 44 authOpts := jiraAuthOpts{ 45 username: opts.BasicAuthOpts.Username, 46 password: opts.BasicAuthOpts.Password, 47 addBasicAuthHeader: opts.BasicAuthOpts.UseBasicAuth, 48 accessToken: opts.Oauth1Opts.AccessToken, 49 tokenSecret: opts.Oauth1Opts.TokenSecret, 50 privateKey: opts.Oauth1Opts.PrivateKey, 51 consumerKey: opts.Oauth1Opts.ConsumerKey, 52 } 53 if err := j.opts.client.Authenticate(ctx, authOpts); err != nil { 54 return nil, errors.Wrap(err, "authenticating") 55 } 56 57 if err := j.SetLevel(l); err != nil { 58 return nil, err 59 } 60 61 fallback := log.New(os.Stdout, "", log.LstdFlags) 62 if err := j.SetErrorHandler(ErrorHandlerFromLogger(fallback)); err != nil { 63 return nil, err 64 } 65 66 j.SetName(id) 67 j.reset = func() { 68 fallback.SetPrefix(fmt.Sprintf("[%s] ", j.Name())) 69 } 70 71 return j, nil 72 } 73 74 // Send post issues via jiraCommentJournal with information in the message.Composer 75 func (j *jiraCommentJournal) Send(m message.Composer) { 76 if j.Level().ShouldLog(m) { 77 issue := j.issueID 78 if c, ok := m.Raw().(*message.JIRAComment); ok { 79 issue = c.IssueID 80 } 81 82 j.ErrorHandler()(j.opts.client.PostComment(issue, m.String()), m) 83 } 84 } 85 86 func (j *jiraCommentJournal) Flush(_ context.Context) error { return nil }