From f514c5cc0606e0f75cad08f473ba22fa1d689122 Mon Sep 17 00:00:00 2001 From: Laszlo Gecse Date: Mon, 7 Aug 2023 13:47:34 +0200 Subject: [PATCH] Updating component when it's not on the issue Signed-off-by: Laszlo Gecse --- internal/jira/issue/issue.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/internal/jira/issue/issue.go b/internal/jira/issue/issue.go index 3c831ccc..2e95b9ac 100644 --- a/internal/jira/issue/issue.go +++ b/internal/jira/issue/issue.go @@ -140,6 +140,10 @@ func DidIssueChange(cfg *config.Config, ghIssue *gogh.Issue, jIssue *gojira.Issu anyDifferent = true } + if GetMissingComponents(cfg, jIssue) != nil { + anyDifferent = true + } + if len(ghIssue.Labels) > 0 { //nolint:nestif // TODO(lint) ghLabels := githubLabelsToStrSlice(ghIssue.Labels) @@ -211,6 +215,11 @@ func UpdateIssue( ID: jIssue.ID, } + missingComponents := GetMissingComponents(cfg, jIssue) + for i := range missingComponents { + issue.Fields.Components = append(issue.Fields.Components, missingComponents[i]) + } + _, err := jClient.UpdateIssue(issue) if err != nil { return fmt.Errorf("updating Jira issue: %w", err) @@ -233,6 +242,33 @@ func UpdateIssue( return nil } +// GetMissingComponents compares configurated components with the Jira issue +// components. Returns the components that are missing from the Jira issue. +func GetMissingComponents(cfg *config.Config, jIssue *gojira.Issue) []*gojira.Component { + var returnComponents []*gojira.Component + + components := cfg.GetJiraComponents() + for i := range components { + configComponent := *components[i] + + found := false + + for j := range jIssue.Fields.Components { + issueComponent := *jIssue.Fields.Components[j] + + if issueComponent.Name == configComponent.Name { + found = true + break + } + } + + if !found { + returnComponents = append(returnComponents, &configComponent) + } + } + return returnComponents +} + // CreateIssue generates a Jira issue from the various fields on the given GitHub issue, then // sends it to the Jira API. func CreateIssue(cfg *config.Config, issue *gogh.Issue, ghClient github.Client, jClient jira.Client) error {