Skip to content

Commit

Permalink
RESET ME: WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Eddie Zaneski <eddiezane@gmail.com>
  • Loading branch information
eddiezane committed Aug 12, 2021
1 parent 4024d2d commit f9617b3
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
95 changes: 95 additions & 0 deletions prow/plugins/transfer-issue/transfer-issue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package transferissue

import (
"fmt"
"regexp"

"github.com/sirupsen/logrus"

"k8s.io/test-infra/prow/config"
"k8s.io/test-infra/prow/github"
"k8s.io/test-infra/prow/pluginhelp"
"k8s.io/test-infra/prow/plugins"
)

const pluginName = "transfer-issue"

var (
transferRe = regexp.MustCompile(`(?mi)^/transfer(?:-issue)?\s*(.*)$`)
)

func init() {
plugins.RegisterGenericCommentHandler(pluginName, handleGenericComment, helpProvider)
}

func helpProvider(_ *plugins.Configuration, _ []config.OrgRepo) (*pluginhelp.PluginHelp, error) {
pluginHelp := &pluginhelp.PluginHelp{
Description: "The transfer-issue plugin transfers a GitHub issue from one repo to another in the same organization.",
}
pluginHelp.AddCommand(pluginhelp.Command{
Usage: "/transfer-issue <destination repo in same org>",
Description: "Transfers an issue to a different repo in the same org.",
Featured: true,
WhoCanUse: "Org members.",
Examples: []string{"/transfer-issue kubectl"},
})
return pluginHelp, nil
}

type githubClient interface {
GetRepo(org, name string) (github.FullRepo, error)
CreateComment(org, repo string, number int, comment string) error
IsCollaborator(org, repo, user string) (bool, error)
TransferIssue(org, dstRepoNodeID string, issueNodeID string) (*github.TransferIssueMutation, error)
}

func handleGenericComment(pc plugins.Agent, e github.GenericCommentEvent) error {
// TODO: check if this is an actual comment on an issue?
if e.Action != github.GenericCommentActionCreated {
return nil
}
matches := transferRe.FindAllStringSubmatch(e.Body, -1)
if len(matches) == 0 {
return nil
}
if len(matches) != 1 && len(matches[0]) != 1 {
return fmt.Errorf("/transfer-issue must only be used once and with a single destination repo")
}
return handleTransfer(pc.GitHubClient, pc.Logger, e, matches[0][0])
}

func handleTransfer(gc githubClient, log *logrus.Entry, e github.GenericCommentEvent, dstRepoName string) error {
org := e.Repo.Owner.Login
srcRepo := e.Repo.Name
user := e.User.Login

dstRepo, err := gc.GetRepo(org, dstRepoName)
if err != nil {
log.WithError(err).Errorf("could not fetch destination repo: %s/%s", org, dstRepo)
// TODO: post comment. error here means something went wrong or that the repo doesn't exist (404)
}

isCollaboratorSrc, err := gc.IsCollaborator(org, srcRepo, user)
if err != nil {
log.WithError(err).Errorf("could not fetch if user: %s is collaborator of source repo: %s/%s", user, org, srcRepo)
// TODO: comment something went wrong?
}
isCollaboratorDst, err := gc.IsCollaborator(org, dstRepoName, user)
if err != nil {
log.WithError(err).Errorf("could not fetch if user: %s is collaborator of destination repo: %s/%s", user, org, dstRepoName)
// TODO: comment something went wrong?
}
if !(isCollaboratorSrc && isCollaboratorDst) {
// TODO: comment user is not able to transfer because they aren't an org member
}

issue, err := gc.TransferIssue(org, dstRepo.NodeID, e.NodeID)
if err != nil {
log.WithError(err).Errorf("could not transfer issue: from: to:")
// TODO: comment that issue could not be transferred
}
// TODO: is commenting at this point necessary or a race condition?
_ = fmt.Sprintf("Transferring issue to %s", issue.TransferIssue.Issue.URL)

return nil
}
1 change: 1 addition & 0 deletions prow/plugins/transfer-issue/transfer-issue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package transferissue

0 comments on commit f9617b3

Please sign in to comment.