repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

nushell/modules/package/github.nu

Raw
# If the environment variable `GITHUB_TOKEN` is found, it is passed on as a header
# to the api calls.

# HTTP calls to githubs API.
def api [
	org:string # Owner
	name:string # Repo name
	field:string # Field in the response to get
	path:string = "" # github repo API sub path, must contain leading slash. Can be empty.
] {
	let url = $"https://api.github.com/repos/($org)/($name)($path)"
	mut headers = []
	if ($env | get --ignore-errors GITHUB_TOKEN | is-not-empty) { $headers = [ Authorization $"Bearer ($env.GITHUB_TOKEN)"] }
	try {
		http get --headers $headers $url | get $field
	} catch {
		|error|
		print --stderr $"The github repo ($org)/($name) does not seem to have ($path):($field)!"
		print --stderr $error
	}
}

# Tries to fetch the latests release version of the given repo on github.
# If no release can be found, returns `null`.
export def fetch-latest-release-version [
	org: string
	name: string
] {
	api $org $name "tag_name" "/releases/latest"
}

# Tries to fetch the homepage of a github repo.
# Returns `null`, if none can be found.
export def fetch-homepage [
	org:string
	name:string
] {
	api $org $name "homepage" | default --empty $"https://github.com/($org)/($name)"
}

# Tries to fetch the description of a github repo.
# If none can be found, returns `null`.
export def fetch-description [
	org: string
	name: string
] {
	api $org $name "description"
}

# Tries to fetch the release artifacts from the latest version of given repo.
# Returns a `list<record<string,string,string>` or `null`, if none can be found.
# The fields of the record are `name`, `content_type`, and `browser_download_url`.
export def fetch-release-artifacts [
	org:string
	name:string
] {
	let assets = api $org $name "assets" "/releases/latest"
	if ($assets | is-empty) {return null}
	$assets | select name content_type browser_download_url
}