| #!/bin/bash |
| # Copyright 2018 The Fuchsia Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file |
| |
| # Prints to stdout a script that creates a new third_party fuchsia mirror of a |
| # github project. Sets appropriate ACLs, copy config, and Gerrit access. |
| # |
| # Usage: |
| # $ new-github-repo <github-project-name> > generated-script |
| # $ bash generated-script |
| # |
| # TODO(dbort): Don't create non-upstream versions of branches, only master. |
| # E.g., it's fine to have 'origin/upstream/release-b', but we don't need |
| # 'origin/release-b' as well. We only want 'origin/master'. |
| |
| set -o errexit |
| set -o nounset |
| set -o pipefail |
| |
| if [[ "$#" -ne 1 ]]; then |
| echo "Usage: $0 <github-project-name>" >&2 |
| echo " github-project-name is relative to, "$( |
| )"and does not include, 'github.com'" >&2 |
| exit 1 |
| fi |
| readonly GITHUB_PROJECT="$1" |
| readonly UPSTREAM_GOB="github/${GITHUB_PROJECT}" |
| readonly REPO_BASENAME="$(basename "${UPSTREAM_GOB}")" |
| readonly MIRROR_GOB="fuchsia/third_party/${REPO_BASENAME}" |
| |
| # Prints a gob-ctl command to stdout. If args are supplied, prints them. If |
| # not, reads from stdin and emits HERE-style lines to write that same data to |
| # gob-ctl. |
| run-gob-ctl() { |
| if [[ "$#" -gt 0 ]]; then |
| echo "gob-ctl $@" |
| else |
| echo "gob-ctl <<EOF" |
| cat |
| echo "EOF" |
| fi |
| } |
| |
| echo "#!/bin/bash" |
| echo "set -eu" |
| echo "set -o pipefail" |
| |
| run-gob-ctl create -fork "${UPSTREAM_GOB}" "${MIRROR_GOB}" |
| echo 'echo "Waiting 5 seconds for the creation to finish..."' |
| # Without this sleep, the acl commands sometimes fail. |
| echo 'sleep 5' |
| |
| run-gob-ctl acl "${MIRROR_GOB}" --reader 'all_users' |
| run-gob-ctl acl "${MIRROR_GOB}" --owner 'mdbgroup/fuchsia-team' |
| run-gob-ctl acl "${MIRROR_GOB}" --revoke "mdbuser/${USER}" |
| run-gob-ctl acl "${MIRROR_GOB}" --revoke "email/${USER}@google.com" |
| |
| run-gob-ctl <<EOF |
| copy { |
| source: "${UPSTREAM_GOB}" |
| destination: "${MIRROR_GOB}" |
| create: < |
| source_scope: < |
| all: USERS |
| > |
| destination_scope: < |
| mdb_group: "fuchsia-git-admins" |
| > |
| add_refspec: < |
| source: "refs/heads/*" |
| destination: "refs/heads/upstream/*" |
| > |
| add_refspec: < |
| source: "refs/tags/*" |
| destination: "refs/tags/*" |
| > |
| > |
| } |
| EOF |
| |
| run-gob-ctl show "${MIRROR_GOB}" |
| |
| # fix_parent <MIRROR_GOB> |
| fix_parent() { |
| local gob_name="$1" |
| # The part before the first slash |
| local host_name="$(echo "${gob_name}" | sed -e 's,/.*,,')" |
| # The part after the first slash, with slashes URL-encoded |
| local project_id="$(echo "${gob_name}" | sed -e 's,[^/]*/,,;s,/,%2F,g')" |
| |
| local host_url="https://${host_name}-review.googlesource.com/a" |
| local new_parent='Third-Party-Projects' |
| |
| cat <<HERE |
| gob-curl \ |
| -d '{"parent": "${new_parent}", "commit_message": "Set parent to ${new_parent}"}' \ |
| -H "Content-Type: application/json" \ |
| -X PUT \ |
| "${host_url}/projects/${project_id}/parent" |
| HERE |
| } |
| |
| echo 'read -p "Press enter to set the parent project..."' |
| fix_parent "${MIRROR_GOB}" |
| echo 'echo Done.' |