Skip to content

Commit

Permalink
feat: support branches only active in the current repository (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
v1v authored Dec 11, 2024
1 parent 67e2bd4 commit 0d55f7a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
9 changes: 5 additions & 4 deletions elastic/active-branches/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ Fetch the current list of active branches in Elastic (the ones based on the Unif

## Inputs
<!--inputs-->
| Name | Description | Required | Default |
|--------------------|----------------------------------|----------|---------|
| `exclude-branches` | Exclude branches comma separated | `false` | ` ` |
| Name | Description | Required | Default |
|--------------------|---------------------------------------------------------------------------------|----------|---------|
| `exclude-branches` | Exclude branches comma separated | `false` | ` ` |
| `filter-branches` | Whether to fitler those branches that only exist in the {{ github.repository }} | `false` | `false` |
| `github-token` | The GitHub access token. | `false` | ` ` |
<!--/inputs-->

## Outputs
Expand Down Expand Up @@ -49,7 +51,6 @@ jobs:
with:
ref: "${{ matrix.branch }}"


# ...
```
<!--/usage-->
12 changes: 12 additions & 0 deletions elastic/active-branches/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ inputs:
required: false
type: string
default: ''
filter-branches:
description: "Whether to fitler those branches that only exist in the {{ github.repository }}"
default: false
type: boolean
github-token:
description: 'The GitHub access token.'
required: false
type: string
default: ''
outputs:
matrix:
description: "Processed matrix with the branches (using the include format)"
Expand All @@ -22,6 +31,9 @@ runs:
run: python ${{ github.action_path }}/script.py
env:
EXCLUDE_BRANCHES: ${{ inputs.exclude-branches }}
FILTER: ${{ inputs.filter-branches }}
REPOSITORY: ${{ github.repository }}
GITHUB_TOKEN: ${{ inputs.github-token }}
- id: debug
shell: bash
run: |
Expand Down
23 changes: 23 additions & 0 deletions elastic/active-branches/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,29 @@ def fails(msg):
if exclude_branches:
branches = list(filter(lambda branch: branch not in exclude_branches, branches))

# Filter branches based on the existence in the GitHub repository
filter_str = os.environ.get('FILTER', 'false')
filter = filter_str.lower() in ('true', '1', 't', 'y', 'yes')
repository = os.environ.get('REPOSITORY', '')
github_token = os.environ.get('GITHUB_TOKEN', '')
if filter and repository and github_token:
# Check if branches exist in the GitHub repository
headers = {
'Authorization': f'token {github_token}',
'Accept': 'application/vnd.github.v3+json'
}

existing_branches = []
for branch in branches:
branch_url = f'https://api.github.com/repos/{repository}/branches/{branch}'
response = requests.get(branch_url, headers=headers)
if response.status_code == 200:
existing_branches.append(branch)
else:
print(f"Branch {branch} does not exist in the repository")

branches = existing_branches

include_branches = list(map(lambda branch: {"branch": branch}, branches))
matrix = {'include': include_branches}

Expand Down

0 comments on commit 0d55f7a

Please sign in to comment.