...

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

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

     1  package task
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"edge-infra.dev/pkg/lib/logging"
     9  
    10  	"github.com/google/go-github/v47/github"
    11  
    12  	"edge-infra.dev/pkg/f8n/devinfra/jack/constants"
    13  	guestservices "edge-infra.dev/pkg/f8n/devinfra/jack/guest_services"
    14  	"edge-infra.dev/pkg/f8n/devinfra/jack/plugin"
    15  )
    16  
    17  func addKindToNewIssue(ctx context.Context, log logging.EdgeLogger, client plugin.GithubClientInterface, event github.IssuesEvent) error {
    18  	hasKindLabel := false
    19  	labels := event.GetIssue().Labels
    20  	for _, label := range labels {
    21  		_, isKind := validateKindCommand(label.GetName())
    22  		if isKind {
    23  			hasKindLabel = true
    24  		}
    25  	}
    26  	// It hasKindLabel, exit
    27  	if hasKindLabel {
    28  		return nil
    29  	}
    30  
    31  	// check for a kind command in the issue body
    32  	// if none add the triage label
    33  	taskCommands := guestservices.GetSpecificCommandFromBody(event.Issue.GetBody(), "kind", false, false)
    34  	if len(taskCommands) <= 0 {
    35  		taskCommands = append(taskCommands, string(constants.Task))
    36  	}
    37  
    38  	// Validate the label, if not right label --> bounce
    39  	tempCommand := taskCommands[0]
    40  	kind, validKind := validateKindCommand(tempCommand)
    41  	if !validKind {
    42  		log.Info("command is not a valid KIND-LABEL")
    43  		kind = string(constants.Task)
    44  	}
    45  
    46  	// if there is a kind cmd, validate
    47  	// if the command is not valid add the triage label
    48  	issueNumber := event.GetIssue().GetNumber()
    49  	repo := event.GetRepo()
    50  	repoName := repo.GetName()
    51  	repoOwner := repo.GetOwner().GetLogin()
    52  
    53  	// if the command is valid add the appropriate label to the issue
    54  	labelBody := []string{}
    55  	// takes in the first kind label that the user wants to add in even if they want to add in multiple
    56  	// updates the labels
    57  	labelBody = append(labelBody, kind)
    58  	_, _, err := client.Issues().AddLabelsToIssue(ctx, repoOwner, repoName, issueNumber, labelBody)
    59  	if err != nil {
    60  		log.Error(err, "Task: Failed to update issue desc")
    61  		return err
    62  	}
    63  	return nil
    64  }
    65  
    66  func addTaskLabel(ctx context.Context, log logging.EdgeLogger, client plugin.GithubClientInterface, event github.IssuesEvent) error {
    67  	// Prevent double check
    68  	if event.GetSender().GetType() == "Bot" {
    69  		return nil
    70  	}
    71  	issueNumber := event.GetIssue().GetNumber()
    72  	repo := event.GetRepo()
    73  	repoName := repo.GetName()
    74  	repoOwner := repo.GetOwner().GetLogin()
    75  
    76  	// check if the new label is a kind / triage kind label if not bounce
    77  	labelName, valid := validateKindCommand(event.Label.GetName())
    78  
    79  	// If label is not a valid kind/ label or diff than "kind/task"
    80  	// Exit --> Autobot rollout!
    81  	if !valid || !strings.Contains(labelName, string(constants.Task)) {
    82  		log.Info("not a valid KIND-LABEL ----> exiting removeTaskLabel")
    83  		return nil
    84  	}
    85  
    86  	log.Info(fmt.Sprintf("%s is a valid KIND command", labelName))
    87  
    88  	//remove old kind label
    89  	labelNames := []string{}
    90  	for _, label := range event.GetIssue().Labels {
    91  		name := label.GetName()
    92  		if strings.Contains(name, "kind/") && name != string(constants.Task) { //name == labelName ||
    93  			log.Info(fmt.Sprintf("adding %s", name))
    94  			labelNames = append(labelNames, name)
    95  		}
    96  	}
    97  
    98  	// update labels with old kind label removed
    99  	githubIssueBody := &github.IssueRequest{Labels: &labelNames}
   100  	_, _, err := client.Issues().Edit(ctx, repoOwner, repoName, issueNumber, githubIssueBody)
   101  	if err != nil {
   102  		log.Error(err, "Task: Failed to update issue desc")
   103  		return err
   104  	}
   105  	return nil
   106  }
   107  
   108  func removeTaskLabel(ctx context.Context, log logging.EdgeLogger, client plugin.GithubClientInterface, event github.IssuesEvent) error {
   109  	issueNumber := event.GetIssue().GetNumber()
   110  	repo := event.GetRepo()
   111  	repoName := repo.GetName()
   112  	repoOwner := repo.GetOwner().GetLogin()
   113  
   114  	// bounce if the label isnt a valid kind and isnt the kind/epic label
   115  	//labelName := event.Label.GetName()
   116  
   117  	labelName, valid := validateKindCommand(event.Label.GetName())
   118  	if !valid && labelName != string(constants.Task) {
   119  		log.Info("not a valid size label exiting removelabel")
   120  		return nil
   121  	}
   122  	log.Info("Removing previous KIND-LABEL")
   123  	labelBody := createNewLabelBodyWithKind(event.GetIssue().Labels)
   124  
   125  	err := updateIssueTaskLabels(ctx, client, labelBody, repoOwner, repoName, issueNumber)
   126  	if err != nil {
   127  		log.Error(err, "Task: Failed to update issue desc")
   128  		return err
   129  	}
   130  	return nil
   131  }
   132  
   133  // update an issues labels (sets all labels)
   134  func updateIssueTaskLabels(ctx context.Context, client plugin.GithubClientInterface, labels []string, repoOwner, repoName string, issueNumber int) error {
   135  	githubIssueBody := &github.IssueRequest{Labels: &labels}
   136  	_, _, err := client.Issues().Edit(ctx, repoOwner, repoName, issueNumber, githubIssueBody)
   137  	if err != nil {
   138  		return err
   139  	}
   140  	return nil
   141  }
   142  
   143  /*
   144  func createNewLabelBodyNoKind(labels []*github.Label) []string {
   145  	label := []string{}
   146  	// Add all existing labels to new label slice and toggle kind/ Label
   147  	for _, v := range labels {
   148  		name := v.GetName()
   149  		if !strings.Contains(name, "kind/") {
   150  			label = append(label, name)
   151  		}
   152  	}
   153  	return label
   154  }
   155  */
   156  
   157  func createNewLabelBodyWithKind(labels []*github.Label) []string {
   158  	label := []string{}
   159  	kindLabelString := string(constants.Task)
   160  
   161  	// Add all existing labels to new label slice and toggle kind Label
   162  	hasKindLabel := false
   163  	for _, v := range labels {
   164  		name := v.GetName()
   165  		label = append(label, name)
   166  		if strings.Contains(name, "kind/") {
   167  			hasKindLabel = true
   168  		}
   169  	}
   170  
   171  	// Add kind/task label if non detected
   172  	if !hasKindLabel {
   173  		label = append(label, kindLabelString)
   174  	}
   175  	return label
   176  }
   177  

View as plain text