all repos — min @ 55bddeb1e0244d56289057cabf2eb47813ef6fe9

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
#!/usr/bin/env min

; Read files
".env.yml" fread from-yaml :vars
"min.yml" fread from-yaml :config
"next-release.md" fread escape :release-body
"tasks/templates/draft-release.json" fread :draft-release-template 

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

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

(
  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
) :handle-errors
(
  :data
  data /endpoint :endpoint
  "api" :subdomain
  (data ?subdomain) (data /subdomain @subdomain) when
  "https://$#.github.com/repos/h3rald/min$#" (subdomain endpoint) =% :url
  {}
    url %url
    data /method %method
    (data ?headers) 
      (data /headers %headers) 
      (headers %headers) 
    if
    (data ?body) (data /body %body) when
  request :response
  response /status :status
  response /body :body
  handle-errors 
  (body length 0 >) (body from-json) ({}) if
) :gh-req

(
  :data
  data /id :id
  {}
    "/releases/$#/assets" (id) =% %endpoint
    "GET" %method
  gh-req
) :get-assets

(
 config get-assets =assets
 config /id :id
 assets (/id) map =assets-ids
 assets-ids (
  :asset
  {}
    "/releases/assets/$#" (asset) =% %endpoint
    "DELETE" %method
  gh-req
 ) foreach
) :delete-assets

; Module symbols
(
  {}
    "/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
) %draft
(
  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
) %update
(
  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 
  assets
) %assets
(
  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 
  ) foreach
) %upload
+github-tasks