diff --git a/backend/plugins/jenkins/api/remote_api.go b/backend/plugins/jenkins/api/remote_api.go index fad6e3d610f..e825fecba2d 100644 --- a/backend/plugins/jenkins/api/remote_api.go +++ b/backend/plugins/jenkins/api/remote_api.go @@ -19,6 +19,7 @@ package api import ( "fmt" + "golang.org/x/exp/slices" "github.com/apache/incubator-devlake/core/errors" "github.com/apache/incubator-devlake/core/plugin" @@ -32,6 +33,8 @@ type JenkinsRemotePagination struct { PerPage int `json:"per_page"` } +var scopesWithJobs = []string{"org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject"} + func listJenkinsRemoteScopes( connection *models.JenkinsConnection, apiClient plugin.ApiClient, @@ -53,13 +56,17 @@ func listJenkinsRemoteScopes( parentId = &groupId } getJobsPageCallBack := func(job *models.Job) errors.Error { - switch job.Class { - case "org.jenkinsci.plugins.workflow.job.WorkflowJob": - fallthrough - case "org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject": - fallthrough - case "hudson.model.FreeStyleProject": - // this is a scope + if isGroup(job) { + // This is a group + job.Path = groupId + children = append(children, dsmodels.DsRemoteApiScopeListEntry[models.JenkinsJob]{ + Type: api.RAS_ENTRY_TYPE_GROUP, + Id: fmt.Sprintf("%s/job/%s", job.Path, job.Name), + Name: job.Name, + ParentId: parentId, + }) + } else { + // This is a scope jenkinsJob := job.ToJenkinsJob() children = append(children, dsmodels.DsRemoteApiScopeListEntry[models.JenkinsJob]{ Type: api.RAS_ENTRY_TYPE_SCOPE, @@ -69,15 +76,6 @@ func listJenkinsRemoteScopes( Data: jenkinsJob, ParentId: parentId, }) - default: - // this is a group - job.Path = groupId - children = append(children, dsmodels.DsRemoteApiScopeListEntry[models.JenkinsJob]{ - Type: api.RAS_ENTRY_TYPE_GROUP, - Id: fmt.Sprintf("%s/job/%s", job.Path, job.Name), - Name: job.Name, - ParentId: parentId, - }) } return nil @@ -95,6 +93,10 @@ func listJenkinsRemoteScopes( return } +func isGroup(job *models.Job) bool { + return job.Jobs != nil && !slices.Contains(scopesWithJobs, job.Class) +} + // RemoteScopes list all available scopes on the remote server // @Summary list all available scopes on the remote server // @Description list all available scopes on the remote server