Skip to content

gh ruleset should offer export and import #8019

Description

@jsoref

Describe the feature or problem you’d like to solve

I have built a ruleset for a repository in one owner (for testing) and would like to copy it to another repository (in this case, a fork-related repository, although as my rules are fairly generic, i'd probably like to be able to copy it generally).

Proposed solution

gh ruleset view --repo jsoref/check-spelling 94531

Something like:

gh ruleset view --repo jsoref/check-spelling 94531 --format=json > ruleset.json

Should allow generating a format that could be consumed.

Something like

gh ruleset create --repo check-spelling/check-spelling --file=ruleset.json

Should allow consuming the ruleset.

How will it benefit CLI and its users?

Users could copy rulesets from one repository to another or even a template ruleset from the web.

Additional context

https://jessehouwing.net/protect-the-repository-hosting-your-github-action/ has graphical instructions for creating rulesets, but it's quite tedious to do it for even a single repository, and it'd be much nicer if the instructions just gave a cli for creating the rules.

Activity

  1. williammartin commented on Sep 19, 2023

    @williammartin
    Member

    @vaindil can I get your 👀 on this please? It seems like an interesting use case to me but I'm not particularly familiar with the ruleset offering.

  2. vaindil commented on Sep 19, 2023

    @vaindil
    Contributor

    For some context, a create subcommand was originally not added because creating a ruleset is just too complex of an operation to really support interactively. You'd essentially have to pass the API endpoint's raw JSON payload into the subcommand, at which point there's no real benefit over using gh api. There was some discussion about accepting YAML input that's transformed into the correct JSON, but that would likely end up too complex for no major benefit.

    My gut reaction is that this feature request would probably be simple enough to do:

    This would limit the CLI's role to just transforming the JSON from one endpoint to the other, which shouldn't be too large of a scope.

    My only concern is that rulesets are still in active development and more features are in the works, so the API shape will likely change enough that the CLI has to be updated when these new features roll out. This isn't inherently a problem, but it's just something to keep in mind because it ties the CLI to the shape of the feature a bit more tightly than the other CLI features I'm familiar with.

  3. williammartin commented on Sep 20, 2023

    @williammartin
    Member

    Thanks for the explanation and historical context.

    My only concern is that rulesets are still in active development and more features are in the works, so the API shape will likely change enough that the CLI has to be updated when these new features roll out. This isn't inherently a problem, but it's just something to keep in mind because it ties the CLI to the shape of the feature a bit more tightly than the other CLI features I'm familiar with.

    Can you describe some of the changes the API might make that might require a CLI change? I'm thinking these APIs are versioned and shouldn't be introducing breaking changes right? If it is a problem, perhaps an extension would be a natural place for this to live until things stabilise.

  4. vaindil commented on Sep 20, 2023

    @vaindil
    Contributor

    I checked with the rulesets team, and they're actually currently working on import/export via the UI. There are tentative plans for API endpoints to support this as well, but those are still in the earlier stages so no promises on a timeline.

    The CLI could add support now using the process I put above and swap over to the new endpoints if/when they're added, but it probably makes more sense to wait a bit to see what happens.

  5. added
    coreThis issue is not accepting PRs from outside contributors
    and removed on Sep 21, 2023
  6. williammartin commented on Sep 21, 2023

    @williammartin
    Member

    Ok I'm going to keep this open so that it is visible, and label it with core until we get more clarity on what the team are going to do.

  7. andyfeller commented on Sep 25, 2023

    @andyfeller
    Contributor

    @jsoref : How would this request work with organizational repository rulesets?

    I could see this feature being necessary for duplicating organization repository rulesets across enterprises with 100s of organizations. Otherwise, I would configure select repositories at the organizational repository ruleset.

    Screenshot 2023-09-25 at 11 29 38 AM

  8. jsoref commented on Sep 26, 2023

    @jsoref
    ContributorAuthor

    Assuming ruleset patterns only include repository names and not the owners, I don't see a problem in including that in the output or consumed input.

    My starting point is a repository (jsoref/check-spelling) that's a fork of an upstream (check-spelling/check-spelling). Before I deploy changes to the upstream, I try to test them in my fork.

    Beyond that, I control a handful of organizations and being able to copy rulesets across them seems incredibly handy.

  9. jsoref commented on Sep 26, 2023

    @jsoref
    ContributorAuthor

    For example, I could definitely see the value in writing a ruleset that applied to .github and .github-private.

  10. macel94 commented on Nov 28, 2024

    @macel94

    I would really like this, it would help me automate initial setups of repos that we internally create from templates(they're not forks so they don't inherit this)

    Currently it's still a manual setup

  11. RadxaYuntian commented on Mar 27, 2025

    @RadxaYuntian

    Example bash script for how we update exported ruleset with gh api:

    		if ! gh api --method POST -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" \
    			"/repos/$ORGANIZATION/$TARGET/rulesets" --input "default.json"; then
    			local RULESET_ID
    			RULESET_ID="$(gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" \
    				"/repos/$ORGANIZATION/$TARGET/rulesets" --jq '.[] | select(.name = "default").id')"
    			gh api --method PUT -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" \
    				"/repos/$ORGANIZATION/$TARGET/rulesets/$RULESET_ID" --input "default.json"
    		fi
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    coreThis issue is not accepting PRs from outside contributorsenhancementa request to improve CLIgh-rulesetrelating to the gh ruleset command

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions