code-coverage-policy
Skill1 starsChanged 3 months agoMITView on GitHub
Check and enforce code coverage branch policies on Azure DevOps repositories. Use when the user requests to: (1) Check existing code coverage policies on one or more repos (2) Add a code coverage status check policy to a branch (3) Update policy settings (blocking, enabled, threshold) on existing policies (4) Configure the azurepipelines-coverage.yml file for PR diff coverage (5) Bulk-enforce coverage policies across many repos at once Triggers include: any mention of 'code coverage policy', 'branch policy', 'coverage status check', 'azurepipelines-coverage.yml', 'diff coverage target', or requests to enforce, add, or audit code coverage policies in Azure DevOps.
In this repository10 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- bug-reportSkill
- coding-agents-setupSkill
- repo-agents-setupSkill
- review-designSkill
- review-prSkill
- self-reviewSkill
- sync-skillsSkill
- threat-modelSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
14 rules. About 0 are common, 6 are distinctive, and 8 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈6
Distinctive · estimate
This author's own choices. Worth a look.
8
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 59
If required parameters are missing, use AskUserQuestion to collect them interactively.
- Distinctive: line 67
All Azure DevOps API calls require an OAuth access token.
- Very specific: line 88
After any mutation (add, update, configure-yaml), re-run check to confirm the change took effect and report results to the user.
- Distinctive: line 97
Process with Python: Use a single Python script to cross-reference repos, policies, and branches.
- Very specific: line 99
Execute updates in parallel: Use curl background jobs (&) to PUT/POST multiple policy updates concurrently in a single Bash call.
- Very specific: line 125
jqmay not be available: Use python -c "..." for JSON processing instead of jq. - Distinctive: line 131
Critical: Before adding or enforcing a policy on a branch, always verify the branch exists.
- Very specific: line 138
If value[] is empty, the branch does not exist.
- Very specific: line 225
Pitfall: Do NOT include policyApplicability as a string (e.g., "default").
- Distinctive: line 245
Pitfall: PUT replaces the entire configuration.
- Very specific: line 286
Commit and push azurepipelines-coverage.yml to the default branch
- Distinctive: line 288
After the next PR build, the code coverage status will appear
- Distinctive: line 308
Always validate branches: before creating or enforcing policies.
- Very specific: line 311
Use Python for JSON processing: instead of jq — more reliable cross-platform.
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Code Coverage Branch Policy Skill
Manage code coverage branch policies on Azure DevOps repositories — check existing policies, add new status check policies, update settings, and configure PR diff coverage thresholds. Supports single-repo and bulk operations.
Prerequisites
Permissions required: The add and update actions require the Edit policies permission at the repository or branch level. Most engineers do not have this by default.
Groups that have Edit policies by default:
- Project Administrators
Groups that do not have it by default but can be granted it:
- Build Administrators, Contributors, or any custom security group
To check your permissions: go to Project Settings > Repositories > {repo} > Security, find your user or group, and look for Edit policies = Allow.
The check action (read-only) works with standard Read permissions (Contributors, Build Admins, etc.). If you get a 403 error on mutation operations, ask a Project Administrator to grant Edit policies on the target repository.
See references/troubleshooting.md for more details on permission issues.
When to Use
- User wants to see what code coverage policies exist on a repo or set of repos
- User wants to add a code coverage status check policy to block PRs without sufficient coverage
- User wants to update settings on an existing code coverage policy (e.g., enabled/disabled)
- User wants to create or update the
azurepipelines-coverage.ymlfile for PR diff coverage - User wants to enforce coverage policies across many repos in bulk
Input Parsing
Parse arguments from $ARGUMENTS:
- First positional arg: Action — one of
check,add,update,configure-yaml, or omit to interactively choose --org <org>: Azure DevOps organization name--project <project>: Project name--repo <repo>: Repository name or ID. Accepts comma-separated list for bulk operations (e.g.,--repo Repo1,Repo2,Repo3)--branch <branch>: Branch to apply policy to. If omitted, the tool automatically resolves the default branch for each repo via the API (defaultBranchfield from the repositories endpoint) — no need to specifymain,live, etc.--pipeline <name>: Pipeline name (used for status naming convention{pipeline}/codecoverage)--target <percent>: Diff coverage target percentage (default: 60)--blocking: Make the policy required (blocking)--optional: Make the policy optional (non-blocking)
If required parameters are missing, use AskUserQuestion to collect them interactively.
High-Level Workflow
Phase 1: Authentication
All Azure DevOps API calls require an OAuth access token. See references/azure-devops-api.md for full API patterns.
TOKEN=$(az account get-access-token \
--resource 499b84ac-1321-427f-aa17-267ca6975798 \
--query accessToken -o tsv)Pitfall: On Windows, tokens do not persist across Bash tool invocations. Re-fetch the token at the top of every Bash call.
Phase 2: Execute Action
Route to the appropriate action based on user input. If no action is specified, present an interactive menu:
- Check existing policies — See what code coverage policies are configured
- Add a code coverage policy — Create a new status check policy
- Update an existing policy — Change blocking/enabled settings
- Configure coverage YAML — Create/update the
azurepipelines-coverage.ymlfile
Phase 3: Verify
After any mutation (add, update, configure-yaml), re-run check to confirm the change took effect and report results to the user.
Performance: Bulk Operations Strategy
Azure DevOps does NOT have a general-purpose batch API for policy operations. Use a fetch-once, process-locally pattern:
- Fetch all data in a single Bash call: Download repos list, all policy configurations, and policy types to local files in one Bash invocation (one token fetch, three parallel curls).
- Process with Python: Use a single Python script to cross-reference repos, policies, and branches.
- Write updates to local files: Generate all JSON payloads locally.
- Execute updates in parallel: Use
curlbackground jobs (&) to PUT/POST multiple policy updates concurrently in a single Bash call.
Example: Bulk fetch pattern
TOKEN=$(az account get-access-token \
--resource 499b84ac-1321-427f-aa17-267ca6975798 \
--query accessToken -o tsv)
WORKDIR="$(pwd)"
# Fetch all data in parallel
curl -s -H "Authorization: Bearer $TOKEN" "$ORG/$PROJECT/_apis/git/repositories?api-version=7.1" \
-o "$WORKDIR/repos.json" &
curl -s -H "Authorization: Bearer $TOKEN" "$ORG/$PROJECT/_apis/policy/configurations?api-version=7.1" \
-o "$WORKDIR/policies.json" &
curl -s -H "Authorization: Bearer $TOKEN" "$ORG/$PROJECT/_apis/policy/types?api-version=7.1" \
-o "$WORKDIR/types.json" &
waitThen process locally in Python — no additional API calls needed for check. For add/update, generate JSON files and batch the curl calls.
Windows file path pitfalls
$TEMPwith backslashes: Paths likeC:\Users\...\Tempbreakcurl -d @path. Use forward-slash paths in the working directory instead.- Python vs bash paths:
/tmp/paths from bash are not accessible in Python on Windows. Use absolute paths with forward slashes that both can resolve. jqmay not be available: Usepython -c "..."for JSON processing instead ofjq.
Branch Validation
Critical: Before adding or enforcing a policy on a branch, always verify the branch exists.
curl -s -H "Authorization: Bearer $TOKEN" \
"$ORG/$PROJECT/_apis/git/repositories/$REPO_ID/refs?filter=heads/$BRANCH&api-version=7.1"If value[] is empty, the branch does not exist. Skip it and report to the user. Do NOT create or enforce policies on non-existent branches.
Action: check — Inspect Current Policies
Supports a single repo or a list of repos. When given multiple repos, produce a summary table.
Steps
- Fetch all data (once) — Download repos list and all policy configurations (see Bulk Operations Strategy).
- Resolve repository IDs — Match repo names against the repos list. Report any repos not found.
- Cross-reference policies — For each repo, filter for:
- Build validation policies scoped to that repo
- Status check policies with
statusName == "codecoverage"scoped to that repo
- Present findings
For a single repo, show a detailed table:
| # | Type | Name/Display | Blocking | Enabled | Branch |
|---|--------|--------------------|----------|---------|-----------------|
| 1 | Build | CI Pipeline | Yes | Yes | refs/heads/main |
| 2 | Status | CI/codecoverage | No | Yes | refs/heads/main |For multiple repos, show a summary table:
| Repo | Default Branch | Build Policy | Coverage Status | Enforcement |
|------------|---------------|-------------|----------------|---------------------|
| MyRepo | main | main | main | ENFORCED (blocking) |
| OtherRepo | develop | develop | None | Not configured |Enforcement states:
- ENFORCED (blocking) — status check exists and is blocking
- Advisory (non-blocking) — status check exists but is non-blocking
- Not configured — no status check policy for code coverage
- Not configured (no build) — no build or status policies at all
Action: add — Add Code Coverage Status Check Policy
Prerequisites
- A build pipeline must already exist that publishes code coverage results
- The pipeline must use
PublishCodeCoverageResults@2(V2, not V1)
Steps
- Validate branch existence — Verify the target branch exists (see Branch Validation). Skip non-existent branches.
- Determine status name — Code coverage status follows the convention:
{pipeline-name}/codecoverage. If--pipelineis not specified, look up build definitions and ask the user. - Resolve policy type ID — Query the policy types API to get the Status policy type ID.
- Create the status check policy — Write JSON payload to a file, then POST:
{
"isEnabled": true,
"isBlocking": true,
"type": { "id": "{statusPolicyTypeId}" },
"settings": {
"statusName": "codecoverage",
"statusGenre": "{pipeline-name}",
"authorId": "",
"invalidateOnSourceUpdate": true,
"displayName": "Code Coverage Policy",
"scope": [
{
"repositoryId": "{repoId}",
"refName": "refs/heads/{branch}",
"matchKind": "Exact"
}
]
}
}Key settings:
statusName: Alwayscodecoverage(matches what the pipeline posts)statusGenre: The pipeline name (status context is{genre}/{name})isBlocking:truefor required,falsefor optionalauthorId: Empty string = any identity can post the status
- Verify — Re-run
checkto confirm the policy was created.
Pitfall: Do NOT include policyApplicability as a string (e.g., "default"). The API expects a numeric enum or null. Omit the field entirely.
Action: update — Update an Existing Policy
Steps
- Find the policy — Run
checkto list policies and identify the one to update. - Validate branch existence — If the policy targets a specific branch, verify it exists.
- Get current configuration — GET the full policy configuration.
- Modify and PUT — Apply requested changes, write modified JSON to a file, then PUT.
curl -s -H "Authorization: Bearer $TOKEN" \
-X PUT -H "Content-Type: application/json" \
"$ORG/$PROJECT/_apis/policy/configurations/$POLICY_ID?api-version=7.1" \
-d @"$WORKDIR/cc_update_$POLICY_ID.json"Pitfall: PUT replaces the entire configuration. Always GET the current config first, modify it, then PUT it back.
Bulk update pattern
TOKEN=$(az account get-access-token \
--resource 499b84ac-1321-427f-aa17-267ca6975798 \
--query accessToken -o tsv)
for PID in $POLICY_IDS; do
curl -s -H "Authorization: Bearer $TOKEN" -X PUT -H "Content-Type: application/json" \
"$ORG/$PROJECT/_apis/policy/configurations/$PID?api-version=7.1" \
-d @"$WORKDIR/cc_update_$PID.json" &
done
wait- Verify — Fetch the updated policies to confirm changes.
Action: configure-yaml — Configure Coverage YAML
Creates or updates azurepipelines-coverage.yml at the repository root.
Steps
- Check if file exists — Look for
azurepipelines-coverage.ymlat the repo root. Ask for the local repo path if needed. - Create or update the file:
coverage:
status:
comments: on
diff:
target: 60%Override with --target to set a different threshold.
- Instruct next steps — Tell the user:
- Commit and push
azurepipelines-coverage.ymlto the default branch - The pipeline must publish coverage via
PublishCodeCoverageResults@2(V2) - After the next PR build, the code coverage status will appear
- Commit and push
Error Handling
| Problem | Cause | Solution |
| 403 Forbidden | Missing permissions | User needs Project Administrators or repo-level Edit policies permission |
| Policy type not found | Type ID varies by org | Always query the types API first — never hardcode |
| Policy never evaluates | Status name mismatch | Verify statusGenre/statusName matches what the pipeline posts ({pipeline-name}/codecoverage) |
| Coverage status stuck | V1 task, >100 files, or multiple policies | Upgrade to V2, reduce PR size, or remove duplicate policies |
| Branch not found | Branch deleted or typo | Validate via refs API before creating policies |
For a full troubleshooting guide, see references/troubleshooting.md.
Best Practices
- Always validate branches before creating or enforcing policies. Skip non-existent branches and report them.
- Fetch once, process locally for bulk operations — minimizes API calls and token refreshes.
- Write JSON payloads to files and use
curl -d @file.json— avoids shell escaping issues. - Use Python for JSON processing instead of
jq— more reliable cross-platform. - Re-fetch tokens per Bash call on Windows — tokens don't persist across shell invocations.
- Use forward-slash paths even on Windows — avoids breakage with
curl, Python, and temp file operations. - Never include
policyApplicabilityas a string — the API expects a numeric enum or null. - PUT replaces entirely — always GET the existing config, modify it, then PUT the full object.
- Verify after mutations — always re-run
checkafteraddorupdateto confirm success.
Reference Files
| File | Purpose |
| references/azure-devops-api.md | REST API reference: endpoints, authentication, payload formats, known pitfalls |
| references/troubleshooting.md | Common issues and solutions for code coverage policies |
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

