Skip to content

Commit b9b0373

Browse files
authored
feat(webhook): Issues and PullRequests new endpoint by projectName (#8915)
* feat(webhook): Issues and PullRequests new endpoint by projectName * ci: fix invalid push * ci: fix lint issues * ci: add missing lint.
1 parent 46c5c51 commit b9b0373

4 files changed

Lines changed: 55 additions & 7 deletions

File tree

backend/plugins/webhook/api/deployments.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,18 +126,18 @@ func PostDeploymentsByName(input *plugin.ApiResourceInput) (*plugin.ApiResourceO
126126
// @Router /projects/:projectName/deployments [POST]
127127
func PostDeploymentsByProjectName(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
128128
// find or create the connection for this project
129-
connection, err, shouldReturn := getOrCreateConnection(input)
129+
connection, err, shouldReturn := getOrCreateConnection(input, "deployments")
130130
if shouldReturn {
131131
return nil, err
132132
}
133133

134134
return postDeployments(input, connection, err)
135135
}
136136

137-
func getOrCreateConnection(input *plugin.ApiResourceInput) (*models.WebhookConnection, errors.Error, bool) {
137+
func getOrCreateConnection(input *plugin.ApiResourceInput, webhookSuffix string) (*models.WebhookConnection, errors.Error, bool) {
138138
connection := &models.WebhookConnection{}
139139
projectName := input.Params["projectName"]
140-
webhookName := fmt.Sprintf("%s_deployments", projectName)
140+
webhookName := fmt.Sprintf("%s_%s", projectName, webhookSuffix)
141141
err := findByProjectName(connection, input.Params, pluginName, webhookName)
142142
dal := basicRes.GetDal()
143143
if err != nil {

backend/plugins/webhook/api/issues.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ package api
1919

2020
import (
2121
"fmt"
22-
"github.com/apache/incubator-devlake/core/log"
23-
"github.com/apache/incubator-devlake/helpers/dbhelper"
2422
"net/http"
2523
"time"
2624

25+
"github.com/apache/incubator-devlake/core/log"
26+
"github.com/apache/incubator-devlake/helpers/dbhelper"
27+
2728
"github.com/apache/incubator-devlake/core/dal"
2829
"github.com/apache/incubator-devlake/core/errors"
2930
"github.com/apache/incubator-devlake/core/models/domainlayer"
@@ -112,6 +113,26 @@ func PostIssueByName(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput,
112113
return postIssue(input, err, connection)
113114
}
114115

116+
// PostIssuesByProjectName
117+
// @Summary create issue by project name
118+
// @Description Create issue by project name. The webhook connection will be created automatically if it does not exist.<br/>
119+
// @Description example: {"url":"","issue_key":"DLK-1234","title":"a feature from DLK","description":"","epic_key":"","type":"BUG","status":"TODO","original_status":"created","story_point":0,"resolution_date":null,"created_date":"2020-01-01T12:00:00+00:00","updated_date":null,"lead_time_minutes":0,"parent_issue_key":"DLK-1200","priority":"","original_estimate_minutes":0,"time_spent_minutes":0,"time_remaining_minutes":0,"creator_id":"user1131","creator_name":"Nick name 1","assignee_id":"user1132","assignee_name":"Nick name 2","severity":"","component":""}
120+
// @Tags plugins/webhook
121+
// @Param body body WebhookIssueRequest true "json body"
122+
// @Success 200 {string} noResponse ""
123+
// @Failure 400 {string} errcode.Error "Bad Request"
124+
// @Failure 403 {string} errcode.Error "Forbidden"
125+
// @Failure 500 {string} errcode.Error "Internal Error"
126+
// @Router /projects/:projectName/issues [POST]
127+
func PostIssuesByProjectName(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
128+
// find or create the connection for this project
129+
connection, err, shouldReturn := getOrCreateConnection(input, "issues")
130+
if shouldReturn {
131+
return nil, err
132+
}
133+
return postIssue(input, err, connection)
134+
}
135+
115136
func postIssue(input *plugin.ApiResourceInput, err errors.Error, connection *models.WebhookConnection) (*plugin.ApiResourceOutput, errors.Error) {
116137
if err != nil {
117138
return nil, err

backend/plugins/webhook/api/pull_requests.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ type WebhookPullRequestReq struct {
7777
// @Failure 400 {string} errcode.Error "Bad Request"
7878
// @Failure 403 {string} errcode.Error "Forbidden"
7979
// @Failure 500 {string} errcode.Error "Internal Error"
80-
// @Router /plugins/webhook/connections/:connectionId/pullrequests [POST]
80+
// @Router /plugins/webhook/connections/:connectionId/pull_requests [POST]
8181
func PostPullRequests(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
8282
connection := &models.WebhookConnection{}
8383
err := connectionHelper.First(connection, input.Params)
@@ -96,14 +96,35 @@ func PostPullRequests(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput
9696
// @Failure 400 {string} errcode.Error "Bad Request"
9797
// @Failure 403 {string} errcode.Error "Forbidden"
9898
// @Failure 500 {string} errcode.Error "Internal Error"
99-
// @Router /plugins/webhook/connections/by-name/:connectionName/pullrequests [POST]
99+
// @Router /plugins/webhook/connections/by-name/:connectionName/pull_requests [POST]
100100
func PostPullRequestsByName(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
101101
connection := &models.WebhookConnection{}
102102
err := connectionHelper.FirstByName(connection, input.Params)
103103

104104
return postPullRequests(input, connection, err)
105105
}
106106

107+
// PostPullRequestsByProjectName
108+
// @Summary create pull requests by project name
109+
// @Description Create pull request by project name. The webhook connection will be created automatically if it does not exist.<br/>
110+
// @Description example1: {"id": "pr1","baseRepoId": "webhook:1","headRepoId": "repo_fork1","status": "MERGED","originalStatus": "OPEN","displayTitle": "Feature: Add new functionality","description": "This PR adds new features","url": "https://github.com/org/repo/pull/1","authorName": "johndoe","authorId": "johnd123","mergedByName": "janedoe","mergedById": "janed123","parentPrId": "","pullRequestKey": 1,"createdDate": "2025-02-20T16:17:36Z","mergedDate": "2025-02-20T17:17:36Z","closedDate": null,"type": "feature","component": "backend","mergeCommitSha": "bf0a79c57dff8f5f1f393de315ee5105a535e059","headRef": "repo_fork1:feature-branch","baseRef": "main","baseCommitSha": "e73325c2c9863f42ea25871cbfaeebcb8edcf604","headCommitSha": "b22f772f1197edfafd4cc5fe679a2d299ec12837","additions": 100,"deletions": 50,"isDraft": false}<br/>
111+
// @Description "baseRepoId" should be equal to "webhook:{connectionId}" for consistent DORA calculations
112+
// @Tags plugins/webhook
113+
// @Param body body WebhookPullRequestReq true "json body"
114+
// @Success 200
115+
// @Failure 400 {string} errcode.Error "Bad Request"
116+
// @Failure 403 {string} errcode.Error "Forbidden"
117+
// @Failure 500 {string} errcode.Error "Internal Error"
118+
// @Router /projects/:projectName/pull_requests [POST]
119+
func PostPullRequestsByProjectName(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
120+
connection, err, shouldReturn := getOrCreateConnection(input, "pull_requests")
121+
if shouldReturn {
122+
return nil, err
123+
}
124+
125+
return postPullRequests(input, connection, err)
126+
}
127+
107128
func postPullRequests(input *plugin.ApiResourceInput, connection *models.WebhookConnection, err errors.Error) (*plugin.ApiResourceOutput, errors.Error) {
108129
if err != nil {
109130
return nil, err

backend/plugins/webhook/impl/impl.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,11 @@ func (p Webhook) ApiResources() map[string]map[string]plugin.ApiResourceHandler
131131
"projects/:projectName/deployments": {
132132
"POST": api.PostDeploymentsByProjectName,
133133
},
134+
"projects/:projectName/issues": {
135+
"POST": api.PostIssuesByProjectName,
136+
},
137+
"projects/:projectName/pull_requests": {
138+
"POST": api.PostPullRequestsByProjectName,
139+
},
134140
}
135141
}

0 commit comments

Comments
 (0)