项目作者: azu

项目描述 :
CLI: Delete GitHub Branches by pattern matching.
高级语言: TypeScript
项目地址: git://github.com/azu/delete-github-branches.git
创建时间: 2020-03-02T09:47:11Z
项目社区:https://github.com/azu/delete-github-branches

开源协议:MIT License

下载


delete-github-branches

Delete GitHub Branches that match patterns.

Features

  • Can define includesBranchPatterns
  • Can define excludesBranchPatterns
  • If the branch has associated Pull Requests, Does not delete the branch
  • Have dryRun mode

Install

Install with npm:

  1. npm install delete-github-branches

Usage: CLI

  1. Usage
  2. $ delete-github-branches
  3. Options
  4. --owner Owner name for repository: **owner**/repo
  5. --repo Repo name for repository: owner/**repo**
  6. --token GitHub Token. you can use als GITHUB_TOKEN env
  7. --includesBranchPatterns includes branch patterns split by comma. Default: "/^.*$/" (all)
  8. --excludesBranchPatterns excludes branch patterns split by comma. Default: "master,develop,dev,gh-pages"
  9. --stalledDays Deletable days after the branch is stalled. Default: 30
  10. --format Output formatter. Available: "markdown", "json". Default: "markdown"
  11. --baseUrl GitHub API base Url.
  12. --dryRun if this flag is on, run dry-run mode
  13. --config path to config file
  14. Examples
  15. $ delete-github-branches --owner azu --repo delete-github-branches-test --token <TOKEN>
  16. $ delete-github-branches --owner azu --repo delete-github-branches-test --token <TOKEN> --includesBranchPatterns "/feature\/.*/" --dryRun

Also this package includes helper cli tool.
delete-github-branches-check-branch-name just detect if the branch name is matched patterns.

  1. Usage
  2. $ delete-github-branches-check-branch-name [branchName]
  3. Options
  4. --includesBranchPatterns includes branch patterns split by comma. Default: "/^.*$/" (all)
  5. --excludesBranchPatterns excludes branch patterns split by comma. Default: "master,develop,dev,gh-pages"
  6. --config path to config file
  7. Examples
  8. $ delete-github-branches-check-branch-name "feature/009"
  9. $ echo $? # 0
  10. # It will be alive and exit code: 0
  11. $ delete-github-branches-check-branch-name "patch-101"
  12. $ echo $? # 1
  13. # It will be deleted and exit code: 1

Config File

Config file is following JSON format.

All property is optional and its can be combined with command line flags.

  1. {
  2. /**
  3. * Repository owner name
  4. */
  5. owner?: string;
  6. /**
  7. * Repository name
  8. */
  9. repo?: string;
  10. /**
  11. * allow list that match branch names
  12. * Match all branches without excludesBranchPatterns's default by default
  13. * It means that matches branches without ["master", "develop", "dev", "gh-pages"]
  14. *
  15. * You can use RegExp-like string for this list
  16. * https://github.com/textlint/regexp-string-matcher#regexp-like-string
  17. * Default: ["/^.*$/"]
  18. */
  19. includesBranchPatterns?: string[];
  20. /**
  21. * Deny list that match branch names
  22. * You can use RegExp-like string for this list
  23. * https://github.com/textlint/regexp-string-matcher#regexp-like-string
  24. * Default: ["master", "develop", "dev", "gh-pages"]
  25. */
  26. excludesBranchPatterns?: string[];
  27. /**
  28. * You can set deletable stalled days after the branch is last pushed
  29. * Delete branches that are stalled 30 days by default
  30. * if today >= lastPushedDate + 30, its deletable
  31. * Default: 30
  32. */
  33. stalledDays?: number;
  34. /**
  35. * Default: 'https://api.github.com'
  36. */
  37. baseUrl?: string;
  38. /**
  39. * GitHub Token
  40. */
  41. token?: string;
  42. /**
  43. * If `dryRun` is `true`, does not delete actually
  44. * Dry-run mode fetch and dump
  45. * Default: false
  46. */
  47. dryRun?: boolean;
  48. }

For example, delete-github-branches.json is following config.

delete-github-branches.json:

  1. {
  2. "includesBranchPatterns": ["/^.*$/"],
  3. "excludesBranchPatterns": ["master", "develop", "dev", "gh-pages", "/^feature\/.*$/"]
  4. }

And you can pass other options as command line flags

