Skip to content

Commit

Permalink
Fix config defaulting
Browse files Browse the repository at this point in the history
  • Loading branch information
lerebear committed Oct 6, 2023
1 parent 9e18bde commit e8b3b76
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 78 deletions.
2 changes: 1 addition & 1 deletion badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 1 addition & 8 deletions dist/config/schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 28 additions & 13 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 1 addition & 8 deletions src/config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,5 @@
"sizeup": {
"$ref": "https://raw.githubusercontent.com/lerebear/sizeup/main/src/config/schema.json"
}
},
"required": [
"applyCategoryLabels",
"categoryLabelPrefix",
"addCommentWhenScoreThresholdHasBeenExceeded",
"scoreThresholdExceededCommentTemplate",
"sizeup"
]
}
}
91 changes: 62 additions & 29 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@ export interface Configuration {
/**
* Whether or not to apply the score category label to each assessed pull request
*/
applyCategoryLabels: boolean
applyCategoryLabels?: boolean
/**
* The prefix to add to each category label that we apply
*/
categoryLabelPrefix: string
categoryLabelPrefix?: string
/**
* Whether or not to add a comment to each assessed pull request that exceeds the configured score threshold
*/
addCommentWhenScoreThresholdHasBeenExceeded: boolean
addCommentWhenScoreThresholdHasBeenExceeded?: boolean
/**
* The template for the comment that should be added to each pull request that exceeds the configured score threshold
*/
scoreThresholdExceededCommentTemplate: string
scoreThresholdExceededCommentTemplate?: string
/**
* A list of GitHub handles for users or teams that have opted into this workflow
*
* @minItems 1
*/
optIns?: [string, ...string[]]
sizeup: Configuration1
sizeup?: Configuration1
}
/**
* YAML configuration accepted by sizeup
Expand All @@ -41,37 +41,70 @@ export interface Configuration1 {
*
* @minItems 1
*/
categories?: {
/**
* human-friendly name of the category
*/
name: string
/**
* A visual label that should be used to represent this category
*/
label?: {
categories?: [
{
/**
* name of the label that should be used to represent this category
* human-friendly name of the category
*/
name: string
/**
* describes the meaning of the label that will be used to represent this category
* A visual label that should be used to represent this category
*/
description?: string
label?: {
/**
* name of the label that should be used to represent this category
*/
name: string
/**
* describes the meaning of the label that will be used to represent this category
*/
description?: string
/**
* preferred CSS hex color label that should be used to represent this category
*/
color?: string
}
/**
* preferred CSS hex color label that should be used to represent this category
* inclusive upper bound on the score that a pull request must have to be assigned this category
*/
color?: string
}
/**
* inclusive upper bound on the score that a pull request must have to be assigned this category
*/
lte?: number
/**
* Whether or not this category marks the threshold at which we should warn about the diff being difficult to review
*/
threshold?: boolean
}[]
lte?: number
/**
* Whether or not this category marks the threshold above which we should warn about the diff being difficult to review
*/
threshold?: boolean
},
...{
/**
* human-friendly name of the category
*/
name: string
/**
* A visual label that should be used to represent this category
*/
label?: {
/**
* name of the label that should be used to represent this category
*/
name: string
/**
* describes the meaning of the label that will be used to represent this category
*/
description?: string
/**
* preferred CSS hex color label that should be used to represent this category
*/
color?: string
}
/**
* inclusive upper bound on the score that a pull request must have to be assigned this category
*/
lte?: number
/**
* Whether or not this category marks the threshold above which we should warn about the diff being difficult to review
*/
threshold?: boolean
}[]
]
scoring?: {
/**
* an expression, written in prefix-notation, that describes how to combine features to produce a score
Expand Down
51 changes: 32 additions & 19 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ import * as fs from 'fs'
import * as path from 'path'
import { Configuration } from './configuration'

const DEFAULT_LABEL_PREFIX = 'sizeup/'
const DEFAULT_COMMENT_TEMPLATE = `
👋 @{{author}} this pull request exceeds the configured reviewability score threshold of {{threshold}}. Your actual score was {{score}}.
[Research](https://www.cabird.com/static/93aba3256c80506d3948983db34d3ba3/rigby2013convergent.pdf) has shown that this makes it harder for reviewers to provide quality feedback.
We recommend that you reduce the size of this PR by separating commits into stacked PRs.
`

/**
* The main function for the action.
*
Expand Down Expand Up @@ -63,9 +72,7 @@ function loadConfiguration(): Configuration {
configFile = path.resolve(__dirname, './config/default.yaml')
}

const config = fs.readFileSync(configFile, 'utf8')
core.debug(`Retrieved config: ${config}`)
return YAML.parse(config) as Configuration
return YAML.parse(fs.readFileSync(configFile, 'utf8')) as Configuration
}

/**
Expand All @@ -86,7 +93,6 @@ async function evaluatePullRequest(
core.info(`Evaluating pull request ${pullRequestNickname}`)

const octokit = github.getOctokit(core.getInput('token'))
const sizeupConfigFile = path.resolve(__dirname, './tmp/sizeup.yaml')
const diff = (
await octokit.rest.pulls.get({
owner: pull.base.repo.owner.login,
Expand All @@ -96,18 +102,18 @@ async function evaluatePullRequest(
})
).data as unknown as string

fs.mkdirSync(path.dirname(sizeupConfigFile))
const yaml = YAML.stringify(config.sizeup)
core.debug(
`Writing to ${sizeupConfigFile} with ${yaml} derived from ${JSON.stringify(
config.sizeup
)}`
)
fs.writeFileSync(sizeupConfigFile, yaml)
let sizeupConfigFile = undefined
if (config.sizeup) {
sizeupConfigFile = path.resolve(__dirname, './tmp/sizeup.yaml')
fs.mkdirSync(path.dirname(sizeupConfigFile))
fs.writeFileSync(sizeupConfigFile, YAML.stringify(config.sizeup))
}

const score = SizeUp.evaluate(diff, sizeupConfigFile)

fs.rmSync(sizeupConfigFile, { force: true, recursive: true })
if (sizeupConfigFile) {
fs.rmSync(sizeupConfigFile, { force: true, recursive: true })
}

core.info(
`${pullRequestNickname} received a score of ${score.result} (${
Expand Down Expand Up @@ -135,7 +141,7 @@ async function applyCategoryLabel(
config: Configuration
): Promise<void> {
if (!score.category?.label) {
core.info('Skipping labelling because no category label was provided')
core.info('Skipping labeling because no category label was provided')
return
}

Expand Down Expand Up @@ -200,16 +206,18 @@ async function applyLabel(
config: Configuration
): Promise<void> {
const octokit = github.getOctokit(core.getInput('token'))
const newLabelName = `${config.categoryLabelPrefix}${
score.category!.label!.name
}`
const labelPrefix =
config.categoryLabelPrefix !== undefined
? config.categoryLabelPrefix
: DEFAULT_LABEL_PREFIX
const newLabelName = `${labelPrefix}${score.category!.label!.name}`
const labelsToAdd = [newLabelName]
const labelsToRemove = []

for (const existingLabel of pull.labels) {
if (existingLabel.name === newLabelName) {
labelsToAdd.pop()
} else if (existingLabel.name.startsWith(config.categoryLabelPrefix)) {
} else if (existingLabel.name.startsWith(labelPrefix)) {
labelsToRemove.push(existingLabel.name)
}
}
Expand Down Expand Up @@ -264,7 +272,12 @@ async function addScoreThresholdExceededComment(
): Promise<void> {
if (score.result! <= score.threshold!) return

const comment = config.scoreThresholdExceededCommentTemplate
const commentTemplate =
config.scoreThresholdExceededCommentTemplate !== undefined
? config.scoreThresholdExceededCommentTemplate
: DEFAULT_COMMENT_TEMPLATE

const comment = commentTemplate
.replaceAll('{{author}}', pull.user.login)
.replaceAll('{{score}}', `${score.result!}`)
.replaceAll('{{category}}', score.category!.name)
Expand Down

0 comments on commit e8b3b76

Please sign in to comment.