message: Determine `repo-age` with its first commit
diff: @@ -5,8 +5,8 @@ import select from 'select-dom';
  import repoIcon from 'octicon/repo.svg';
  import elementReady from 'element-ready';
  import features from '../libs/features';
  -import * as api from '../libs/api';
  -import {getRepoGQL, getRepoURL} from '../libs/utils';
  +import fetchDom from '../libs/fetch-dom';
  +import {getRepoURL} from '../libs/utils';
  const dateFormatter = new Intl.DateTimeFormat('en-US', {
  year: 'numeric',
  @@ -14,20 +14,41 @@ const dateFormatter = new Intl.DateTimeFormat('en-US', {
  day: 'numeric'
  });
  -const getRepoCreationDate = cache.function(async (): Promise<string> => {
  - const {repository} = await api.v4(`
  - repository(${getRepoGQL()}) {
  - createdAt
  +const getFirstCommitDate = cache.function(async (): Promise<string | undefined> => {
  + const commitInfo = await elementReady<HTMLAnchorElement | HTMLScriptElement>('a.commit-tease-sha, include-fragment.commit-tease');
  + const commitUrl = commitInfo instanceof HTMLAnchorElement ? commitInfo.href : commitInfo!.src;
  + const commitSha = commitUrl.split('/').pop()!;
  +
  + const commitsCount = Number(select('li.commits .num')!.textContent!.replace(',', ''));
  +
  + // Returning undefined will make sure that it is not cached. It will check again for commits on the next load.
  + // Reference: https://github.com/fregante/webext-storage-cache/#getter
  + if (commitsCount === 0) {
  + return;
  }
  - `);
  - return repository.createdAt;
  + if (commitsCount === 1) {
  + return select('.commit-tease-sha + span relative-time')!.attributes.datetime.value;
  + }
  +
  + const relativeTime = await fetchDom(
  + `${getRepoURL()}/commits?after=${commitSha}+${commitsCount - 2}`,
  + 'ol:last-child > li.commits-list-item relative-time'
  + );
  +
  + return relativeTime!.attributes.datetime.value;
  }, {
  cacheKey: () => __featureName__ + ':' + getRepoURL()
  });
  async function init(): Promise<void> {
  - const date = new Date(await getRepoCreationDate());
  + const firstCommitDate = await getFirstCommitDate();
  +
  + if (!firstCommitDate) {
  + return;
  + }
  +
  + const date = new Date(firstCommitDate);
  // `twas` could also return `an hour ago` or `just now`
  const [value, unit] = twas(date.getTime())
  @@ -36,7 +57,7 @@ async function init(): Promise<void> {
  .split(' ');
  const element = (
  - <li className="text-gray" title={`Repository created on ${dateFormatter.format(date)}`}>
  + <li className="text-gray" title={`First commit dated ${dateFormatter.format(date)}`}>
  {repoIcon()} <span className="num text-emphasized">{value}</span> {unit} old
  </li>
  );
  