...

Source file src/edge-infra.dev/pkg/f8n/devinfra/jack/plugin/triage_parent/issue.go

Documentation: edge-infra.dev/pkg/f8n/devinfra/jack/plugin/triage_parent

     1  package triageparent
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"edge-infra.dev/pkg/lib/logging"
     8  
     9  	"github.com/google/go-github/v47/github"
    10  
    11  	"edge-infra.dev/pkg/f8n/devinfra/jack/constants"
    12  	guestservices "edge-infra.dev/pkg/f8n/devinfra/jack/guest_services"
    13  	"edge-infra.dev/pkg/f8n/devinfra/jack/plugin"
    14  )
    15  
    16  // handle parent command in body when creating an issue
    17  // if there's no /parent command, and it's not an epic add triage parent
    18  func checkForParentInNewIssue(ctx context.Context, log logging.EdgeLogger, client plugin.GithubClientInterface, event github.IssuesEvent, org, repo string) error {
    19  	labels := event.Issue.Labels
    20  
    21  	isEpic := false
    22  	for _, label := range labels {
    23  		if label.GetName() == string(constants.Epic) {
    24  			isEpic = true
    25  		}
    26  	}
    27  
    28  	parentCommands := guestservices.GetCommands(event.GetIssue().GetBody())
    29  
    30  	if len(parentCommands.Parents.Added) <= 0 && !isEpic {
    31  		newLabels := []string{constants.TriageParentLabel}
    32  		_, _, err := client.Issues().AddLabelsToIssue(ctx, org, repo, event.Issue.GetNumber(), newLabels)
    33  		if err != nil {
    34  			log.Error(err, fmt.Sprintf("Failed to add %s", constants.TriageParentLabel))
    35  			return err
    36  		}
    37  	}
    38  
    39  	return nil
    40  }
    41  
    42  func labelAdded(hp plugin.HandlerParams, event github.IssuesEvent) error {
    43  	hp.Log.Info("triage labeled event")
    44  
    45  	// if a bot added a label we don't want to handle it twice
    46  	if event.GetSender().GetType() == "Bot" {
    47  		return nil
    48  	}
    49  
    50  	// if epic label is added and the triage parent label exists remove it
    51  	labels := event.Issue.Labels
    52  	repoID := event.GetRepo().GetID()
    53  
    54  	sender := guestservices.ParentChild{}
    55  	sender.New(event.GetIssue().GetBody(), labels, constants.Preamble, constants.Postamble, repoID)
    56  
    57  	isEpic := false
    58  	for _, label := range labels {
    59  		if label.GetName() == string(constants.Epic) {
    60  			isEpic = true
    61  		}
    62  	}
    63  
    64  	// if there's an epic label or
    65  	// if there are parents in the list and the new label is triage parent
    66  	// then just remove the triage parent label
    67  	if isEpic || (len(sender.List.Parent) > 0 && event.Label.GetName() == constants.TriageParentLabel) {
    68  		hp.Log.Info("removing needs parent label")
    69  		_, err := hp.Client.Issues().RemoveLabelForIssue(hp.Ctx, hp.Org, hp.Repo, event.Issue.GetNumber(), constants.TriageParentLabel)
    70  		if err != nil {
    71  			hp.Log.Error(err, fmt.Sprintf("Failed to remove %s", constants.TriageParentLabel))
    72  			return err
    73  		}
    74  	}
    75  	return nil
    76  }
    77  
    78  func labelRemoved(hp plugin.HandlerParams, event github.IssuesEvent) error {
    79  	hp.Log.Info("triage unlabeled event")
    80  
    81  	labels := event.Issue.Labels
    82  
    83  	isEpic := false
    84  	for _, label := range labels {
    85  		if label.GetName() == string(constants.Epic) {
    86  			isEpic = true
    87  		}
    88  	}
    89  	repoID := event.GetRepo().GetID()
    90  
    91  	sender := guestservices.ParentChild{}
    92  	sender.New(event.GetIssue().GetBody(), labels, constants.Preamble, constants.Postamble, repoID)
    93  
    94  	// if there's no epic label and no parents in the list add the triage parent label
    95  	if !isEpic && len(sender.List.Parent) <= 0 {
    96  		hp.Log.Info("adding needs parent label")
    97  
    98  		// add triage
    99  		newLabels := []string{"triage/needs-parent"}
   100  		_, _, err := hp.Client.Issues().AddLabelsToIssue(hp.Ctx, hp.Org, hp.Repo, event.Issue.GetNumber(), newLabels)
   101  		if err != nil {
   102  			hp.Log.Error(err, fmt.Sprintf("Failed to add %s", constants.TriageParentLabel))
   103  			return err
   104  		}
   105  	}
   106  
   107  	return nil
   108  }
   109  

View as plain text