Skip to content

Commit

Permalink
Add more fields to the output CSV
Browse files Browse the repository at this point in the history
Specifically, and fields for the pull request number, the opt-in status
of the pull request author, and a timestamp for when the data was
generated.
  • Loading branch information
lerebear committed Mar 4, 2024
1 parent b012348 commit 3d44dec
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 46 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.
90 changes: 73 additions & 17 deletions dist/index.js

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

56 changes: 56 additions & 0 deletions src/archiver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import * as core from '@actions/core'
import { DefaultArtifactClient } from '@actions/artifact'
import { PullRequest } from '@octokit/webhooks-types' // eslint-disable-line import/no-unresolved
import * as fs from 'fs'
import * as path from 'path'
import { Score } from 'sizeup-core'
import { Configuration } from './configuration'
import { OptInStatus } from './initializer'

export async function createScoreArtifact(
pull: PullRequest,
score: Score,
optInStatus: OptInStatus,
config: Configuration
): Promise<void> {
if (!config.archiving?.persistScoreArtifact) {
core.info('Skipping score artifact creation')
return
}

core.info('Creating score artifact')

const tmpDir = path.resolve(__dirname, './tmp')
const scoreFile = path.resolve(tmpDir, './sizeup-score/sizeup-score.csv')

fs.mkdirSync(path.dirname(scoreFile), { recursive: true })
fs.writeFileSync(scoreFile, scoreFileContents(pull, score, optInStatus))

const client = new DefaultArtifactClient()
await client.uploadArtifact('sizeup-score', [scoreFile], tmpDir, {
retentionDays: config.archiving?.artifactRetention
})
}

function scoreFileContents(
pull: PullRequest,
score: Score,
optInStatus: OptInStatus
): string {
const fields = [
['pull-request-number', `${pull.number}`],
['opted-in', `${optInStatus === OptInStatus.In}`],
['score', `${score.result}`],
['category', score.category?.name || ''],
['timestamp', `${Date.now()}`]
]

const header = []
const data = []
for (const [key, value] of fields) {
header.push(key)
data.push(value)
}

return [header.join(','), data.join(',')].join('\n')
}
30 changes: 2 additions & 28 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as core from '@actions/core'
import * as github from '@actions/github'
import { DefaultArtifactClient } from '@actions/artifact'
import { PullRequest, Label } from '@octokit/webhooks-types' // eslint-disable-line import/no-unresolved
import { SizeUp, Score } from 'sizeup-core'
import * as YAML from 'yaml'
Expand All @@ -15,6 +14,7 @@ import {
} from './initializer'
import { Git } from './git'
import { addOrUpdateScoreThresholdExceededComment } from './commenting'
import { createScoreArtifact } from './archiver'

const DEFAULT_LABEL_PREFIX = 'sizeup/'

Expand Down Expand Up @@ -51,7 +51,7 @@ export async function run(): Promise<void> {
optInStatus,
config
)
await createScoreArtifact(score, config)
await createScoreArtifact(pullRequest, score, optInStatus, config)
} catch (error) {
if (error instanceof Error) core.setFailed(error.message)
}
Expand Down Expand Up @@ -259,29 +259,3 @@ async function applyLabel(
}
}
}

async function createScoreArtifact(
score: Score,
config: Configuration
): Promise<void> {
if (!config.archiving?.persistScoreArtifact) {
core.info('Skipping score artifact creation')
return
}

core.info('Creating score artifact')

const tmpDir = path.resolve(__dirname, './tmp')
const scoreFile = path.resolve(tmpDir, './sizeup-score/sizeup-score.csv')

fs.mkdirSync(path.dirname(scoreFile), { recursive: true })
fs.writeFileSync(
scoreFile,
`score,category\n${score.result},${score.category?.name || ''}`
)

const client = new DefaultArtifactClient()
await client.uploadArtifact('sizeup-score', [scoreFile], tmpDir, {
retentionDays: config.archiving?.artifactRetention
})
}

0 comments on commit 3d44dec

Please sign in to comment.