Bitbucket Branch Protection and Hygiene Auditor
On a schedule, the flow audits Bitbucket repositories for branch-protection compliance (required reviewers, build checks, no direct pushes to main) and stale branches, records gaps to Dataverse, posts a compliance scorecard to Teams, and emails repo owners to remediate. Keeps the codebase governed across many repos.
Provided as-is, without warranty of any kind. Review and test each pattern in a non-production environment before deploying it to live automations. See our Terms.
Overview
This flow audits Bitbucket repositories for branch-protection governance and branch hygiene. On a weekly schedule it lists every repository in the workspace, checks each one's protected branch for required reviewers, a passing-build merge check, and a direct-push restriction, and counts stale branches. It writes one audit record per repository to a Dataverse table, posts a compliance scorecard to a Teams channel, and emails repo owners the specific gaps to remediate.
Why it matters: Branch-protection drift across many repositories is a real security and quality risk — unreviewed or unverified merges into main. A scheduled audit keeps standards enforced and visible, with a durable Dataverse record for trend reporting.
Status: Built and verified (Flow Checker 0 errors / 0 warnings). Ships Off — going live requires only connection authorization, a Bitbucket access token, and env-var configuration.
Use Case
A platform / dev-governance team wants continuous assurance that Bitbucket repositories meet branch-protection standards (required approvals, build gates, no direct pushes to the mainline) and aren't accumulating stale branches. The flow produces a weekly governed record plus an actionable Teams scorecard and per-owner remediation emails.
Flow Architecture
Run Audit On Schedule
Recurrence (Week / 1)Weekly audit cadence that drives the branch-protection and hygiene sweep.
Initialize run variables
Initialize VariableMint a correlationId (guid()), set min required reviewers (int of env var), compute the stale-branch cutoff (utcNow() minus the threshold), and seed the non-compliant counter and the HTML scorecard accumulator.
List Repositories
Bitbucket ListRepositoriesList all repositories in the configured workspace.
For Each Repository (audit)
Foreach (concurrency 1)For each repo, read branch-restrictions and the branch list via Bitbucket REST (HTTP), filter the approval/build/push protection rules and stale branches, compose reviewers-found, violation count, stale count, findings, and a Compliant/Non-Compliant verdict, then create one Dataverse audit record. If non-compliant, increment the counter, append to the scorecard, and email the repo owner.
Compose Summary
ComposeBuild the scorecard HTML (repos audited, non-compliant count, gap list).
Post Scorecard To Teams
Teams PostMessageToConversationPost the compliance scorecard to the governance channel.
Environment Variables
| Schema name | Type | Default | Description |
|---|---|---|---|
| flowlibs_BitbucketWorkspace | String | your-workspace | Workspace (account) slug to audit |
| flowlibs_BitbucketApiBaseUrl | String | https://api.bitbucket.org/2.0 | Bitbucket REST API base (already includes /2.0) |
| flowlibs_BitbucketAccessToken | String | REPLACE_WITH_BITBUCKET_ACCESS_TOKEN | Bearer token for the REST branch-restriction reads |
| flowlibs_ProtectedBranchName | String | main | Mainline branch that must be protected |
| flowlibs_MinRequiredReviewers | String | 1 | Minimum required approvals to pass |
| flowlibs_StaleBranchThresholdDays | String | 30 | Inactivity (days) past which a branch is stale |
| flowlibs_TeamsGroupId | String | REPLACE_WITH_TEAMS_GROUP_ID | Teams team (group) id for the scorecard |
| flowlibs_TeamsChannelId | String | REPLACE_WITH_TEAMS_CHANNEL_ID | Teams channel id for the scorecard |
| flowlibs_OwnerNotificationEmail | String | alerts@yourcompany.com | Repo-owner remediation recipient |
Connectors & Connections
| Connector | API name | Actions used |
|---|---|---|
| Bitbucket | shared_bitbucket | ListRepositories |
| Microsoft Dataverse | shared_commondataserviceforapps | CreateRecord |
| Microsoft Teams | shared_teams | PostMessageToConversation |
| Office 365 Outlook | shared_office365 | SendEmailV2 |
Note — All connections are referenced as solution connection references; the flow is portable between environments as long as a connection is mapped at import time.
Customization Guide
Almost every realistic variant of this flow can be implemented by changing environment variable values. A few cases require small edits inside the flow definition — those are called out explicitly below.
- Auto-remediate
- Add an Approvals gate, then PUT a baseline branch-restriction set back to Bitbucket for approved repos.
- Stale-branch cleanup
- Extend the loop to propose (or, behind approval, perform) deletion of merged stale branches.
- Per-team scorecards
- Drive the workspace/owner from a Dataverse mapping table and post each team's scorecard to its own channel.
- Cadence
- Change the Run_Audit_On_Schedule recurrence (e.g. daily) for higher-frequency governance.
- Stricter policy
- Raise flowlibs_MinRequiredReviewers or add additional kind checks (e.g. enforce_merge_checks, delete restriction) in the filters.
Key Expressions
The flow is intentionally light on Power Fx / WDL gymnastics — the heaviest expressions are the branch-name concatenation and the approval outcome check. They are listed below in the order they appear in the flow.
EXPR.01Stale-branch filter (per branch)
Marks a branch stale when its last commit is older than the cutoff.
EXPR.02Reviewers found
Required reviewers configured on the protected branch (0 if none).
EXPR.03Violation count
Sum of the three protection violations.
EXPR.04Compliance verdict
Compliant only when there are no violations and no stale branches.
EXPR.05Stale cutoff
Timestamp threshold for stale-branch detection.
Customize & download
Generate a ready-to-import copy of this solution with your environment-variable values baked in — available on Base, Pro, or Team.
Upgrade to customize
Comments
Sign in to join the conversation.
Sign inNo comments yet. Be the first to share your experience with this flow.