1
0
mirror of synced 2025-12-20 02:19:14 -05:00
Files
docs/script/helpers/git-utils.js
Kevin Heis 42e785b0a8 Migrate CommonJS to ESM (#20301)
* First run of script

* Get the app running --- ish

* Get NextJS working

* Remove `node:`

* Get more tests passing in unit directory

* Update FailBot test to use nock

* Update test.yml

* Update Dockerfile

* tests/content fixes

* Update page.js

* Update build-changelog.js

* updating tests/routing

* Update orphan-tests.js

* updating tests/rendering

* Update .eslintrc.js

* Update .eslintrc.js

* Install jest/globals

* "linting" tests

* staging update to server.mjs

* Change '.github/allowed-actions.js' to a ESM export

* Lint

* Fixes for the main package.json

* Move Jest to be last in the npm test command so we can pass args

* Just use 'npm run lint' in the npm test command

* update algolia label script

* update openapi script

* update require on openapi

* Update enterprise-algolia-label.js

* forgot JSON.parse

* Update lunr-search-index.js

* Always explicitly include process.cwd() for JSON file reads pathed from project root

* update graphql/update-files.js script

* Update other npm scripts using jest to pass ESM NODE_OPTIONS

* Update check-for-enterprise-issues-by-label.js for ESM

* Update create-enterprise-issue.js for ESM

* Import jest global for browser tests

* Convert 'script/deploy' to ESM

Co-authored-by: Grace Park <gracepark@github.com>
Co-authored-by: James M. Greene <jamesmgreene@github.com>
2021-07-14 13:49:18 -07:00

129 lines
3.1 KiB
JavaScript

#!/usr/bin/env node
import xGithub from './github.js'
const github = xGithub()
// https://docs.github.com/rest/reference/git#get-a-reference
export async function getCommitSha (owner, repo, ref) {
try {
const { data } = await github.git.getRef({
owner,
repo,
ref
})
return data.object.sha
} catch (err) {
console.log('error getting tree')
throw (err)
}
}
// 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)
}
}
// https://docs.github.com/rest/reference/git#get-a-commit
export async function getTreeSha (owner, repo, commitSha) {
try {
const { data } = await github.git.getCommit({
owner,
repo,
commit_sha: commitSha
})
return data.tree.sha
} catch (err) {
console.log('error getting tree')
throw (err)
}
}
// https://docs.github.com/rest/reference/git#get-a-tree
export async function getTree (owner, repo, ref, allowedPaths = []) {
const commitSha = await getCommitSha(owner, repo, ref)
const treeSha = await getTreeSha(owner, repo, commitSha)
try {
const { data } = await github.git.getTree({
owner,
repo,
tree_sha: treeSha,
recursive: 1
})
// only return files that match the patterns in allowedPaths
// skip actions/changes files
return data.tree
} catch (err) {
console.log('error getting tree')
throw (err)
}
}
// https://docs.github.com/rest/reference/git#get-a-blob
export async function getContentsForBlob (owner, repo, blob) {
const { data } = await github.git.getBlob({
owner,
repo,
file_sha: blob.sha
})
// decode blob contents
return Buffer.from(data.content, 'base64')
}
// https://docs.github.com/rest/reference/repos#get-repository-content
export async function getContents (owner, repo, ref, path) {
try {
const { data } = await github.repos.getContent({
owner,
repo,
ref,
path
})
// decode contents
return Buffer.from(data.content, 'base64').toString()
} catch (err) {
console.log(`error getting ${path} from ${owner}/${repo} at ref ${ref}`)
throw (err)
}
}
// https://docs.github.com/en/rest/reference/pulls#list-pull-requests
export async function listPulls (owner, repo) {
try {
const { data } = await github.pulls.list({
owner,
repo,
per_page: 100
})
return data
} catch (err) {
console.log(`error listing pulls in ${owner}/${repo}`)
throw (err)
}
}
export async function createIssueComment (owner, repo, pullNumber, body) {
try {
const { data } = await github.issues.createComment({
owner,
repo,
issue_number: pullNumber,
body
})
return data
} catch (err) {
console.log(`error creating a review comment on PR ${pullNumber} in ${owner}/${repo}`)
throw (err)
}
}