Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add cache #66

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ But the comparison isn't quite as strict, generally leading to a shorter list of
* `--commit-url`:A URL template which will be used to generate commit URLs for a repository not hosted in GitHub. `{ref}` is the placeholder that will be replaced with the commit, i.e. `--commit-url=https://gitlab.com/myUser/myRepo/commit/{ref}`. `{ghUser}` and `{ghRepo}` are available if they can be derived from package.json (Gitlab and Bitbucket URLs should be understood in package.json).
* `--user`: Override the auto-detected GitHub user/org derived from package.json
* `--repo`: Override the auto-detected GitHub repository name derived from package.json
* `--cache`: Cache references to commits excluded from a comparison to avoid hitting GitHub APIs again.

## License

Expand Down
43 changes: 42 additions & 1 deletion branch-diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const ghId = {
user: pkgId.user || 'nodejs',
repo: pkgId.name || 'node'
}
const cacheFilename = path.resolve('.branch-diff-cache.json')
let excludedLabelsCache = new Map()

function replace (s, m) {
Object.keys(m).forEach(function (k) {
Expand Down Expand Up @@ -75,13 +77,21 @@ async function diffCollected (options, branchCommits) {

let list = branchCommits[1].filter((commit) => !isInList(commit))

list = list.filter((commit) => !excludedLabelsCache.has(commit.sha))

await collectCommitLabels(list)

if (options.excludeLabels.length > 0) {
list = list.filter((commit) => {
return !commit.labels || !commit.labels.some((label) => {
const included = !commit.labels || !commit.labels.some((label) => {
return options.excludeLabels.indexOf(label) >= 0
})
// excluded commits will be stored in a cache to avoid hitting the
// GH API again for as long as the cache option is active
if (!included) {
excludedLabelsCache.set(commit.sha, 1)
}
return included
})
}

Expand Down Expand Up @@ -156,6 +166,27 @@ async function main () {
ghId.repo = argv.repo
}

if (argv.cache) {
let cacheFile
try {
cacheFile = fs.readFileSync(cacheFilename, 'utf8')
} catch (err) {
console.log(`No cache file found. A new file:
${path.resolve(cacheFilename)}
will be created when completing a successful branch-diff run.
`)
}
if (cacheFile) {
const cacheData = JSON.parse(cacheFile)
const excludedLabels = cacheData.excludedLabels
if (excludedLabels && excludedLabels.length >= 0) {
excludedLabelsCache = new Map(excludedLabels.map(i => ([i, 1])))
} else {
console.error('Missing excludedLabels on cache file.')
}
}
}

const options = {
group,
excludeLabels,
Expand All @@ -169,6 +200,16 @@ async function main () {
}

await processCommits(argv, ghId, list)

// only stores to cache on success
if (argv.cache) {
fs.writeFileSync(
cacheFilename,
JSON.stringify({
excludedLabels: [...excludedLabelsCache.keys()],
}, null, 2)
)
}
}

main().catch((err) => {
Expand Down
Loading