{"id":15672465,"url":"https://github.com/azu/delete-github-branches","last_synced_at":"2025-05-06T21:25:52.515Z","repository":{"id":66143287,"uuid":"244334691","full_name":"azu/delete-github-branches","owner":"azu","description":"CLI: Delete GitHub Branches by pattern matching.","archived":false,"fork":false,"pushed_at":"2022-08-23T04:08:21.000Z","size":147,"stargazers_count":16,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-19T10:54:47.830Z","etag":null,"topics":["api","branch","branches","delerte","github"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/azu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"azu"}},"created_at":"2020-03-02T09:47:11.000Z","updated_at":"2024-08-28T09:23:20.000Z","dependencies_parsed_at":"2023-02-22T01:15:40.590Z","dependency_job_id":null,"html_url":"https://github.com/azu/delete-github-branches","commit_stats":{"total_commits":59,"total_committers":3,"mean_commits":"19.666666666666668","dds":0.0847457627118644,"last_synced_commit":"c237410c490f4277559dac0a84b507059388e4ce"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/azu%2Fdelete-github-branches","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/azu%2Fdelete-github-branches/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/azu%2Fdelete-github-branches/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/azu%2Fdelete-github-branches/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/azu","download_url":"https://codeload.github.com/azu/delete-github-branches/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252264485,"owners_count":21720520,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","branch","branches","delerte","github"],"created_at":"2024-10-03T15:26:45.847Z","updated_at":"2025-05-06T21:25:52.452Z","avatar_url":"https://github.com/azu.png","language":"TypeScript","readme":"# delete-github-branches\n\nDelete GitHub Branches that match patterns.\n\n## Features\n\n- Can define `includesBranchPatterns`\n- Can define `excludesBranchPatterns`\n- If the branch has associated Pull Requests, Does not delete the branch\n- Have `dryRun` mode\n\n## Install\n\nInstall with [npm](https://www.npmjs.com/):\n\n    npm install delete-github-branches\n\n## Usage: CLI\n\n\n    Usage\n      $ delete-github-branches\n \n    Options\n      --owner Owner name for repository: **owner**/repo\n      --repo  Repo name for repository: owner/**repo**\n      --token GitHub Token. you can use als GITHUB_TOKEN env\n      --includesBranchPatterns includes branch patterns split by comma. Default: \"/^.*$/\" (all)\n      --excludesBranchPatterns excludes branch patterns split by comma. Default: \"master,develop,dev,gh-pages\"\n      --stalledDays Deletable days after the branch is stalled. Default: 30\n      --format Output formatter. Available: \"markdown\", \"json\". Default: \"markdown\"\n      --baseUrl GitHub API base Url.\n      --dryRun if this flag is on, run dry-run mode\n      --config path to config file\n \n    Examples\n      $ delete-github-branches --owner azu --repo delete-github-branches-test --token \u003cTOKEN\u003e\n      $ delete-github-branches --owner azu --repo delete-github-branches-test --token \u003cTOKEN\u003e --includesBranchPatterns \"/feature\\/.*/\" --dryRun\n\nAlso this package includes helper cli tool.\n`delete-github-branches-check-branch-name` just detect if the branch name is matched patterns.\n\n    Usage\n      $ delete-github-branches-check-branch-name [branchName]\n \n    Options\n      --includesBranchPatterns includes branch patterns split by comma. Default: \"/^.*$/\" (all)\n      --excludesBranchPatterns excludes branch patterns split by comma. Default: \"master,develop,dev,gh-pages\"\n      --config path to config file\n \n    Examples\n      $ delete-github-branches-check-branch-name \"feature/009\"\n      $ echo $? # 0\n      # It will be alive and exit code: 0\n      $ delete-github-branches-check-branch-name \"patch-101\"\n      $ echo $? # 1\n      # It will be deleted and exit code: 1\n    \n\n### Config File\n\nConfig file is following JSON format.\n\nAll property is optional and its can be combined with command line flags.\n\n```ts\n{\n    /**\n     * Repository owner name\n     */\n    owner?: string;\n    /**\n     * Repository name\n     */\n    repo?: string;\n    /**\n     * allow list that match branch names\n     * Match all branches without excludesBranchPatterns's default by default\n     * It means that matches branches without [\"master\", \"develop\", \"dev\", \"gh-pages\"]\n     *\n     * You can use RegExp-like string for this list\n     * https://github.com/textlint/regexp-string-matcher#regexp-like-string\n     * Default: [\"/^.*$/\"]\n     */\n    includesBranchPatterns?: string[];\n    /**\n     * Deny list that match branch names\n     * You can use RegExp-like string for this list\n     * https://github.com/textlint/regexp-string-matcher#regexp-like-string\n     * Default: [\"master\", \"develop\", \"dev\", \"gh-pages\"]\n     */\n    excludesBranchPatterns?: string[];\n    /**\n     * You can set deletable stalled days after the branch is last pushed\n     * Delete branches that are stalled 30 days by default\n     * if today \u003e= lastPushedDate + 30, its deletable\n     * Default: 30\n     */\n    stalledDays?: number;\n    /**\n     * Default: 'https://api.github.com'\n     */\n    baseUrl?: string;\n    /**\n     * GitHub Token\n     */\n    token?: string;\n    /**\n     * If `dryRun` is `true`, does not delete actually\n     * Dry-run mode fetch and dump\n     * Default: false\n     */\n    dryRun?: boolean;\n}\n```\n\nFor example, `delete-github-branches.json` is following config.\n\n`delete-github-branches.json`:\n\n```json\n{\n    \"includesBranchPatterns\":  [\"/^.*$/\"],\n    \"excludesBranchPatterns\": [\"master\", \"develop\", \"dev\", \"gh-pages\", \"/^feature\\/.*$/\"]\n}\n```\n\nAnd you can pass other options as command line flags \n\n```shell script\n$ GITHUB_TOKEN=$GH_TOKEN delete-github-branches --owner azu --repo delete-github-branches-test --config ./delete-github-branches.json\n```\n\n## Usage: Library\n\n```ts\n(async () =\u003e {\n    const results = await deleteGitHubBranches({\n        owner: \"azu\",\n        repo: \"delete-github-branches-test\",\n        excludesBranchPatterns: [\"master\", \"develop\", \"/feature/.*/\"],\n        token: process.env.token!,\n        dryRun: true // \u003c= dry run mode\n    });\n    assert.deepStrictEqual(results, [\n        { branchName: \"develop\", deleted: false, reason: \"It is ignored by includes/excludes patterns\" },\n        { branchName: \"feature/a\", deleted: false, reason: \"It is ignored by includes/excludes patterns\" },\n        { branchName: \"feature/b\", deleted: false, reason: \"It is ignored by includes/excludes patterns\" },\n        { branchName: \"master\", deleted: false, reason: \"It is ignored by includes/excludes patterns\" },\n        { branchName: \"will-be-deleted\", deleted: true }\n    ]);\n})()\n```\n\n## GitHub Actions\n\nYou can delete mismatch branch automatically using GitHub Actions.\n\n- [azu/delete-github-branches-actions-demo: Demo: GitHub Actions + delete-github-branches](https://github.com/azu/delete-github-branches-actions-demo)\n    - It is demo project\n\n**Demo Features**\n\n- Cron delete mismatch branches at 00:00 everyday\n- If a PR is opend with mismatch branch, reply comment via bot\n\nCreate a config file for `delete-github-branches` and put it to `.github/delete-github-branches.json`.\n\n```json\n{\n  \"owner\": \"XXXXXXX\",\n  \"repo\": \"XXXXXXXX\",\n  \"excludesBranchPatterns\": [\n    \"master\",\n    \"develop\",\n    \"gh-pages\",\n    \"/^feature\\/.*$/\",\n    \"/^renovate\\/.*$/\"\n  ],\n  \"stalledDays\": 30\n}\n```\n\nAnd create following GitHub Action yml and put it to `.github/workflows/delete-github-branches.yml`.\n    \n```yaml\nname: delete-github-branches\n\non:\n  pull_request:\n    types: [opened]\n  # At 00:00 everyday\n  schedule:\n    - cron: '0 0 * * *'\n\npermissions:\n  contents: write\n  issues: write\n  pull-requests: write\n\njobs:\n  delete-branch:\n    runs-on: ubuntu-latest\n    if: github.event_name == 'schedule'\n    steps:\n      - name: checkout\n        uses: actions/checkout@v2\n      - name: Setup Node ${{ matrix.node_version }}\n        uses: actions/setup-node@v2\n        with:\n          node_version: 12.x\n      - name: Run delete-github-branches\n        run: |\n          npm install -g delete-github-branches@1\n          delete-github-branches --config ./.github/delete-github-branches.json\n        env:\n          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}\n\n  check-pull-request-branch:\n    runs-on: ubuntu-latest\n    if: github.event_name == 'pull_request'\n    steps:\n      - name: checkout\n        uses: actions/checkout@v2\n      - name: Setup Node ${{ matrix.node_version }}\n        uses: actions/setup-node@v2\n        with:\n          node_version: 12.x\n      - name: Check branch name\n        id: check_branch_name\n        shell: bash -x {0}\n        run: |\n          echo \"GITHUB_BRANCH: ${BRANCH_NAME}\"\n          npm install -g delete-github-branches@1\n          RESULT_DELETE_GITHUB_BRANCH=$(delete-github-branches-check-branch-name --config ./.github/delete-github-branches.json \"${BRANCH_NAME}\")\n          RET=$?\n          if [ \"$RET\" = \"1\" ]; then\n              # multi-line issue https://github.community/t5/GitHub-Actions/set-output-Truncates-Multiline-Strings/td-p/37870\n              RESULT_DELETE_GITHUB_BRANCH=\"${RESULT_DELETE_GITHUB_BRANCH//'%'/'%25'}\"\n              RESULT_DELETE_GITHUB_BRANCH=\"${RESULT_DELETE_GITHUB_BRANCH//$'\\n'/'%0A'}\"\n              RESULT_DELETE_GITHUB_BRANCH=\"${RESULT_DELETE_GITHUB_BRANCH//$'\\r'/'%0D'}\"\n              echo \"::set-output name=message::${RESULT_DELETE_GITHUB_BRANCH}\"\n              echo \"::set-output name=invalid_branch_name::true\"\n              echo \"this branch name is invalid\"\n              exit 0\n          fi\n          echo \"Good branch name\"\n          echo \"${RESULT_DELETE_GITHUB_BRANCH}\"\n        env:\n          BRANCH_NAME: ${{ github.event.pull_request.head.ref }}\n          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}\n      - uses: actions/github-script@0.8.0\n        if: steps.check_branch_name.outputs.invalid_branch_name == 'true'\n        with:\n          github-token: ${{secrets.GITHUB_TOKEN}}\n          script: |\n            github.issues.createComment({\n              issue_number: context.issue.number,\n              owner: context.repo.owner,\n              repo: context.repo.repo,\n              body: `@${{ github.actor }} This branch name is mismatch branch naming rule.\u003cbr/\u003e\u003cpre\u003e${{steps.check_branch_name.outputs.message}}\u003c/pre\u003e`\n            })\n```\n\n## Changelog\n\nSee [Releases page](https://github.com/azu/delete-github-branches/releases).\n\n## Running tests\n\nAdd `.env` with `token`\n\n```\nGITHUB_TOKEN=XXXX\n```\n\nRun tests\n\n    npm test\n\n## Contributing\n\nPull requests and stars are always welcome.\n\nFor bugs and feature requests, [please create an issue](https://github.com/azu/delete-github-branches/issues).\n\n1. Fork it!\n2. Create your feature branch: `git checkout -b my-new-feature`\n3. Commit your changes: `git commit -am 'Add some feature'`\n4. Push to the branch: `git push origin my-new-feature`\n5. Submit a pull request :D\n\n## Author\n\n- [github/azu](https://github.com/azu)\n- [twitter/azu_re](https://twitter.com/azu_re)\n\n## License\n\nMIT © azu\n","funding_links":["https://github.com/sponsors/azu"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fazu%2Fdelete-github-branches","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fazu%2Fdelete-github-branches","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fazu%2Fdelete-github-branches/lists"}