```shell script
$ GITHUB_TOKEN=$GH_TOKEN delete-github-branches —owner azu —repo delete-github-branches-test —config ./delete-github-branches.json

  1. ## Usage: Library
  2. ```ts
  3. (async () => {
  4. const results = await deleteGitHubBranches({
  5. owner: "azu",
  6. repo: "delete-github-branches-test",
  7. excludesBranchPatterns: ["master", "develop", "/feature/.*/"],
  8. token: process.env.token!,
  9. dryRun: true // <= dry run mode
  10. });
  11. assert.deepStrictEqual(results, [
  12. { branchName: "develop", deleted: false, reason: "It is ignored by includes/excludes patterns" },
  13. { branchName: "feature/a", deleted: false, reason: "It is ignored by includes/excludes patterns" },
  14. { branchName: "feature/b", deleted: false, reason: "It is ignored by includes/excludes patterns" },
  15. { branchName: "master", deleted: false, reason: "It is ignored by includes/excludes patterns" },
  16. { branchName: "will-be-deleted", deleted: true }
  17. ]);
  18. })()

GitHub Actions

You can delete mismatch branch automatically using GitHub Actions.

Demo Features

  • Cron delete mismatch branches at 00:00 everyday
  • If a PR is opend with mismatch branch, reply comment via bot

Create a config file for delete-github-branches and put it to .github/delete-github-branches.json.

  1. {
  2. "owner": "XXXXXXX",
  3. "repo": "XXXXXXXX",
  4. "excludesBranchPatterns": [
  5. "master",
  6. "develop",
  7. "gh-pages",
  8. "/^feature\/.*$/",
  9. "/^renovate\/.*$/"
  10. ],
  11. "stalledDays": 30
  12. }

And create following GitHub Action yml and put it to .github/workflows/delete-github-branches.yml.

  1. name: delete-github-branches
  2. on:
  3. pull_request:
  4. types: [opened]
  5. # At 00:00 everyday
  6. schedule:
  7. - cron: '0 0 * * *'
  8. permissions:
  9. contents: write
  10. issues: write
  11. pull-requests: write
  12. jobs:
  13. delete-branch:
  14. runs-on: ubuntu-latest
  15. if: github.event_name == 'schedule'
  16. steps:
  17. - name: checkout
  18. uses: actions/checkout@v2
  19. - name: Setup Node ${{ matrix.node_version }}
  20. uses: actions/setup-node@v2
  21. with:
  22. node_version: 12.x
  23. - name: Run delete-github-branches
  24. run: |
  25. npm install -g delete-github-branches@1
  26. delete-github-branches --config ./.github/delete-github-branches.json
  27. env:
  28. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  29. check-pull-request-branch:
  30. runs-on: ubuntu-latest
  31. if: github.event_name == 'pull_request'
  32. steps:
  33. - name: checkout
  34. uses: actions/checkout@v2
  35. - name: Setup Node ${{ matrix.node_version }}
  36. uses: actions/setup-node@v2
  37. with:
  38. node_version: 12.x
  39. - name: Check branch name
  40. id: check_branch_name
  41. shell: bash -x {0}
  42. run: |
  43. echo "GITHUB_BRANCH: ${BRANCH_NAME}"
  44. npm install -g delete-github-branches@1
  45. RESULT_DELETE_GITHUB_BRANCH=$(delete-github-branches-check-branch-name --config ./.github/delete-github-branches.json "${BRANCH_NAME}")
  46. RET=$?
  47. if [ "$RET" = "1" ]; then
  48. # multi-line issue https://github.community/t5/GitHub-Actions/set-output-Truncates-Multiline-Strings/td-p/37870
  49. RESULT_DELETE_GITHUB_BRANCH="${RESULT_DELETE_GITHUB_BRANCH//'%'/'%25'}"
  50. RESULT_DELETE_GITHUB_BRANCH="${RESULT_DELETE_GITHUB_BRANCH//$'\n'/'%0A'}"
  51. RESULT_DELETE_GITHUB_BRANCH="${RESULT_DELETE_GITHUB_BRANCH//$'\r'/'%0D'}"
  52. echo "::set-output name=message::${RESULT_DELETE_GITHUB_BRANCH}"
  53. echo "::set-output name=invalid_branch_name::true"
  54. echo "this branch name is invalid"
  55. exit 0
  56. fi
  57. echo "Good branch name"
  58. echo "${RESULT_DELETE_GITHUB_BRANCH}"
  59. env:
  60. BRANCH_NAME: ${{ github.event.pull_request.head.ref }}
  61. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  62. - uses: actions/github-script@0.8.0
  63. if: steps.check_branch_name.outputs.invalid_branch_name == 'true'
  64. with:
  65. github-token: ${{secrets.GITHUB_TOKEN}}
  66. script: |
  67. github.issues.createComment({
  68. issue_number: context.issue.number,
  69. owner: context.repo.owner,
  70. repo: context.repo.repo,
  71. body: `@${{ github.actor }} This branch name is mismatch branch naming rule.<br/><pre>${{steps.check_branch_name.outputs.message}}</pre>`
  72. })

Changelog

See Releases page.

Running tests

Add .env with token

  1. GITHUB_TOKEN=XXXX

Run tests

  1. npm test

Contributing

Pull requests and stars are always welcome.

For bugs and feature requests, please create an issue.

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

Author

License

MIT © azu