Skip to content

Commit

Permalink
Merge pull request #429 from ipierre1/main
Browse files Browse the repository at this point in the history
[DevOps] Add CodeClimate style fomatter to use apigee-lint into GitLab CI/CD
  • Loading branch information
ssvaidyanathan authored Mar 19, 2024
2 parents c5dd1fd + 0bc4f66 commit 5dfd347
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 1 deletion.
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ apigeelint -s sampleProxy/apiproxy -f table.js

Where `-s` points to the apiProxy source directory and `-f` is the output formatter desired.

Possible formatters are: "json.js" (the default), "stylish.js", "compact.js", "codeframe.js", "html.js", "table.js", "unix.js", "visualstudio.js", "checkstyle.js", "jslint-xml.js", "junit.js" and "tap.js".
Possible formatters are: "json.js" (the default), "stylish.js", "compact.js", "codeframe.js", "codeclimate.js", "html.js", "table.js", "unix.js", "visualstudio.js", "checkstyle.js", "jslint-xml.js", "junit.js" and "tap.js".

### More Examples

Expand Down Expand Up @@ -162,6 +162,29 @@ The selection of a profile affects other checks, too. For example, [the Google
Authentication feature](https://cloud.google.com/apigee/docs/api-platform/security/google-auth/overview)
is available only in X/hybrid.

#### Pipeline lint job integration

##### GitLab CI/CD

On GitLab CI/CD, on your `.gitlab-ci.yml` you can use codequality report artifact to get
a report supported by GitLab. Once the CI/CD has been completed, a new tab appears in your
Pipeline named "[Code Quality](https://docs.gitlab.com/ee/ci/testing/code_quality.html)".
This new tab lets you easily view the information from the apigeelint job, with the associated
severity level and lines affected. A widget with the same information appears during merge requests.

```yml
apigeelint:
stage: lint
image: node:12-alpine
before_script:
- npm install -g apigeelint
script:
- apigeelint -f codeclimate.js > apigeelint-results.json
artifacts:
reports:
codequality:
- "${CI_PROJECT_DIR}/apigeelint-results.json"
```
## Does this tool just lint or does it also check style?
Expand Down
79 changes: 79 additions & 0 deletions lib/package/third_party/formatters/codeclimate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* @fileoverview CodeClimate Reporter
* @author ipierre1
*/
"use strict";
const crypto = require('crypto');

/**
* Returns the severity of warning or error
* @param {Object} message message object to examine
* @returns {string} severity level
* @private
*/
function getMessageType(message) {
if (message.fatal || message.severity === 2) {
return "major";
} else if (message.severity === 1) {
return "minor";
} else {
return "info";
}
}

/**
* Generates a fingerprint for the issue based on its properties
* @param {Object} issue issue object
* @returns {string} fingerprint
* @private
*/
function generateFingerprint(issue) {
const data = `${issue.check_name}-${issue.location.path}-${issue.location.lines.begin}`;
return crypto.createHash('md5').update(data).digest('hex');
}

//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------

module.exports = function(results) {

let output = [];

results.forEach(result => {

const messages = result.messages;

messages.forEach(message => {
const type = getMessageType(message);

const issue = {
"type": "issue",
"check_name": message.ruleId || "unknown_rule",
"description": message.message || "",
"categories": ["lint"],
"location": {
"path": result.filePath,
"lines": {
"begin": message.line || 0
}
},
"fingerprint": generateFingerprint({
"check_name": message.ruleId || "unknown_rule",
"location": {
"path": result.filePath,
"lines": {
"begin": message.line || 0
}
}
}),
"severity": type
};

output.push(issue);
});

});

return JSON.stringify(output, null, 2);
};

0 comments on commit 5dfd347

Please sign in to comment.