Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
fwuensche committed Sep 22, 2024
1 parent b5d32be commit bee8a47
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 92 deletions.
21 changes: 8 additions & 13 deletions bin/commands/run.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
import * as git from '../../src/git.js'

import {
allowMultipleValues,
buildMetricsPayload,
buildSarifPayload,
buildSonarGenericImportPayload,
countByMetric,
sortObject,
} from '../helpers.js'
import { allowMultipleValues, buildMetricsPayload, countByMetric, sortObject } from '../helpers.js'

import Codeowners from '../../src/codeowners.js'
import { Command } from 'commander'
import _ from 'lodash'
import { findOccurrences } from '../../src/occurrences.js'
import fs from 'fs'
import _ from 'lodash'
import Codeowners from '../../src/codeowners.js'
import { getConfiguration } from '../../src/configuration.js'
import { getFiles } from '../../src/files.js'
import { panic } from '../../src/error.js'
import { getFiles } from '../../src/files.js'
import { buildSarifPayload } from '../../src/helpers/sarif.js'
import { buildSonarGenericImportPayload } from '../../src/helpers/sonar.js'
import { findOccurrences } from '../../src/occurrences.js'

const ALLOWED_FORMATS = ['json', 'sarif', 'sonar']

Expand Down Expand Up @@ -70,7 +65,7 @@ export default function (program: Command) {
} else if (format === 'sarif') {
const branch = await git.branchName()
const sha = await git.sha()
const sarif = buildSarifPayload(configuration.project_name, branch, sha, occurrences)
const sarif = buildSarifPayload(configuration.repository, branch, sha, occurrences)
content = JSON.stringify(sarif, null, 2)
} else if (format === 'sonar') {
const sonar = buildSonarGenericImportPayload(occurrences)
Expand Down
68 changes: 0 additions & 68 deletions bin/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import _ from 'lodash'
import Spinnies from 'spinnies'
import { v4 } from 'uuid'
import { panic } from '../src/error.js'
import { buildRepoURL } from '../src/permalink.js'

export const spinnies = new Spinnies()

Expand Down Expand Up @@ -115,73 +114,6 @@ const buildPushPayload = ({
metrics: buildMetricsPayload(occurrences),
})

export const buildSarifPayload = (projectName: string, branch: string, sha: string, occurrences: Occurrence[]) => {
const rules = _(occurrences)
.groupBy('metricName')
.map((occurrences) => ({
id: occurrences[0].metricName,
}))

const results = occurrences.map((occurrence) => ({
ruleId: occurrence.metricName,
level: 'none',
message: { text: `${occurrence.metricName} at ${occurrence.text}` },
locations: [
{
physicalLocation: {
artifactLocation: {
uri: occurrence.text.split(':')[0],
},
region: {
startLine: parseInt(occurrence.text.split(':')[1]) || 1,
},
},
},
],
}))

return {
$schema: 'https://json.schemastore.org/sarif-2.1.0.json',
version: '2.1.0',
runs: [
{
versionControlProvenance: [
{
repositoryUri: buildRepoURL(projectName),
revisionId: sha,
branch,
},
],
tool: {
driver: {
name: 'cherry',
version: process.env.npm_package_version,
informationUri: 'https://github.com/cherrypush/cherrypush.com',
rules,
},
},
results,
},
],
}
}

export const buildSonarGenericImportPayload = (occurrences: Occurrence[]) => ({
issues: occurrences.map((occurrence) => ({
engineId: 'cherry',
ruleId: occurrence.metricName,
type: 'CODE_SMELL',
severity: 'INFO',
primaryLocation: {
message: `${occurrence.metricName} at ${occurrence.text}`,
filePath: occurrence.text.split(':')[0],
textRange: {
startLine: parseInt(occurrence.text.split(':')[1]) || 1,
},
},
})),
})

export const sortObject = (object: object) => _(object).toPairs().sortBy(0).fromPairs().value()

export function isEvalMetric(metric: Metric): metric is EvalMetric {
Expand Down
54 changes: 54 additions & 0 deletions src/helpers/sarif.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import _ from 'lodash'
import { buildRepoURL } from '../repository.js'
import { Occurrence, Repository } from '../types.js'

export const buildSarifPayload = (repository: Repository, branch: string, sha: string, occurrences: Occurrence[]) => {
const rules = _(occurrences)
.groupBy('metricName')
.map((occurrences) => ({
id: occurrences[0].metricName,
}))

const results = occurrences.map((occurrence) => ({
ruleId: occurrence.metricName,
level: 'none',
message: { text: `${occurrence.metricName} at ${occurrence.text}` },
locations: [
{
physicalLocation: {
artifactLocation: {
uri: occurrence.text.split(':')[0],
},
region: {
startLine: parseInt(occurrence.text.split(':')[1]) || 1,
},
},
},
],
}))

return {
$schema: 'https://json.schemastore.org/sarif-2.1.0.json',
version: '2.1.0',
runs: [
{
versionControlProvenance: [
{
repositoryUri: buildRepoURL(repository),
revisionId: sha,
branch,
},
],
tool: {
driver: {
name: 'cherry',
version: process.env.npm_package_version,
informationUri: 'https://github.com/cherrypush/cherrypush.com',
rules,
},
},
results,
},
],
}
}
17 changes: 17 additions & 0 deletions src/helpers/sonar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Occurrence } from '../types.js'

export const buildSonarGenericImportPayload = (occurrences: Occurrence[]) => ({
issues: occurrences.map((occurrence) => ({
engineId: 'cherry',
ruleId: occurrence.metricName,
type: 'CODE_SMELL',
severity: 'INFO',
primaryLocation: {
message: `${occurrence.metricName} at ${occurrence.text}`,
filePath: occurrence.text.split(':')[0],
textRange: {
startLine: parseInt(occurrence.text.split(':')[1]) || 1,
},
},
})),
})
8 changes: 5 additions & 3 deletions src/occurrences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import Spinnies from 'spinnies'
import { isEvalMetric } from '../bin/helpers.js'
import { panic } from './error.js'
import { readLines } from './files.js'
import { buildPermalink } from './permalink.js'
import eslint from './plugins/eslint.js'
import jsCircularDependencies from './plugins/js_circular_dependencies.js'
import jsUnimported from './plugins/js_unimported.js'
import loc from './plugins/loc.js'
import npmOutdated from './plugins/npm_outdated.js'
import rubocop from './plugins/rubocop.js'
import yarnOutdated from './plugins/yarn_outdated.js'
import { buildPermalink } from './repository.js'

const spinnies = new Spinnies()

Expand Down Expand Up @@ -190,7 +190,6 @@ export const findOccurrences = async ({
quiet: boolean
}) => {
let metrics = configuration.metrics
const { project_name: projectName, permalink } = configuration

// Prevent running all metrics if a subset is provided
if (metricNames) metrics = metrics.filter(({ name }) => metricNames.includes(name))
Expand All @@ -211,7 +210,10 @@ export const findOccurrences = async ({
value,
metricName,
// The url might have been provided by plugins or eval metrics
url: url !== undefined ? url : filePath && buildPermalink(permalink, projectName, filePath, lineNumber),
url:
url !== undefined
? url
: filePath && buildPermalink(configuration.permalink, configuration.repository, filePath, lineNumber),
owners: owners !== undefined ? owners : filePath && codeOwners.getOwners(filePath),
}))

Expand Down
7 changes: 0 additions & 7 deletions src/permalink.js

This file was deleted.

15 changes: 15 additions & 0 deletions src/repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { PermalinkFn, Repository } from './types.js'

export const buildRepoURL = (repository: Repository) =>
`https://${repository.host}/${repository.owner}/${repository.name}`

export const buildPermalink = (
permalink: PermalinkFn | undefined,
repository: Repository,
filePath: string,
lineNumber: number | undefined
) => {
if (!permalink) return `${buildRepoURL(repository)}/blob/HEAD/${filePath}${lineNumber ? `#L${lineNumber}` : ''}`

return permalink({ filePath, lineNumber })
}
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ export type Repository = {
subdir: string // e.g. src, or an empty string for the root
}

export type PermalinkFn = (options: { filePath: string; lineNumber?: number }) => string

export type Configuration = {
project_name: string
repository: Repository
permalink?: () => string
permalink?: PermalinkFn
metrics: Metric[]
plugins?: Plugins
}
Expand Down

0 comments on commit bee8a47

Please sign in to comment.