From 0aa5c58d5f8ae3c0e85b8b0aadd694ce613b16fe Mon Sep 17 00:00:00 2001 From: Patrick Hulce Date: Tue, 29 Oct 2019 22:32:10 -0500 Subject: [PATCH] feat(server): add commitedAt and ancestorCommittedAt --- packages/cli/src/upload/upload.js | 23 ++++++++-------- .../server/src/api/storage/sql/build-model.js | 2 ++ .../sql/migrations/20191029-committed-at.js | 26 +++++++++++++++++++ packages/server/test/server-test-suite.js | 8 ++++++ types/server.d.ts | 3 ++- 5 files changed, 49 insertions(+), 13 deletions(-) create mode 100644 packages/server/src/api/storage/sql/migrations/20191029-committed-at.js diff --git a/packages/cli/src/upload/upload.js b/packages/cli/src/upload/upload.js index f404a8e70..ebaf009e9 100644 --- a/packages/cli/src/upload/upload.js +++ b/packages/cli/src/upload/upload.js @@ -83,17 +83,13 @@ function getCurrentHash() { } /** - * @param {string} currentHash + * @param {string} hash * @return {string} */ -function getCommitTime(currentHash) { - const result = childProcess.spawnSync( - 'git', - ['log', '-n1', '-pretty="format:%cI"', currentHash], - { - encoding: 'utf8', - } - ); +function getCommitTime(hash) { + const result = childProcess.spawnSync('git', ['log', '-n1', '-pretty="format:%cI"', hash], { + encoding: 'utf8', + }); if (result.status !== 0) { throw new Error('Unable to retrieve committer timestamp from commit'); } @@ -194,7 +190,7 @@ function getAncestorHashForBranch() { }); if (result.status !== 0) { - throw new Error('Unable to determine current hash with `git merge-base HEAD master`'); + throw new Error('Unable to determine ancestor hash with `git merge-base HEAD master`'); } return result.stdout.trim(); @@ -331,19 +327,22 @@ async function runLHCITarget(options) { const hash = getCurrentHash(); const branch = getCurrentBranch(); + const ancestorHash = + branch === 'master' ? getAncestorHashForMaster() : getAncestorHashForBranch(); const build = await api.createBuild({ projectId: project.id, lifecycle: 'unsealed', hash, branch, + ancestorHash, commitMessage: getCommitMessage(hash), author: getAuthor(hash), - committedAt: getCommitTime(hash), avatarUrl: getAvatarUrl(hash), - ancestorHash: branch === 'master' ? getAncestorHashForMaster() : getAncestorHashForBranch(), externalBuildUrl: getExternalBuildUrl(), runAt: new Date().toISOString(), + committedAt: getCommitTime(hash), + ancestorCommittedAt: getCommitTime(ancestorHash), }); print(`Saving CI project ${project.name} (${project.id})\n`); diff --git a/packages/server/src/api/storage/sql/build-model.js b/packages/server/src/api/storage/sql/build-model.js index f9d2f62b0..cbc626de0 100644 --- a/packages/server/src/api/storage/sql/build-model.js +++ b/packages/server/src/api/storage/sql/build-model.js @@ -25,6 +25,8 @@ const attributes = { ancestorHash: {type: Sequelize.STRING(40)}, externalBuildUrl: {type: Sequelize.STRING(256)}, runAt: {type: Sequelize.DATE()}, // should mostly be equal to createdAt but modifiable by the consumer + committedAt: {type: Sequelize.DATE()}, + ancestorCommittedAt: {type: Sequelize.DATE()}, }; module.exports = { diff --git a/packages/server/src/api/storage/sql/migrations/20191029-committed-at.js b/packages/server/src/api/storage/sql/migrations/20191029-committed-at.js new file mode 100644 index 000000000..a95595348 --- /dev/null +++ b/packages/server/src/api/storage/sql/migrations/20191029-committed-at.js @@ -0,0 +1,26 @@ +/** + * @license Copyright 2019 Google Inc. All Rights Reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. + */ +'use strict'; + +/* eslint-disable new-cap */ + +module.exports = { + /** + * @param {import('sequelize').QueryInterface} queryInterface + * @param {typeof import('sequelize')} Sequelize + */ + up: async (queryInterface, Sequelize) => { + await queryInterface.addColumn('builds', 'committedAt', {type: Sequelize.DATE()}); + await queryInterface.addColumn('builds', 'ancestorCommittedAt', {type: Sequelize.DATE()}); + }, + /** + * @param {import('sequelize').QueryInterface} queryInterface + */ + down: async queryInterface => { + await queryInterface.removeColumn('builds', 'committedAt'); + await queryInterface.removeColumn('builds', 'ancestorCommittedAt'); + }, +}; diff --git a/packages/server/test/server-test-suite.js b/packages/server/test/server-test-suite.js index ae19d7676..2250479e7 100644 --- a/packages/server/test/server-test-suite.js +++ b/packages/server/test/server-test-suite.js @@ -91,6 +91,8 @@ function runTests(state) { commitMessage: 'feat: add some awesome features', ancestorHash: '0ed0fdcfdce0acdd5eb2508498be50cc55c696ea', runAt: new Date().toISOString(), + committedAt: new Date().toISOString(), + ancestorCommittedAt: new Date().toISOString(), }; buildA = await client.createBuild(payload); @@ -111,6 +113,8 @@ function runTests(state) { commitMessage: 'feat: add some more awesome features', ancestorHash: buildA.hash, runAt: new Date().toISOString(), + committedAt: new Date().toISOString(), + ancestorCommittedAt: new Date().toISOString(), }; buildB = await client.createBuild(payload); @@ -131,6 +135,8 @@ function runTests(state) { commitMessage: 'feat: a branch without an ancestor', ancestorHash: '', runAt: new Date().toISOString(), + committedAt: new Date().toISOString(), + ancestorCommittedAt: new Date().toISOString(), }; buildC = await client.createBuild(payload); @@ -151,6 +157,8 @@ function runTests(state) { commitMessage: 'feat: initial commit', ancestorHash: '', runAt: new Date().toISOString(), + committedAt: new Date().toISOString(), + ancestorCommittedAt: new Date().toISOString(), }; buildD = await client.createBuild(payload); diff --git a/types/server.d.ts b/types/server.d.ts index 45b5bfd7c..4f06ee866 100644 --- a/types/server.d.ts +++ b/types/server.d.ts @@ -36,9 +36,10 @@ declare global { author?: string; avatarUrl?: string; ancestorHash?: string; + committedAt?: string; + ancestorCommittedAt?: string; createdAt?: string; updatedAt?: string; - committedAt?: string; } export interface Run {