Skip to content

Commit

Permalink
kicking off new build manually will push build to build list (#378)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Wilson authored Sep 9, 2021
1 parent f1ddc8e commit 9afe3e2
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 59 deletions.
131 changes: 81 additions & 50 deletions src/components/pages/repo/build-list/build-list.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import classNames from 'classnames/bind';
import { formatDistanceStrict } from 'date-fns';
import PropTypes from 'prop-types';
import React from 'react';
import { Link } from 'react-router-dom';

Expand All @@ -11,59 +12,89 @@ import styles from './build-list.module.scss';

const cx = classNames.bind(styles);

const BuildList = (props) => {
const { url, data } = props;
return (
<ul className={cx('list')}>
{props.data.map((build) => (
<li key={build.id}>
<Link to={`${url}/${build.number}`}>
<div className={cx('list-item')}>
<div className={cx('status')}>
<Status status={build.status} />
<div className={cx('connector')} />
const BuildListItem = ({ build, url }) => (
<li>
<Link to={`${url}/${build.number}`}>
<div className={cx('list-item')}>
<div className={cx('status')}>
<Status status={build.status} />
<div className={cx('connector')} />
</div>
<div className={cx('container')}>
<div className={cx('header')}>
<div className={cx('number')}>{build.number}</div>
<div className={cx('commit')}>{build.title || build.message?.trim()}</div>
</div>
<div className={cx('content')}>
<Activity
className={cx('activity')}
number={build.number}
event={build.event}
action={build.action}
actor={build.sender}
avatar={build.author_avatar}
commit={build.after}
branch={build.target}
target={build.deploy_to}
parent={build.parent}
cron={build.cron}
refs={build.ref}
/>
<div className={cx('timing')}>
<div>
{formatDistanceStrict(new Date(build.created * 1000), new Date(), { addSuffix: true })}
</div>
<div className={cx('container')}>
<div className={cx('header')}>
<div className={cx('number')}>{build.number}</div>
<div className={cx('commit')}>{build.title || build.message?.trim()}</div>
</div>
<div className={cx('content')}>
<Activity
className={cx('activity')}
number={build.number}
event={build.event}
action={build.action}
actor={build.sender}
avatar={build.author_avatar}
commit={build.after}
branch={build.target}
target={build.deploy_to}
parent={build.parent}
cron={build.cron}
refs={build.ref}
/>
<div className={cx('timing')}>
<div>
{formatDistanceStrict(new Date(build.created * 1000), new Date(), { addSuffix: true })}
</div>
<div className={cx('dot')}><span /></div>
<div>
<Elapsed
started={build.started}
finished={build.finished}
className={cx('elapsed')}
/>
</div>
</div>
</div>
<div className={cx('dot')}><span /></div>
<div>
<Elapsed
started={build.started}
finished={build.finished}
className={cx('elapsed')}
/>
</div>
</div>
</Link>
</li>
))}
</ul>
);
</div>
</div>
</div>
</Link>
</li>
);

const BuildList = ({ data, url }) => (
<ul className={cx('list')}>
{data.map((build) => <BuildListItem key={build.number} build={build} url={url} />)}
</ul>
);

const buildPropTypes = PropTypes.shape({
id: PropTypes.number,
number: PropTypes.number,
status: PropTypes.string,
title: PropTypes.number,
message: PropTypes.string,
event: PropTypes.string,
action: PropTypes.string,
sender: PropTypes.string,
author_avatar: PropTypes.string,
after: PropTypes.string,
target: PropTypes.string,
deploy_to: PropTypes.string,
parent: PropTypes.string,
cron: PropTypes.bool,
ref: PropTypes.string,
created: PropTypes.number,
started: PropTypes.number,
finished: PropTypes.number,
});

BuildListItem.propTypes = {
build: buildPropTypes.isRequired,
url: PropTypes.string.isRequired,
};

BuildList.propTypes = {
data: PropTypes.arrayOf(buildPropTypes).isRequired,
url: PropTypes.string.isRequired,
};

export default BuildList;
2 changes: 2 additions & 0 deletions src/components/pages/repo/new-build-form/new-build-form.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,13 @@ const NewBuildForm = ({ handleSubmit, handleCancel }) => {
) : null}
<div className={cx('new-build-form-parameters-fields')}>
<Field.Input
name="newKey"
placeholder="key"
value={parameterState.key}
onChange={handleParameterChange('key')}
/>
<Field.Input
name="newVal"
placeholder="value"
value={parameterState.value}
onChange={handleParameterChange('value')}
Expand Down
4 changes: 3 additions & 1 deletion src/components/shared/bar-chart/bar-chart.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const BarChart = (props) => {
if (data.length < barsShown) {
const barPlaceholders = Array
.from({ length: barsShown - data.length },
(_, i) => ({ number: data.length + i + 1 }));
(_, i) => ({ number: 0 - i }));
return withHeight.concat(barPlaceholders);
}
return withHeight;
Expand Down Expand Up @@ -143,12 +143,14 @@ BarChart.propTypes = {
height: PropTypes.number.isRequired,
onClick: PropTypes.func.isRequired,
minBarWidth: PropTypes.number,
minGapWidth: PropTypes.number,
barsShown: PropTypes.number,
};

BarChart.defaultProps = {
className: '',
minBarWidth: MIN_BAR_WIDTH,
minGapWidth: MIN_GAP_WIDTH,
barsShown: BARS_SHOWN,
};

Expand Down
6 changes: 3 additions & 3 deletions src/hooks/swr/builds.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const updateLatestRepos = (repo) => (latest) => {
};

// activity feed updater
const updateBuilds = async (slug, repo) => {
const updateBuilds = async (slug, repo, newBuild = null) => {
if (`/${repo.namespace}/${repo.name}` !== slug) {
// no need to do anything actually since user
// is on the activity feed of another repo
Expand All @@ -103,7 +103,7 @@ const updateBuilds = async (slug, repo) => {
*/
const path = `arg@"many"@"/api/repos${slug}/builds?page=1&per_page=${MAX_PAGE_LENGTH}"`;
mutateGlobal(path, async (builds) => {
const { build } = repo;
const build = newBuild ?? repo.build;
/* if no cache or no data,
/ just return an array with received build
*/
Expand Down Expand Up @@ -187,5 +187,5 @@ const useStreamBuildEvents = () => {
};

export {
useBuilds, useBuild, useStreamBuildEvents,
useBuilds, useBuild, useStreamBuildEvents, updateBuilds,
};
3 changes: 2 additions & 1 deletion src/hooks/swr/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
useBuild, useBuilds, useStreamBuildEvents,
useBuild, useBuilds, useStreamBuildEvents, updateBuilds,
} from './builds';
import { useLogs, useStreamLogs } from './logs';
import { useRepo, useLatestRepos } from './repo';
Expand All @@ -15,6 +15,7 @@ export {
useSyncAccount,
useBuild,
useBuilds,
updateBuilds,
useBranches,
useDeployments,
useRepo,
Expand Down
16 changes: 12 additions & 4 deletions src/pages/repo/repo.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import classNames from 'classnames/bind';
import PropTypes from 'prop-types';
import React, {
useCallback, useMemo, useContext,
} from 'react';
Expand All @@ -13,7 +14,7 @@ import Button from 'components/shared/button';
import Modal, { useModal } from 'components/shared/modal';
import { AppContext } from 'context';
import { useToast } from 'hooks';
import { useRepo } from 'hooks/swr';
import { useRepo, updateBuilds } from 'hooks/swr';
import NotFound from 'pages/not-found';
import { Routes } from 'routes/routes';
import { ReactComponent as DemoIcon } from 'svg/demo.svg';
Expand Down Expand Up @@ -57,7 +58,7 @@ const getTabProps = ({
const Repo = ({ user }) => {
const { params } = useRouteMatch();
const {
namespace, name, build, stage = 1, step = 1,
namespace, name,
} = params;
const [context] = useContext(AppContext);
const { isRepoNavDisabled } = context;
Expand Down Expand Up @@ -106,16 +107,17 @@ const Repo = ({ user }) => {
}
}
try {
await axiosWrapper(endpoint, {
const newBuild = await axiosWrapper(endpoint, {
method: 'POST',
});
showSuccess('New build has started successfully');
updateBuilds(window.location.pathname, repo, newBuild);
} catch (e) {
showError(`Unable to start a new build: ${e.message}`);
// eslint-disable-next-line no-console
console.warn(e.message);
}
}, [name, namespace, showError, showSuccess]);
}, [name, namespace, showError, showSuccess, repo]);
if (isRepoDataLoading) {
return null;
}
Expand Down Expand Up @@ -214,4 +216,10 @@ const Repo = ({ user }) => {
);
};

Repo.propTypes = {
user: PropTypes.shape({
admin: PropTypes.bool,
}).isRequired,
};

export default Repo;

0 comments on commit 9afe3e2

Please sign in to comment.