Skip to content

Add add-tag-to-commits feature - #2026

Merged
sindresorhus merged 21 commits into
refined-github:masterfrom
dotconnor:add-tags-to-commits
Oct 28, 2019
Merged

sindresorhus merged 21 commits into
refined-github:masterfrom
dotconnor:add-tags-to-commits

Conversation

@dotconnor

@dotconnor dotconnor commented May 12, 2019 •

Copy link
Copy Markdown
Contributor

Note: For some reason, the API returns empty data on some tags

Closes #807


IssueHunt Summary

Referenced issues

This pull request has been submitted to:


IssueHunt has been backed by the following sponsors. Become a sponsor

@dotconnor dotconnor changed the title feat: add tags to commit list Add add-tag-to-commits feature May 12, 2019

@fregante fregante left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread source/features/add-tag-to-commits.tsx Resolved
Comment thread source/features/add-tag-to-commits.tsx Resolved
@dotconnor

Copy link
Copy Markdown
Contributor Author

Note: For some reason, the API returns empty data on some tags

The API only returns the Tag type on annotated tags and not lightweight ones for example: 123425d, and from what I've can tell you can't retrieve the tag name for the Github API.

@fregante

Copy link
Copy Markdown
Member

The API only returns the Tag type on annotated tags and not lightweight ones

This works: https://stackoverflow.com/a/53923870/288906

Screenshot 2019-05-25 at 16 26 03

Compared to the query in this PR:

Screenshot 2019-05-25 at 16 26 11

@fregante fregante left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we now have the cache, can you pick this up again?

It will need a couple of big changes:

  • first look into the cache, they need to be stored as commit: [tag, tag]
  • then query pages as needed

Perhaps generators would be useful here:

yield cache; // Record<string, string[]>

yield getPage(); // also needs to return the same format that's already cached

while hasNextPage
	yield getPage(next page)

and then progressively look for the current page's commits in the cache and then next pages, excluding them until:

  • the pages are finished, or
  • the commitsOnPage is empty
commitsOnPage = select.all(...)
for (page /* one of which could be the cache */ of generator) {
	for (commit of commitsOnPage) { // loop through remaining commits
		if (page[commit]) {
			// add tags from page[commit]
			commitsOnPage.delete(commit)
		}
	}
}

Thoughts?

Comment thread source/features/add-tag-to-commits.tsx Resolved
Comment thread source/features/add-tag-to-commits.tsx Resolved
Comment thread source/features/add-tag-to-commits.tsx Resolved
@dotconnor

Copy link
Copy Markdown
Contributor Author

I hate the concept of pages because there is no way to guarantee you get the same response in each request. Therefore caching based on pages would also be problematic, they would have to be very short-lived because the first page will always change anytime a new tag is created, otherwise the feature will become unreliable. Heres what I think:

  1. Keep all the commits and their tags in a single object for each repo, being kept in the cache for a longer amount of time.
commitsOnPage = select.all()
let tags = cache.get()
while (not all commitsOnPage in tags) {
	tags = tags + getMoreTags()
}
cache.set(tags)
for (commit of commitsOnPage) {
	// add tags[commit] to page
}

Cons:

  • If another tag is added to commit, it may not show for a while
  1. Keep a different cache item for each commit, and only fetch the remaining commits:
commitsOnPage = select.all()
for (commit of commitsOnPage) {
	tag = cache.get(commit) // if the commit was found while fetching the previous commit it will be in the cache now
	if (!tag) {
      tag = getTagForCommit(commit)
    }
    // add tag to page
}
function getTagForCommit(commit) {
	const tags = {};
	while (!commitFound) {
		tags = {...tags, apiCall()}
        for (tag of tags) {
        	cache.set(tag)
		}
        if (tags[commit]) {
        	return tags[commit]
        }
	}
	return []
}

@fregante

fregante commented Jun 8, 2019 •

Copy link
Copy Markdown
Member

The cache would not be per page, but per commit: That doesn’t change. For every fetched page, you can extend the cache with new commit->tags matches.

However… I just realized that neither of our solutions actually limits the pages fetched because both keep fetching pages until all the commits on the page are tagged… which is wrong. Not all commits will have a tag so the code will always fetch all the pages looking for a match.

I guess this search would have to be even smarter and complicated because the search has to stop when it finds a tag that is on a commit older than what’s on the page.

🤷‍♂️

@fregante

fregante commented Jun 8, 2019 •

Copy link
Copy Markdown
Member

Unless you have a better idea, let’s keep it easy for now:

  • Fetch pages until the fetched page has some overlap with the cache.

The cache should already have all the tags, except the very latest. When you fetch a page with tag already in the cache (i.e. overlap), it can stop fetching more pages.

Also perhaps a “first run” should avoid any fetch if the current page is found “in the middle” of the cache.

@dotconnor

Copy link
Copy Markdown
Contributor Author

We can get the timestamp from tag/commit and also ask for the time of the last commit on the page:

{
  repository(owner: "sindresorhus", name: "refined-github") {
    refs(first: 100, refPrefix: "refs/tags/", orderBy: {field: TAG_COMMIT_DATE, direction: DESC}) {
      pageInfo {
        hasNextPage
        endCursor
      }
      nodes {
        name
        target {
          commitResourcePath
          ... on Tag {
            tagger {
              date
            }
          }
          ... on Commit {
            committedDate
          }
        }
      }
    }
    object(expression: "9b2acbbc0023c5df62c55b2595febd8277d652eb") {
      ... on Commit {
        committedDate
      }
    }
  }
}

If the date of the last commit is younger than the last tag then stop looking for tags?

@fregante

fregante commented Jun 8, 2019

Copy link
Copy Markdown
Member

I didn’t think about the date, that works too. However I’m trying to find the least complex solution and maybe having to keep track of dates as well might be more complicated than the last solution I proposed.

Thoughts?

@dotconnor

dotconnor commented Jun 8, 2019 •

Copy link
Copy Markdown
Contributor Author

I'll try both and see which one works better.

Found a possible problem in your solution, using nodejs/node as a test page, using the cache overlap solution you still will fetch all the pages. Comparing dates you will only fetch the first one.

Both of them also share a problem but I don't know how major they will be: If we stumble upon a commit not in the cache, no matter what "page" the commit appears on, we will still have to fetch all pages leading up to that page. This should be minor unless you go back a few pages in the UI (I don't know why you would do this though.)

Also, if there are two tags on the same commit separated by a page, there is a chance that only one of these tags will appear.

@dotconnor

Copy link
Copy Markdown
Contributor Author

@bfred-it Since we are keeping each commit as a separate cache entry, it would be nice to have a cache function to get/set multiple at the same time:

cache.getAll([...commits])

Maybe convert our current API to mimic the native one while still adding expiration

@fregante

fregante commented Jun 8, 2019 •

Copy link
Copy Markdown
Member

Found a possible problem in your solution, using nodejs/node as a test page, using the cache overlap solution you still will fetch all the pages. Comparing dates you will only fetch the first one.

Good point, my solution will fetch all the pages at first to fill the cache, dates can allow us to be more selective.

Both of them also share a problem but I don't know how major they will be: If we stumble upon a commit not in the cache, no matter what "page" the commit appears on, we will still have to fetch all pages leading up to that page. This should be minor unless you go back a few pages in the UI (I don't know why you would do this though.)

Hmmm no, because as I suggested here we stop as soon as there's any overlap, we don't look for every commit.

Also, if there are two tags on the same commit separated by a page, there is a chance that only one of these tags will appear.

My later solution fetches all the tags at first, it doesn't have this problem.

A date-based solution would not suffer this problem either, unless this commit is the last on the current page (github.com) as well as the last tag on the current tag page (api). It's a rare occurrence, I'd say, especially considering how rarely a commit has multiple tags.

@bfred-it Since we are keeping each commit as a separate cache entry, it would be nice to have a cache function to get/set multiple at the same time:

The cache should be grouped by repo because it's easier to get and set:

cache.set('tags:sindresorhus/refined-github', {
	'eb9feb9': ['v1'],
	'a0ce6e6': ['v2', 'v2-beta']
});

There's no way to query the storage by "key starts with abc:*"

@dotconnor

dotconnor commented Jun 8, 2019 •

Copy link
Copy Markdown
Contributor Author

Okay so we're back to the two different solutions

  • fetch all the tags on the first run
    • large upfront cost
  • fetch based on the dates
    • large cost down the road if you wander too far back into the past

@fregante

fregante commented Jun 8, 2019

Copy link
Copy Markdown
Member

Dates sound good, if the resulting code isn't too complex. Do you think you'll also have to store the commit date in the cache for this to work?

cache.set('tags:sindresorhus/refined-github', {
	'eb9feb9': {tags: ['v1'], date},
	'a0ce6e6': {tags: ['v2', 'v2-beta'], date}
});

@dotconnor

Copy link
Copy Markdown
Contributor Author

I have it working without storing the dates, but I also store when a commit has no tags:

cache.set('tags:sindresorhus/refined-github', {
	'eb9feb9': ['v1'],
	'a0ce6e6': ['v2', 'v2-beta'],
    'cc2c967': []
});

@fregante

fregante commented Jun 8, 2019 •

Copy link
Copy Markdown
Member

I also store when a commit has no tags:

I thought about that, but wouldn't that get too heavy? Some repos have maaany commits. Let's see it, if you already wrote it

@dotconnor

Copy link
Copy Markdown
Contributor Author

I also store when a commit has no tags

I did this to avoid an API call if every commit is cached, the same effect can be achieved other ways I believe.

I thought about that, but wouldn't that get too heavy? Some repos have maaany commits.

Yeah storing it in a single key would make it a little heavy, since when you add a key to it, it will basically never get expired.

That's why I assumed we would keep a different key for each commit that way you can expire the commits that you no longer see.

Let's see it, if you already wrote it

I did 😃, cleaning up lint things

@jerone jerone left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread source/features/add-tag-to-commits.tsx Resolved

@fregante fregante left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To collaborators: Feel free to merge if it passes your review

@sindresorhus

Copy link
Copy Markdown
Member

@dotconnor Can you fix the merge conflict?

@sindresorhus
sindresorhus merged commit 9f3c0c7 into refined-github:master Oct 28, 2019
@sindresorhus

Copy link
Copy Markdown
Member

Thanks for working on this, @dotconnor 👌

sindresorhus added a commit that referenced this pull request Oct 28, 2019
@fregante

fregante commented Nov 9, 2019 •

Copy link
Copy Markdown
Member

This is pretty useful even to Refined GitHub, when looking at the commit log:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

Show tags in commit history

4 participants