...

Source file src/edge-infra.dev/pkg/f8n/devinfra/jack/plugin/enforce_milestone/issues.go

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

     1  package enforcemilestone
     2  
     3  import (
     4  	"github.com/google/go-github/v47/github"
     5  
     6  	"edge-infra.dev/pkg/f8n/devinfra/jack/constants"
     7  	"edge-infra.dev/pkg/f8n/devinfra/jack/plugin"
     8  )
     9  
    10  // Check if a milestone was initially added and is valid
    11  func openingMilestone(hp plugin.HandlerParams, ie github.IssuesEvent) {
    12  	milestone := ie.GetIssue().GetMilestone()
    13  	if milestone == nil {
    14  		// Milestone isn't there. Need to add label
    15  		addMilestoneLabel(hp, ie.GetIssue().GetNumber())
    16  		return
    17  	}
    18  }
    19  
    20  // Simple add label
    21  func addMilestoneLabel(hp plugin.HandlerParams, num int) {
    22  	labels := []string{constants.TriageMilestoneLabel}
    23  	_, _, err := hp.Client.Issues().AddLabelsToIssue(hp.Ctx, hp.Org, hp.Repo, num, labels)
    24  	if err != nil {
    25  		hp.Log.Error(err, "Failed to add needs-milestone label")
    26  	}
    27  }
    28  
    29  // Simple remove label
    30  func removeMilestoneLabel(hp plugin.HandlerParams, ie github.IssuesEvent) {
    31  	hasTriage := false
    32  	for _, x := range ie.Issue.Labels {
    33  		if x.GetName() == constants.TriageMilestoneLabel {
    34  			hasTriage = true
    35  			break
    36  		}
    37  	}
    38  
    39  	// exit if theres no triage label
    40  	if !hasTriage {
    41  		hp.Log.Info("no triage milestone label exists, ignoring")
    42  		return
    43  	}
    44  
    45  	_, err := hp.Client.Issues().RemoveLabelForIssue(hp.Ctx, hp.Org, hp.Repo, ie.GetIssue().GetNumber(), constants.TriageMilestoneLabel)
    46  	if err != nil {
    47  		hp.Log.Error(err, "Failed to remove needs-milestone label")
    48  	}
    49  }
    50  
    51  // If label added is triage/needs-milestone, if it is and has a valid milestone remove it
    52  func checkLabel(hp plugin.HandlerParams, ie github.IssuesEvent) {
    53  	log := hp.Log
    54  	log.Info("Check label for milestone")
    55  	if ie.GetSender().GetType() == constants.Bot {
    56  		hp.Log.Info("Bot check on addLabel")
    57  		return
    58  	}
    59  	if ie.Label.GetName() == constants.TriageMilestoneLabel {
    60  		milestone := ie.GetIssue().GetMilestone()
    61  		if milestone != nil {
    62  			// You have a valid milestone
    63  			removeMilestoneLabel(hp, ie)
    64  		}
    65  	}
    66  }
    67  
    68  // See if the label removed is triage/needs-milestone, if it is put it back
    69  func checkRemoveLabel(hp plugin.HandlerParams, ie github.IssuesEvent) {
    70  	log := hp.Log
    71  	log.Info("CHECKING REMOVAL")
    72  	if ie.GetSender().GetType() == constants.Bot {
    73  		log.Info("Bot check on addLabel")
    74  		return
    75  	}
    76  	if ie.Label.GetName() == constants.TriageMilestoneLabel {
    77  		milestone := ie.GetIssue().GetMilestone()
    78  		if milestone != nil {
    79  			return
    80  			// You have a valid milestone
    81  		}
    82  	}
    83  	addMilestoneLabel(hp, ie.GetIssue().GetNumber())
    84  }
    85  

View as plain text