Note
Custom coding guidelines are limited to selected participants in the public preview of Copilot code review, and only available as part of a subscription to GitHub Copilot Enterprise.
About coding guidelines
You can customize Copilot code review with custom coding guidelines written in natural language. For more information on Copilot code review, see "Using GitHub Copilot code review."
With coding guidelines, Copilot can give feedback based on your organization's specific coding style and best practices.
Because Copilot code review is powered by a large language model, it can help with enforcing coding guidelines that are not covered by your linter or static analysis tool.
Coding guidelines are configured at the repository level. You can create and enable up to 6 coding guidelines per repository.
Note
- Coding guidelines only work with languages supported by Copilot code review. For a list of supported languages, see "Using GitHub Copilot code review."
- Coding guidelines only apply to code reviews carried out by Copilot. The guidelines do not affect Copilot code completion suggestions, or code suggested in Copilot Chat responses.
Dos and don'ts for coding guidelines
- Do use simple, clear and concise language to describe your coding guideline.
- Do be as specific as possible about what Copilot should look for - that is, what you *o or don't want to see in your code.
- Do take a look at the "Coding guidelines examples" *elow for some inspiration.
- Don't try to use coding guidelines to enforce style guidelines that can be covered *y your linter or static analysis tool.
- Don't use wording that is ambiguous or could be interpreted in different ways.
- Don't try to fit multiple different ideas into a single coding guideline.*
Creating a coding guideline
-
На GitHubперейдите на главную страницу репозитория.
-
Под именем репозитория щелкните Параметры. Если вкладка "Параметры" не отображается, выберите раскрывающееся меню и щелкните Параметры.
-
In the "Code & automation" section of the side bar, click Copilot, then Code review.
-
Click Create guideline.
-
Under "Name," give the coding guideline a name.
-
Under "Description," provide a description of the coding guideline up to 600 characters long. This will be used by Copilot to understand your coding style and to decide when to leave a comment.
How you write your description has a big impact on the quality of comments that Copilot will generate. For help with writing effective coding guidelines, see "Dos and don'ts for coding guidelines" above, and "Coding guidelines examples" below.
-
Optionally, limit the coding guideline to specific file types or paths by clicking Add file path and adding path patterns.
You can use
fnmatch
syntax to define paths to target, with*
as a wildcard to match any string of characters.Так как GitHub использует
File::FNM_PATHNAME
флаг синтаксисаFile.fnmatch
, дикий*
карта не соответствует разделителям каталогов (/
). Например, будет соответствовать всем ветвям,qa/*
начинающимся сqa/
и содержащим одну косую черту, но не совпадатьqa/foo/bar
. Вы можете включить любое количество косых черт послеqa
qa/**/*
, которое будет соответствовать, напримерqa/foo/bar/foobar/hello-world
. Можно также расширитьqa
строку,qa**/**/*
чтобы сделать правило более инклюзивным.Дополнительные сведения о параметрах синтаксиса см. в документации по fnmatch.
Примечание. Не все выражения из синтаксиса
fnmatch
поддерживаются в правилах защиты ветви. Обратите внимание на следующие ограничения:- Символ обратной косой черты (
\
) нельзя использовать в качестве символа кавыка, так как GitHub не поддерживает использование обратной косой черты в правилах защиты ветви. - Можно указать наборы символов в квадратных скобках (
[]
), но в настоящее время нельзя дополнить набор оператором^
(например,[^charset]
). - Хотя GitHub поддерживается
File::FNM_PATHNAME
вfnmatch
синтаксисе,File::FNM_EXTGLOB
не поддерживается.
- Символ обратной косой черты (
-
Test your coding guideline to make sure it works as expected.
- Click Add sample.
- Add your own sample, or press Generate code sample to automatically generate a code sample based on your title and description.
- Click Save to save the code sample.
- Test the coding guideline against your sample by pressing Run.
-
Save your coding guideline, and turn it on, by clicking Save guideline.
Running a review with coding guidelines
When you request a review from Copilot, it will automatically use the repository's enabled coding guidelines to review your code. For more information, see "Using GitHub Copilot code review."
Comments generated based on a coding guideline will include a message, highlighting their source.
Coding guidelines examples
Example 1: Avoid using magic numbers
Title: Avoid using magic numbers
Description: Don't use magic numbers in code. Numbers should be defined as constants or variables with meaningful names.
Path patterns: **/*.py
Example 2: Don't use SELECT *
in SQL queries
Title: Don't use `SELECT *` in SQL queries
Description: Don't use `SELECT *` in SQL queries. Always specify the columns you want to select. `COUNT(*)` is allowed.
Path patterns: None (applies to all file types, as SQL queries may be embedded in code).
Example 3: Use fetch
for HTTP requests
Title: Use `fetch` for HTTP requests
Description: Use `fetch` for HTTP requests, not `axios` or `superagent` or other libraries.
Path patterns: **/*.ts
, **/*.js
, **/*.jsx
, **/*.tsx
Example 4: Always tag metrics with the current environment
Title: Always tag metrics with the current environment
Description: Always include a `env` tag with the current environment when emitting metrics, for example, `env:prod` or `env:dev`.
Path patterns: */*.go
, */*.java