Skip to content

SonarQube issues plugin #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
d6e6be3
SonarQube plugin
martindstone Aug 26, 2024
f9a76ba
remove extra images, fix mistake in column defs
martindstone Aug 26, 2024
da76bf8
prettier
martindstone Aug 26, 2024
e6876ba
lint
martindstone Aug 26, 2024
ecf9c57
words +1
martindstone Aug 26, 2024
e53bc96
moar tests
martindstone Aug 26, 2024
ee8cfc7
prettier tests
martindstone Aug 26, 2024
72fefa5
move modal to new component, fix tests, better user feedback on comment
martindstone Nov 8, 2024
4c4eaf6
update readme
martindstone Nov 8, 2024
00f2b2c
take out hardcoded URL, fix tests and README
martindstone Nov 19, 2024
4e362ee
lint
martindstone Nov 19, 2024
cff5f0e
punctuation
martindstone Nov 19, 2024
80a8988
add entity type to message
martindstone Nov 19, 2024
fb06996
lint
martindstone Nov 19, 2024
12a731d
Merge branch 'sonarqube-issues' of https://github.com/cortexapps/cort…
jreock Nov 19, 2024
04bbe2f
Merge in without rebase
jreock Nov 19, 2024
4f4051b
Merge back in pre-merge no rebase changes
jreock Nov 19, 2024
cdea449
Merge linting changes back in, something is inconsistent between yarn…
jreock Nov 19, 2024
888bec0
Align linting in yarn, should fix build
jreock Nov 19, 2024
9d86d8e
Resolve yarn inconsistencies
jreock Nov 19, 2024
461e3c9
Merge branch 'master' into sonarqube-issues
jreock Nov 19, 2024
991c498
Merge branch 'sonarqube-issues' of https://github.com/cortexapps/cort…
jreock Nov 19, 2024
3f89742
fix review issues, add hooks, gently componentize
martindstone Nov 20, 2024
916e5d8
oops make the name match the screenshot
martindstone Nov 20, 2024
683a529
Merge branch 'sonarqube-issues' of https://github.com/cortexapps/cort…
jreock Nov 20, 2024
6e9848a
fix readme
martindstone Nov 20, 2024
a4fc0fc
Update README.md
martindstone Nov 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions plugins/sonarqube-issues/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
dist/
29 changes: 29 additions & 0 deletions plugins/sonarqube-issues/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module.exports = {
env: {
browser: true,
es2021: true,
},
extends: [
"plugin:react/jsx-runtime",
"plugin:react-hooks/recommended",
"standard-with-typescript",
"prettier",
],
overrides: [],
parserOptions: {
ecmaVersion: "latest",
project: "tsconfig.json",
sourceType: "module",
tsconfigRootDir: __dirname,
},
plugins: ["react"],
rules: {
// conflicts with no-extra-boolean-cast
"@typescript-eslint/strict-boolean-expressions": "off",
},
settings: {
react: {
version: "detect",
},
},
};
12 changes: 12 additions & 0 deletions plugins/sonarqube-issues/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# OSX
*.DS_Store

# IDEs
.idea
*.iml
.vscode

# This project
node_modules/
dist/
yarn-error.log
2 changes: 2 additions & 0 deletions plugins/sonarqube-issues/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
dist/
133 changes: 133 additions & 0 deletions plugins/sonarqube-issues/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# SonarQube Issues Cortex Plugin

View SonarQube Issues associated with your services!!

<div align="center"><img src="img/sonarqube-plugin.png" /></div>

The SonarQube Issues plugin shows open SonarQube issues associated with the SonarQube project specified in the entity's `cortex.yaml`. If the `cortex.yaml` has a [SonarQube Project key](https://docs.cortex.io/docs/reference/integrations/sonarqube#entity-descriptor) defined in its `x-cortex-static-analysis` configuration, it will query for issues pertaining to that project. For example:

```yaml
openapi: 3.0.1
info:
title: Funrepo
description: it is a fun repo
x-cortex-git:
github:
alias: cortex
repository: martindstone-org/funrepo
x-cortex-tag: funrepo
x-cortex-type: service
x-cortex-static-analysis:
sonarqube:
project: martindstone-org_funrepo
```

## Setup

This plugin requires a proxy to SonarQube. To set up:

- Create a token in SonarQube by clicking on your profile > My Account > Security
- In Cortex, define a secret whose value is your new token. Name it `sonarqube_plugin`.
- Create a proxy:

- Navigate to Plugins, then click on the Proxies tab, then click on Create Proxy
- Give the proxy a name, like SonarQube Proxy, then click on Add URL.
- For the URL Prefix, type in the API base URL of your SonarQube instance. The default for cloud is `https://sonarcloud.io`. **If you are self-hosting SonarQube, you will have to put in your own base URL instead.**
- Click on Add Header and add a header whose name is `Authorization` and whose value is `Bearer {{{secrets.sonarqube_plugin}}}` (include the curly braces!)

- Once you are done, the proxy should look like the below:

<div align="center"><img src="img/sonarqube-proxy.png"></div>

### Self-Hosted setup

The plugin uses `https://sonarcloud.io` as its default API base URL. If you are self-hosting Sonarqube, then you will have a different URL. To configure the plugin to use that URL, you can create a Sonarqube plugin configuration entity in Cortex with your own API base URL.

- Consider creating a new entity type, so that any existing scorecards are not affected by ths configuration entity. In this example, we have created a new entity type called `plugin-configuration`
- Create a new entity with the tag `sonarqube-plugin-config`
- Set `x-cortex-definition.sonarqube-url` to the value of your ServiceNow Instance URL. For example, if my Sonarqube API base URL was `https://sonarqube.martindstone.com`, my `sonarqube-plugin-config` entity would look like this:

```yaml
openapi: 3.0.1
info:
title: Sonarqube Plugin Config
description: ""
x-cortex-tag: sonarqube-plugin-config
x-cortex-type: plugin-configuration
x-cortex-definition:
sonarqube-url: https://sonarqube.martindstone.com
```

Now, you can build and add the plugin.

- Build the plugin:
- Make sure you have npm or yarn.
- In your terminal, in the `sonarqube-issues` directory, type `yarn` or `npm install` to install the dependencies; then type `npm run build` or `yarn build` to build the plugin.
- The compiled plugin will be created in `dist/ui.html`.
- In Plugins > All, click **Register Plugin**.
- Give the plugin a name, like SonarQube Issues. This is the name users will see in the plugin listing.
- Under **Associated Proxy**, choose the proxy you just created.
- Under **Plugin Context**, click on Add another context; choose Selection type: Include, and Entity types: service.
- This plugin does not work in the Global context. Turn off the switch labeled **Include in global context**.
- In The **Plugin code** section, upload the `dist/ui.html` file you just built.
- Click on **Save plugin**.

### Create a plugin configuration entity (self-hosted only)

This plugin will connect to SonarQube's cloud instance out of the box, so if you are using SonarQube in the cloud, you should skip this step. If you are self-hosting SonarQube and need to direct the plugin to a different REST API endpoint, create a plugin configuration entity with your SonarQube REST API base URL as follows:

- Consider creating a new entity type, so that any existing scorecards are not affected by this configuration entity. In this example, we have created a new entity type called `plugin-configuration`
- Create a new entity with the tag `sonarqube-plugin-config`
- Set `x-cortex-definition.sonarqube-url` to the value of your SonarQube API base URL. For example, if my SonarQube API base URL was `https://sonarqube.martindstone.com`, my `sonarqube-plugin-config` entity would look like this:

```
openapi: 3.0.1
info:
title: SonarQube Plugin Config
description: ""
x-cortex-tag: sonarqube-plugin-config
x-cortex-type: plugin-configuration
x-cortex-definition:
sonarqube-url: https://sonarqube.martindstone.com
```

Now, when you navigate to a Cortex service that has a SonarQube project associated with it, you should be able to click on Plugins > SonarQube Issues and see the SonarQube Issues associated with the project that is linked to the service.

## Troubleshooting

### Getting a message that "No SonarQube details were found for this entity"

If you get the following message:

<div align="center"><img src="img/sonarqube-no-project.png" width="540" /></div>

This means that the plugin did not find a SonarQube project defined as described [here](https://docs.cortex.io/docs/reference/integrations/sonarqube#entity-descriptor).

# Setting up your dev environment

SonarQube Issues Cortex Plugin is a [Cortex](https://www.cortex.io/) plugin. To see how to run the plugin inside of Cortex, see [our docs](https://docs.cortex.io/docs/plugins).

### Prerequisites

Developing and building this plugin requires either [yarn](https://classic.yarnpkg.com/lang/en/docs/install/) or [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm).

## Getting started

1. Run `yarn` or `npm install` to download all dependencies
2. Run `yarn build` or `npm run build` to compile the plugin code into `./dist/ui.html`
3. Upload `ui.html` into Cortex on a create or edit plugin page
4. Add or update the code and repeat steps 2-3 as necessary

### Notable scripts

The following commands come pre-configured in this repository. You can see all available commands in the `scripts` section of [package.json](./package.json). They can be run with npm via `npm run {script_name}` or with yarn via `yarn {script_name}`, depending on your package manager preference. For instance, the `build` command can be run with `npm run build` or `yarn build`.

- `build` - compiles the plugin. The compiled code root is `./src/index.tsx` (or as defined by [webpack.config.js](webpack.config.js)) and the output is generated into `dist/ui.html`.
- `test` - runs all tests defined in the repository using [jest](https://jestjs.io/)
- `lint` - runs lint and format checking on the repository using [prettier](https://prettier.io/) and [eslint](https://eslint.org/)
- `lintfix` - runs eslint in fix mode to fix any linting errors that can be fixed automatically
- `formatfix` - runs Prettier in fix mode to fix any formatting errors that can be fixed automatically

### Available React components

See available UI components via our [Storybook](https://cortexapps.github.io/plugin-core/).
1 change: 1 addition & 0 deletions plugins/sonarqube-issues/__mocks__/fileMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "test-file-stub";
1 change: 1 addition & 0 deletions plugins/sonarqube-issues/__mocks__/styleMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {};
8 changes: 8 additions & 0 deletions plugins/sonarqube-issues/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
plugins: ["@babel/plugin-syntax-jsx"],
presets: [
["@babel/preset-env", { targets: { node: "current" } }],
"@babel/preset-typescript",
["@babel/preset-react", { runtime: "automatic" }],
],
};
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions plugins/sonarqube-issues/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
moduleNameMapper: {
// map static asset imports to a stub file under the assumption they are not important to our tests
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$":
"<rootDir>/__mocks__/fileMock.js",
// map style asset imports to a stub file under the assumption they are not important to our tests
"\\.(css|less)$": "<rootDir>/__mocks__/styleMock.js",
"@cortexapps/plugin-core/components":
"<rootDir>/../../node_modules/@cortexapps/plugin-core/dist/components.cjs.js",
"@cortexapps/plugin-core":
"<rootDir>/../../node_modules/@cortexapps/plugin-core/dist/index.cjs.js",
},
setupFilesAfterEnv: ["<rootDir>/setupTests.ts"],
testEnvironment: "jsdom",
transform: {
"^.+\\.tsx?$": "babel-jest",
},
};
66 changes: 66 additions & 0 deletions plugins/sonarqube-issues/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"name": "SonarQube-Issues-Cortex-Plugin",
"version": "0.1.0",
"license": "MIT",
"dependencies": {
"@chakra-ui/react": "2",
"@cortexapps/plugin-core": "^2.0.0",
"@emotion/react": "^11.13.3",
"@emotion/styled": "^11.13.0",
"framer-motion": "^11.11.17",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@babel/core": "^7.21.3",
"@babel/plugin-syntax-jsx": "^7.18.6",
"@babel/preset-env": "^7.20.2",
"@babel/preset-react": "^7.18.6",
"@babel/preset-typescript": "^7.21.0",
"@popperjs/core": "^2.11.8",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^14.0.0",
"@types/react": "^18.0.28",
"@types/react-dom": "^18.0.11",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.55.0",
"babel-jest": "^29.5.0",
"css-loader": "^6.7.3",
"eslint": "^8.0.1",
"eslint-config-prettier": "^8.7.0",
"eslint-config-standard-with-typescript": "^34.0.0",
"eslint-plugin-import": "^2.25.2",
"eslint-plugin-n": "^15.6.1",
"eslint-plugin-promise": "^6.0.0",
"eslint-plugin-react": "^7.32.2",
"eslint-plugin-react-hooks": "^4.6.0",
"html-webpack-plugin": "^5.5.0",
"jest": "^29.5.0",
"jest-environment-jsdom": "^29.5.0",
"jest-fetch-mock": "^3.0.3",
"npm-run-all": "^4.1.5",
"prettier": "^2.8.4",
"prop-types": "^15.8.1",
"react-dev-utils": "^12.0.1",
"style-loader": "^3.3.1",
"terser-webpack-plugin": "^5.3.7",
"ts-loader": "^9.4.2",
"typescript": "^4.9.5",
"url-loader": "^4.1.1",
"webpack": "^5.76.1",
"webpack-cli": "^5.0.1",
"webpack-dev-server": "^4.15.0"
},
"scripts": {
"build": "webpack --mode=production",
"clean": "rm -r ./dist",
"dev": "webpack serve --mode=development",
"fix": "run-p formatfix lintfix",
"formatfix": "yarn prettier . --write",
"formatcheck": "yarn prettier . --check",
"lint": "run-p formatcheck lintcheck",
"lintcheck": "yarn eslint src",
"lintfix": "yarn lintcheck --fix",
"test": "jest"
}
}
60 changes: 60 additions & 0 deletions plugins/sonarqube-issues/setupTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import "@testing-library/jest-dom/extend-expect";
import fetchMock from "jest-fetch-mock";

fetchMock.enableMocks();

const mockContext = {
apiBaseUrl: "https://api.getcortexapp.com",
entity: {
definition: null,
description: null,
groups: null,
name: "Inventory planner",
ownership: {
emails: [
{
description: null,
email: "[email protected]",
inheritance: null,
id: 1,
},
],
},
tag: "inventory-planner",
type: "service",
},
location: "ENTITY",
user: {
email: "[email protected]",
name: "Ganesh Datta",
role: "ADMIN",
},
};

jest.mock("@cortexapps/plugin-core/components", () => {
const originalModule = jest.requireActual(
"@cortexapps/plugin-core/components"
);
return {
...originalModule,
usePluginContext: () => {
return mockContext;
},
PluginProvider: ({ children }) => {
return children;
},
};
});

jest.mock("@cortexapps/plugin-core", () => {
const originalModule = jest.requireActual("@cortexapps/plugin-core");
return {
...originalModule,
CortexApi: {
...originalModule.CortexApi,
getContext: () => {
return mockContext;
},
},
};
});
8 changes: 8 additions & 0 deletions plugins/sonarqube-issues/src/api/Cortex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const getEntityYaml = async (
baseUrl: string,
entityTag: string
): Promise<Record<string, any>> => {
const res = await fetch(`${baseUrl}/catalog/${entityTag}/openapi`);

return await res.json();
};
3 changes: 3 additions & 0 deletions plugins/sonarqube-issues/src/baseStyles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
font: 14px sans-serif;
}
15 changes: 15 additions & 0 deletions plugins/sonarqube-issues/src/components/App.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { render } from "@testing-library/react";
import App from "./App";
import { waitForLoading } from "../../../testUtils/testUtils";

fetchMock.mockResponse(JSON.stringify({}));
describe("App", () => {
it("verifies that the plugin works", async () => {
render(<App />);

expect(fetch).toHaveBeenCalledWith(
"https://api.getcortexapp.com/catalog/inventory-planner/openapi"
);
await waitForLoading();
});
});
Loading
Loading