all repos — min @ d0d644faeeecdd1812927dbd61b3061ab3e81187

A small but practical concatenative programming language.

tasks/github.min

 1
#!/usr/bin/env min

"_helpers" require :helpers 'helpers import

"next-release.md" fread escape :release-body
"tasks/data/draft-release.json" fread :draft-release-template 

config "version" dget :min-version
env "github-token" dget :token
draft-release-template ("version" min-version "body" release-body) =% :draft-release-body

{}  
  "application/vnd.github.v3+json" "Accept" dset
  "token $#" (token) =% "Authorization" dset  
:headers

(
  symbol handle-errors
  (dict :response ==>)
  (
    response to-json "response.json" fwrite
    response "status" dget :status
    (status 300 >) (
      response "body" dget from-json :body
      body "message" dget :message
      status string @status
      "Error $#: $#" (status message) =% error status int exit
    ) when
  )
) ::
;; Handles HTTP errors.

(
  symbol gh-req
  (dict :data ==> dict|quot :result)
  (
    data "endpoint" dget :endpoint
    "api" :subdomain
    (data 'subdomain dhas?) (data "subdomain" dget @subdomain) when
    "https://$#.github.com/repos/h3rald/min$#" (subdomain endpoint) =% :url
    {}
      url "url" dset
      data "method" dget "method" dset
      (data 'headers dhas?) 
        (data "headers" dget "headers" dset) 
        (headers "headers" dset) 
      if
      (data 'body dhas?) (data "body" dget "body" dset) when
    request :response
    response "status" dget :status
    response "body" dget :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" dget :id
    {}
      "/releases/$#/assets" (id) =% "endpoint" dset
      "GET" "method" dset
    gh-req @result
  )
) ::
;; Retrieve the assets from an existing Github release.

(
  symbol delete-assets
  (dict :config ==>)
  (
    config get-assets :assets
    config "id" dget :id
    assets ("id" dget) map :assets-ids
    assets-ids (
      :asset
      {}
        "/releases/assets/$#" (asset) =% "endpoint" dset
        "DELETE" "method" dset
      gh-req pop
    ) foreach
  )
) ::
;; Delete all assets from an existing Github release.

#|| Tasks ||#

(
  symbol draft
  (==>)
  (
    {}
    "/releases" "endpoint" dset
    "POST" "method" dset
    draft-release-body "body" dset
    gh-req "id" dget string :id
    ;; Save Release ID to min.yml
    config id "id" dset to-yaml "min.yml" fwrite
    "Draft release v$# ($#) created successfully" (min-version id) =% notice!
  )
) ::
;; Creates a new draft release on Github.

(
  symbol update
  (==>)
  (
    config "id" dget :id
  {}
    "/releases/$#" (id) =% "endpoint" dset
    "PATCH" "method" dset
    draft-release-body "body" dset
  gh-req "id" dget string :id
  "Draft release v$# ($#) updated successfully" (min-version id) =% 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) =% notice!
    assets ("name" dget () cons "- $#" swap % notice!) foreach 
  )
) ::
;; Retrieve a list of all the assets of the current draft Github release.

(
  symbol upload
  (==>)
  (
    config "id" dget :id
    config delete-assets
    pwd ls ("\\.zip$" match?) filter 
    (
      filename :file
      "Uploading: $#" (file) =% notice!
      file fread :body
      headers "application/zip" "Content-Type" dset :asset-headers
      {}
        "/releases/$#/assets?name=$#" (id file) =% "endpoint" dset
        asset-headers "headers" dset
        "uploads" "subdomain" dset
        "POST" "method" dset
        body "body" dset
      gh-req pop
    ) foreach 
  )
) ::
;; Uploads all assets to the current draft Github release, deleting any existing asset.