Skip to content

Instantly share code, notes, and snippets.

@mg6
Last active May 28, 2018 21:54
Show Gist options
  • Save mg6/cfdca04b9254435d3d0cf82bd71b2c75 to your computer and use it in GitHub Desktop.
Save mg6/cfdca04b9254435d3d0cf82bd71b2c75 to your computer and use it in GitHub Desktop.
Draft cleanup
#!/bin/bash
# MIT License
#
# Copyright (c) 2018 Maciej Gamrat <maciej@gamrat.it>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# PURPOSE
# A tool for release draft cleanup. Operates directly on GitHub API v3.
# RUNNING
# export OAUTH_TOKEN=<GitHub developer token>
# ./cleanup-drafts <organization name>
# DEPENDENCIES
# curl jq parallel
set -euo pipefail
base_url="https://api.github.com"
token="${OAUTH_TOKEN}"
export base_url
export token
#
# Filters
#
full_repo_names() {
jq -r '.[].full_name'
}
export -f full_repo_names
draft_ids() {
jq '.[] | select(.draft) | .id'
}
export -f draft_ids
#
# API calls
#
api() {
curl -sSfL \
-H "Authorization: token ${token}" \
-H "Accept: application/vnd.github.v3+json" \
"$@"
}
export -f api
get_repos() {
local organization="$1"
api "${base_url}/orgs/${organization}/repos"
}
export -f get_repos
get_releases() {
local repo="$1"
api "${base_url}/repos/${repo}/releases"
}
export -f get_releases
get_release() {
local repo="$1"
local release_id="$2"
api "${base_url}/repos/${repo}/releases/${release_id}"
}
export -f get_release
delete_release() {
local repo="$1"
local release_id="$2"
echo >&2 "Deleting release ${release_id} in ${repo} repository..."
api -X DELETE "${base_url}/repos/${repo}/releases/${release_id}"
}
export -f delete_release
#
# Artifact maintenance
#
cleanup_drafts() {
local repo="$1"
echo >&2 "Starting draft cleanup for ${repo} repository..."
get_releases "${repo}" \
| draft_ids \
| parallel delete_release "${repo}"
}
export -f cleanup_drafts
cleanup_organization_drafts() {
local organization="$1"
echo >&2 "Starting cleanup for organization ${organization}..."
get_repos "${organization}" \
| full_repo_names \
| parallel cleanup_drafts
}
export -f cleanup_organization_drafts
#
# Main
#
main() {
local org="$1"
cleanup_organization_drafts "${org}"
echo >&2 "Done."
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment