forked from brave/brave-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync-and-rebase-pr-from-fork.yml
More file actions
85 lines (72 loc) · 3.29 KB
/
sync-and-rebase-pr-from-fork.yml
File metadata and controls
85 lines (72 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
name: Sync and rebase PR from fork
on:
workflow_dispatch:
inputs:
PR_NUMBER:
required: false
description: "Number of the PR from the fork"
COMMIT_HASH:
required: true
description: "Commit hash of the PR"
permissions:
contents: write
pull-requests: write
jobs:
sync-and-rebase-pr-from-fork:
runs-on: ubuntu-slim
steps:
- name: Checkout repository
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
with:
fetch-depth: 0
- name: Setup Git
run: |
git config user.name brave-builds
git config user.email devops@brave.com
git config push.default simple
- name: Sync and rebase PR from fork
env:
GITHUB_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
PR_NUMBER: ${{ inputs.PR_NUMBER }}
COMMIT_HASH: ${{ inputs.COMMIT_HASH }}
run: |
set -x
if ! grep -qix '[0-9a-f]\{40,40\}' <<<"$COMMIT_HASH"; then
echo "Provide a full commit hash; got: $COMMIT_HASH"
exit 1
fi
pr_number="${PR_NUMBER:-"$(gh pr list --search "$COMMIT_HASH" --json number -q '.[].number')"}"
gh_pr_view=$(gh pr view "${pr_number:?}" --json baseRefName,headRefName,headRepository,headRepositoryOwner,id,isCrossRepository,url)
JQ="jq -e -r"
baseRefName=$($JQ ".baseRefName" <<<"$gh_pr_view")
headRefName=$($JQ ".headRefName" <<<"$gh_pr_view")
headRepositoryName=$($JQ ".headRepository.name" <<<"$gh_pr_view")
headRepositoryOwnerLogin=$($JQ ".headRepositoryOwner.login" <<<"$gh_pr_view")
isCrossRepository=$($JQ ".isCrossRepository" <<<"$gh_pr_view")
url=$($JQ ".url" <<<"$gh_pr_view")
[[ "$isCrossRepository" = "true" ]] || { echo "PR is not cross repo. Exiting!"; exit 1; }
git remote add contributor "https://github.com/$headRepositoryOwnerLogin/$headRepositoryName.git" || :
contribHeadRefName="contributor-$headRepositoryOwnerLogin-$headRefName"
git fetch --no-tags contributor +"$headRefName":"$contribHeadRefName"
# Note we are checking out the provided commit hash instead of the pr-branch to avoid race condition
# The author could have commited malicious code between the last review and the execution of this workflow
git checkout "$COMMIT_HASH"
# Rebase the branch with the latest targeting branch HEAD
git fetch origin "$baseRefName"
if ! git rebase FETCH_HEAD; then
echo "Rebase conflict detected. Please resolve the conflicts and push the changes to your fork. Exiting!"
exit 1
fi
git push -f origin HEAD:"refs/heads/$contribHeadRefName"
existing_prs=$(gh pr list -H "$contribHeadRefName" --json number)
if [[ "$existing_prs" = "[]" ]]; then
gh pr create \
--draft \
--base "$baseRefName" \
--head "$contribHeadRefName" \
--title "CI run for contributor PR #$pr_number" \
--body \
"## Description
This PR is created to run CI on the changes proposed in PR #$pr_number by @$headRepositoryOwnerLogin.
**This PR should not be merged.**"
fi