Home

Awesome

github: functions module for shellfire

This module provides a simple framework for using GitHub v3 REST API with a shellfire application. It currently supports a subset of features for managing releases. We welcome contributions to increase the coverage.

Compatibility

Overview

To use the API, you first need to initialise it. We use OAuth2 Personal Access Tokens, which you can create yourself.

github_api_v3_initialise "$tokenFilePath"

tokenFilePath should be a path to a file that contains one line with an OAuth2 Personal Access Token in it. You can create these tokens using the settings for your account. On no account should you check in this file! You may want to add a rule to your .gitignore file just to make sure.

You can then make API calls. For example, to retrieve a list of release URLs:-

github_api_v3_releases_list "$owner" "$repo"

url=''
some_event_callback()
{
	if jsonreader_eventMatches ":${jsonreader_path_index}/url" string; then
		echo "Url for release is $eventValue"
	fi
}
jsonreader_parse "$github_api_v3_responseFilePath" some_event_callback

This example uses the jsonreader parser and event handler. For an example of how to handle more complex needs, see the function _github_api_v3_errorMessage_event() in the v3.functions source code.

Importing

To import this module, add a git submodule to your repository. From the root of your git repository in the terminal, type:-

mkdir -p lib/shellfire
cd lib/shellfire
git submodule add "https://github.com/shellfire-dev/github.git"
cd -
git submodule init --update

You may need to change the url https://github.com/shellfire-dev/github.git above if using a fork.

You will also need to add paths - include the module paths.d.

You will also need to import the [curl], urlencode, jsonreader, jsonwriter, unicode and version modules.

Namespace github_api_v3

This namespace contains the core functionality needed to access the API.

To use in code

If calling from another shellfire module, add to your shell code the line

core_usesIn github/api v3

in the global scope (ie outside of any functions). A good convention is to put it above any function that depends on functions in this module. If using it directly in a program, put this line inside the _program() function:-

_program()
{
	core_usesIn github/api v3
	…
}

Functions


github_api_v3_initialise

ParameterValueOptional
tokenFilePathPath to a file that contains one line with an OAuth2 Personal Access Token in it.No
github_api_v3_endpointBase URL of endpoint (for GitHub Enterprise). Defaults to https://api.github.com.Yes

Creates temporary file paths for request and response bodies in github_api_v3_responseFilePath and github_api_v3_requestFilePath (the latter can be overriden by setting curl_uploadFile).


github_api_v3_timestampToEpochSeconds

ParameterValueOptional
timestampTimestamp from GitHubNo

Helper function to convert GitHub timestamps into epoch seconds (ie seconds since the Unix epoch of 1970).

Namespace github_api_v3_releases

This namespace contains functionality to create releases. It is very closely linked to the underlying REST API, and you should refer to that documentation for specific meanings.

To use in code

If calling from another shellfire module, add to your shell code the line

core_usesIn github/api/v3 releases

in the global scope (ie outside of any functions). A good convention is to put it above any function that depends on functions in this module. If using it directly in a program, put this line inside the _program() function:-

_program()
{
	core_usesIn github/api/v3 releases
	…
}

Functions


github_api_v3_releases_list

ParameterValueOptional
ownerRepository ownerNo
repoRepositoryNo

Returns a JSON document listing releases in github_api_v3_responseFilePath. Use the jsonreader module to parse it.


github_api_v3_releases_create

ParameterValueOptional
ownerRepository ownerNo
repoRepositoryNo
tagNametag nameNo
commitishcommitishNo
nameName of this releaseNo
bodyA non empty string of markdownNo
drafttrue or falseNo
prereleasetrue or falseNo

Creates a release.

Sets the variable github_api_v3_header_Location to the location URL of the created release. Sets the variable github_api_v3_releases_id to the release's id. Sets the variable github_api_v3_releases_uploadUrlTemplate to the upload url template for assets.


github_api_v3_releases_edit

ParameterValueOptional
ownerRepository ownerNo
repoRepositoryNo
idRelease idNo
tagNametag nameNo
commitishcommitishNo
nameName of this releaseNo
bodyA non empty string of markdownNo
drafttrue or falseNo
prereleasetrue or falseNo

Edits a release id. Other fields as for github_api_v3_releases_create.


github_api_v3_releases_delete

ParameterValueOptional
ownerRepository ownerNo
repoRepositoryNo
idRelease id (eg from github_api_v3_releases_list())No

Deletes the release.


github_api_v3_releases_uploadAsset

ParameterValueOptional
uploadUrlTemplateRepository ownerNo
filePathPath to file to uploadNo
contentTypeMIME Content-Type of file to uploadNo
labelHuman-friendly file name, effectivelyNo

Uploads a release asset and then changes its label using a PATCH only if the label doesn't match the basename of filePath. uploadUrlTemplate can be obtained from github_api_v3_releases_uploadUrlTemplate after a call to github_api_v3_releases_create().