From 3d44dec7a228b0bc8f234691865b61d9c0c27ff8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9r=C3=A8?= Date: Sun, 3 Mar 2024 21:46:20 -0800 Subject: [PATCH] Add more fields to the output CSV 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. --- badges/coverage.svg | 2 +- dist/index.js | 90 ++++++++++++++++++++++++++++++++++++--------- src/archiver.ts | 56 ++++++++++++++++++++++++++++ src/main.ts | 30 +-------------- 4 files changed, 132 insertions(+), 46 deletions(-) create mode 100644 src/archiver.ts diff --git a/badges/coverage.svg b/badges/coverage.svg index 9b9b3b2..a10c71b 100644 --- a/badges/coverage.svg +++ b/badges/coverage.svg @@ -1 +1 @@ -Coverage: 66.3%Coverage66.3% \ No newline at end of file +Coverage: 64.97%Coverage64.97% \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index 8fbee79..a98e00c 100644 --- a/dist/index.js +++ b/dist/index.js @@ -105468,6 +105468,77 @@ utils.walkdir = function(dirpath, base, callback) { }; +/***/ }), + +/***/ 79048: +/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) { + +"use strict"; + +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { return m[k]; } }; + } + Object.defineProperty(o, k2, desc); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; +Object.defineProperty(exports, "__esModule", ({ value: true })); +exports.createScoreArtifact = void 0; +const core = __importStar(__nccwpck_require__(42186)); +const artifact_1 = __nccwpck_require__(79450); +const fs = __importStar(__nccwpck_require__(57147)); +const path = __importStar(__nccwpck_require__(71017)); +const initializer_1 = __nccwpck_require__(79477); +async function createScoreArtifact(pull, score, optInStatus, config) { + 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 artifact_1.DefaultArtifactClient(); + await client.uploadArtifact('sizeup-score', [scoreFile], tmpDir, { + retentionDays: config.archiving?.artifactRetention + }); +} +exports.createScoreArtifact = createScoreArtifact; +function scoreFileContents(pull, score, optInStatus) { + const fields = [ + ['pull-request-number', `${pull.number}`], + ['opted-in', `${optInStatus === initializer_1.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'); +} + + /***/ }), /***/ 13198: @@ -105817,7 +105888,6 @@ Object.defineProperty(exports, "__esModule", ({ value: true })); exports.run = void 0; const core = __importStar(__nccwpck_require__(42186)); const github = __importStar(__nccwpck_require__(95438)); -const artifact_1 = __nccwpck_require__(79450); const sizeup_core_1 = __nccwpck_require__(24296); const YAML = __importStar(__nccwpck_require__(44083)); const fs = __importStar(__nccwpck_require__(57147)); @@ -105825,6 +105895,7 @@ const path = __importStar(__nccwpck_require__(71017)); const initializer_1 = __nccwpck_require__(79477); const git_1 = __nccwpck_require__(36350); const commenting_1 = __nccwpck_require__(13198); +const archiver_1 = __nccwpck_require__(79048); const DEFAULT_LABEL_PREFIX = 'sizeup/'; /** * The main function for the action. @@ -105849,7 +105920,7 @@ async function run() { const score = await evaluatePullRequest(pullRequest, diff, config); await applyCategoryLabel(pullRequest, score, optInStatus, config); await (0, commenting_1.addOrUpdateScoreThresholdExceededComment)(pullRequest, score, optInStatus, config); - await createScoreArtifact(score, config); + await (0, archiver_1.createScoreArtifact)(pullRequest, score, optInStatus, config); } catch (error) { if (error instanceof Error) @@ -106011,21 +106082,6 @@ async function applyLabel(pull, score, config) { } } } -async function createScoreArtifact(score, config) { - 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 artifact_1.DefaultArtifactClient(); - await client.uploadArtifact('sizeup-score', [scoreFile], tmpDir, { - retentionDays: config.archiving?.artifactRetention - }); -} /***/ }), diff --git a/src/archiver.ts b/src/archiver.ts new file mode 100644 index 0000000..9d895d2 --- /dev/null +++ b/src/archiver.ts @@ -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 { + 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') +} diff --git a/src/main.ts b/src/main.ts index 39fc83f..b0c54f0 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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' @@ -15,6 +14,7 @@ import { } from './initializer' import { Git } from './git' import { addOrUpdateScoreThresholdExceededComment } from './commenting' +import { createScoreArtifact } from './archiver' const DEFAULT_LABEL_PREFIX = 'sizeup/' @@ -51,7 +51,7 @@ export async function run(): Promise { optInStatus, config ) - await createScoreArtifact(score, config) + await createScoreArtifact(pullRequest, score, optInStatus, config) } catch (error) { if (error instanceof Error) core.setFailed(error.message) } @@ -259,29 +259,3 @@ async function applyLabel( } } } - -async function createScoreArtifact( - score: Score, - config: Configuration -): Promise { - 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 - }) -}