Skip to content

🐛 Bug: branch-ruleset API crashes if a ruleset already exists (transition mode) #1950

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

Closed
3 tasks done
JoshuaKGoldberg opened this issue Mar 4, 2025 · 0 comments · Fixed by #1951
Closed
3 tasks done
Labels
type: bug Something isn't working :(

Comments

@JoshuaKGoldberg
Copy link
Owner

JoshuaKGoldberg commented Mar 4, 2025

Bug Report Checklist

  • I have tried restarting my IDE and the issue persists.
  • I have pulled the latest main branch of the repository.
  • I have searched for related issues and found none that matched my issue.

Expected

Running npx create-typescript-app@beta with a config/preset that includes blockRepositoryBranchRuleset should pass on --mode transition.

Actual

If the ruleset already exists, the currently used API crashes:

◓  Running create-typescript-appError in request branch-ruleset RequestError [HttpError]: Validation Failed: "Name must be unique" - https://docs.github.com/rest/repos/rules#create-a-repository-ruleset
    at fetchWrapper (file:///Users/josh/repos/bingo/node_modules/.pnpm/@[email protected]/node_modules/@octokit/request/dist-bundle/index.js:122:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async requestWithGraphqlErrorHandling (file:///Users/josh/repos/bingo/node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@octokit/plugin-retry/dist-bundle/index.js:36:20)
    at async Job.doExecute (/Users/josh/repos/bingo/node_modules/.pnpm/[email protected]/node_modules/bottleneck/light.js:405:18) {
  status: 422,
  request: {
    method: 'POST',
    url: 'https://api.github.com/repos/JoshuaKGoldberg/my-typescript-app/rulesets',
    headers: {
      accept: 'application/vnd.github.v3+json',
      'user-agent': 'octokit.js/0.0.0-development octokit-core.js/6.1.4 Node.js/22',
      authorization: 'token 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\n',
      'content-type': 'application/json; charset=utf-8'
    },
    body: [Object: null prototype] {
      bypass_actors: [Array],
      conditions: [Object],
      enforcement: 'active',
      name: 'Branch protection for main',
      rules: [Array],
      target: 'branch'
    },
    request: {
      fetch: [Function: fetch],
      hook: [Function: bound bound register]
    }
  },
  response: {
    url: 'https://api.github.com/repos/JoshuaKGoldberg/my-typescript-app/rulesets',
    status: 422,
    headers: {
      'access-control-allow-origin': '*',
      'access-control-expose-headers': 'ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset',
      'content-length': '170',
      'content-security-policy': "default-src 'none'",
      'content-type': 'application/json; charset=utf-8',
      date: 'Tue, 04 Mar 2025 14:58:46 GMT',
      'referrer-policy': 'origin-when-cross-origin, strict-origin-when-cross-origin',
      server: 'github.com',
      'strict-transport-security': 'max-age=31536000; includeSubdomains; preload',
      vary: 'Accept-Encoding, Accept, X-Requested-With',
      'x-accepted-oauth-scopes': '',
      'x-content-type-options': 'nosniff',
      'x-frame-options': 'deny',
      'x-github-api-version-selected': '2022-11-28',
      'x-github-media-type': 'github.v3; format=json',
      'x-github-request-id': 'F8AA:76702:CBBC1D:19952E7:67C71526',
      'x-oauth-client-id': '178c6fc778ccc68e1d6a',
      'x-oauth-scopes': 'gist, read:org, repo, workflow',
      'x-ratelimit-limit': '5000',
      'x-ratelimit-remaining': '4921',
      'x-ratelimit-reset': '1741101493',
      'x-ratelimit-resource': 'core',
      'x-ratelimit-used': '79',
      'x-xss-protection': '0'
    },
    data: {
      message: 'Validation Failed',
      errors: [Array],
      documentation_url: 'https://docs.github.com/rest/repos/rules#create-a-repository-ruleset',
      status: '422'
    }
  }
}
◇  Ran create-typescript-app
│
└  Done. Enjoy your updated repository! 💝

Additional Info

Per https://docs.github.com/en/rest/repos/rules?apiVersion=2022-11-28#update-a-repository-ruleset, it looks like there's an update endpoint too.

🎁

@JoshuaKGoldberg JoshuaKGoldberg added the type: bug Something isn't working :( label Mar 4, 2025
JoshuaKGoldberg added a commit that referenced this issue Mar 4, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
## PR Checklist

- [x] Addresses an existing open issue: fixes #1950
- [x] That issue was marked as [`status: accepting
prs`](https://github.com/JoshuaKGoldberg/create-typescript-app/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22)
- [x] Steps in
[CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/create-typescript-app/blob/main/.github/CONTRIBUTING.md)
were taken

## Overview

Previously, `blockRepositoryBranchRuleset` would _always_ create a
ruleset on `main` with the hardcoded name. This is fine for _setup_ mode
but crashes on a duplicate ID creation error in _transition_ mode.

Now, its creations are split between the two modes:

* Setup mode `POST` creates the ruleset as before
* Transition mode `PUT` updates the existing ruleset

The `PUT` update requires a `ruleset_id`, so I added that as a new
option in the Base populated by a new `inputFromOctkit`.

🎁
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Something isn't working :(
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant