Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/autolabels-action-sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ jobs:
uses: actions/checkout@v3

- name: "Run autolabelling"
id: autolabelling
uses: aznashwan/github-autolabeler@main
with:
token: ${{ github.token }}
config-path: "./autolabels.yml"
target-from-action-env: ${{ toJSON(github) }}
command: sync

- name: "Echo labels"
run: echo "${{ steps.autolabelling.labels }}"
88 changes: 68 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ $ docker run -v /tmp:/tmp -e GITHUB_TOKEN="<your token>" \
generate

# You can review the list of available arguments using:
$ docker run ghcr.io/cloudbase/gh-auto-labeler:latest help
$ docker run ghcr.io/cloudbase/gh-auto-labeler:main help
```

### GitHub Actions
Expand Down Expand Up @@ -214,30 +214,17 @@ permissions:
jobs:
autolabel:
runs-on: ubuntu-latest
container: ghcr.io/cloudbase/gh-auto-labeler:main
steps:
- name: Checkout
uses: actions/checkout@v3

- name: "Run autolabelling"
run: |
# This will check whether this action was triggered by a PR/Issue
# event and update the target string accordingly.
# If your `on:` section only triggers on certain types of resources,
# you can safely hardcode the exact target format directly.
TARGET="${{ github.repository }}"
if [ "${{ github.event.pull_request.number }}" ]; then
TARGET="$TARGET/pull/${{ github.event.pull_request.number }}"
elif [ "${{ github.event.issue.number }}" ]; then
TARGET="$TARGET/issue/${{ github.event.issue.number }}"
fi

gh-auto-labeler \
--github-token ${{ secrets.GITHUB_TOKEN }} \
--label-definitions-file "path/to/your/repos/autolabels.yml" \
--run-post-labelling-actions \
"$TARGET" \
sync
uses: cloudbase/gh-auto-labeler@main
with:
target-from-action-env: ${{ toJSON(github) }}
# default is ".github/autolabels.yml"
config-path: "./autolabels.yml"
command: sync
```

## Configuration
Expand Down Expand Up @@ -528,4 +515,65 @@ one-liner-only-file:

## Advanced Usage

### Advanced configs

Please review the `./samples` folder for more advanced config examples.

### Advanced actions setup

#### Using autolabeler container directly from actions.

```yaml
# Workflow definition for automatically labelling on various triggers.
name: Autolabeler Sync
description: Workflow definition for automatically labelling on various triggers.

on:
workflow_dispatch: # manual triggers from Actions menu
push: # triggers on pushes to repo branches
branches: [main] # triggers on pushes to 'main'
tags: ['v*.*.*'] # triggers on certain tags
issues: # triggers on issue-related operations
types: [opened, edited, closed] # C_UD operations on the issue
pull_request: # triggers on PR-related operations
branches: [main] # PRs must have been opened against 'main'
types: [opened, edited, reopened] # triggers on PRs being (re)opened/edited
issue_comment: # triggers on *both* PR/issue comments
types: [created, edited, deleted] # C_UD operations on any comments

permissions:
contents: read
issues: write
pull-requests: write

jobs:
autolabel-through-container:

runs-on: ubuntu-latest
# NOTE: replace 'main' with any specific tag you'd need.
container: ghcr.io/cloudbase/gh-auto-labeler:main

steps:
- name: Checkout
uses: actions/checkout@v3

- name: "Run autolabelling"
run: |
# This will check whether this action was triggered by a PR/Issue
# event and update the target string accordingly.
# If your `on:` section only triggers on certain types of resources,
# you can safely hardcode the exact target format directly.
TARGET="${{ github.repository }}"
if [ "${{ github.event.pull_request.number }}" ]; then
TARGET="$TARGET/pull/${{ github.event.pull_request.number }}"
elif [ "${{ github.event.issue.number }}" ]; then
TARGET="$TARGET/issue/${{ github.event.issue.number }}"
fi

gh-auto-labeler \
--github-token ${{ secrets.GITHUB_TOKEN }} \
--label-definitions-file "path/to/your/repos/autolabels.yml" \
--run-post-labelling-actions \
"$TARGET" \
sync
```
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ inputs:
GitHub token to use for the labelling actions. ('${ secrets.GITHUB_TOKEN }')
Can be a 'classic' token or a 'fine-grained' token with full repo/issue/PR access.
default: ${{ github.token }}
target: # id of input
target:
description: |
Target for the labelling action.
Can be the full URL or resource path of the repo or repo object, such as:
Expand Down
7 changes: 4 additions & 3 deletions autolabeler/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def load_yaml_file(path_or_file: str|IOBase) -> dict:
return yaml.safe_load(file)


def main_with_args(argv: list[str]):
def main_with_args(argv: list[str]) -> list[dict]:
parser = argparse.ArgumentParser(
"github-autolabeler",
description="Python 3 utility for automatically labelling/triaging "
Expand Down Expand Up @@ -113,8 +113,9 @@ def main_with_args(argv: list[str]):
raise ValueError(f"Unsupported command: {other}")

labels_dicts = [l.to_dict() for l in labels]
print(json.dumps(labels_dicts, indent=4))
return labels_dicts


def main():
main_with_args(sys.argv[1:])
labels_dicts = main_with_args(sys.argv[1:])
print(json.dumps(labels_dicts, indent=4))
19 changes: 17 additions & 2 deletions entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import json
import os
import sys
import uuid

from autolabeler.main import main_with_args

Expand Down Expand Up @@ -101,15 +102,29 @@ def load_args_from_env():
vars_map['COMMAND']]


def set_github_output_var(key: str, val: str):
outfile_path = os.getenv("GITHUB_OUTPUT")
if not outfile_path:
raise ValueError(f"No 'GITHUB_OUTPUT' defined in env: {os.environ}")

delim = f"cbsl_autolabels_{uuid.uuid4()}"
# NOTE: can add output variables to GITHUB_OUTPUT with bash heredoc syntax:
vardef = f"{key}<<{delim}{os.linesep}{val}{os.linesep}{delim}{os.linesep}"
with open(outfile_path, "a") as fout:
fout.write(vardef)


def main():
args = []
if len(sys.argv) > 1:
args = sys.argv[1:]
else:
args = load_args_from_env()

main_with_args(args)

labels_dicts = main_with_args(args)
serialized = json.dumps(labels_dicts, indent=4)
print(serialized)
set_github_output_var("labels", serialized)


if __name__ == "__main__":
Expand Down
1 change: 1 addition & 0 deletions samples/101-showcase.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ example-label:
label-color: 0075ca # blue
label-description: This is a simple example static label.

# Test comment with one line.

# This defines two "namespaced" labels named: label-prefix/sublabel-{1,2}.
label-prefix:
Expand Down