Make GH actions clone from the correct fork and cleanup

Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com>
This commit is contained in:
Šimon Brandner 2021-08-29 13:42:12 +02:00
parent 5e934c17da
commit 5b65528038
No known key found for this signature in database
GPG Key ID: 55C211A1226CB17D
2 changed files with 29 additions and 33 deletions

View File

@ -10,6 +10,8 @@ on:
jobs: jobs:
end-to-end: end-to-end:
runs-on: ubuntu-latest runs-on: ubuntu-latest
env:
PR_NUMBER: ${{github.event.number}}
container: vectorim/element-web-ci-e2etests-env:latest container: vectorim/element-web-ci-e2etests-env:latest
steps: steps:
- name: Checkout code - name: Checkout code

View File

@ -10,6 +10,7 @@ defbranch="$3"
rm -r "$defrepo" || true rm -r "$defrepo" || true
# A fnction that clones a branch of a repo based on the org, repo and branch
clone() { clone() {
org=$1 org=$1
repo=$2 repo=$2
@ -21,45 +22,38 @@ clone() {
fi fi
} }
# Try the PR author's branch in case it exists on the deps as well. # A function that gets info about a PR from the GitHub API based on its number
# First we check if GITHUB_HEAD_REF is defined, getPRInfo() {
# Then we check if BUILDKITE_BRANCH is defined, number=$1
# if they aren't we can assume this is a Netlify build if [ -n "$number" ]; then
if [ -n "$GITHUB_HEAD_REF" ]; then echo "Getting info about a PR with number $number"
head=$GITHUB_HEAD_REF
elif [ -n "$BUILDKITE_BRANCH" ]; then
head=$BUILDKITE_BRANCH
else
# Netlify doesn't give us info about the fork so we have to get it from GitHub API
apiEndpoint="https://api.github.com/repos/matrix-org/matrix-react-sdk/pulls/"
apiEndpoint+=$REVIEW_ID
head=$(curl $apiEndpoint | jq -r '.head.label')
fi
# If head is set, it will contain on Buildkite either: apiEndpoint="https://api.github.com/repos/matrix-org/matrix-react-sdk/pulls/"
# * "branch" when the author's branch and target branch are in the same repo apiEndpoint+=$number
# * "fork:branch" when the author's branch is in their fork or if this is a Netlify build
# We can split on `:` into an array to check.
# For GitHub Actions we need to inspect GITHUB_REPOSITORY and GITHUB_ACTOR
# to determine whether the branch is from a fork or not
BRANCH_ARRAY=(${head//:/ })
if [[ "${#BRANCH_ARRAY[@]}" == "1" ]]; then
if [ -n "$GITHUB_HEAD_REF" ]; then head=$(curl $apiEndpoint | jq -r '.head.label')
if [[ "$GITHUB_REPOSITORY" == "$deforg"* ]]; then
clone $deforg $defrepo $GITHUB_HEAD_REF
else
REPO_ARRAY=(${GITHUB_REPOSITORY//\// })
clone $REPO_ARRAY[0] $defrepo $GITHUB_HEAD_REF
fi
else
clone $deforg $defrepo $BUILDKITE_BRANCH
fi fi
}
elif [[ "${#BRANCH_ARRAY[@]}" == "2" ]]; then # Some CIs don't give us enough info, so we just get the PR number and ask the
clone ${BRANCH_ARRAY[0]} $defrepo ${BRANCH_ARRAY[1]} # GH API for more info - "fork:branch". Some give us this directly.
if [ -n "$BUILDKITE_BRANCH" ]; then
# BuildKite
head=$BUILDKITE_BRANCH
elif [ -n "$PR_NUMBER" ]; then
# GitHub
getPRInfo $PR_NUMBER
elif [ -n "$REVIEW_ID" ]; then
# Netlify
getPRInfo $REVIEW_ID
fi fi
# $head will always be in the format "fork:branch", so we split it by ":" into
# an array. The first element will then be the fork and the second the branch.
# Based on that we clone
BRANCH_ARRAY=(${head//:/ })
clone ${BRANCH_ARRAY[0]} $defrepo ${BRANCH_ARRAY[1]}
# Try the target branch of the push or PR. # Try the target branch of the push or PR.
if [ -n $GITHUB_BASE_REF ]; then if [ -n $GITHUB_BASE_REF ]; then
clone $deforg $defrepo $GITHUB_BASE_REF clone $deforg $defrepo $GITHUB_BASE_REF