peter-evans / create-pull-request

A GitHub action to create a pull request for changes to your repository in the actions workspace
MIT License
2.12k stars 414 forks source link

Branch is not created from default branch and it is creating from base branch #3364

Open Poornachand200 opened 2 weeks ago

Poornachand200 commented 2 weeks ago

Branch is not created from default branch and it is creating from base branch

Describe your issue here.

I need to create a pull request to target branch test1 or test2. But I want to create the branch with reference to main. This means branch should be created or checked out from main branch. But the branch doesn't have changes from main. Instead it is getting created from test1 branch. Below is the example. The workflow actions runs on main branch.

Steps to reproduce

name: "kickstart the creation of a dynamic environment"

permissions:
  contents: write
  issues: write
  pull-requests: write
  id-token: write

on:
  workflow_dispatch:
    inputs:
      JIRA-ID:
        description: "Jira ticket number, e.g. ID-1234"
        required: true
        default: "ID-1234"
      PR-type:
        type: choice
        description: "choose the PR type from the given options"
        options:
          - fix
          - feat
          - breaking-change
          - build
          - chore
          - ci
          - docs
          - style
          - refactor
          - perf
        required: true
        default: "feat"
      PR-title:
        description: "PR title in the format - e.g: Adding Refresh token to Oauth client"
        required: true
      label:
        type: choice
        description: "label to be added to PR"
        required: true
        options:
          - AD
          - AM
        default: "AD"
      test-environment:
        type: choice
        description: "test environment to promote to"
        options:
          - test1
          - test2
        required: true
        default: "test1"

env:
  PING_HELM_CHART_VERSION: 0.9.12

jobs:
  kickstart_dynamic_environment:
    if: inputs.JIRA-ID && inputs.JIRA-ID != '' && contains(inputs.JIRA-ID, 'ID-')
    runs-on: ubuntu-latest
    name: Kick start dynamic environment
    environment: conf-${{ inputs.JIRA-ID }}
    steps:
      - uses: actions/checkout@v2

      - name: Dump GitHub context
        id: github_context_step
        run: echo '${{ toJSON(github) }}'

      - name: Convert JIRA ticket number to lowercase
        run: |
          head_ref_TEMP="${{ inputs.JIRA-ID }}"
          echo "LOWERCASE_JIRA_ID=${head_ref_TEMP,,}" >>${GITHUB_ENV}

      - name: Create a random tmp file
        run: |
          touch ./.github/tmp/${{ inputs.JIRA-ID}}.txt

      - name: check if branch exists
        id: set_branch
        run: |
          if git ls-remote --exit-code --heads origin ${{ inputs.JIRA-ID }}; then
            echo "branch variable :  ${{ inputs.JIRA-ID}}"
          else
            echo "branch variable :  ${{ inputs.JIRA-ID}}"
            echo "branch_not_exists=yes" >> $GITHUB_ENV
            echo "not exists"
          fi

      - name: Create Pull Request
        if: env.branch_not_exists != ' ' 
        uses: peter-evans/create-pull-request@v5
        with:
          token: ${{ secrets.REPOSITORY_ROBOT_PAT }}
          branch: ${{ inputs.JIRA-ID }}
          commit-message: "Kickstart the creation of a Dynamic Environment"
          title: "${{ inputs.PR-type }}(${{inputs.JIRA-ID}}): ${{ inputs.PR-title }}"
          assignees: ${{ github.actor }}
          base: ${{ inputs.test-environment }}
          draft: true
          labels: ${{ inputs.label }}
          body: |
            Jira ticket: ${{ env.LOWERCASE_JIRA_ID }}

            The dynamic environment will be available at the following addresses when it's ready:
            - https://${{ env.LOWERCASE_JIRA_ID }}-federate.dev.ciam.non-prod.managed-eks.aws.nuuday.nu
            - https://${{ env.LOWERCASE_JIRA_ID }}-federate-engine.dev.ciam.non-prod.managed-eks.aws.nuuday.nu
            - https://${{ env.LOWERCASE_JIRA_ID }}-directory.dev.ciam.non-prod.managed-eks.aws.nuuday.nu

If this issue is describing a possible bug please provide (or link to) your GitHub Actions workflow.

Poornachand200 commented 1 week ago

@peter-evans Please update on this.

peter-evans commented 1 week ago

Hi @Poornachand200

This is not a particularly common workflow, so I'm not 100% sure if what you want to do will work, but it looks like it should.

First of all, check that main is being checked out properly in the first step. You could explicitly check it out by adding ref. Also, just in case, update checkout to v4.

- uses: actions/checkout@v4
  with:
    ref: main
rwader-swi commented 1 week ago

I already had a branch and wanted to create a pullrequest whose base will be the other branch which I had defined while calling this action. But what this action does is just resets my already present PR branch to make it same as base branch and then says "this branch no longer differs from the base". Then I also tried creating another branch from my already created PR branch by giving different input to branch but then it just says _Branch 'newbranch' is not ahead of base '***' and will not be created

rwader-swi commented 1 week ago

can you suggest me a workaround for above? @peter-evans

Poornachand200 commented 1 week ago

@peter-evans checkout is updated as below. It seems the reference is still from the target branch test1 or test2.

Poornachand200 commented 1 week ago

@peter-evans We are also encountering conflicts because of this case.

Poornachand200 commented 1 week ago

@peter-evans Please update

Poornachand200 commented 1 week ago

@peter-evans We are facing conflicts because of this issue. Please assist.

peter-evans commented 1 week ago

@Poornachand200 I'm afraid I don't have a lot of time at the moment. The next thing you can do is provide a log of the complete run so I can see what is happening.

Poornachand200 commented 1 week ago

logs_28879649215.zip PFA of PR creation workflow log

peter-evans commented 1 week ago

@Poornachand200 I think I understand what you mean now. You need to follow the example here: https://github.com/peter-evans/create-pull-request/blob/main/docs/examples.md#keep-a-branch-up-to-date-with-another

i.e. Checkout the target, e.g. test1, then reset the branch using main. Then call this action.