tasks/github.min
1 |
#!/usr/bin/env min
"_helpers" require :helpers 'helpers import
"next-release.md" fs.read escape :release-body
"tasks/data/draft-release.json" fs.read :draft-release-template
config "version" dict.get :min-version
env "github-token" dict.get :token
draft-release-template ("version" min-version "body" release-body) =% :draft-release-body
{}
"application/vnd.github.v3+json" "Accept" dict.set
"token $#" (token) =% "Authorization" dict.set
:headers
(
symbol handle-errors
(dict :response ==>)
(
response to-json "response.json" fs.write
response "status" dict.get :status
(status 300 >) (
response "body" dict.get from-json :body
body "message" dict.get :message
status string @status
"Error $#: $#" (status message) =% io.error status int exit
) when
)
) ::
;; Handles HTTP errors.
(
symbol gh-req
(dict :data ==> dict|quot :result)
(
data "endpoint" dict.get :endpoint
"api" :subdomain
(data 'subdomain dict.has?) (data "subdomain" dict.get @subdomain) when
"https://$#.github.com/repos/h3rald/min$#" (subdomain endpoint) =% :url
{}
url "url" dict.set
data "method" dict.get "method" dict.set
(data 'headers dict.has?)
(data "headers" dict.get "headers" dict.set)
(headers "headers" dict.set)
if
(data 'body dict.has?) (data "body" dict.get "body" dict.set) when
request :response
response "status" dict.get :status
response "body" dict.get :body
response handle-errors
(body length 0 >) (body from-json) ({}) if
@result
)
) ::
;; Execute a request using the Github API.
(
symbol get-assets
(dict :data ==> quot :result)
(
data "id" dict.get :id
{}
"/releases/$#/assets" (id) =% "endpoint" dict.set
"GET" "method" dict.set
gh-req @result
)
) ::
;; Retrieve the assets from an existing Github release.
(
symbol delete-assets
(dict :config ==>)
(
config get-assets :assets
config "id" dict.get :id
assets ("id" dict.get) map :assets-ids
assets-ids (
:asset
{}
"/releases/assets/$#" (asset) =% "endpoint" dict.set
"DELETE" "method" dict.set
gh-req stack.pop
) foreach
)
) ::
;; Delete all assets from an existing Github release.
#|| Tasks ||#
(
symbol draft
(==>)
(
{}
"/releases" "endpoint" dict.set
"POST" "method" dict.set
draft-release-body "body" dict.set
gh-req "id" dict.get string :id
;; Save Release ID to min.yml
config id "id" dict.set to-yaml "min.yml" fs.write
"Draft release v$# ($#) created successfully" (min-version id) =% io.notice!
)
) ::
;; Creates a new draft release on Github.
(
symbol update
(==>)
(
config "id" dict.get :id
{}
"/releases/$#" (id) =% "endpoint" dict.set
"PATCH" "method" dict.set
draft-release-body "body" dict.set
gh-req "id" dict.get string :id
"Draft release v$# ($#) updated successfully" (min-version id) =% io.notice!
)
) ::
;; Updates the text of the current draft Github release.
(
symbol assets
(==>)
(
config get-assets =assets
assets size :total
"are" :verb
(total 1 ==) ("is" @verb) when
"There $# $# assets in release v$#" (verb total min-version) =% io.notice!
assets ("name" dict.get () stack.cons "- $#" stack.swap % io.notice!) foreach
)
) ::
;; Retrieve a list of all the assets of the current draft Github release.
(
symbol upload
(==>)
(
config "id" dict.get :id
config delete-assets
sys.pwd sys.ls ("\\.zip$" match?) filter
(
fs.filename :file
"Uploading: $#" (file) =% io.notice!
file fs.read :body
headers "application/zip" "Content-Type" dict.set :asset-headers
{}
"/releases/$#/assets?name=$#" (id file) =% "endpoint" dict.set
asset-headers "headers" dict.set
"uploads" "subdomain" dict.set
"POST" "method" dict.set
body "body" dict.set
gh-req stack.pop
) foreach
)
) ::
;; Uploads all assets to the current draft Github release, deleting any existing asset.
|