Skip to content

Instantly share code, notes, and snippets.

@peterbe
Created November 8, 2023 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterbe/53fa4f89c76587a446e3b376085be6b0 to your computer and use it in GitHub Desktop.
Save peterbe/53fa4f89c76587a446e3b376085be6b0 to your computer and use it in GitHub Desktop.
import { RequestError } from '@octokit/request-error'
import Github from './src/workflows/github.js'
const github = Github()
// https://docs.github.com/rest/reference/git#list-matching-references
export async function listMatchingRefs(owner, repo, ref) {
try {
// if the ref is found, this returns an array of objects;
// if the ref is not found, this returns an empty array
const { data } = await github.git.listMatchingRefs({
owner,
repo,
ref,
})
return data
} catch (err) {
console.log('error getting tree')
throw err
}
}
console.log('LIST.....')
console.time('Using listMatchingRefs')
console.log((await listMatchingRefs('github', 'github', 'heads/master')).length > 0)
console.log((await listMatchingRefs('github', 'github', 'heads/masterxxx')).length > 0)
console.log((await listMatchingRefs('github', 'github', 'heads/ghae')).length > 0)
console.timeEnd('Using listMatchingRefs')
export async function hasMatchingRef(owner, repo, ref) {
try {
await github.git.getRef({
owner,
repo,
ref,
})
return true
} catch (err) {
if (err instanceof RequestError && err.status === 404) {
return false
}
console.log('error getting tree')
throw err
}
}
console.log('HAS.....')
console.time('Using hasMatchingRef')
console.log(await hasMatchingRef('github', 'github', 'heads/master'))
console.log(await hasMatchingRef('github', 'github', 'heads/masterxxx'))
console.log(await hasMatchingRef('github', 'github', 'heads/ghae'))
console.timeEnd('Using hasMatchingRef')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment