Skip to content

Commit

Permalink
ci: Fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
CycleBai committed Jul 29, 2024
1 parent 1df667b commit 5128adf
Showing 1 changed file with 20 additions and 67 deletions.
87 changes: 20 additions & 67 deletions .github/workflows/auto_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@ on:
- dev

jobs:
create_branch_and_pr:
create_or_update_pr:
runs-on: ubuntu-latest

outputs:
branch_name: ${{ steps.hash_committer.outputs.branch_name }}
branch_exists: ${{ steps.check_branch.outputs.branch_exists }}

steps:
- name: Checkout repository
uses: actions/checkout@v2
Expand All @@ -39,41 +35,18 @@ jobs:
git config --global user.name "${{ env.committer_username }}"
git config --global user.email "${{ env.committer_email }}"
- name: Check if PR branch exists
id: check_branch
run: |
git fetch origin
if git show-ref --verify --quiet refs/remotes/origin/${{ env.branch_name }}; then
echo "::set-output name=branch_exists::true"
else
echo "::set-output name=branch_exists::false"
fi
- name: Create and push new branch based on dev
if: steps.check_branch.outputs.branch_exists == 'false'
run: |
git fetch origin
git checkout dev
git pull origin dev
git checkout -b ${{ env.branch_name }}
echo "$(date +%s%N)" > timestamp.txt # Create a file with current timestamp in nanoseconds
git add timestamp.txt
git commit -m "Add timestamp file to ensure branch differences"
git push origin ${{ env.branch_name }}
- name: Fetch, rebase and push to existing branch
if: steps.check_branch.outputs.branch_exists == 'true'
run: |
git fetch origin
git checkout ${{ env.branch_name }}
git pull origin ${{ env.branch_name }} --rebase
git checkout -b ${{ steps.hash_committer.outputs.branch_name }}
echo "$(date +%s%N)" > timestamp.txt # Create a file with current timestamp in nanoseconds
git add timestamp.txt
git commit -m "Add timestamp file to ensure branch differences"
git push origin ${{ env.branch_name }}
git push origin ${{ steps.hash_committer.outputs.branch_name }}
- name: Create Pull Request
if: steps.check_branch.outputs.branch_exists == 'false'
- name: Create or Update Pull Request
id: create_pr
uses: peter-evans/create-pull-request@v3
with:
Expand All @@ -83,38 +56,11 @@ jobs:
title: "Auto PR from ${{ steps.hash_committer.outputs.branch_name }}"
body: "This is an automated PR created from ${{ steps.hash_committer.outputs.branch_name }}"
draft: false

- name: Wait for PR creation
if: steps.check_branch.outputs.branch_exists == 'false'
run: sleep 30 # Wait for 30 seconds to ensure PR creation is processed

- name: Get PR number
if: steps.check_branch.outputs.branch_exists == 'false'
id: get_pr_number
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
pr_number=$(gh pr list --state open --head ${{ steps.hash_committer.outputs.branch_name }} --json number -q '.[0].number')
echo "pr_number=$pr_number" >> $GITHUB_ENV
echo "PR number: $pr_number"
- name: Append commit to existing PR
if: steps.check_branch.outputs.branch_exists == 'true'
id: append_commit
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
pr_number=$(gh pr list --state open --head ${{ steps.hash_committer.outputs.branch_name }} --json number -q '.[0].number')
if [ -n "$pr_number" ]; then
echo "pr_number=$pr_number" >> $GITHUB_ENV
gh pr comment $pr_number --body "New commits have been added to the PR."
else
echo "No open PR found, creating a new one."
gh pr create --head ${{ steps.hash_committer.outputs.branch_name }} --base main --title "Auto PR from ${{ steps.hash_committer.outputs.branch_name }}" --body "This is an automated PR created from ${{ steps.hash_committer.outputs.branch_name }}"
update_existing: true # Automatically update the existing PR if it exists

sync_commits:
runs-on: ubuntu-latest
needs: create_branch_and_pr
needs: create_or_update_pr

steps:
- name: Checkout repository
Expand All @@ -123,14 +69,21 @@ jobs:
- name: Install GitHub CLI
run: sudo apt-get install gh

- name: Configure git
- name: Get committer username
run: |
git config --global user.name "${{ env.committer_username }}"
git config --global user.email "${{ env.committer_email }}"
committer_username=$(git log -1 --pretty=format:'%an')
echo "committer_username=$committer_username" >> $GITHUB_ENV
- name: Hash committer username
id: hash_committer
run: |
branch_name=$(echo -n "${{ env.committer_username }}" | md5sum | awk '{ print $1 }')
echo "branch_name=dev_pr_$branch_name" >> $GITHUB_ENV
echo "::set-output name=branch_name::dev_pr_$branch_name"
- name: Fetch and rebase changes
- name: Fetch and rebase on existing branch
run: |
git fetch origin
git checkout ${{ needs.create_branch_and_pr.outputs.branch_name }}
git pull origin dev --rebase
git push origin ${{ needs.create_branch_and_pr.outputs.branch_name }}
git checkout ${{ needs.create_or_update_pr.outputs.branch_name }}
git pull origin ${{ needs.create_or_update_pr.outputs.branch_name }} --rebase
git push origin ${{ needs.create_or_update_pr.outputs.branch_name }}

0 comments on commit 5128adf

Please sign in to comment.