1*cfb92d14SAndroid Build Coastguard Worker#!/bin/bash 2*cfb92d14SAndroid Build Coastguard Worker# 3*cfb92d14SAndroid Build Coastguard Worker# Copyright (c) 2020, The OpenThread Authors. 4*cfb92d14SAndroid Build Coastguard Worker# All rights reserved. 5*cfb92d14SAndroid Build Coastguard Worker# 6*cfb92d14SAndroid Build Coastguard Worker# Redistribution and use in source and binary forms, with or without 7*cfb92d14SAndroid Build Coastguard Worker# modification, are permitted provided that the following conditions are met: 8*cfb92d14SAndroid Build Coastguard Worker# 1. Redistributions of source code must retain the above copyright 9*cfb92d14SAndroid Build Coastguard Worker# notice, this list of conditions and the following disclaimer. 10*cfb92d14SAndroid Build Coastguard Worker# 2. Redistributions in binary form must reproduce the above copyright 11*cfb92d14SAndroid Build Coastguard Worker# notice, this list of conditions and the following disclaimer in the 12*cfb92d14SAndroid Build Coastguard Worker# documentation and/or other materials provided with the distribution. 13*cfb92d14SAndroid Build Coastguard Worker# 3. Neither the name of the copyright holder nor the 14*cfb92d14SAndroid Build Coastguard Worker# names of its contributors may be used to endorse or promote products 15*cfb92d14SAndroid Build Coastguard Worker# derived from this software without specific prior written permission. 16*cfb92d14SAndroid Build Coastguard Worker# 17*cfb92d14SAndroid Build Coastguard Worker# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18*cfb92d14SAndroid Build Coastguard Worker# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19*cfb92d14SAndroid Build Coastguard Worker# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20*cfb92d14SAndroid Build Coastguard Worker# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 21*cfb92d14SAndroid Build Coastguard Worker# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22*cfb92d14SAndroid Build Coastguard Worker# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23*cfb92d14SAndroid Build Coastguard Worker# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24*cfb92d14SAndroid Build Coastguard Worker# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25*cfb92d14SAndroid Build Coastguard Worker# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26*cfb92d14SAndroid Build Coastguard Worker# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27*cfb92d14SAndroid Build Coastguard Worker# POSSIBILITY OF SUCH DAMAGE. 28*cfb92d14SAndroid Build Coastguard Worker# 29*cfb92d14SAndroid Build Coastguard Worker# Description: 30*cfb92d14SAndroid Build Coastguard Worker# A tool to download projects with the ability to apply dependent PRs. 31*cfb92d14SAndroid Build Coastguard Worker# 32*cfb92d14SAndroid Build Coastguard Worker 33*cfb92d14SAndroid Build Coastguard Workerset -euxo pipefail 34*cfb92d14SAndroid Build Coastguard Worker 35*cfb92d14SAndroid Build Coastguard Workerapply_dependencies() 36*cfb92d14SAndroid Build Coastguard Worker{ 37*cfb92d14SAndroid Build Coastguard Worker local project_name 38*cfb92d14SAndroid Build Coastguard Worker 39*cfb92d14SAndroid Build Coastguard Worker project_name=$(git remote get-url origin | grep -oE '[^/:]+/[^/:]+\.git$' | cut -d. -f1) 40*cfb92d14SAndroid Build Coastguard Worker echo "project name: ${project_name}" 41*cfb92d14SAndroid Build Coastguard Worker 42*cfb92d14SAndroid Build Coastguard Worker git config user.name || git config user.name 'OpenThread Git' 43*cfb92d14SAndroid Build Coastguard Worker git config user.email || git config user.email 'git@openthread' 44*cfb92d14SAndroid Build Coastguard Worker 45*cfb92d14SAndroid Build Coastguard Worker while read -r dependency; do 46*cfb92d14SAndroid Build Coastguard Worker echo "${dependency}" 47*cfb92d14SAndroid Build Coastguard Worker depends_on_pr="$(echo "${dependency}" | tr -d '\r\n' | cut -d# -f2)" 48*cfb92d14SAndroid Build Coastguard Worker echo "pr: #${depends_on_pr}" 49*cfb92d14SAndroid Build Coastguard Worker git pull --no-edit --no-rebase origin "pull/${depends_on_pr}/merge" 50*cfb92d14SAndroid Build Coastguard Worker done < <(grep -E "^Depends-On: *${project_name}" || true) 51*cfb92d14SAndroid Build Coastguard Worker} 52*cfb92d14SAndroid Build Coastguard Worker 53*cfb92d14SAndroid Build Coastguard Workerget_pr_body() 54*cfb92d14SAndroid Build Coastguard Worker{ 55*cfb92d14SAndroid Build Coastguard Worker local pr 56*cfb92d14SAndroid Build Coastguard Worker 57*cfb92d14SAndroid Build Coastguard Worker [[ ${GITHUB_ACTIONS+x} && ${GITHUB_REF-} =~ ^refs/pull/[0-9]+/merge ]] || { 58*cfb92d14SAndroid Build Coastguard Worker echo 'Not a pull request.' >&2 59*cfb92d14SAndroid Build Coastguard Worker return 0 60*cfb92d14SAndroid Build Coastguard Worker } 61*cfb92d14SAndroid Build Coastguard Worker 62*cfb92d14SAndroid Build Coastguard Worker pr="$(echo "${GITHUB_REF}" | cut -d/ -f3)" 63*cfb92d14SAndroid Build Coastguard Worker 64*cfb92d14SAndroid Build Coastguard Worker # do not print GITHUB_TOKEN 65*cfb92d14SAndroid Build Coastguard Worker set +x 66*cfb92d14SAndroid Build Coastguard Worker curl -H "authorization: Bearer ${GITHUB_TOKEN}" -H 'content-type: application/json' \ 67*cfb92d14SAndroid Build Coastguard Worker "https://api.github.com/repos/${GITHUB_REPOSITORY}/pulls/${pr}" \ 68*cfb92d14SAndroid Build Coastguard Worker | jq -r .body 69*cfb92d14SAndroid Build Coastguard Worker set -x 70*cfb92d14SAndroid Build Coastguard Worker} 71*cfb92d14SAndroid Build Coastguard Worker 72*cfb92d14SAndroid Build Coastguard Workertry_update() 73*cfb92d14SAndroid Build Coastguard Worker{ 74*cfb92d14SAndroid Build Coastguard Worker cd "$1" 75*cfb92d14SAndroid Build Coastguard Worker 76*cfb92d14SAndroid Build Coastguard Worker get_pr_body | apply_dependencies 77*cfb92d14SAndroid Build Coastguard Worker} 78*cfb92d14SAndroid Build Coastguard Worker 79*cfb92d14SAndroid Build Coastguard Workertry_clone() 80*cfb92d14SAndroid Build Coastguard Worker{ 81*cfb92d14SAndroid Build Coastguard Worker local dest_dir 82*cfb92d14SAndroid Build Coastguard Worker 83*cfb92d14SAndroid Build Coastguard Worker if [[ $1 == no-depend ]]; then 84*cfb92d14SAndroid Build Coastguard Worker shift 85*cfb92d14SAndroid Build Coastguard Worker git clone "$@" 2>&1 86*cfb92d14SAndroid Build Coastguard Worker else 87*cfb92d14SAndroid Build Coastguard Worker dest_dir="$(git clone "$@" 2>&1 | tee | cut -d\' -f2)" 88*cfb92d14SAndroid Build Coastguard Worker 89*cfb92d14SAndroid Build Coastguard Worker cd "${dest_dir}" 90*cfb92d14SAndroid Build Coastguard Worker get_pr_body | apply_dependencies 91*cfb92d14SAndroid Build Coastguard Worker fi 92*cfb92d14SAndroid Build Coastguard Worker} 93*cfb92d14SAndroid Build Coastguard Worker 94*cfb92d14SAndroid Build Coastguard Workerprint_help() 95*cfb92d14SAndroid Build Coastguard Worker{ 96*cfb92d14SAndroid Build Coastguard Worker echo "EXAMPLES: 97*cfb92d14SAndroid Build Coastguard Worker $0 http://github.com/openthread/ot-br-posix.git 98*cfb92d14SAndroid Build Coastguard Worker $0 http://github.com/openthread/ot-ns.git --depth 1 /tmp/ot-ns" 99*cfb92d14SAndroid Build Coastguard Worker} 100*cfb92d14SAndroid Build Coastguard Worker 101*cfb92d14SAndroid Build Coastguard Workermain() 102*cfb92d14SAndroid Build Coastguard Worker{ 103*cfb92d14SAndroid Build Coastguard Worker if [[ $# == 0 ]]; then 104*cfb92d14SAndroid Build Coastguard Worker print_help 105*cfb92d14SAndroid Build Coastguard Worker return 0 106*cfb92d14SAndroid Build Coastguard Worker fi 107*cfb92d14SAndroid Build Coastguard Worker 108*cfb92d14SAndroid Build Coastguard Worker case $1 in 109*cfb92d14SAndroid Build Coastguard Worker update) 110*cfb92d14SAndroid Build Coastguard Worker shift 111*cfb92d14SAndroid Build Coastguard Worker try_update "$@" 112*cfb92d14SAndroid Build Coastguard Worker ;; 113*cfb92d14SAndroid Build Coastguard Worker clone) 114*cfb92d14SAndroid Build Coastguard Worker shift 115*cfb92d14SAndroid Build Coastguard Worker try_clone "$@" 116*cfb92d14SAndroid Build Coastguard Worker ;; 117*cfb92d14SAndroid Build Coastguard Worker *) 118*cfb92d14SAndroid Build Coastguard Worker print_help 119*cfb92d14SAndroid Build Coastguard Worker return 1 120*cfb92d14SAndroid Build Coastguard Worker ;; 121*cfb92d14SAndroid Build Coastguard Worker esac 122*cfb92d14SAndroid Build Coastguard Worker} 123*cfb92d14SAndroid Build Coastguard Worker 124*cfb92d14SAndroid Build Coastguard Workermain "$@" 125