Use templated strings for better readability

This commit is contained in:
Andrew Eisenberg 2020-07-22 10:07:27 -07:00 committed by Andrew Eisenberg
parent 9769e4a6df
commit 93dd64d351
4 changed files with 26 additions and 28 deletions

View file

@ -1,4 +1,4 @@
## Contributing # Contributing
[fork]: https://github.com/github/codeql-action/fork [fork]: https://github.com/github/codeql-action/fork
[pr]: https://github.com/github/codeql-action/compare [pr]: https://github.com/github/codeql-action/compare
@ -37,6 +37,7 @@ As well as the unit tests (see _Common tasks_ above), there are integration test
3. Make your change, add tests, and make sure the tests still pass 3. Make your change, add tests, and make sure the tests still pass
4. Push to your fork and [submit a pull request][pr] 4. Push to your fork and [submit a pull request][pr]
5. Pat yourself on the back and wait for your pull request to be reviewed and merged. 5. Pat yourself on the back and wait for your pull request to be reviewed and merged.
If you're a GitHub staff member, you can merge your own PR once it's approved; for external contributors, GitHub staff will merge your PR once it's approved. If you're a GitHub staff member, you can merge your own PR once it's approved; for external contributors, GitHub staff will merge your PR once it's approved.
Here are a few things you can do that will increase the likelihood of your pull request being accepted: Here are a few things you can do that will increase the likelihood of your pull request being accepted:

25
lib/fingerprints.js generated
View file

