0

I have a pre-commit hook that I would like to apply to all my existing and new repositories in my GitHub Organization.

That pre-commit hook is created for "enforcing the GitHub Branch naming policy" by referring this URL https://betterprogramming.pub/how-to-prevent-git-commit-naming-mistakes-a34c8a7c6ae6

Now I'm stuck how can I share or apply the same hook(script) across all the repositories in my GitHub organization, that too this script shouldn't not be modified or changed by any developer or user except authorized owner

benhorgen
  • 1,928
  • 1
  • 33
  • 38

2 Answers2

0

Hooks are specific to a particular clone of a repository, and as such, it's not possible to force them to be specified from a remote repository, like on GitHub. Git doesn't allow you to specify hooks in a repository because they can execute arbitrary code, and allowing you to do that would be a security vulnerability.

If you wanted to provide a set of hooks for developers to use, you could provide a repository with them and then add a script to set core.hooksPath in the global config, which is the location of hooks if it is specified.

However, as outlined in the Git FAQ, you cannot rely on hooks as an effective control, because individual developers can bypass them trivially without being noticed. If you need to enforce policy, it will have to be done on the server side, with a CI job (or, on GitHub Enterprise Server only, a pre-receive hook).

The FAQ mentions this downside of mandatory hooks as well:

In addition, some advanced users find pre-commit hooks to be an impediment to workflows that use temporary commits to stage work in progress or that create fixup commits, so it’s better to push these kinds of checks to the server anyway.

Option pre-commit hooks can be a helpful tool for developers who wish to use them, though, and you could legitimately provide some suggested options for users who opt in.

bk2204
  • 64,793
  • 6
  • 84
  • 100
-1

You can create organization level hooks from your organization's settings > webhooks tab: https://github.com/organizations/{org}/settings/hooks

There should be an option to create a new hook. You will still need a specific payload URL, but you can select what events you would like to trigger the webhook. The docs have been changing recently, but here's a url for more info on webhook events: https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads

There will always be a repository object within the payload, so you can apply repository specific code with that information. However, more specific to your case, it seems like github actions may be a better solution for applying a test to any commit/pull request. You can check the branch name and the test will fail if it does not meet certain guidlines.