postCreateIssue runs post-creation logic for an issue: webhook, approval finding, and auto-approve.
( ctx context.Context, stores *store.Store, webhookManager *webhook.Manager, licenseService *enterprise.LicenseService, b *bus.Bus, project *store.ProjectMessage, creatorName string, creatorEmail string, issue *store.IssueMessage, )
| 19 | |
| 20 | // postCreateIssue runs post-creation logic for an issue: webhook, approval finding, and auto-approve. |
| 21 | func postCreateIssue( |
| 22 | ctx context.Context, |
| 23 | stores *store.Store, |
| 24 | webhookManager *webhook.Manager, |
| 25 | licenseService *enterprise.LicenseService, |
| 26 | b *bus.Bus, |
| 27 | project *store.ProjectMessage, |
| 28 | creatorName string, |
| 29 | creatorEmail string, |
| 30 | issue *store.IssueMessage, |
| 31 | ) (*store.IssueMessage, error) { |
| 32 | // Trigger ISSUE_CREATED webhook. |
| 33 | webhookManager.CreateEvent(ctx, &webhook.Event{ |
| 34 | Type: storepb.Activity_ISSUE_CREATED, |
| 35 | Project: webhook.NewProject(project), |
| 36 | IssueCreated: &webhook.EventIssueCreated{ |
| 37 | Creator: &webhook.User{ |
| 38 | Name: creatorName, |
| 39 | Email: creatorEmail, |
| 40 | }, |
| 41 | Issue: webhook.NewIssue(issue), |
| 42 | }, |
| 43 | }) |
| 44 | |
| 45 | // Trigger approval finding based on issue type. |
| 46 | switch issue.Type { |
| 47 | case |
| 48 | storepb.Issue_ACCESS_GRANT, |
| 49 | storepb.Issue_ROLE_GRANT, |
| 50 | storepb.Issue_DATABASE_EXPORT: |
| 51 | |
| 52 | if err := approval.FindAndApplyApprovalTemplate(ctx, stores, webhookManager, licenseService, issue); err != nil { |
| 53 | slog.Error("failed to find approval template", |
| 54 | slog.String("project", issue.ProjectID), slog.Int64("issue_uid", issue.UID), |
| 55 | slog.String("issue_title", issue.Title), |
| 56 | log.BBError(err)) |
| 57 | } |
| 58 | |
| 59 | // Refresh issue to get updated approval payload. |
| 60 | uid := issue.UID |
| 61 | var err error |
| 62 | issue, err = stores.GetIssue(ctx, &store.FindIssueMessage{Workspace: common.GetWorkspaceIDFromContext(ctx), ProjectIDs: []string{issue.ProjectID}, UID: &uid}) |
| 63 | if err != nil { |
| 64 | return nil, errors.Wrapf(err, "failed to refresh issue") |
| 65 | } |
| 66 | |
| 67 | if issue.Type == storepb.Issue_DATABASE_EXPORT { |
| 68 | return issue, nil |
| 69 | } |
| 70 | |
| 71 | // For ACCESS_GRANT/ROLE_GRANT that is auto-approved (no approval template), complete it. |
| 72 | approved, err := utils.CheckApprovalApproved(issue.Payload.Approval) |
| 73 | if err != nil { |
| 74 | return nil, errors.Wrapf(err, "failed to check if approval is approved") |
| 75 | } |
| 76 | if !approved { |
| 77 | return issue, nil |
| 78 | } |
no test coverage detected