...

Source file src/edge-infra.dev/pkg/f8n/devinfra/jack/plugin/triage_issue/handler.go

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

     1  package triageissue
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"strings"
     7  
     8  	"github.com/google/go-github/v47/github"
     9  
    10  	"edge-infra.dev/pkg/f8n/devinfra/jack/constants"
    11  	guestservices "edge-infra.dev/pkg/f8n/devinfra/jack/guest_services"
    12  	"edge-infra.dev/pkg/f8n/devinfra/jack/plugin"
    13  )
    14  
    15  func init() {
    16  	plugin.RegisterIssueCommentHandler(constants.PluginPRIssue, handlePRComment)
    17  	plugin.RegisterPullRequestHandler(constants.PluginPRIssue, handlePR)
    18  }
    19  
    20  func handlePRComment(hp plugin.HandlerParams, ice github.IssueCommentEvent) {
    21  	hp.Log.WithName(constants.PluginPRIssue)
    22  
    23  	if ice.GetAction() != "created" {
    24  		return
    25  	}
    26  	if !ice.GetIssue().IsPullRequest() {
    27  		return
    28  	}
    29  
    30  	prBody := ice.Issue.GetBody()
    31  	prComment := ice.Comment.GetBody()
    32  
    33  	// if the new comment doesnt have the relevant command exit
    34  	if !strings.Contains(prComment, "/link") {
    35  		return
    36  	}
    37  
    38  	issueCommand := guestservices.GetCommandsFromPRBody(prComment, "link")
    39  
    40  	//LinkNum needed to isolate the relevant link in the PR body that will be changed
    41  	//LastIndex is the where the linkNum is located
    42  	//CommentSplit parses the comment body to separate the link
    43  	//CommentLink stores the link from commentSplit
    44  	linkNum, _ := guestservices.ParsePRIssueLinkBody(prBody)
    45  	lastIndex := strings.LastIndex(prBody, linkNum)
    46  
    47  	commentSplit := strings.SplitN(prComment, "#", -1)
    48  	if len(commentSplit) < 2 {
    49  		hp.Log.Info("comment doesnt have enough info to link an issue ignoring")
    50  		return
    51  	}
    52  	commentLink := commentSplit[1]
    53  
    54  	// if there is an issue command to remove issue, add label
    55  	if len(issueCommand.RemoveIssue) > 0 {
    56  		// Editing the PR body to include ISSUE_NUMBER_HERE at the end of the link
    57  		prBody = prBody[:lastIndex] + strings.Replace(prBody[lastIndex:], linkNum, "ISSUE_NUMBER_HERE", 1)
    58  		edited := github.IssueRequest{Body: &prBody}
    59  		_, _, err := hp.Client.Issues().Edit(hp.Ctx, hp.Org, hp.Repo, ice.Issue.GetNumber(), &edited)
    60  		if err != nil {
    61  			return
    62  		}
    63  
    64  		// Adding the needs-issue label
    65  		hp.Log.Info(fmt.Sprintf("attempting to add %s", constants.TriageIssueLabel))
    66  		newLabels := []string{constants.TriageIssueLabel}
    67  		_, _, err = hp.Client.Issues().AddLabelsToIssue(hp.Ctx, hp.Org, hp.Repo, ice.Issue.GetNumber(), newLabels)
    68  		if err != nil {
    69  			return
    70  		}
    71  		labelApplied = true
    72  		return
    73  	}
    74  
    75  	// if there is an issue command to add issue, remove label
    76  	if len(issueCommand.AddIssue) > 0 {
    77  		// Verifying that the new link works
    78  		_, err := http.Get(ghURL + constants.Address + commentLink)
    79  		if err != nil {
    80  			return
    81  		}
    82  
    83  		// Editing the PR body to include the new link
    84  		prBody = prBody[:lastIndex] + strings.Replace(prBody[lastIndex:], linkNum, commentLink, 1)
    85  		edited := github.IssueRequest{Body: &prBody}
    86  		_, _, err = hp.Client.Issues().Edit(hp.Ctx, hp.Org, hp.Repo, ice.Issue.GetNumber(), &edited)
    87  		if err != nil {
    88  			return
    89  		}
    90  
    91  		// Removing the needs-issue label
    92  		hp.Log.Info(fmt.Sprintf("attempting to remove %s", constants.TriageIssueLabel))
    93  		_, err = hp.Client.Issues().RemoveLabelForIssue(hp.Ctx, hp.Org, hp.Repo, ice.Issue.GetNumber(), constants.TriageIssueLabel)
    94  		if err != nil {
    95  			return
    96  		}
    97  		labelApplied = false
    98  		return
    99  	}
   100  }
   101  
   102  // handlePR checks the log for four specific responses.
   103  // Anytime those responses appear, the appropriate function will be called.
   104  func handlePR(hp plugin.HandlerParams, pre github.PullRequestEvent) {
   105  	hp.Log.WithName(constants.PluginPRIssue)
   106  
   107  	action := pre.GetAction()
   108  	switch action {
   109  	case "opened":
   110  		if err := checkForIssueInPR(hp, pre); err != nil {
   111  			hp.Log.Error(err, "Issue: Failed to check for issue in PR")
   112  			return
   113  		}
   114  	case "labeled":
   115  		if err := addLabel(hp, pre); err != nil {
   116  			hp.Log.Error(err, "Issue: Failed to add triage-issue label")
   117  			return
   118  		}
   119  	case "unlabeled":
   120  		if err := removeLabel(hp, pre); err != nil {
   121  			hp.Log.Error(err, "Issue: Failed to remove triage-issue label")
   122  			return
   123  		}
   124  	case "edited":
   125  		if err := editedPRBody(hp, pre); err != nil {
   126  			hp.Log.Error(err, "Issue: Failed to handle edited PR body")
   127  			return
   128  		}
   129  	}
   130  }
   131  

View as plain text