Skip to content

Commit

Permalink
Merge pull request #3716 from PetrusZ/ft/jira_webhook_status
Browse files Browse the repository at this point in the history
support jira status change in jira webhook
  • Loading branch information
jamsman94 authored Sep 30, 2024
2 parents 4cbc9c7 + 1ebd23a commit 611f908
Show file tree
Hide file tree
Showing 11 changed files with 770 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1020,10 +1020,21 @@ type WorkflowV4Hook struct {
}

type JiraHook struct {
Name string `bson:"name" json:"name"`
Enabled bool `bson:"enabled" json:"enabled"`
Description string `bson:"description" json:"description"`
WorkflowArg *WorkflowV4 `bson:"workflow_arg" json:"workflow_arg"`
Name string `bson:"name" json:"name"`
Enabled bool `bson:"enabled" json:"enabled"`
Description string `bson:"description" json:"description"`
JiraID string `bson:"jira_id" json:"jira_id"`
JiraSystemIdentity string `bson:"jira_system_identity" json:"jira_system_identity"`
JiraURL string `bson:"jira_url" json:"jira_url"`
EnabledIssueStatusChange bool `bson:"enabled_issue_status_change" json:"enabled_issue_status_change"`
FromStatus JiraHookStatus `bson:"from_status" json:"from_status"`
ToStatus JiraHookStatus `bson:"to_status" json:"to_status"`
WorkflowArg *WorkflowV4 `bson:"workflow_arg" json:"workflow_arg"`
}

type JiraHookStatus struct {
ID string `bson:"id,omitempty" json:"id,omitempty"`
Name string `bson:"name,omitempty" json:"name,omitempty"`
}

