-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil
executable file
·162 lines (128 loc) · 4.92 KB
/
util
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
160
161
162
#!/bin/bash
MY_DIR="$(dirname "$0")"
ROOT_DIR=$(cat "$MY_DIR/root-dir.albert.txt")
check_access_token(){
if [[ "$ACCESS_TOKEN" == "" && -f "$HOME/.github_token" ]]; then
ACCESS_TOKEN=$(cat "$HOME/.github_token")
fi
if [[ "$ACCESS_TOKEN" == "" ]]; then
echo "Please set env variable ACCESS_TOKEN to a Github access token or store it in ~/.github_token; go to https://github.com/settings/tokens to create one."
exit 1;
fi
}
get_repos(){
TEAM_ID=$(cat "$MY_DIR/team-id.albert.txt")
# filter out repos our team is not admin of; otherwise, e.g. security-configuration would appear in the list as of now (2017-12)
REPO_NAME_FILTER="map(select((.permissions.admin == true) and (.archived == false))) | map(.name) | .[]"
http GET "https://api.github.com/teams/$TEAM_ID/repos?per_page=100&access_token=$ACCESS_TOKEN" | jq -r "$REPO_NAME_FILTER" | sort
}
# TODO: this is almost same as get_repos
get_archived_repos(){
TEAM_ID=$(cat "$MY_DIR/team-id.albert.txt")
REPO_NAME_FILTER="map(select((.permissions.admin == true) and (.archived == true))) | map(.name) | .[]"
http GET "https://api.github.com/teams/$TEAM_ID/repos?per_page=100&access_token=$ACCESS_TOKEN" | jq -r "$REPO_NAME_FILTER" | sort
}
# TODO: Make this function safer, so that we don't accidentally cause people to lose changes
git_refresh(){
git stash
git checkout master
git pull --rebase
}
git_commit_and_push(){
COMMIT_MSG="$1"
DRY_RUN="$2"
if [[ $DRY_RUN == 0 ]]; then
git add -A
git commit -m "$COMMIT_MSG"
git push
else
git stash >> /dev/null
echo "Would have committed with message '$COMMIT_MSG' and pushed it to master!"
fi
}
git_create_label(){
REPO="$1"
LABEL_NAME="$2"
LABEL_DESC="$3"
LABEL_COLOR_CODE="$4"
JSON=$( jq -n \
--arg name "$LABEL_NAME" \
--arg desc "$LABEL_DESC" \
--arg color "$LABEL_COLOR_CODE" \
'{name: $name, description: $desc, color: $color}' )
curl -d "$JSON" -X POST "https://api.github.com/repos/AutoScout24/$REPO/labels?access_token=$ACCESS_TOKEN"
}
git_commit_and_create_pr(){
COMMIT_MSG="$1"
REPO="$2"
DRY_RUN="$3"
if [[ $DRY_RUN == 0 ]]; then
git diff --exit-code
if [[ $? != 0 ]]; then
git branch -D wip/albert
git push origin --delete wip/albert
git checkout -b wip/albert
git add -A
git commit -m "$COMMIT_MSG"
git push --set-upstream origin wip/albert
JSON=$( jq -n \
--arg msg "$COMMIT_MSG" \
'{title: $msg, body: $msg, head: "wip/albert", base: "master"}' )
curl -d "$JSON" -X POST "https://api.github.com/repos/AutoScout24/$REPO/pulls?access_token=$ACCESS_TOKEN"
git checkout master
else
echo "Nothing to commit"
fi
else
git stash >> /dev/null
echo "Would have created PR with message '$COMMIT_MSG'!"
fi
}
git_commit_and_create_pr_to_custom_branch(){
REPO="$1"
BRANCH="$2"
COMMIT_MSG="$3"
DRY_RUN="$4"
if [[ $DRY_RUN == 0 ]]; then
git diff --exit-code 1> /dev/null
if [[ $? != 0 ]]; then
echo -e "$(tput setaf 2)Change detected, going to open a PR for it now$(tput sgr0)\n"
git checkout -b "$BRANCH" || git checkout "$BRANCH"
git add -A
git commit -m "$COMMIT_MSG"
git push --set-upstream origin "$BRANCH"
JSON=$( jq -n \
--arg msg "$COMMIT_MSG" \
--arg branch "$BRANCH" \
'{title: $msg, body: $msg, head: $branch, base: "master"}' )
RESULT=$(curl -d "$JSON" -X POST "https://api.github.com/repos/AutoScout24/$REPO/pulls?access_token=$ACCESS_TOKEN")
STATE=$(echo "$RESULT" | jq -r ".state")
if [[ "$STATE" == "open" ]]; then
echo -e "$(tput setaf 2)PR opened$(tput sgr0)\n"
else
MESSAGE=$(echo "$RESULT" | jq ".message")
echo -e "$(tput setaf 1)failed: $MESSAGE$(tput sgr0)\n"
fi
git checkout master
else
echo "Nothing to commit"
fi
else
echo "Would have created PR with message '$COMMIT_MSG' and branch '$BRANCH'!"
fi
}
root_dir_not_found(){
echo "Root directory not found, exiting." && exit 1
}
merge_pr() {
PR_NUMBER=$1
echo -n "Merging PR $PR_NUMBER... "
RESULT=$(http PUT "https://api.github.com/repos/AutoScout24/$REPO/pulls/$PR_NUMBER/merge?access_token=$ACCESS_TOKEN" merge_method=merge)
MERGED=$(echo "$RESULT" | jq ".merged")
if [[ "$MERGED" == "true" ]]; then
echo -e "$(tput setaf 2)merged$(tput sgr0)\n"
else
MESSAGE=$(echo "$RESULT" | jq ".message")
echo -e "$(tput setaf 1)failed: $MESSAGE$(tput sgr0)\n"
fi
}