Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to customize branch in runVersion #254

Open
ernestognw opened this issue Jan 19, 2023 · 10 comments
Open

Allow to customize branch in runVersion #254

ernestognw opened this issue Jan 19, 2023 · 10 comments

Comments

@ernestognw
Copy link
Contributor

ernestognw commented Jan 19, 2023

Description

Hello, we recently implemented an automated release cycle via Changesets in OpenZeppelin contracts and we had problems trying to create a custom Github Actions Workflow with chained jobs using needs: [...] because it was impossible to tell the Changesets Action what branch to use. I think it'd be ideal to define it as an input.

Basically, we just want to override this option.

Is there something to consider? We already merge the changesets integration but I'd love to send a PR with this change as well.

@Andarist
Copy link
Member

Hm, so you have a workflow that starts in branch A but one that creates a branch B (or something like that) and you want to create a versioning PR from changeset-release/B to B. Did I get this right?

@ernestognw
Copy link
Contributor Author

That's correct, we need to chain the B branch creation with your action, but since the run starts in A, we can't customize it.

@Andarist
Copy link
Member

I would accept a PR for this - we can bikeshed the input's name in the review comments. For now let's just go with a simple branch

@ernestognw
Copy link
Contributor Author

I would accept a PR for this - we can bikeshed the input's name in the review comments. For now let's just go with a simple branch

Thanks for the fast response! I created a PR with the desired change. Would love also feedback for #250

@rohit-gohri
Copy link

We have a similar usecase to this:

  • Default branch: "develop" - Workflow runs here
  • Production branch: "main" - We want Version PR to target this

@typeofweb
Copy link

Hey @Andarist! Any updates on this? The PR is open and looks like a rather small change.

@bgfernandes
Copy link

Hey all, this is very interesting for our use case as well, we have a monorepo and and to have a different release PR for each package in the monorepo, we are doing a github action for each package using changeset version --ignore <package1> --ignore <package2>, the problem is that they clash with each other on the branch name and can't create a second PR.

@nicvickeryw
Copy link

Hey all, this is very interesting for our use case as well, we have a monorepo and and to have a different release PR for each package in the monorepo, we are doing a github action for each package using changeset version --ignore <package1> --ignore <package2>, the problem is that they clash with each other on the branch name and can't create a second PR.

We're in an almost identical situation, would love to have the ability to use separate branches for each changeset PR.

@rohit-gohri
Copy link

I wanted to test if the PR #255 would work but since actions runs from dist folder it's not possible to test the branch directly.
Tried overwriting the GITHUB_REF option with an env var but Github actions doesn't allow overwriting GITHUB_* env vars.

Would be happy to help move the PR forward

@rohit-gohri
Copy link

I ended up adding a hacky workflow that does the same thing as this action with custom branches:

hacky workflow
  # Hack while custom branch is not officially supported in changeset action - https://github.com/changesets/action/issues/254
  release_pr:
    name: Release PR
    if: ${{ github.event_name == 'push' && github.ref_name == 'develop' }}
steps:
  - name: Checkout Repo
    uses: actions/checkout@v4
    with:
      fetch-depth: 0

  - name: git config
    run: |
	  git config user.name github-actions[bot]
      git config user.email 41898282+github-actions[bot]@users.noreply.github.com

  - name: Setup Node.js
    uses: actions/setup-node@v3
    with:
      node-version-file: .nvmrc
      cache: 'yarn'

  - name: 📦 Install dependencies
    run: yarn install --immutable

  - name: Changeset status
    id: changeset-status
    shell: bash
    run: |
      mkdir -p out
      yarn run changeset status --since=origin/main --output out/changeset_status.json
      cat out/changeset_status.json | jq '.releases | length' > "out/releases_size.json"
      echo "release_size=$(cat out/releases_size.json)"  >> "$GITHUB_OUTPUT"

  - name: Create branch
    if: ${{ steps.changeset-status.outputs.release_size != '0' }}
    id: version
    shell: bash
    run: |
      set -x
      git branch -D changeset-release/${{ github.ref_name }} || true
      git checkout -b changeset-release/${{ github.ref_name }}
      git reset --hard ${{ github.sha }}
      yarn run version
      git push origin HEAD:changeset-release/${{ github.ref_name }} --force
      {
        echo 'changelog<<EOF'
        yarn run generate-changelog
        echo EOF
      } >> "$GITHUB_OUTPUT"
    env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  - name: 🔀 Create Release PR
    if: ${{ steps.changeset-status.outputs.release_size != '0' }}
    uses: actions/github-script@v7
    env:
      RELEASE_NOTES: ${{ steps.version.outputs.changelog }}
    with:
      script: |
        let base = 'main';
        let head = 'changeset-release/${{ github.ref_name }}';

        let title = `Release PR`;
        let body = `${process.env.RELEASE_NOTES}`.trim();

        let searchQuery = `repo:${context.repo.owner}/${context.repo.repo}+state:open+head:${head}+base:${base}+is:pull-request`;

        const [existingPR] = (await github.rest.search.issuesAndPullRequests({
          q: searchQuery,
        })).data.items;

        if (existingPR) {
          await github.rest.pulls.update({
            owner: context.repo.owner,
            repo: context.repo.repo,
            pull_number: existingPR.number,
            base,
            body,
            title,
          });
        }
        else {
          await github.rest.pulls.create({
            owner: context.repo.owner,
            repo: context.repo.repo,
            draft: true,
            head,
            base,
            body,
            title,
          });
        }

Maybe a better solution would be to instead solve #310 and #317 and let users handle the PR creation process on their own.
Right now I had to create my own custom generate-changelog script and remove the action completely as there is no option to disable PR creation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants