From 26a70dce2738f0b002b2ee90e9512d56f666cf34 Mon Sep 17 00:00:00 2001 From: ViViDboarder Date: Wed, 1 Jul 2020 12:45:20 -0700 Subject: [PATCH] Refactor of scripts to add a target for auto creating a pr I plan to use this for automated builds --- .gitignore | 1 + Makefile | 34 +++-- .../archive-docset.sh | 10 +- scripts/create-pr.sh | 124 ++++++++++++++++++ .../package-docset.sh | 0 5 files changed, 157 insertions(+), 12 deletions(-) rename archive-docset.sh => scripts/archive-docset.sh (84%) create mode 100755 scripts/create-pr.sh rename package-docset.sh => scripts/package-docset.sh (100%) diff --git a/.gitignore b/.gitignore index 0d075d0..959f078 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ build/ archive/ .DS_Store docset-gen +repotmp/ diff --git a/Makefile b/Makefile index f899f9b..521abb6 100644 --- a/Makefile +++ b/Makefile @@ -19,30 +19,42 @@ run-lightning: clean-index .PHONY: package-apex package-apex: run-apex - ./package-docset.sh apexcode + ./scripts/package-docset.sh apexcode .PHONY: package-vf package-vf: run-vf - ./package-docset.sh pages + ./scripts/package-docset.sh pages .PHONY: package-lightning package-lightning: run-lightning - ./package-docset.sh lightning + ./scripts/package-docset.sh lightning .PHONY: archive-apex archive-apex: package-apex - ./archive-docset.sh apexcode + ./scripts/archive-docset.sh apexcode + +./archive/Salesforce_Apex: archive-apex .PHONY: archive-vf archive-vf: package-vf - ./archive-docset.sh pages + ./scripts/archive-docset.sh pages + +./archive/Salesforce_Visualforce: archive-vf .PHONY: archive-lightning -archive-lightning: package-lightning - ./archive-docset.sh lightning +./archive-lightning: package-lightning + ./scripts/archive-docset.sh lightning + +./archive/Salesforce_Lightning: archive-lightning .PHONY: archive-all -archive-all: archive-apex archive-vf archive-lightning +archive-all: archive-apex archive-vf # archive-lightning Lightning package isn't functional + +./archive: archive-all + +.PHONY: create-pr +create-pr: ./archive + ./scripts/create-pr.sh .PHONY: clean-index clean-index: @@ -64,5 +76,9 @@ clean: clean-index clean-package clean-archive clean-build: rm -fr ./build +.PHONY: clean-pr +clean-pr: + rm -fr ./repotmp + .PHONY: clean-all -clean-all: clean clean-build +clean-all: clean clean-build clean-pr diff --git a/archive-docset.sh b/scripts/archive-docset.sh similarity index 84% rename from archive-docset.sh rename to scripts/archive-docset.sh index 699b27d..da7f774 100755 --- a/archive-docset.sh +++ b/scripts/archive-docset.sh @@ -10,7 +10,8 @@ deliverable=$1 function get_friendly_name { local deliverable=$1 - local name="$(tr '[:lower:]' '[:upper:]' <<< "${deliverable:0:1}")${deliverable:1}" + local name + name="$(tr '[:lower:]' '[:upper:]' <<< "${deliverable:0:1}")${deliverable:1}" case "$deliverable" in "apexcode") name="Apex" @@ -34,14 +35,17 @@ function get_icon_name { } function main { - local name=$(get_friendly_name "$deliverable") + local name + name=$(get_friendly_name "$deliverable") local package="$out_dir/Salesforce $name.docset" local archive_dir="$archive_dir/Salesforce_$name" local archive="$archive_dir/Salesforce_$name.tgz" - local icon=$(get_icon_name "$deliverable") + local icon + icon=$(get_icon_name "$deliverable") mkdir -p "$archive_dir" # Generate docset.json + local version version=$(cat "$build_dir/$deliverable-version.txt") sed "s/VERSION/$version/" "$files_dir/docset-$deliverable.json" > "$archive_dir/docset.json" # Generated tgz archive diff --git a/scripts/create-pr.sh b/scripts/create-pr.sh new file mode 100755 index 0000000..56de8aa --- /dev/null +++ b/scripts/create-pr.sh @@ -0,0 +1,124 @@ +#! /bin/bash + +set -eu +set -o pipefail + +if ! git config --global user.name ;then + # If no global git configs exist, let's set some temporary values + export GIT_COMMITTER_NAME="${GIT_COMMITTER_NAME:-"ViViDboarder"}" + export GIT_COMMITTER_EMAIL="${GIT_COMMITTER_EMAIL:-"ViViDboarder@gmail.com"}" + export GIT_AUTHOR_NAME="$GIT_COMMITTER_NAME" + export GIT_AUTHOR_EMAIL="$GIT_COMMITTER_EMAIL" +fi +# Get name of the fork and target repo +FORK_REPO="${FORK_REPO:-"ViViDboarder/Dash-User-Contributions"}" +TARGET_REPO="${TARGET_REPO:-"ViViDboarder/Dash-User-Contributions"}" + +# If no github user is provided, take it from the fork name +if [ -z "${GITHUB_USER:-""}" ]; then + GITHUB_USER="${FORK_REPO%%/*}" +fi +GITHUB_TOKEN="${GITHUB_TOKEN:-default}" + +WORKDIR=$(pwd) +TMP_DIR="$WORKDIR/repotmp" +REPO_DIR="$TMP_DIR/Dash-User-Contributions" + +function validate() { + if [ -z "$GITHUB_TOKEN" ]; then + echo "Must provide \$GITHUB_TOKEN as an environment variable" + exit 1 + fi + + echo "Creating PR for $GITHUB_USER to $TARGET_REPO" +} + +function read_version() { + local apex_version + apex_version="$(cat ./build/apexcode-version.txt)" + local pages_version + pages_version="$(cat ./build/pages-version.txt)" + local lightning_version + lightning_version="$(cat ./build/lightning-version.txt)" + + if [ "$apex_version" != "$pages_version" ] || [ "$apex_version" != "$lightning_version" ]; then + echo "Apex: $apex_version, Pages: $pages_version, Lightning: $lightning_version" + echo "One of the doc versions doesn't match" + exit 1 + fi + # All versions match, return one of them + echo "$apex_version" +} + +function workdir_git() { + cd "$REPO_DIR" + if ! git "$@" ;then + # Be sure to return to workdir after a failed git command + cd "$WORKDIR" + return 1 + fi + cd "$WORKDIR" +} + +function shallow_clone_or_pull() { + mkdir -p "$TMP_DIR" + if [ -d "$REPO_DIR" ]; then + workdir_git checkout master + workdir_git pull --ff-only origin master + else + git clone --depth 1 "https://$GITHUB_USER:$GITHUB_TOKEN@github.com/$FORK_REPO" "$REPO_DIR" + fi +} + +function copy_release() { + cp -r archive/* "$REPO_DIR/docsets/" +} + +function create_release_branch() { + local branch="$1" + if ! workdir_git checkout -b "$branch" ; then + echo "Could not create release branch. Release likely already exists." + exit 1 + fi +} + +function create_pr() { + local version="$1" + local branch="$2" + local title="Update Salesforce docsets to $version" + workdir_git checkout "$branch" + workdir_git add . + workdir_git commit -m "$title" + workdir_git push origin HEAD + local result + result=$(curl \ + -u "$GITHUB_USER:$GITHUB_TOKEN" \ + -X POST \ + -H "Accept: application/vnd.github.v3+json" \ + "https://api.github.com/repos/$TARGET_REPO/pulls" \ + -d "{\"title\":\"$title\",\"body\":\"This branch contains auto-generated updates to version $version\", \"head\":\"$GITHUB_USER:$branch\",\"base\":\"master\"}") + + + local result_url + if result_url="$(echo "$result" | jq --exit-status --raw-output .html_url)" ;then + echo "Pull request created at $result_url" + else + echo "$result" + exit 1 + fi +} + +function main() { + validate + + local version + version="$(read_version)" || { echo "$version"; exit 1; } + local branch="salesforce-$version" + + shallow_clone_or_pull + copy_release + create_release_branch "$branch" + create_pr "$version" "$branch" +} + +main diff --git a/package-docset.sh b/scripts/package-docset.sh similarity index 100% rename from package-docset.sh rename to scripts/package-docset.sh