Created
June 12, 2024 07:27
-
-
Save Genzer/0904589ee93e7f5abd01e86a30ccc882 to your computer and use it in GitHub Desktop.
Unarchive a GitHub repository using CLI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -ueo pipefail | |
get_repo_id() { | |
local -r org="$1" | |
local -r repo_name="$2" | |
cat <<_PAYLOAD_ \ | |
| curl \ | |
--silent \ | |
--fail \ | |
--show-error \ | |
--url https://api.github.com/graphql \ | |
-H 'content-type: application/json' \ | |
-K <(echo "--header \"Authorization: Bearer $GITHUB_TOKEN\"") \ | |
--data @- \ | |
| jq -r '.data.repository.id' | |
{ | |
"query": "query (\$org: String!, \$repo: String!) {repository(owner: \$org, name: \$repo) { id } }", | |
"variables": { | |
"org": "$org", | |
"repo": "$repo_name" | |
} | |
} | |
_PAYLOAD_ | |
} | |
unarchive() { | |
local -r repo_id=$1 | |
cat <<_PAYLOAD_ \ | |
| curl \ | |
--silent \ | |
--fail \ | |
--show-error \ | |
--url https://api.github.com/graphql \ | |
-K <(echo "--header \"Authorization: Bearer $GITHUB_TOKEN\"") \ | |
-H 'content-type: application/json' \ | |
--data @- \ | |
| jq -r | |
{ | |
"query": "mutation UnArchiveRepository(\$repo: ID!) {\ | |
unarchiveRepository(input:{clientMutationId: \"true\",repositoryId: \$repo}) {\ | |
repository {\ | |
name,\ | |
isArchived,\ | |
}\ | |
}\ | |
}", | |
"variables": { | |
"repo": "$repo_id" | |
} | |
} | |
_PAYLOAD_ | |
} | |
main() { | |
if (( "$#" == 0 )); then | |
echo "Missing GitHub Org and Repository." | |
echo "Usage:" | |
echo "$ bash unarchive_github_repo.sh ORG REPO" | |
exit 0 | |
fi | |
if [[ -z "${GITHUB_TOKEN+x}" ]]; then | |
echo "Missing GITHUB_TOKEN environment variable" | |
exit 1 | |
fi | |
local org="$1" | |
local repo_name="$2" | |
local repo_id; | |
repo_id="$(get_repo_id "$org" "$repo_name")" | |
unarchive "$repo_id" | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment