Skip to content

Commit

Permalink
ci: skip if labels did not changed
Browse files Browse the repository at this point in the history
  • Loading branch information
shanduur committed Jun 14, 2023
1 parent 87a48cb commit d7ae8e1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .github/labels.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
¯\_(ツ)_/¯:
name: ¯\_(ツ)_/¯
color: f9d0c4
description: ¯\\\_(ツ)_/¯
api:
name: api
color: 6f42c1
Expand Down
18 changes: 15 additions & 3 deletions .github/workflows/example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,22 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- id: labels-changed
run: |
if git diff --name-only HEAD~1 HEAD | grep -q ".github/labels.yaml"; then
echo "changed=true" >> $GITHUB_OUTPUT
else
echo "changed=false" >> $GITHUB_OUTPUT
fi
- uses: actions/setup-go@v4
if: steps.labels-changed.outputs.changed == 'true'
with:
go-version: '1.20'
- run: go install github.com/shanduur/labeler/cmd/labeler@main
- run: labeler upload --owner shanduur --repo labeler ./.github/labels.yaml
- if: steps.labels-changed.outputs.changed == 'true'
run: go install github.com/shanduur/labeler/cmd/labeler@main
- if: steps.labels-changed.outputs.changed == 'true'
run: labeler upload --owner shanduur --repo labeler ./.github/labels.yaml
env:
GITHUB_TOKEN: ${{ secrets.LABELER_TOKEN }}
LABELER_TOKEN: ${{ secrets.LABELER_TOKEN }}
11 changes: 8 additions & 3 deletions cmd/labeler/upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ func New() *cli.Command {
return errors.New("wrong number of arguments, expected single <file>")
}

token, ok := os.LookupEnv("GITHUB_TOKEN")
token, ok := os.LookupEnv("LABELER_TOKEN")
if !ok {
return errors.New("GitHub Token not found")
return errors.New("GitHub Token (env = LABELER_TOKEN) not found")
}

ts := oauth2.StaticTokenSource(
Expand All @@ -73,7 +73,7 @@ func New() *cli.Command {
for name, label := range lbls {
slog.Info("processing label", "label_name", name, "label.color", label.Color)

err = uploadLabel(ctx.Context, client, "shanduur", "cluster-api-provider-pve", label)
err = uploadLabel(ctx.Context, client, owner, repo, label)
if err != nil {
return fmt.Errorf("unable to update label: %w", err)
}
Expand All @@ -91,6 +91,11 @@ func uploadLabel(ctx context.Context, client *github.Client, owner, repo string,
return errors.New("client is nil")
}

err := label.Validate()
if err != nil {
return fmt.Errorf("lablel validation failed: %w", err)
}

// first, check if label exist
ghLabel, err := getLabel(ctx, client, owner, repo, label.Name)
if err != nil {
Expand Down
14 changes: 14 additions & 0 deletions labels/labels.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package labels

import (
"errors"

"github.com/google/go-github/v53/github"
)

Expand All @@ -10,6 +12,18 @@ type Label struct {
Description *string `yaml:"description,omitempty"`
}

func (l *Label) Validate() error {
if l.Name == "" {
return errors.New("name is empty")
}

if len(l.Color) != 6 {
return errors.New("color is empty")
}

return nil
}

func (l *Label) ToGitHub() *github.Label {
return &github.Label{
Name: &l.Name,
Expand Down

0 comments on commit d7ae8e1

Please sign in to comment.