Skip to content

Commit

Permalink
IT'S ALIVE
Browse files Browse the repository at this point in the history
  • Loading branch information
GaryPWhite committed Feb 28, 2020
1 parent 0b50ee6 commit f5d1403
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 21 deletions.
12 changes: 6 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ func main() {
},
Commands: []*cli.Command{
{
Name: "tag",
Usage: "Tag one Pull Request (--pr, --pull-request) with a specified tags JSON array (--tags, --t)",
Name: "label",
Usage: "label one Pull Request (-pr, -pull-request) with a specified tags JSON array (-tags, -t)",
Action: func(c *cli.Context) error {
tagger, err := makeTagger(c.String("enterprise-URL"), c.String("git-api-key"), c.String("tags"))
tc, err := makeTagger(c.String("enterprise-URL"), c.String("git-api-key"), c.String("tags"))
if err != nil {
return err
}
owner, repo, pullRequestNumber, err := getPullRequestDetails(c.String("pull-request"))
pr, _, err := tagger.Client.PullRequests.Get(
owner, repo, pullRequestNumber, err := tagger.GetPullRequestDetails(c.String("pull-request"))
pr, _, err := tc.Client.PullRequests.Get(
context.Background(),
owner,
repo,
Expand All @@ -99,7 +99,7 @@ func main() {
if err != nil {
return err
}
return tagger.PostTagsToPullRequest(pr)
return tc.PostTagsToPullRequest(pr)
},
Flags: []cli.Flag{
&cli.StringFlag{
Expand Down
34 changes: 27 additions & 7 deletions pkg/tagger/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,38 @@ package tagger
import (
context "context"
"fmt"
"strconv"
"strings"

github "github.com/google/go-github/github"
)

// GetPullRequestDetails will provide the necessary fields for fetching from the GH client through parsing a URL
func GetPullRequestDetails(prURL string) (owner, repo string, pullRequestNumber int, err error) {
// assuming a url with trailing ..../owner/repo/pull/number....
splitStrings := strings.Split(strings.Trim(prURL, "/"), "/")
lenStrings := len(splitStrings)
if lenStrings < 4 { // sanity check -- even partial URL will have at least 4
err = fmt.Errorf("Invalid PR input, should be like https://github.com/garypwhite/tag-your-git/pull/10, got %s", prURL)
return
}
owner = splitStrings[lenStrings-4]
repo = splitStrings[lenStrings-3]
pullRequestNumber, err = strconv.Atoi(splitStrings[lenStrings-1])
return
}

// PostTagsToPullRequest will post the tags found in t tagger to pull request pr
func (t Tagger) PostTagsToPullRequest(pr *github.PullRequest) error {
prRepo := pr.Base.GetRepo()
owner, repo, number, err := GetPullRequestDetails(pr.GetIssueURL())
if err != nil {
return err
}
files, _, err := t.Client.PullRequests.ListFiles(
context.Background(),
*prRepo.Owner.Name,
*prRepo.Name,
*pr.Number,
owner,
repo,
number,
&github.ListOptions{PerPage: 3000},
)
if err != nil {
Expand All @@ -35,9 +55,9 @@ func (t Tagger) PostTagsToPullRequest(pr *github.PullRequest) error {
// after finding all appropriate tags, post them to PR
t.Client.Issues.AddLabelsToIssue(
context.Background(),
*prRepo.Owner.Name,
*prRepo.Name,
*pr.Number,
owner,
repo,
number,
tagList,
) // here you might notice that t-y-g doesn't care about duplicates. Waiting to see if that's an issue or not with the API.
return nil
Expand Down
16 changes: 8 additions & 8 deletions main_test.go → pkg/tagger/writer_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package tagger

import (
"fmt"
Expand All @@ -17,7 +17,7 @@ func fixutures() (owner string, repo string, pullRequestID int) {
func TestGetPullRequestDetailsNoTrailing(t *testing.T) {
owner, repo, pullRequestID := fixutures()
fullURL := fmt.Sprintf("https://github.com/%s/%s/pull/%d", owner, repo, pullRequestID)
cOwner, cRepo, cPullRequestID, err := getPullRequestDetails(fullURL)
cOwner, cRepo, cPullRequestID, err := GetPullRequestDetails(fullURL)
assert.Equal(t, owner, cOwner)
assert.Equal(t, repo, cRepo)
assert.Equal(t, pullRequestID, cPullRequestID)
Expand All @@ -27,7 +27,7 @@ func TestGetPullRequestDetailsNoTrailing(t *testing.T) {
func TestGetPullRequestDetailsWithTrailing(t *testing.T) {
owner, repo, pullRequestID := fixutures()
fullURL := fmt.Sprintf("https://github.com/%s/%s/pull/%d/", owner, repo, pullRequestID)
cOwner, cRepo, cPullRequestID, err := getPullRequestDetails(fullURL)
cOwner, cRepo, cPullRequestID, err := GetPullRequestDetails(fullURL)
assert.Equal(t, owner, cOwner)
assert.Equal(t, repo, cRepo)
assert.Equal(t, pullRequestID, cPullRequestID)
Expand All @@ -37,7 +37,7 @@ func TestGetPullRequestDetailsWithTrailing(t *testing.T) {
func TestGetPullRequestDetailsWithNoHTTPS(t *testing.T) {
owner, repo, pullRequestID := fixutures()
fullURL := fmt.Sprintf("github.com/%s/%s/pull/%d/", owner, repo, pullRequestID)
cOwner, cRepo, cPullRequestID, err := getPullRequestDetails(fullURL)
cOwner, cRepo, cPullRequestID, err := GetPullRequestDetails(fullURL)
assert.Equal(t, owner, cOwner)
assert.Equal(t, repo, cRepo)
assert.Equal(t, pullRequestID, cPullRequestID)
Expand All @@ -47,7 +47,7 @@ func TestGetPullRequestDetailsWithNoHTTPS(t *testing.T) {
func TestGetPullRequestDetailsWithSSHPrefix(t *testing.T) {
owner, repo, pullRequestID := fixutures()
fullURL := fmt.Sprintf("ssh://github.com/%s/%s/pull/%d/", owner, repo, pullRequestID)
cOwner, cRepo, cPullRequestID, err := getPullRequestDetails(fullURL)
cOwner, cRepo, cPullRequestID, err := GetPullRequestDetails(fullURL)
assert.Equal(t, owner, cOwner)
assert.Equal(t, repo, cRepo)
assert.Equal(t, pullRequestID, cPullRequestID)
Expand All @@ -57,19 +57,19 @@ func TestGetPullRequestDetailsWithSSHPrefix(t *testing.T) {
func TestGetPullRequestDetailsWithGitPrefix(t *testing.T) {
owner, repo, pullRequestID := fixutures()
fullURL := fmt.Sprintf("git://github.com/%s/%s/pull/%d/", owner, repo, pullRequestID)
cOwner, cRepo, cPullRequestID, err := getPullRequestDetails(fullURL)
cOwner, cRepo, cPullRequestID, err := GetPullRequestDetails(fullURL)
assert.Equal(t, owner, cOwner)
assert.Equal(t, repo, cRepo)
assert.Equal(t, pullRequestID, cPullRequestID)
assert.Empty(t, err)
}

func TestErrorOnGarbageInput(t *testing.T) {
_, _, _, err := getPullRequestDetails("heyo this is a bunch of CRUSJKDHJSAIUHDAISJDKLFJLDKJLJF no sasdflklsjah!JKLSD/.12,./,/3.4,/23,./4,23er7d867c78scy bjkn434n 9y89y3r ")
_, _, _, err := GetPullRequestDetails("heyo this is a bunch of CRUSJKDHJSAIUHDAISJDKLFJLDKJLJF no sasdflklsjah!JKLSD/.12,./,/3.4,/23,./4,23er7d867c78scy bjkn434n 9y89y3r ")
assert.NotEmpty(t, err)
}

func TestErrorOnEmptyInput(t *testing.T) {
_, _, _, err := getPullRequestDetails("")
_, _, _, err := GetPullRequestDetails("")
assert.NotEmpty(t, err)
}

0 comments on commit f5d1403

Please sign in to comment.