type MeegoHook struct {
Expand Down
34 changes: 33 additions & 1 deletion pkg/microservice/aslan/core/system/handler/project_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,16 +311,48 @@ func SearchJiraProjectIssuesWithJQL(c *gin.Context) {
ctx.Resp, ctx.Err = service.SearchJiraProjectIssuesWithJQL(c.Param("id"), c.Query("project"), strings.ReplaceAll(c.Query("jql"), "{{.system.username}}", ctx.UserName), c.Query("summary"))
}

// @Summary Get Jira Types
// @Description Get Jira Types
// @Tags system
// @Accept json
// @Produce json
// @Param id path string true "jira id"
// @Param project query string true "jira project key"
// @Success 200 {array} jira.IssueTypeWithStatus
// @Router /api/aslan/system/project_management/{id}/jira/type [get]
func GetJiraTypes(c *gin.Context) {
ctx := internalhandler.NewContext(c)
defer func() { internalhandler.JSONResponse(c, ctx) }()
ctx.Resp, ctx.Err = service.GetJiraTypes(c.Param("id"), c.Query("project"))
}

// @Summary Get Jira Project Status
// @Description Get Jira Project Status
// @Tags system
// @Accept json
// @Produce json
// @Param id path string true "jira id"
// @Param project query string true "jira project id"
// @Success 200 {array} string
// @Router /api/aslan/system/project_management/{id}/jira/status [get]
func GetJiraProjectStatus(c *gin.Context) {
ctx := internalhandler.NewContext(c)
defer func() { internalhandler.JSONResponse(c, ctx) }()
ctx.Resp, ctx.Err = service.GetJiraProjectStatus(c.Param("id"), c.Query("project"))
}

// @Summary Get Jira All Status
// @Description Get Jira All Status
// @Tags system
// @Accept json
// @Produce json
// @Param id path string true "jira id"
// @Success 200 {array} jira.Status
// @Router /api/aslan/system/project_management/{id}/jira/allStatus [get]
func GetJiraAllStatus(c *gin.Context) {
ctx := internalhandler.NewContext(c)
defer func() { internalhandler.JSONResponse(c, ctx) }()
ctx.Resp, ctx.Err = service.GetJiraAllStatus(c.Param("id"), c.Query("project"))
ctx.Resp, ctx.Err = service.GetJiraAllStatus(c.Param("id"))
}

func HandleJiraEvent(c *gin.Context) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/microservice/aslan/core/system/handler/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,8 @@ func (*Router) Inject(router *gin.RouterGroup) {
pm.GET("/:id/jira/issue", SearchJiraIssues)
pm.GET("/:id/jira/issue/jql", SearchJiraProjectIssuesWithJQL)
pm.GET("/:id/jira/type", GetJiraTypes)
pm.GET("/:id/jira/status", GetJiraAllStatus)
pm.GET("/:id/jira/status", GetJiraProjectStatus)
pm.GET("/:id/jira/allStatus", GetJiraAllStatus)
pm.POST("/jira/webhook/:workflowName/:hookName", HandleJiraEvent)
pm.POST("/meego/webhook/:workflowName/:hookName", HandleMeegoEvent)
}
Expand Down
49 changes: 47 additions & 2 deletions pkg/microservice/aslan/core/system/service/project_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,15 +319,26 @@ func GetJiraTypes(id, project string) ([]*jira.IssueTypeWithStatus, error) {
return jira.NewJiraClientWithAuthType(info.JiraHost, info.JiraUser, info.JiraToken, info.JiraPersonalAccessToken, info.JiraAuthType).Issue.GetTypes(project)
}

func GetJiraAllStatus(id, project string) ([]string, error) {
func GetJiraProjectStatus(id, project string) ([]string, error) {
info, err := mongodb.NewProjectManagementColl().GetJiraByID(id)
if err != nil {
if err == mongo.ErrNoDocuments {
return nil, nil
}
return nil, err
}
return jira.NewJiraClientWithAuthType(info.JiraHost, info.JiraUser, info.JiraToken, info.JiraPersonalAccessToken, info.JiraAuthType).Project.ListAllStatues(project)
return jira.NewJiraClientWithAuthType(info.JiraHost, info.JiraUser, info.JiraToken, info.JiraPersonalAccessToken, info.JiraAuthType).Project.ListProjectStatues(project)
}

func GetJiraAllStatus(id string) ([]*jira.Status, error) {
info, err := mongodb.NewProjectManagementColl().GetJiraByID(id)
if err != nil {
if err == mongo.ErrNoDocuments {
return nil, nil
}
return nil, err
}
return jira.NewJiraClientWithAuthType(info.JiraHost, info.JiraUser, info.JiraToken, info.JiraPersonalAccessToken, info.JiraAuthType).Platform.ListAllStatues()
}

func SearchJiraIssues(id, project, _type, status, summary string, ne bool) ([]*jira.Issue, error) {
Expand Down Expand Up @@ -402,6 +413,40 @@ func HandleJiraHookEvent(workflowName, hookName string, event *jira.Event, logge
logger.Error(errMsg)
return errors.New(errMsg)
}

if jiraHook.EnabledIssueStatusChange {
if event.ChangeLog == nil || len(event.ChangeLog.Items) == 0 {
logger.Errorf("HandleJiraHookEvent: nil change log or change log items, skip")
return nil
}

statusChangeMatched := false
changelog := &jira.ChangeLogItem{}
for _, item := range event.ChangeLog.Items {
if item.Field != "status" || item.FieldType != "jira" {
continue
}

if jiraHook.FromStatus.ID == "000000" {
if item.To == jiraHook.ToStatus.ID {
statusChangeMatched = true
}
} else {
if item.From == jiraHook.FromStatus.ID && item.To == jiraHook.ToStatus.ID {
statusChangeMatched = true
}
}

changelog = item
break
}

if !statusChangeMatched {
logger.Infof("HandleJiraHookEvent: hook %s/%s status change not matched, skip. status changelog: %+v", workflowName, hookName, changelog)
return nil
}
}

taskInfo, err := workflow.CreateWorkflowTaskV4ByBuildInTrigger(setting.JiraHookTaskCreator, jiraHook.WorkflowArg, logger)
if err != nil {
errMsg := fmt.Sprintf("HandleJiraHookEvent: failed to create workflow task: %s", err)
Expand Down
Loading

0 comments on commit 611f908

Please sign in to comment.