Use src in scripts and agents
Your script or agent needs a few values from the forge: issue numbers, a pull request title, the status of a run. The tables that src prints are for people. A script that cuts them apart breaks when a table changes.
src prints JSON with only the fields that you ask for. It filters that JSON with jq, or formats it with a Go template. The flags and the field names are the same as in gh.
1. Ask for the fields with --json
src issue list --json number,title
[
{
"number": 12,
"title": "Login fails on Safari"
},
{
"number": 9,
"title": "Add dark mode"
}
]
- To see the fields of a command, give
--jsonwith no field names.srclists the fields, and exits with 1. - A field that the forge has no value for is
null. - In a terminal, the JSON is indented and in color. In a pipe or a file, it is on one line.
- Most
listandviewcommands have--json.src <command> --helpshows it.
2. Filter with --jq
--jq runs a jq filter on the JSON. jq is built into src, so you install nothing.
src issue list --json number,title --jq '.[] | "#\(.number) \(.title)"'
#12 Login fails on Safari
#9 Add dark mode
- A string result prints as plain text, with no quotes.
- An array or an object prints as JSON.
A CI script can find the failed runs, then read their logs:
src run list --status failure --json databaseId --jq '.[].databaseId'
src run view <run> --log-failed
3. Format text with --template
--template formats the JSON with a Go template, with gh's functions.
src pr list --json number,title,author --template '{{range .}}{{tablerow .number .title .author.login}}{{end}}'
7 Count down to zero alem
5 Add dark mode sam
The functions of gh:
tablerowandtablerenderput the fields in aligned columns.timeagogives the age of a time:{{timeago .createdAt}}prints2 days ago.truncatecuts a text:{{truncate 40 .title}}.colorandautocolorcolor a text in a terminal:{{color "green" .title}}.join,pluck,timefmtandhyperlink, and Go'sprintf,len,eqand more.
This prints the age of each open issue:
src issue list --json number,title,createdAt --template '{{range .}}#{{.number}} {{.title}} ({{timeago .createdAt}}){{"\n"}}{{end}}'
4. Read any REST route with src api
src api sends a request to the REST API with your login. --paginate reads every page of a list:
src api 'repos/{owner}/{repo}/issues' --paginate --jq '.items[].title'
{owner}and{repo}come from the repository of your clone.- A list gives 100 items and a
nextcursor on each page.--paginateasks for the next page until there is none. --jqand--templaterun on each page.--paginateworks withGETonly.
To keep all the pages in one JSON array, add --slurp:
src api 'repos/{owner}/{repo}/issues' --paginate --slurp > issues.json
Each item of the array is one page, with its items. --slurp does not work with --jq or --template.
Done
src issue list --json number --jq '.[].number'
Your script gets the issue numbers, one on each line, and nothing else to cut apart. An agent runs the same commands. To give a script or an agent a login, see scripts and CI.