From 28e558162a6b0d59342e6c21ecfed3c833fa821b Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 20 May 2025 14:18:54 +0100 Subject: [PATCH] Add branch matching (#2329) --- .github/workflows/build_and_test.yaml | 1 + .github/workflows/build_prepare.yaml | 25 +++++++++++++- scripts/branch-match.sh | 48 +++++++++++++++++++++++++++ scripts/fetchdep.sh | 41 ----------------------- 4 files changed, 73 insertions(+), 42 deletions(-) create mode 100755 scripts/branch-match.sh delete mode 100755 scripts/fetchdep.sh diff --git a/.github/workflows/build_and_test.yaml b/.github/workflows/build_and_test.yaml index 2fd9971..24fcd86 100644 --- a/.github/workflows/build_and_test.yaml +++ b/.github/workflows/build_and_test.yaml @@ -15,6 +15,7 @@ jobs: with: config: ${{ (github.event.pull_request.base.ref || github.ref_name) == 'develop' && 'element.io/nightly' || 'element.io/release' }} version: ${{ (github.event.pull_request.base.ref || github.ref_name) == 'develop' && 'develop' || '' }} + branch-matching: true windows: needs: fetch diff --git a/.github/workflows/build_prepare.yaml b/.github/workflows/build_prepare.yaml index 64253f5..8be9200 100644 --- a/.github/workflows/build_prepare.yaml +++ b/.github/workflows/build_prepare.yaml @@ -20,6 +20,11 @@ on: required: false default: false description: "Whether the build should be deployed to production" + branch-matching: + type: boolean + required: false + default: false + description: "Whether the branch name should be matched to find the element-web commit" secrets: # Required if `nightly` is set CF_R2_ACCESS_KEY_ID: @@ -59,7 +64,25 @@ jobs: - name: Install Deps run: "yarn install --frozen-lockfile" - - name: Fetch Element Web + - name: Fetch Element Web (matching branch) + id: branch-matching + if: inputs.branch-matching + continue-on-error: true + run: | + scripts/branch-match.sh + cp "$CONFIG_DIR/config.json" element-web/ + yarn --cwd element-web install --frozen-lockfile + yarn --cwd element-web run build + mv element-web/webapp . + yarn asar-webapp + env: + # These must be set for branch-match.sh to get the right branch + REPOSITORY: ${{ github.repository }} + PR_NUMBER: ${{ github.event.pull_request.number }} + CONFIG_DIR: ${{ inputs.config }} + + - name: Fetch Element Web (${{ inputs.version }}) + if: steps.branch-matching.outcome == 'failure' || steps.branch-matching.outcome == 'skipped' run: yarn run fetch --noverify -d ${{ inputs.config }} ${{ inputs.version }} # We split this out to save the build_* scripts having to do it to make use of `hashFiles` in the cache action diff --git a/scripts/branch-match.sh b/scripts/branch-match.sh new file mode 100755 index 0000000..c42073d --- /dev/null +++ b/scripts/branch-match.sh @@ -0,0 +1,48 @@ +#!/bin/bash + +# Script for downloading a branch of element-web matching the branch a PR is contributed from + +set -x + +deforg="element-hq" +defrepo="element-web" + +# The PR_NUMBER variable must be set explicitly. +default_org_repo=${GITHUB_REPOSITORY:-"$deforg/$defrepo"} +PR_ORG=${PR_ORG:-${default_org_repo%%/*}} +PR_REPO=${PR_REPO:-${default_org_repo##*/}} + +# A function that clones a branch of a repo based on the org, repo and branch +clone() { + org=$1 + repo=$2 + branch=$3 + if [ -n "$branch" ] + then + echo "Trying to use $org/$repo#$branch" + # Disable auth prompts: https://serverfault.com/a/665959 + GIT_TERMINAL_PROMPT=0 git clone https://github.com/$org/$repo.git $repo --branch "$branch" --depth 1 && exit 0 + fi +} + +echo "Getting info about a PR with number $PR_NUMBER" +apiEndpoint="https://api.github.com/repos/$PR_ORG/$PR_REPO/pulls/$PR_NUMBER" +head=$(curl "$apiEndpoint" | jq -r '.head.label') + +# for forks, $head will be in the format "fork:branch", so we split it by ":" +# into an array. On non-forks, this has the effect of splitting into a single +# element array given ":" shouldn't appear in the head - it'll just be the +# branch name. Based on the results, we clone. +BRANCH_ARRAY=(${head//:/ }) +TRY_ORG=$deforg +TRY_BRANCH=${BRANCH_ARRAY[0]} +if [[ "$head" == *":"* ]]; then + # ... but only match that fork if it's a real fork + if [ "${BRANCH_ARRAY[0]}" != "$PR_ORG" ]; then + TRY_ORG=${BRANCH_ARRAY[0]} + fi + TRY_BRANCH=${BRANCH_ARRAY[1]} +fi +clone "$TRY_ORG" "$defrepo" "$TRY_BRANCH" + +exit 1 diff --git a/scripts/fetchdep.sh b/scripts/fetchdep.sh deleted file mode 100755 index 517ed89..0000000 --- a/scripts/fetchdep.sh +++ /dev/null @@ -1,41 +0,0 @@ -#!/bin/bash - -set -x - -deforg="$1" -defrepo="$2" -defbranch="$3" - -[ -z "$defbranch" ] && defbranch="develop" - -rm -r "$defrepo" || true - -clone() { - org=$1 - repo=$2 - branch=$3 - if [ -n "$branch" ] - then - echo "Trying to use $org/$repo#$branch" - # Disable auth prompts: https://serverfault.com/a/665959 - GIT_TERMINAL_PROMPT=0 git clone https://github.com/$org/$repo.git $repo --branch "$branch" --depth 1 && exit 0 - fi -} - -# Try the PR author's branch in case it exists on the deps as well. -# If BUILDKITE_BRANCH is set, it will contain either: -# * "branch" when the author's branch and target branch are in the same repo -# * "author:branch" when the author's branch is in their fork -# We can split on `:` into an array to check. -BUILDKITE_BRANCH_ARRAY=(${BUILDKITE_BRANCH//:/ }) -if [[ "${#BUILDKITE_BRANCH_ARRAY[@]}" == "1" ]]; then - clone $deforg $defrepo $BUILDKITE_BRANCH -elif [[ "${#BUILDKITE_BRANCH_ARRAY[@]}" == "2" ]]; then - clone ${BUILDKITE_BRANCH_ARRAY[0]} $defrepo ${BUILDKITE_BRANCH_ARRAY[1]} -fi -# Try the target branch of the push or PR. -clone $deforg $defrepo $BUILDKITE_PULL_REQUEST_BASE_BRANCH -# Try the current branch from Jenkins. -clone $deforg $defrepo `"echo $GIT_BRANCH" | sed -e 's/^origin\///'` -# Use the default branch as the last resort. -clone $deforg $defrepo $defbranch