@ -146,10 +146,10 @@ function locationUpdateCallback(result, location) {
result.partialFingerprints.primaryLocationLineHash = hash; result.partialFingerprints.primaryLocationLineHash = hash;
} }
else if (existingFingerprint !== hash) { else if (existingFingerprint !== hash) {
core.warning("Calculated fingerprint of " + hash + core.warning('Calculated fingerprint of ' + hash +
" for file " + location.physicalLocation.artifactLocation.uri + ' for file ' + location.physicalLocation.artifactLocation.uri +
" line " + lineNumber + ' line ' + lineNumber +
", but found existing inconsistent fingerprint value " + existingFingerprint); ', but found existing inconsistent fingerprint value ' + existingFingerprint);
} }
}; };
} }
@ -164,14 +164,14 @@ function resolveUriToFile(location, artifacts) {
location.index < 0 || location.index < 0 ||
location.index >= artifacts.length || location.index >= artifacts.length ||
typeof artifacts[location.index].location !== 'object') { typeof artifacts[location.index].location !== 'object') {
core.debug('Ignoring location as index "' + location.index + '" is invalid'); core.debug(`Ignoring location as URI "${location.index}" is invalid`);
return undefined; return undefined;
} }
location = artifacts[location.index].location; location = artifacts[location.index].location;
} }
// Get the URI and decode // Get the URI and decode
if (typeof location.uri !== 'string') { if (typeof location.uri !== 'string') {
core.debug('Ignoring location as uri "' + location.uri + '" is invalid'); core.debug(`Ignoring location as index "${location.uri}" is invalid`);
return undefined; return undefined;
} }
let uri = decodeURIComponent(location.uri); let uri = decodeURIComponent(location.uri);
@ -181,13 +181,13 @@ function resolveUriToFile(location, artifacts) {
uri = uri.substring(fileUriPrefix.length); uri = uri.substring(fileUriPrefix.length);
} }
if (uri.indexOf('://') !== -1) { if (uri.indexOf('://') !== -1) {
core.debug('Ignoring location URI "' + uri + "' as the scheme is not recognised"); core.debug(`Ignoring location URI "${uri}" as the scheme is not recognised`);
return undefined; return undefined;
} }
// Discard any absolute paths that aren't in the src root // Discard any absolute paths that aren't in the src root
const srcRootPrefix = process.env['GITHUB_WORKSPACE'] + '/'; const srcRootPrefix = process.env['GITHUB_WORKSPACE'] + '/';
if (uri.startsWith('/') && !uri.startsWith(srcRootPrefix)) { if (uri.startsWith('/') && !uri.startsWith(srcRootPrefix)) {
core.debug('Ignoring location URI "' + uri + "' as it is outside of the src root"); core.debug(`Ignoring location URI "${uri}" as it is outside of the src root`);
return undefined; return undefined;
} }
// Just assume a relative path is relative to the src root. // Just assume a relative path is relative to the src root.
@ -198,7 +198,7 @@ function resolveUriToFile(location, artifacts) {
} }
// Check the file exists // Check the file exists
if (!fs.existsSync(uri)) { if (!fs.existsSync(uri)) {
core.debug("Unable to compute fingerprint for non-existent file: " + uri); core.debug(`Unable to compute fingerprint for non-existent file: ${uri}`);
return undefined; return undefined;
} }
return uri; return uri;
@ -207,6 +207,7 @@ exports.resolveUriToFile = resolveUriToFile;
// Compute fingerprints for results in the given sarif file // Compute fingerprints for results in the given sarif file
// and return an updated sarif file contents. // and return an updated sarif file contents.
function addFingerprints(sarifContents) { function addFingerprints(sarifContents) {
var _a, _b;
let sarif = JSON.parse(sarifContents); let sarif = JSON.parse(sarifContents);
// Gather together results for the same file and construct // Gather together results for the same file and construct
// callbacks to accept hashes for that file and update the location // callbacks to accept hashes for that file and update the location
@ -217,10 +218,8 @@ function addFingerprints(sarifContents) {
for (const result of run.results || []) { for (const result of run.results || []) {
// Check the primary location is defined correctly and is in the src root // Check the primary location is defined correctly and is in the src root
const primaryLocation = (result.locations || [])[0]; const primaryLocation = (result.locations || [])[0];
if (!primaryLocation || if (!((_b = (_a = primaryLocation) === null || _a === void 0 ? void 0 : _a.physicalLocation) === null || _b === void 0 ? void 0 : _b.artifactLocation)) {
!primaryLocation.physicalLocation || core.debug(`Unable to compute fingerprint for invalid location: ${JSON.stringify(primaryLocation)}`);
!primaryLocation.physicalLocation.artifactLocation) {
core.debug("Unable to compute fingerprint for invalid location: " + JSON.stringify(primaryLocation));
continue; continue;
} }
const filepath = resolveUriToFile(primaryLocation.physicalLocation.artifactLocation, artifacts); const filepath = resolveUriToFile(primaryLocation.physicalLocation.artifactLocation, artifacts);

File diff suppressed because one or more lines are too long

View file

@ -148,10 +148,10 @@ function locationUpdateCallback(result: any, location: any): hashCallback {
if (!existingFingerprint) { if (!existingFingerprint) {
result.partialFingerprints.primaryLocationLineHash = hash; result.partialFingerprints.primaryLocationLineHash = hash;
} else if (existingFingerprint !== hash) { } else if (existingFingerprint !== hash) {
core.warning("Calculated fingerprint of " + hash + core.warning('Calculated fingerprint of ' + hash +
" for file " + location.physicalLocation.artifactLocation.uri + ' for file ' + location.physicalLocation.artifactLocation.uri +
" line " + lineNumber + ' line ' + lineNumber +
", but found existing inconsistent fingerprint value " + existingFingerprint); ', but found existing inconsistent fingerprint value ' + existingFingerprint);
} }
}; };
} }
@ -167,7 +167,7 @@ export function resolveUriToFile(location: any, artifacts: any[]): string | unde
location.index < 0 || location.index < 0 ||
location.index >= artifacts.length || location.index >= artifacts.length ||
typeof artifacts[location.index].location !== 'object') { typeof artifacts[location.index].location !== 'object') {
core.debug('Ignoring location as index "' + location.index + '" is invalid'); core.debug(`Ignoring location as URI "${location.index}" is invalid`);
return undefined; return undefined;
} }
location = artifacts[location.index].location; location = artifacts[location.index].location;
@ -175,7 +175,7 @@ export function resolveUriToFile(location: any, artifacts: any[]): string | unde
// Get the URI and decode // Get the URI and decode
if (typeof location.uri !== 'string') { if (typeof location.uri !== 'string') {
core.debug('Ignoring location as uri "' + location.uri + '" is invalid'); core.debug(`Ignoring location as index "${location.uri}" is invalid`);
return undefined; return undefined;
} }
let uri = decodeURIComponent(location.uri); let uri = decodeURIComponent(location.uri);
@ -186,14 +186,14 @@ export function resolveUriToFile(location: any, artifacts: any[]): string | unde
uri = uri.substring(fileUriPrefix.length); uri = uri.substring(fileUriPrefix.length);
} }
if (uri.indexOf('://') !== -1) { if (uri.indexOf('://') !== -1) {
core.debug('Ignoring location URI "' + uri + "' as the scheme is not recognised"); core.debug(`Ignoring location URI "${uri}" as the scheme is not recognised`);
return undefined; return undefined;
} }
// Discard any absolute paths that aren't in the src root // Discard any absolute paths that aren't in the src root
const srcRootPrefix = process.env['GITHUB_WORKSPACE'] + '/'; const srcRootPrefix = process.env['GITHUB_WORKSPACE'] + '/';
if (uri.startsWith('/') && !uri.startsWith(srcRootPrefix)) { if (uri.startsWith('/') && !uri.startsWith(srcRootPrefix)) {
core.debug('Ignoring location URI "' + uri + "' as it is outside of the src root"); core.debug(`Ignoring location URI "${uri}" as it is outside of the src root`);
return undefined; return undefined;
} }
@ -206,7 +206,7 @@ export function resolveUriToFile(location: any, artifacts: any[]): string | unde
// Check the file exists // Check the file exists
if (!fs.existsSync(uri)) { if (!fs.existsSync(uri)) {
core.debug("Unable to compute fingerprint for non-existent file: " + uri); core.debug(`Unable to compute fingerprint for non-existent file: ${uri}`);
return undefined; return undefined;
} }
@ -228,10 +228,8 @@ export function addFingerprints(sarifContents: string): string {
for (const result of run.results || []) { for (const result of run.results || []) {
// Check the primary location is defined correctly and is in the src root // Check the primary location is defined correctly and is in the src root
const primaryLocation = (result.locations || [])[0]; const primaryLocation = (result.locations || [])[0];
if (!primaryLocation || if (!primaryLocation?.physicalLocation?.artifactLocation) {
!primaryLocation.physicalLocation || core.debug(`Unable to compute fingerprint for invalid location: ${JSON.stringify(primaryLocation)}`);
!primaryLocation.physicalLocation.artifactLocation) {
core.debug("Unable to compute fingerprint for invalid location: " + JSON.stringify(primaryLocation));
continue; continue;
} }