forked from travis-ci/apt-package-safelist
/
make_pr.sh
executable file
·142 lines (117 loc) · 3.54 KB
/
make_pr.sh
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
#! /usr/bin/env bash
ANSI_RED="\033[31;1m"
ANSI_GREEN="\033[32;1m"
ANSI_RESET="\033[0m"
ANSI_CLEAR="\033[0K"
EXIT_SUCCSS=0
EXIT_SOURCE_NOT_FOUND=1
EXIT_SOURCE_HAS_SETUID=2
EXIT_NOTHING_TO_COMMIT=3
DEFAULT_BRANCH=master
create_pr=0
has_setuid=0
function notice() {
msg=$1
echo -e "\n${ANSI_GREEN}${msg}${ANSI_RESET}\n"
}
function warn() {
msg=$1
echo -e "\n${ANSI_RED}${msg}${ANSI_RESET}\n"
}
function usage() {
echo "Usage: $0 [-y] repo issue_number [package [additional_packages …]]"
}
while getopts "sy" opt; do
case "$opt" in
s)
has_setuid=1
;;
y)
create_pr=1
;;
esac
done
shift $((OPTIND-1))
if [ $# -lt 2 ]; then
usage
exit 0
fi
## Process arguments; nothing funcy
ISSUE_REPO=$1
shift
ISSUE_NUMBER=$1
shift
PACKAGES=( $@ )
if [ -z "$PACKAGES" ]; then
if [ -f $(dirname $0)/packages ]; then
notice "Reading from $(dirname $0)/packages"
PACKAGES=$(< $(dirname $0)/packages)
else
warn "Unable to determine packages to add"
usage
exit 1
fi
fi
ISSUE_PACKAGE=$(echo $PACKAGES | cut -f1 -d' ')
notice "Setting up PR with\nRepo: ${ISSUE_REPO}\nNUMBER: ${ISSUE_NUMBER}\nPackages: ${PACKAGES[*]}"
BRANCH="test-${ISSUE_REPO}-${ISSUE_NUMBER}"
notice "Setting up Git"
if [ -z "`git config --get --global credential.helper`" ]; then
notice "set up credential.helper"
git config credential.helper "store --file=.git/credentials"
echo "https://${GITHUB_OAUTH_TOKEN}:@github.com" > .git/credentials 2>/dev/null
fi
if [ -z "`git config --get --global user.email`" ]; then
notice "set up user.email"
git config --global user.email "contact@travis-ci.com"
fi
if [ -z "`git config --get --global user.name`" ]; then
notice "set up user.name"
git config --global user.name "Travis CI APT package tester"
fi
notice "Creating commit"
git checkout $DEFAULT_BRANCH
git checkout -b $BRANCH
for p in ${PACKAGES[*]}; do
notice "Adding ${p}"
env PACKAGE=${p} make add > /dev/null
done
git add ubuntu-precise
git commit -m "Add ${ISSUE_PACKAGE} to ubuntu-precise; resolves travis-ci/${ISSUE_REPO}#${ISSUE_NUMBER}
Packages: ${PACKAGES}"
COMMIT_EXIT_STATUS=$?
if [ $COMMIT_EXIT_STATUS -gt 0 ]; then
notice "Nothing to commit"
exit $EXIT_NOTHING_TO_COMMIT
fi
notice "Pushing commit"
if [ -z $GITHUB_OAUTH_TOKEN ]; then
warn '$GITHUB_OAUTH_TOKEN not set'
exit 1
fi
git push origin $BRANCH
if [ $create_pr -le 0 ]; then
# bail before creating a pr
exit 0
fi
notice "Creating PR"
COMMENT="Add packages: ${PACKAGES[*]}"
if [ -n ${TRAVIS_BUILD_ID} ]; then
COMMENT="${COMMENT}\n\nSee http://travis-ci.org/${TRAVIS_REPO_SLUG}/builds/${TRAVIS_BUILD_ID}."
fi
if [ ${has_setuid} -gt 0 ]; then
COMMENT="\n\n***NOTE***\n\nsetuid/seteuid/setgid bits were found. Be sure to check the build result.\n\n${COMMENT}"
fi
curl -X POST -sS -H "Content-Type: application/json" -H "Authorization: token ${GITHUB_OAUTH_TOKEN}" \
-d "{\"title\":\"Pull request for ${ISSUE_PACKAGE}\",\"body\":\"Resolves travis-ci/${ISSUE_REPO}#${ISSUE_NUMBER}.\n${COMMENT}\",\"head\":\"${BRANCH}\",\"base\":\"master\"}" \
https://api.github.com/repos/travis-ci/apt-package-whitelist/pulls > pr_payload
if [ $? -eq 0 -a ${has_setuid} -gt 0 ]; then
curl -X POST -sS -H "Content-Type: application/json" -H "Authorization: token ${GITHUB_OAUTH_TOKEN}" \
-d "[\"textual-suid-present\"]" \
$(jq .issue_url pr_payload | cut -f2 -d\")/labels
fi
curl -X POST -sS -H "Content-Type: application/json" -H "Authorization: token ${GITHUB_OAUTH_TOKEN}" \
-d "[\"apt-whitelist-check-run\"]" \
https://api.github.com/repos/travis-ci/${ISSUE_REPO}/issues/${ISSUE_NUMBER}/labels
git checkout $DEFAULT_BRANCH
git branch -D $BRANCH