all repos — min @ 86540c8ac7829e4d00302d8a05fbbf89aa35b8f3

A small but practical concatenative programming language.

tasks/github.min

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
#!/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 :min-version
env /github-token :token
draft-release-template ("version" min-version "body" release-body) =% :draft-release-body

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

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

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

#| Tasks |#

(
  symbol draft
  (==>)
  (
    {}
    "/releases" %endpoint
    "POST" %method
    draft-release-body %body
    gh-req /id string :id
    ; Save Release ID to min.yml
    config id %id 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 :id
  {}
    "/releases/$#" (id) =% %endpoint
    "PATCH" %method
    draft-release-body %body
  gh-req /id 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 () cons "- $#" swap % notice!) foreach 
  )
) ::
;; Retrieve a list of all the assets of the current draft Github release.

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