diff --git a/main.go b/main.go index a7fb769..bb439d5 100644 --- a/main.go +++ b/main.go @@ -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{ @@ -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", }, @@ -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 { diff --git a/plugin/plugin.go b/plugin/plugin.go index 5dd62d5..40230eb 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -21,6 +21,7 @@ type ( Password string RepoName string RepoOwner string + CommitSha string Update bool Username string Token string @@ -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"), @@ -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\n", p.Message, p.Key) @@ -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 +} diff --git a/plugin/plugin_test.go b/plugin/plugin_test.go index 6bca0ce..2afc7ec 100644 --- a/plugin/plugin_test.go +++ b/plugin/plugin_test.go @@ -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() @@ -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)) @@ -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 { @@ -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() @@ -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() @@ -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() @@ -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)) + }) + }) } diff --git a/testdata/response/search-issues.json b/testdata/response/search-issues.json new file mode 100644 index 0000000..d60c280 --- /dev/null +++ b/testdata/response/search-issues.json @@ -0,0 +1,7 @@ +{ + "items": [ + { + "number": 12 + } + ] +} \ No newline at end of file