Send default branch name to OpsLevel #446
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Send default branch name to OpsLevel | |
on: | |
schedule: | |
- cron: "0 9,12,16 * * *" | |
workflow_dispatch: | |
concurrency: | |
group: ci-${{ github.workflow }}-${{ github.ref }} | |
cancel-in-progress: false | |
jobs: | |
get-github-repositories: | |
name: Fetch repositories from GitHub | |
runs-on: ubuntu-latest | |
outputs: | |
repositories: ${{ steps.fetch-all-repositories.outputs.result }} | |
steps: | |
- name: Get all repositories | |
uses: actions/github-script@v6 | |
id: fetch-all-repositories | |
with: | |
github-token: ${{ secrets.PLEO_READ_TOKEN }} | |
script: | | |
const repositories = (await github.paginate("GET /orgs/pleo-io/repos?type=all", { | |
per_page: 100, | |
headers: { | |
"X-GitHub-Api-Version": "2022-11-28", | |
}, | |
})).filter(r => !r.archived) | |
const withBranchProtections = await Promise.all(repositories.map(async (r) => { | |
const { name, default_branch } = r | |
core.info(`Fetching branch protections for pleo-io/${name}`) | |
try { | |
const { required_pull_request_reviews } = await github.rest.repos.getBranchProtection({ | |
owner: 'pleo-io', | |
repo: name, | |
branch: default_branch, | |
}) | |
const count = required_pull_request_reviews?.required_approving_review_count ?? 0 | |
return { ...r, branchProtectionApprovals: count } | |
} catch (e) { | |
core.warning(`Could not fetch branch protections for pleo-io/${name}`) | |
return { ...r, branchProtectionApprovals: 0 } | |
} | |
})) | |
const mapped = withBranchProtections.map(r => ({ repository: r.name, defaultBranch: r.default_branch, branchProtectionApprovals: r.branchProtectionApprovals })) | |
return mapped | |
send-to-opslevel: | |
name: Send repository data to OpsLevel | |
runs-on: ubuntu-latest | |
needs: get-github-repositories | |
strategy: | |
matrix: | |
repository: ${{ fromJSON(needs.get-github-repositories.outputs.repositories) }} | |
steps: | |
- name: POST default branch name to OpsLevel | |
run: | | |
echo '{ "repository": "${{ matrix.repository.repository }}", "defaultBranch": "${{ matrix.repository.defaultBranch }}", "branchProtectionApprovals": "${{ matrix.repository.branchProtectionApprovals }}" }' | curl --connect-timeout 5 --max-time 10 --retry 5 --retry-delay 0 --retry-max-time 40 -X POST https://app.opslevel.com/integrations/custom_event/${{ secrets.OPSLEVEL_CUSTOM_EVENT_WEBHOOK }} -H 'content-type: application/json' --data-binary @- |