Skip to content

latest-tag-button sometimes fails getting the "ahead-by count" #2927

Closed

Activity

  1. yakov116 commented on Jun 3, 2020

    @yakov116
    Member

    What about something like this?

    const getAheadByCount = cache.function(async (latestTag: string, defaultBranch: string): Promise<number> => {
    	let aheadCount = await fetchDom(`/${getRepoURL()}/releases/tag/${latestTag}`, '.release-header relative-time + a[href*="/compare/"]');
    	if (!aheadCount) {
    		aheadCount = await fetchDom(`/${getRepoURL()}/compare/${latestTag}...${defaultBranch}`, '#js-repo-pjax-container div.overall-summary > ul span');
    	}
    
    	// This text is "4 commits to master since this tag or 4 Commits"
    	return looseParseInt(aheadCount!.textContent!);
    }, {
    	maxAge: 1 / 24, // One hour
    	staleWhileRevalidate: 2,
    	cacheKey: ([latestTag]) => `tag-ahead-by:${getRepoURL()}/${latestTag}`
    });
  2. fregante commented on Jun 3, 2020

    @fregante
    MemberAuthor

    Can't. Compare pages can be pretty heavy and slow as well though, just like the API.

    This page takes a few seconds to load: 1.9.1...master

    For the time being it would be good to just avoid that empty tooltip, for example by adding the tooltipped classes together with aria-label attribute, not before.

  3. fregante commented on Jun 3, 2020

    @fregante
    MemberAuthor

    For the time being it would be good to just avoid that empty tooltip, for example by adding the tooltipped classes together with aria-label attribute, not before.

    Done: e422c8c

  4. yakov116 commented on Jun 3, 2020

    @yakov116
    Member

    Could we make it a silent error?

  5. fregante commented on Jun 6, 2020

    @fregante
    MemberAuthor

    I looked again into GraphQL but, while it has a before query, it appears to be completely ignored 🤷‍♂️

    {
      repository(owner: "sindresorhus", name: "refined-github") {
        ref(qualifiedName: "master") {
          target {
            ... on Commit {
              id
              history(first: 100, before: "e422c8c7fe3f531816d0e0a7867c09b9acc7b266 1") {
                totalCount /* Also useless: it always includes ALL the commits */
                edges {
                  node {
                    message
                  }
                }
              }
            }
          }
        }
      }
    }

    So instead I'm thinking we could:

    1. Get the latest tag’s sha

    2. Look for it in the latest 20 commits on defaultBranchRef

      {
        repository(owner: "sindresorhus", name: "refined-github") {
          defaultBranchRef {
            target {
              ... on Commit {
                history(first: 20) {
                  edges {
                    node {
                      oid
      }}}}}}}}
    3. If found, use its position; if not, just use a number-less <sup>+</sup> in the latest-tag-button

    After all, the point of this information is to know whether we're on the latest commit. Anything above +20 (or missing/incorrect information) isn't particularly useful.

  6. yakov116 commented on Jun 7, 2020

    @yakov116
    Member

    TADA It was a nice try, but commits can be out of order.

    const getAheadByCount = cache.function(async (latestTag: string, defaultBranch: string): Promise<number | undefined> => {
    	const tagPage = await fetchDom(
    		`/${getRepoURL()}/releases/tag/${latestTag}`,
    		'.release-header relative-time + a[href*="/compare/"], .release-header relative-time'
    	);
    	// This text is "4 commits to master since this tag"
    	return tagPage instanceof HTMLAnchorElement ?
    		Number(tagPage.textContent!.replace(/\D/g, '')) :
    		getCommitAheadCountFromApi(tagPage!.attributes.datetime.value, defaultBranch);
    }, {
    	maxAge: 1 / 24, // One hour
    	staleWhileRevalidate: 2,
    	cacheKey: ([latestTag]) => `tag-ahead-by:${getRepoURL()}/${latestTag}`
    });
    
    const getCommitAheadCountFromApi = cache.function(async (date: string, defaultBranch: string): Promise<number> => {
    	const {repository} = await api.v4(`
    		repository(${getRepoGQL()}) {
    			ref(qualifiedName: "${defaultBranch}") {
    				target {
    					... on Commit {
    						history(first: 1, since: "${date}") {
    							totalCount
    						}
    					}
    				}
    			}
    		}
    	`);
    
    	return repository.ref.target.history.totalCount;
    }, {
    	maxAge: 1 / 24, // One hour
    	staleWhileRevalidate: 2,
    	cacheKey: ([latestTag]) => `tag-ahead-by-api:${getRepoURL()}/${latestTag}`
    });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions