Skip to content

Commit 56e8a7c

Browse files
authored
fix: search component not using the correct api (#7)
1 parent 777fb40 commit 56e8a7c

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Show Sonarqube issues when browsing files on Sourcegraph.
1010
</picture>
1111
</p>
1212

13-
➡️ Try it out on [github.com/apache/struts](https://sourcegraph.com/github.com/apache/struts/-/blob/apps/rest-showcase/src/main/java/org/demo/rest/example/OrdersController.java)
13+
➡️ Try it out on [github.com/apache/struts](https://sourcegraph.com/github.com/apache/struts/-/blob/core/src/main/java/org/apache/struts2/action/CspReportAction.java#L60)
1414

1515
You can toggle decorations with the Sonarqube button in the action toolbar.
1616
Each decoration links to the issue on Sonarqube.

src/api.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,14 @@ async function fetchApi(path: string, searchParameters: URLSearchParams, options
7878
}
7979

8080
export async function searchComponents(
81-
options: ApiOptions & { query: string; organization: string }
81+
options: ApiOptions & { query: string; organization: string, component: string }
8282
): Promise<Component[]> {
8383
const searchParameters = new URLSearchParams()
8484
searchParameters.set('organization', options.organization)
85+
searchParameters.set('component', options.component)
8586
searchParameters.set('qualifiers', 'FIL,UTS')
8687
searchParameters.set('q', options.query)
87-
const result = await fetchApi('api/components/search', searchParameters, options)
88+
const result = await fetchApi('api/components/tree', searchParameters, options)
8889
return result.components
8990
}
9091

src/sonarqube.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,27 +88,36 @@ export function activate(context: sourcegraph.ExtensionContext): void {
8888
// For some reason searching for the whole file path doesn't work.
8989
// As soon as the query contains a slash, no results are returned.
9090
const fileName = filePath.split('/').pop()!
91-
const components = await searchComponents({ query: fileName, organization, ...apiOptions })
92-
const component = components.find(
93-
component => component.key.endsWith(filePath) && component.project === project
94-
)
91+
const components = await searchComponents({
92+
query: fileName,
93+
organization,
94+
component: project,
95+
...apiOptions,
96+
})
97+
98+
const component = components.find(component => component.key.endsWith(filePath))
99+
95100
if (!component) {
96101
throw new Error(
97102
`Could not find Sonarqube component for this file in Sonarqube project "${project}"`
98103
)
99104
}
100-
const branches = await listBranches({ project: component.project, ...apiOptions })
105+
106+
const branches = await listBranches({ project, ...apiOptions })
101107
const branch = branches.find(branch => branch.commit.sha === commitID)
102108
if (!branch) {
103109
console.warn(
104110
`No Sonarqube branch found for commit ID ${commitID}, falling back to default branch`
105111
)
106112
}
113+
114+
// Find issues related to the Component & Branch
107115
const issues = await searchIssues({
108116
...apiOptions,
109117
componentKeys: [component.key],
110118
branch: branch?.name,
111119
})
120+
112121
return { editor, issues, errorMessage: null as string | null, sonarqubeUrl }
113122
} catch (error) {
114123
console.error('Error fetching Sonarqube data:', error)

0 commit comments

Comments
 (0)