-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Eddie Zaneski <eddiezane@gmail.com>
- Loading branch information
Showing
2 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package transferissue |