Skip to content

Ability to get the issue number from the commit SHA #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ func main() {
EnvVar: "PLUGIN_ISSUE_NUM,DRONE_PULL_REQUEST",
},
cli.StringFlag{
Name: "key",
Usage: "key to assign comment",
Name: "key",
Usage: "key to assign comment",
EnvVar: "PLUGIN_KEY",
},
cli.StringFlag{
Expand All @@ -66,8 +66,8 @@ func main() {
EnvVar: "PLUGIN_MESSAGE_FILE",
},
cli.BoolFlag{
Name: "update",
Usage: "update an existing comment that matches the key",
Name: "update",
Usage: "update an existing comment that matches the key",
EnvVar: "PLUGIN_UPDATE",
},

Expand All @@ -85,6 +85,11 @@ func main() {
Usage: "repository owner",
EnvVar: "DRONE_REPO_OWNER",
},
cli.StringFlag{
Name: "commit-sha",
Usage: "git commit SHA",
EnvVar: "DRONE_COMMIT_SHA",
},
}

if err := app.Run(os.Args); err != nil {
Expand Down
26 changes: 26 additions & 0 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type (
Password string
RepoName string
RepoOwner string
CommitSha string
Update bool
Username string
Token string
Expand All @@ -39,6 +40,7 @@ func NewFromCLI(c *cli.Context) (*Plugin, error) {
Password: c.String("password"),
RepoName: c.String("repo-name"),
RepoOwner: c.String("repo-owner"),
CommitSha: c.String("commit-sha"),
Token: c.String("api-key"),
Update: c.Bool("update"),
Username: c.String("username"),
Expand Down Expand Up @@ -74,6 +76,18 @@ func (p Plugin) Exec() error {
}

var err error

if p.IssueNum == 0 {
p.IssueNum, err = p.getPullRequestNumber(p.gitContext)
if err != nil {
return err
}
if p.IssueNum == 0 && err == nil {
fmt.Println("Pull request number not found")
return nil
}
}

if p.Update {
// Append plugin comment ID to comment message so we can search for it later
message := fmt.Sprintf("%s\n<!-- id: %s -->\n", p.Message, p.Key)
Expand Down Expand Up @@ -203,3 +217,15 @@ func (p Plugin) validate() error {

return nil
}

func (p Plugin) getPullRequestNumber(ctx context.Context) (int, error) {
res, _, err := p.gitClient.Search.Issues(ctx, fmt.Sprintf("%s repo:%s/%s", p.CommitSha, p.RepoOwner, p.RepoName), nil)
if err != nil {
return 0, err
}
if len(res.Issues) == 0 {
return 0, nil
}

return *res.Issues[0].Number, nil
}
121 changes: 77 additions & 44 deletions plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ func TestPlugin(t *testing.T) {
defer gock.Off()

gock.New("http://server.com").
Post("/repos/test-org/test-repo/issues/12/comments").
Reply(201).
JSON(map[string]string{})
Post("/repos/test-org/test-repo/issues/12/comments").
Reply(201).
JSON(map[string]string{})

defer func() {
r := recover()
Expand Down Expand Up @@ -64,9 +64,9 @@ func TestPlugin(t *testing.T) {
defer gock.Off()

gock.New("http://server.com").
Post("/repos/test-org/test-repo/issues/12/comments").
Reply(201).
JSON(map[string]string{})
Post("/repos/test-org/test-repo/issues/12/comments").
Reply(201).
JSON(map[string]string{})

err := p.Exec()
g.Assert(err == nil).IsTrue(fmt.Sprintf("Received err: %s", err))
Expand All @@ -75,14 +75,14 @@ func TestPlugin(t *testing.T) {

g.Describe("updates existing comment", func() {
pl := Plugin{
BaseURL: "http://server.com",
Message: "test message",
IssueNum: 12,
Key: "123",
RepoName: "test-repo",
RepoOwner: "test-org",
Update: true,
Token: "fake",
BaseURL: "http://server.com",
Message: "test message",
IssueNum: 12,
Key: "123",
RepoName: "test-repo",
RepoOwner: "test-org",
Update: true,
Token: "fake",
}
p, err := NewFromPlugin(pl)
if err != nil {
Expand All @@ -94,15 +94,15 @@ func TestPlugin(t *testing.T) {

// Get Comments
gock.New("http://server.com").
Get("repos/test-org/test-repo/issues/12/comments").
Reply(200).
File("../testdata/response/non-existing-comment.json")
Get("repos/test-org/test-repo/issues/12/comments").
Reply(200).
File("../testdata/response/non-existing-comment.json")

// Create new comment
gock.New("http://server.com").
Post("repos/test-org/test-repo/issues/12/comments").
Reply(201).
JSON(map[string]string{})
Post("repos/test-org/test-repo/issues/12/comments").
Reply(201).
JSON(map[string]string{})

err := p.Exec()

Expand All @@ -120,14 +120,14 @@ func TestPlugin(t *testing.T) {
defer gock.Off()

gock.New("http://server.com").
Get("repos/test-org/test-repo/issues/12/comments").
Reply(200).
File("../testdata/response/existing-comment.json")
Get("repos/test-org/test-repo/issues/12/comments").
Reply(200).
File("../testdata/response/existing-comment.json")

gock.New("http://server.com").
Patch("repos/test-org/test-repo/issues/comments/7").
Reply(200).
JSON(map[string]string{})
Patch("repos/test-org/test-repo/issues/comments/7").
Reply(200).
JSON(map[string]string{})

err := p.Exec()

Expand All @@ -145,20 +145,20 @@ func TestPlugin(t *testing.T) {
defer gock.Off()

gock.New("http://server.com").
Get("repos/test-org/test-repo/issues/12/comments").
Reply(200).
File("../testdata/response/existing-comment.json")
Get("repos/test-org/test-repo/issues/12/comments").
Reply(200).
File("../testdata/response/existing-comment.json")

// We do not expect this endpoint to get called
gock.New("http://server.com").
Post("repos/test-org/test-repo/issues/12/comments").
Reply(201).
JSON(map[string]string{})
Post("repos/test-org/test-repo/issues/12/comments").
Reply(201).
JSON(map[string]string{})

gock.New("http://server.com").
Patch("repos/test-org/test-repo/issues/comments/7").
Reply(200).
JSON(map[string]string{})
Patch("repos/test-org/test-repo/issues/comments/7").
Reply(200).
JSON(map[string]string{})

err := p.Exec()

Expand All @@ -178,21 +178,54 @@ func TestPlugin(t *testing.T) {
defer gock.Off()

gock.New("http://server.com").
Get("repos/test-org/test-repo/issues/12/comments").
Reply(200).
File("../testdata/response/existing-comment.json")
Get("repos/test-org/test-repo/issues/12/comments").
Reply(200).
File("../testdata/response/existing-comment.json")

gock.New("http://server.com").
Patch("repos/test-org/test-repo/issues/comments/7").
MatchType("json").
// Make sure we are sending expected generated message
File("../testdata/request/patch-comment.json").
Reply(201).
JSON(map[string]string{})
Patch("repos/test-org/test-repo/issues/comments/7").
MatchType("json").
// Make sure we are sending expected generated message
File("../testdata/request/patch-comment.json").
Reply(201).
JSON(map[string]string{})

err := p.Exec()

g.Assert(err == nil).IsTrue(fmt.Sprintf("Received err: %s", err))
})
})

g.Describe("add comment with no issue num", func() {
pl := Plugin{
BaseURL: "http://server.com",
Message: "test message",
CommitSha: "deadbeef12",
RepoName: "test-repo",
RepoOwner: "test-org",
Token: "fake",
}
p, err := NewFromPlugin(pl)
if err != nil {
g.Fail("Failed to create plugin for testing")
}

g.It("creates a new comment", func() {
defer gock.Off()

gock.New("http://server.com").
Get("/search/issues").
MatchParam("q", "deadbeef12 repo:test-org/test-repo").
Reply(200).
File("../testdata/response/search-issues.json")

gock.New("http://server.com").
Post("/repos/test-org/test-repo/issues/12/comments").
Reply(201).
JSON(map[string]string{})

err := p.Exec()
g.Assert(err == nil).IsTrue(fmt.Sprintf("Received err: %s", err))
})
})
}
7 changes: 7 additions & 0 deletions testdata/response/search-issues.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"items": [
{
"number": 12
}
]
}