Here is a suggestion to set-up such a system.
To reject a push, you'll need to set up a Git server-side hook, in particular, a pre-receive hook.
On GitHub specifically, I think you need GitHub enterprise to set-up a pre-receive hook to enforce policy. As this post explains:
For obvious reasons, GitHub won’t allow you to execute arbitrary code
on their servers as part of Git hooks. The only hooking they allow is
through their webhooks or the integrated third-party services. If you
need to run some custom code, you will have to host that somewhere and
set up a webhook to run it on your own server.
Now, you'll want to run the style-checker on the server-side hook. Ideally, to facilitate running this via script, the style-checker needs to be able to be run from command line.
For example, see ReSharper's InspectCode command line tool. You can have the server-side hook automatically run InspectCode
, and process the output accordingly to determine whether the code adheres to your style settings.
(This sounds a bit tedious to set-up and requiring GitHub Enterprise may be a non-starter for you.)
Alternatively, you can set up a git pre-commit hook for this purpose. (The downside is that it'll require the pre-commit hook to be manually installed on every machine, and nothing is enforced server-side.) See this related post for details on how to set-up a pre-commit hook. For example, you could set up a pre-commit hook to run ReSharper's InspectCode
, and stop the commit if any code inspections are found. Also, this related post has some similar suggestions on this topic.
As a side note, perhaps there are other practices for accomplishing what you want. You could have a coding style check as part of a pull request & code review workflow, so that code doesn't get merged in if it doesn't satisfy your team's coding standards. It may also be appropriate to discuss and (re-)align with your team on coding styles.