Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial work #1

Merged
merged 2 commits into from
Mar 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions create-labels
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env bash

set -e

# Script variables
GITHUBORG=""
GITHUBREPO=""

# Include shell helpers
source shell-helpers.sh

# Main functionality of the script
main() {
echo_yellow "Creating labels for the following organization and repository:"
echo_yellow "${TAB}Organization: $GITHUBORG"
echo_yellow "${TAB}Repository: $GITHUBREPO"

# Create labels
create_labels

echo_green "Labels created succesfully!"
}

# Create labels for a specific repo
create_labels() {
while IFS= read -r LABELNAME && IFS= read -r LABELCOLOR; do
echo_yellow "${TAB}Creating label with the following settings:"
echo_yellow "${TABx2}Name: $LABELNAME"
echo_yellow "${TABx2}Color: $LABELCOLOR"

curl -X POST --header "Authorization: token ${GITHUBTOKEN}" \
--header "Content-Type: application/json" \
--data "{\"name\":\"${LABELNAME}\", \"color\":\"${LABELCOLOR}\"}" \
"https://api.github.com/repos/${GITHUBORG}/${GITHUBREPO}/labels"

echo ""
done < <(jq -r 'keys[] as $k | (.[$k].name, .[$k].color)' $(pwd)/labels.json)
}

# Function that outputs usage information
usage() {
cat <<EOF

Usage: $(basename $0) <options>

Description

Options:
-o (required) The Github organization (or user) that the repository resides under
-r (required) The name of the repository
-t A Github personal token, used for authenticating API requests
-h Print this message and quit

EOF
exit 0
}

# Function that verifies required input was passed in
verify_input() {
# Verify required inputs are not empty
[ ! -z "${GITHUBORG}" ] && [ ! -z "${GITHUBREPO}" ] && [ ! -z "${GITHUBTOKEN}" ]
}

# Parse input options
while getopts ":o:r:t:h" opt; do
case "$opt" in
o) GITHUBORG=$OPTARG;;
r) GITHUBREPO=$OPTARG;;
t) GITHUBTOKEN=$OPTARG;;
h) usage;;
\?) echo_red "Invalid option: -${OPTARG}." && usage;;
:) die "Option -${OPTARG} requires an argument.";;
esac
done

# Verify input
! verify_input && echo_red "Missing script options." && usage

# Execute main functionality
main
106 changes: 106 additions & 0 deletions delete-labels
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#!/usr/bin/env bash

set -e

# Script variables
GITHUBORG=""
GITHUBREPO=""
LABELS=""
LABELCOUNT="0"

# Include shell helpers
source shell-helpers.sh

# Main functionality of the script
main() {
echo_yellow "Deleting labels for the following organization and repository:"
echo_yellow "${TAB}Organization: $GITHUBORG"
echo_yellow "${TAB}Repository: $GITHUBREPO"

# Populate labels
get_labels

# Check if there are any labels to delete
if [ -z "${LABELS}" ]; then
echo_red "No labels found in repository ${GITHUBREPO}. Halting execution."
exit 0
fi

echo_yellow "Found ${LABELCOUNT} labels to delete..."

# Delete labels
delete_labels

echo_green "Labels deleted succesfully!"
}

# Gets and populates labels for a given repository
get_labels() {
echo_yellow "Getting labels for repository..."

# Git labels from github
LABELS=$(curl -X GET --header "Authorization: token ${GITHUBTOKEN}" \
--header "Content-Type: application/json" \
"https://api.github.com/repos/${GITHUBORG}/${GITHUBREPO}/labels")

# Store label count
LABELCOUNT=$(echo $LABELS | jq '. | length')
}

# Deletes labels found for a specific repo
delete_labels() {
echo_yellow "Deleting labels from repository..."

while IFS= read -r LABELNAME; do
echo_yellow "${TAB} Delete label: ${LABELNAME}..."

# Remove spaces from label name
LABELNAME=$(echo $LABELNAME | sed -e 's/ /%20/g')

curl -X DELETE --header "Authorization: token ${GITHUBTOKEN}" \
--header "Content-Type: application/json" \
"https://api.github.com/repos/${GITHUBORG}/${GITHUBREPO}/labels/${LABELNAME}"
done < <(echo $LABELS | jq -r 'keys[] as $k | (.[$k].name)')
}

# Function that outputs usage information
usage() {
cat <<EOF

Usage: $(basename $0) <options>

Description

Options:
-o (required) The Github organization (or user) that the repository resides under
-r (required) The name of the repository
-t A Github personal token, used for authenticating API requests
-h Print this message and quit

EOF
exit 0
}

# Function that verifies required input was passed in
verify_input() {
# Verify required inputs are not empty
[ ! -z "${GITHUBORG}" ] && [ ! -z "${GITHUBREPO}" ] && [ ! -z "${GITHUBTOKEN}" ]
}

# Parse input options
while getopts ":o:r:t:h" opt; do
case "$opt" in
o) GITHUBORG=$OPTARG;;
r) GITHUBREPO=$OPTARG;;
t) GITHUBTOKEN=$OPTARG;;
h) usage;;
\?) echo_red "Invalid option: -${OPTARG}." && usage;;
:) die "Option -${OPTARG} requires an argument.";;
esac
done

# Verify input
! verify_input && echo_red "Missing script options." && usage

# Execute main functionality
main
70 changes: 70 additions & 0 deletions labels.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
[
{
"name": "All Comments Addressed",
"color": "006b75"
},
{
"name": "Do Not Merge",
"color": "b60205"
},
{
"name": "Final Review Passed",
"color": "0e8a16"
},
{
"name": "Has Dependencies",
"color": "5319e7"
},
{
"name": "Has Unaddressed Comments",
"color": "d93f0b"
},
{
"name": "In Development",
"color": "fbca04"
},
{
"name": "In QA",
"color": "fbca04"
},
{
"name": "In Review",
"color": "fbca04"
},
{
"name": "Initial Review Passed",
"color": "0e8a16"
},
{
"name": "Needs Final Review",
"color": "b60205"
},
{
"name": "Needs Rebase",
"color": "e99695"
},
{
"name": "Pending CI",
"color": "fef2c0"
},
{
"name": "QA Failed",
"color": "b60205"
},
{
"name": "QA Passed",
"color": "0e8a16"
},
{
"name": "Ready for QA",
"color": "c2e0c6"
},
{
"name": "Ready for Review",
"color": "c2e0c6"
},
{
"name": "Tests Failing",
"color": "b60205"
}
]
32 changes: 32 additions & 0 deletions shell-helpers.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env bash

# Color variables
readonly GREEN='\033[0;32m'
readonly RED='\033[0;31m'
readonly YELLOW='\033[1;33m'
readonly NC='\033[0m' # No Color

# Spacing variables
readonly TAB=' '
readonly TABx2=' '

# Function to handle exiting with an error
die() {
echo_red >&2 "$@"
exit 1
}

# Function used to color echo text in green
echo_green() {
echo -e "${GREEN}$@${NC}"
}

# Function used to color echo text in red
echo_red() {
echo -e "${RED}$@${NC}"
}

# Function used to color echo text in yellow
echo_yellow() {
echo -e "${YELLOW}$@${NC}"
}