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