Bug 1839315: part 4) Link from `SheetLoadData::mWasAlternate` to spec. r=emilio DONTBUILD
[gecko.git] / tools / github-sync / sync-to-github.sh
blobd6776497489607a90d1f2374357d37ae03fcee44
1 #!/usr/bin/env bash
3 # This Source Code Form is subject to the terms of the Mozilla Public
4 # License, v. 2.0. If a copy of the MPL was not distributed with this
5 # file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 # Do NOT set -x here, since that will expose a secret API token!
8 set -o errexit
9 set -o nounset
10 set -o pipefail
12 if [[ "$(uname)" != "Linux" ]]; then
13 echo "Error: this script must be run on Linux due to readlink semantics"
14 exit 1
17 # GECKO_PATH should definitely be set
18 if [[ -z "${GECKO_PATH}" ]]; then
19 echo "Error: GECKO_PATH must point to a hg clone of mozilla-central"
20 exit 1
23 # Internal variables, don't fiddle with these
24 MYSELF=$(readlink -f ${0})
25 MYDIR=$(dirname "${MYSELF}")
26 WORKDIR="${HOME}/.ghsync"
27 TMPDIR="${WORKDIR}/tmp"
29 NAME="$1"
30 RELATIVE_PATH="$2"
31 DOWNSTREAM_REPO="$3"
32 BORS="$4"
33 BRANCH="github-sync"
35 mkdir -p "${TMPDIR}"
37 # Bring the project clone to a known good up-to-date state
38 if [[ ! -d "${WORKDIR}/${NAME}" ]]; then
39 echo "Setting up ${NAME} repo..."
40 git clone "https://github.com/${DOWNSTREAM_REPO}" "${WORKDIR}/${NAME}"
41 pushd "${WORKDIR}/${NAME}"
42 git remote add moz-gfx https://github.com/moz-gfx/${NAME}
43 popd
44 else
45 echo "Updating ${NAME} repo..."
46 pushd "${WORKDIR}/${NAME}"
47 git checkout master
48 git pull
49 popd
52 if [[ -n "${GITHUB_SECRET:-}" ]]; then
53 echo "Obtaining github API token..."
54 # Be careful, GITHUB_TOKEN is secret, so don't log it (or any variables
55 # built using it).
56 GITHUB_TOKEN=$(
57 curl -sSfL "$TASKCLUSTER_PROXY_URL/secrets/v1/secret/${GITHUB_SECRET}" |
58 ${MYDIR}/read-json.py "secret/token"
60 AUTH="moz-gfx:${GITHUB_TOKEN}"
61 CURL_AUTH="Authorization: bearer ${GITHUB_TOKEN}"
64 echo "Pushing base ${BRANCH} branch..."
65 pushd "${WORKDIR}/${NAME}"
66 git fetch moz-gfx
67 git checkout -B ${BRANCH} moz-gfx/${BRANCH} || git checkout -B ${BRANCH} master
69 if [[ -n "${GITHUB_SECRET:-}" ]]; then
70 # git may emit error messages that contain the URL, so let's sanitize them
71 # or we might leak the auth token to the task log.
72 git push "https://${AUTH}@github.com/moz-gfx/${NAME}" \
73 "${BRANCH}:${BRANCH}" 2>&1 | sed -e "s/${AUTH}/_SANITIZED_/g"
74 # Re-fetch to update the remote moz-gfx/$BRANCH branch in the local repo;
75 # normally the push does this but we use a fully-qualified URL for
76 # pushing so it doesn't happen.
77 git fetch moz-gfx
79 popd
81 # Run the converter
82 echo "Running converter..."
83 pushd "${GECKO_PATH}"
84 "${MYDIR}/converter.py" "${WORKDIR}/${NAME}" "${RELATIVE_PATH}"
85 popd
87 # Check to see if we have changes that need pushing
88 echo "Checking for new changes..."
89 pushd "${WORKDIR}/${NAME}"
90 PATCHCOUNT=$(git log --oneline moz-gfx/${BRANCH}..${BRANCH}| wc -l)
91 if [[ ${PATCHCOUNT} -eq 0 ]]; then
92 echo "No new patches found, aborting..."
93 exit 0
96 # Log the new changes, just for logging purposes
97 echo "Here are the new changes:"
98 git log --graph --stat moz-gfx/${BRANCH}..${BRANCH}
100 # Collect PR numbers of PRs opened on Github and merged to m-c
101 set +e
102 FIXES=$(
103 git log master..${BRANCH} |
104 grep "\[import_pr\] From https://github.com/${DOWNSTREAM_REPO}/pull" |
105 sed -e "s%.*pull/% Fixes #%" |
106 uniq |
107 tr '\n' ','
109 echo "${FIXES}"
110 set -e
112 if [[ -z "${GITHUB_SECRET:-}" ]]; then
113 echo "Running in try push, exiting now"
114 exit 0
117 echo "Pushing new changes to moz-gfx..."
118 # git may emit error messages that contain the URL, so let's sanitize them
119 # or we might leak the auth token to the task log.
120 git push "https://${AUTH}@github.com/moz-gfx/${NAME}" +${BRANCH}:${BRANCH} \
121 2>&1 | sed -e "s/${AUTH}/_SANITIZED_/g"
123 CURL_HEADER="Accept: application/vnd.github.v3+json"
124 CURL=(curl -sSfL -H "${CURL_HEADER}" -H "${CURL_AUTH}")
125 # URL extracted here mostly to make servo-tidy happy with line lengths
126 API_URL="https://api.github.com/repos/${DOWNSTREAM_REPO}"
128 # Check if there's an existing PR open
129 echo "Listing pre-existing pull requests..."
130 "${CURL[@]}" "${API_URL}/pulls?head=moz-gfx:${BRANCH}" |
131 tee "${TMPDIR}/pr.get"
132 set +e
133 COMMENT_URL=$(cat "${TMPDIR}/pr.get" | ${MYDIR}/read-json.py "0/comments_url")
134 HAS_COMMENT_URL="${?}"
135 set -e
137 if [[ ${HAS_COMMENT_URL} -ne 0 ]]; then
138 echo "Pull request not found, creating..."
139 # The PR doesn't exist yet, so let's create it
140 ( echo -n '{ "title": "Sync changes from mozilla-central '"${RELATIVE_PATH}"'"'
141 echo -n ', "body": "'"${FIXES}"'"'
142 echo -n ', "head": "moz-gfx:'"${BRANCH}"'"'
143 echo -n ', "base": "master" }'
144 ) > "${TMPDIR}/pr.create"
145 "${CURL[@]}" -d "@${TMPDIR}/pr.create" "${API_URL}/pulls" |
146 tee "${TMPDIR}/pr.response"
147 COMMENT_URL=$(
148 cat "${TMPDIR}/pr.response" |
149 ${MYDIR}/read-json.py "comments_url"
153 # At this point COMMENTS_URL should be set, so leave a comment to tell bors
154 # to merge the PR.
155 echo "Posting r+ comment to ${COMMENT_URL}..."
156 echo '{ "body": "'"$BORS"' r=auto" }' > "${TMPDIR}/bors_rplus"
157 "${CURL[@]}" -d "@${TMPDIR}/bors_rplus" "${COMMENT_URL}"
159 echo "All done!"