Closed Init commit from sandbox #68
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | ||
name: Github PR to Gerrit Change request | ||
# yamllint disable-line rule:truthy | ||
on: | ||
pull_request_target: | ||
types: [opened, synchronize, edited, reopened] | ||
branches: | ||
- master | ||
- main | ||
# Permission added at job level or workflow level | ||
# required to for updating comments on the PR | ||
permissions: write-all | ||
concurrency: | ||
group: ${{ github.run_id }} | ||
cancel-in-progress: true | ||
jobs: | ||
actionlint: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Clone Github repo with the PR | ||
uses: actions/checkout@v4 | ||
with: | ||
ssh-user: ${{ vars.GERRIT_SSH_USER_G2G }} | ||
# ref: ${{ github.event.pull_request.head.sha }} | ||
# repository: "${{ github.repository }}" | ||
- name: Download actionlint | ||
id: get_actionlint | ||
# yamllint disable-line rule:line-length | ||
run: bash <(curl https://raw.githubusercontent.com/rhysd/actionlint/main/scripts/download-actionlint.bash) | ||
shell: bash | ||
- name: Check workflow files | ||
run: ${{ steps.get_actionlint.outputs.executable }} -color | ||
shell: bash | ||
github2gerrit: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/setup-python@v5 | ||
id: setup-python | ||
with: | ||
python-version: "3.11" | ||
- name: "Install required dependencies: git-review,jq" | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install "git-review==2.3.1" jq | ||
git review --version | ||
jq --version | ||
# - name: Read .gitreview and populate information | ||
# shell: bash | ||
# run: | | ||
# set -x | ||
# | ||
# cd "$GITHUB_WORKSPACE/${{ env.PROJECT_REPO_GITHUB }}-${{ env.GERRIT_BRANCH }}" | ||
# project_repo_gerrit_git=$(grep -E "project" .gitreview | cut -d "=" -f2) | ||
# # # change any '-' to '/' and remove owner name | ||
# project_repo_gerrit="${project_repo_gerrit_git//.*}" | ||
# project_repo_github="${project_repo_gerrit////-}" | ||
# | ||
# echo "PROJECT_REPO_GERRIT=${project_repo_gerrit}" >> "$GITHUB_ENV" | ||
# echo "PROJECT_REPO_GITHUB=${project_repo_github}" >> "$GITHUB_ENV" | ||
- name: Set env with Gerrit Github repo format | ||
shell: bash | ||
run: | | ||
set -x | ||
project_repo_github="${{ github.repository }}" | ||
# change any '-' to '/' and remove owner name | ||
project_repo_github="${project_repo_github#*/}" | ||
project_repo_gerrit="${project_repo_github//-//}" | ||
echo "PROJECT_REPO_GERRIT=${project_repo_gerrit}" >> "$GITHUB_ENV" | ||
echo "PROJECT_REPO_GITHUB=${project_repo_github}" >> "$GITHUB_ENV" | ||
- name: Set env GITHUB branch in env | ||
shell: bash | ||
run: | | ||
gerrit_branch="${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" | ||
echo "GERRIT_BRANCH=${gerrit_branch}" >> "$GITHUB_ENV" | ||
- name: Clone Github repo where the PR is submitted | ||
uses: actions/checkout@v4 | ||
with: | ||
ssh-user: ${{ vars.GERRIT_SSH_USER_G2G }} | ||
path: "${{ env.PROJECT_REPO_GITHUB }}-${{ env.GERRIT_BRANCH }}" | ||
fetch-depth: 0 | ||
ref: ${{ github.event.pull_request.head.sha }} | ||
# repository: "${{ github.repository }}" | ||
- name: Print last X commits in the git log | ||
if: env.ACT == 'true' | ||
run: | | ||
set -x | ||
cd "$GITHUB_WORKSPACE/${{ env.PROJECT_REPO_GITHUB }}-${{ env.GERRIT_BRANCH }}" | ||
git --no-pager log --graph --all --decorate --pretty=oneline -n5 | ||
- name: Install SSH Key | ||
uses: shimataro/ssh-key-action@d4fffb50872869abe2d9a9098a6d9c5aa7d16be4 # v2.7.0 | ||
with: | ||
key: ${{ secrets.GERRIT_SSH_PRIVKEY_G2G }} | ||
name: "id_rsa" | ||
known_hosts: ${{ vars.GERRIT_KNOWN_HOSTS }} | ||
config: | | ||
Host ${{ vars.GERRIT_SERVER }} | ||
User ${{ vars.GERRIT_SSH_USER_G2G }} | ||
Port 29418 | ||
PubkeyAcceptedKeyTypes +ssh-rsa | ||
IdentityFile ~/.ssh/id_rsa | ||
- name: Setup Gerrit remote | ||
if: env.PROJECT_REPO_GERRIT != '' | ||
shell: bash | ||
run: | | ||
set -x | ||
# Query for a pre-existing gerrit review | ||
cd "$GITHUB_WORKSPACE/${{ env.PROJECT_REPO_GITHUB }}-${{ env.GERRIT_BRANCH }}" | ||
# Setup global git config required by git-review | ||
git config --global gitreview.username ${{ vars.GERRIT_SSH_USER_G2G }} | ||
git config --global user.name ${{ vars.GERRIT_SSH_USER_G2G }} | ||
git config --global user.email ${{ vars.GERRIT_SSH_USER_G2G_EMAIL }} | ||
# Add Gerrit remote and print remote settings | ||
git remote add gerrit ssh://${{ vars.GERRIT_SSH_USER_G2G }}@${{ vars.GERRIT_SERVER }}:29418/${{ env.PROJECT_REPO_GERRIT }}.git | ||
git remote -v | ||
# Workaround for git-review failing to copy the commit-msg hook to submodules | ||
git config core.hooksPath "$(git rev-parse --show-toplevel)/.git/hooks" | ||
# Init gerrit repo | ||
git review -s -v | ||
- name: Print the pull-request | ||
run: | | ||
set -x | ||
export PR_NUMBER="${{ github.event.pull_request.number || github.event.issue.number }}" | ||
echo "PR_NUMBER=$PR_NUMBER" >> "$GITHUB_ENV" | ||
# Set a temp PR to test with ACT | ||
if [[ $PR_NUMBER == '' ]]; then | ||
echo "PR_NUMBER=${{ vars.PR_NUM }}" >> "$GITHUB_ENV" | ||
fi | ||
- name: Download the PR .patch and .diff locally | ||
if: env.PR_NUMBER != '' | ||
run: | | ||
set -x | ||
patch_path="${{ vars.ORGANIZATION }}/${{ env.PROJECT_REPO_GITHUB }}/pull/${{ env.PR_NUMBER }}.patch" | ||
wget -q "https://github.com/${patch_path}" | ||
if [[ -f "${{ env.PR_NUMBER }}.patch" ]]; then | ||
echo "PR-${{ env.PR_NUMBER }} download successfully" | ||
fi | ||
- name: Get the number of commits in the pull-request | ||
if: ${{ github.event_name == 'pull_request_target' }} | ||
run: | | ||
set -x | ||
cd "$GITHUB_WORKSPACE/${{ env.PROJECT_REPO_GITHUB }}-${{ env.GERRIT_BRANCH }}" | ||
num_commits="$(gh pr view ${{ env.PR_NUMBER }} --json commits | jq '.[] | length')" | ||
echo "PR_COMMITS=$num_commits" >> "$GITHUB_ENV" | ||
env: | ||
GH_TOKEN: ${{ github.token }} | ||
- name: Print diff sha and refs | ||
shell: bash | ||
run: | | ||
set -x | ||
echo "github.sha: ${{ github.sha }}" | ||
echo "github.ref: ${{ github.ref }}" | ||
echo "github.ref_name: ${{ github.ref_name }}" | ||
echo "github.event.pull_request.base.ref: ${{ github.event.pull_request.base.ref }}" | ||
echo "github.event.pull_request.base.sha: ${{ github.event.pull_request.base.sha }}" | ||
echo "github.event.pull_request.head.ref: ${{ github.event.pull_request.head.ref }}" | ||
echo "github.event.pull_request.head.sha: ${{ github.event.pull_request.head.sha }}" | ||
echo "github.merge_commit_sha: ${{ github.sha }}" | ||
echo "github.merge_commit_sha: ${{ github.merge_commit_sha }}" | ||
- name: Apply patch on local repo | ||
if: hashFiles('${{ env.PR_NUMBER }}.patch') != '' | ||
shell: bash | ||
run: | | ||
set -x | ||
cd "$GITHUB_WORKSPACE/${{ env.PROJECT_REPO_GITHUB }}-${{ env.GERRIT_BRANCH }}" | ||
# Apply the patch on the HEAD | ||
# git am --show-current-patch=diff "$GITHUB_WORKSPACE/${{ env.PR_NUMBER }}.patch" | ||
git --no-pager log --graph --all --decorate --pretty=oneline -n"${{ env.PR_COMMITS }}" | ||
git status | ||
# Create a Change-id from all the PR | ||
# Capture the last commit HEAD | ||
# git rev-parse HEAD > last-head-commit-id.txt | ||
# Squash all commits in pull-request into a single commit | ||
git reset --soft "${{ github.sha }}" | ||
# Check if change-id is present in each commit and use the first one | ||
# Capture and check for signed-of-by and Change-Id | ||
git log -v --format=%B --reverse HEAD..HEAD@{1} | grep -E "^(Change-Id)" > change-ID.txt || true | ||
git log -v --format=%B --reverse HEAD..HEAD@{1} | grep -E "^(Signed-off-by)" > signed-off-by.txt || true | ||
git log -v --format=%B --reverse HEAD..HEAD@{1} | grep -Ev "^(Signed-off-by|Change-Id)" > commit-msg.txt | ||
if [[ -f commit-msg.txt ]]; then | ||
cat commit-msg.txt | ||
commit_message="${commit_message:-commit-msg.txt}" | ||
fi | ||
if [[ -f change-ID.txt ]]; then | ||
cat change-ID.txt | ||
commit_message+=' ' | ||
commit_message+="change-ID.txt" | ||
fi | ||
if [[ -f signed-off-by.txt ]]; then | ||
cat signed-off-by.txt | sort | uniq -d > signed-off-by-final.txt | ||
cat signed-off-by-final.txt | ||
commit_message+=' ' | ||
commit_message+="signed-off-by-final.txt" | ||
fi | ||
git commit -s -v --amend --no-edit -m "$(cat ${commit_message})" | ||
git log -n2 | ||
# TODO: Get rid of any merge commit in the patch | ||
# TODO: process each commint one at a time in a loop: foreach or iterate | ||
- name: Submit the change to Gerrit repository | ||
id: submit | ||
if: env.PR_NUMBER != '' | ||
run: | | ||
set -x | ||
cd "$GITHUB_WORKSPACE/${{ env.PROJECT_REPO_GITHUB }}-${{ env.GERRIT_BRANCH }}" | ||
# If the reviewers email is unset/empty then use a default | ||
reviewers_email=${REVIEWERS_EMAIL:-"${{ vars.GERRIT_SSH_USER_G2G_EMAIL }}"} | ||
echo "git review .... inprogress" | ||
git review --yes -t "GH-PR-${{ env.PR_NUMBER }}" --reviewers "$reviewers_email" | ||
# retrive change-id from the submitted PR | ||
gerrit_change_id=$(git show HEAD --format=%B -s | grep Change-Id: | cut -d " " -f2;) | ||
# Set a temp PR to test with ACT | ||
if [[ "$gerrit_change_id" == '' ]]; then | ||
echo "FAIL: Could not find the change-Id" | ||
else | ||
echo "GERRIT_CHANGE_ID=${gerrit_change_id}" >> "$GITHUB_ENV" | ||
fi | ||
# echo "GERRIT_CHANGE_ID=Iafa61d4b861941624c4c44b0fde21f534a7344ad" >> "$GITHUB_ENV" | ||
- name: Set fake change-Id to with nektos-act | ||
if: ( steps.submit.outputs.gerrit_change_id != '' && env.ACT == 'true' ) | ||
run: | | ||
set -x | ||
echo "GERRIT_CHANGE_ID=Iafa61d4b861941624c4c44b0fde21f534a7344ad" >> "$GITHUB_ENV" | ||
- name: Retrive the Gerrit change number from Change-ID | ||
if: env.GERRIT_CHANGE_ID != '' | ||
id: change_num | ||
shell: bash | ||
run: | | ||
set -x | ||
# Query for a pre-existing gerrit review to retrive Change-Id | ||
ssh -v -p 29418 "${{ vars.GERRIT_SSH_USER_G2G }}@${{ vars.GERRIT_SERVER }}" \ | ||
gerrit query limit:1 owner:self is:open project:"${{ env.PROJECT_REPO_GERRIT }}" \ | ||
--current-patch-set --format=JSON "${{ env.GERRIT_CHANGE_ID }}" > query_result.txt | ||
query_result_url=$(jq -r '.url | select( . != null )' query_result.txt) | ||
query_result_number=$(jq -r '.number | select( . != null )' query_result.txt) | ||
echo "GERRIT_CHANGE_REQUEST_URL=${query_result_url}" >> "$GITHUB_ENV" | ||
echo "GERRIT_CHANGE_REQUEST_NUMBER=${query_result_number}" >> "$GITHUB_ENV" | ||
- name: PR Comment update CR number | ||
if: env.GERRIT_CHANGE_REQUEST_URL != '' | ||
uses: actions/github-script@v7 | ||
with: | ||
result-encoding: string | ||
retries: 3 | ||
retry-exempt-status-codes: 400,401 | ||
script: | | ||
github.rest.issues.createComment({ | ||
issue_number: context.issue.number, | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
body: `The pull-request PR-${{ env.PR_NUMBER }} is submitted to Gerrit [${{ env.ORGANIZATION }}](https://${{ vars.GERRIT_SERVER }})! | ||
To follow up on the change visit: [${{ env.GERRIT_CHANGE_REQUEST_NUMBER }}](${{ env.GERRIT_CHANGE_REQUEST_URL }}) | ||
NOTE: The pull-request PR-${{ env.PR_NUMBER }} will be closed, re-opening the pull-request will not update the same commit and may result in duplicate changes on Gerrit.` | ||
- name: Close pull-request | ||
run: gh pr close --comment "Auto-closing pull request" --delete-branch "${{ env.PR_NUMBER }}" | ||
env: | ||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
- name: Clean up resources and close the PR | ||
run: | | ||
echo "closing the up PR" |