Skip to content

Commit

Permalink
feat(server): add commitedAt and ancestorCommittedAt
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce committed Oct 30, 2019
1 parent f220566 commit 0aa5c58
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 13 deletions.
23 changes: 11 additions & 12 deletions packages/cli/src/upload/upload.js
Expand Up @@ -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');
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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`);
Expand Down
2 changes: 2 additions & 0 deletions packages/server/src/api/storage/sql/build-model.js
Expand Up @@ -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 = {
Expand Down
@@ -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');
},
};
8 changes: 8 additions & 0 deletions packages/server/test/server-test-suite.js
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion types/server.d.ts
Expand Up @@ -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 {
Expand Down

0 comments on commit 0aa5c58

Please sign in to comment.