Skip to content

Commit

Permalink
feat: init
Browse files Browse the repository at this point in the history
  • Loading branch information
shanduur committed Jun 14, 2023
0 parents commit f6e5f4c
Show file tree
Hide file tree
Showing 16 changed files with 6,843 additions and 0 deletions.
55 changes: 55 additions & 0 deletions .github/labels.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
api:
color: 6f42c1
description: Issues related to the GitHub API
bug:
color: d73a4a
description: Something isn't working as expected
enhancement:
color: 0dd8ac
description: New feature or improvement request

documentation:
color: 007bc7
description: Issues related to documentation
help-wanted:
color: 7057ff
description: Contribution opportunities available
question:
color: ffb000
description: Questions or need for further clarification
invalid:
color: e4e669
description: Issue is no longer valid or applicable
duplicate:
color: cfd3d7
description: Issue already reported or duplicated
wontfix:
color: a2eeef
description: Issue won't be fixed or addressed
feature-request:
color: 61dafb
description: Request for a new feature or enhancement
critical:
color: e00808
description: Critical issue that requires immediate attention
high-priority:
color: ff7b72
description: Issue with high priority
low-priority:
color: 8c8c8c
description: Issue with low priority
in-progress:
color: 2a8b9d
description: Issue currently being worked on
resolved:
color: 28a745
description: Issue has been resolved
feedback:
color: d4c5f9
description: Feedback or suggestions for improvement
testing:
color: 36bcef
description: Issues related to testing or test cases
hacktoberfest:
color: ff6b00
description: Issues related to the Hacktoberfest event
22 changes: 22 additions & 0 deletions .github/workflows/example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Upload labels

on:
push:
branches:
- "main"

permissions:
issues: write

jobs:
upload-labels:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
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
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
159 changes: 159 additions & 0 deletions cmd/labeler/download/download.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package download

import (
"context"
"errors"
"fmt"
"os"
"path"
"strings"

"github.com/google/go-github/v53/github"
"github.com/shanduur/labeler/labels"
"github.com/urfave/cli/v3"
"golang.org/x/exp/slog"
"gopkg.in/yaml.v3"
)

var (
output string

flagOutput = &cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
Destination: &output,
}
)

func New() *cli.Command {
return &cli.Command{
Name: "download",
Aliases: []string{"d", "dl"},
Flags: []cli.Flag{
flagOutput,
},
Action: func(ctx *cli.Context) error {
if ctx.Args().Len() == 0 {
return errors.New("nothing to do")
}

client := github.NewClient(nil)

for i := 0; i < ctx.NArg(); i++ {
ghPath := ctx.Args().Get(i)
owner, repo := getOwnerRepo(ghPath)

l, err := listAll(ctx.Context, client, owner, repo)
if err != nil {
return fmt.Errorf("unable to list all: %w", err)
}

b, err := toYAML(l)
if err != nil {
return fmt.Errorf("converision failed: %w", err)
}

outputPath := ghPath
if output != "" {
outputPath = path.Join(output, ghPath)
}

err = save(outputPath, b)
if err != nil {
return fmt.Errorf("saving failed: %w", err)
}
}

return nil
},
}
}

func getOwnerRepo(arg string) (string, string) {
args := strings.SplitN(arg, "/", 2)
if len(args) == 2 {
return args[0], args[1]
}
return "", ""
}

func listAll(ctx context.Context, client *github.Client, owner, repo string) ([]*github.Label, error) {
slog.Debug("listing", "owner", owner, "repo", repo)
var (
page = 0
allLabels []*github.Label
)

if client == nil {
return nil, errors.New("client is nil")
}

for {
labels, res, err := client.Issues.ListLabels(ctx, owner, repo, &github.ListOptions{
Page: page,
})
if err != nil {
return nil, fmt.Errorf("unable to list labels: %w", err)
}

allLabels = append(allLabels, labels...)

if res.NextPage == 0 {
break
}
page = res.NextPage
}

slog.Debug("listing done", "labels_count", len(allLabels))

return allLabels, nil
}

func toYAML(ghl []*github.Label) ([]byte, error) {
slog.Debug("transforming to YAML")

lbl := make(labels.Labels)

for _, l := range ghl {
name := l.GetName()
if _, ok := lbl[name]; ok {
slog.Warn("duplicate found", "name", name)
continue
}

slog.Debug("new label", "name", name)

lbl[name] = labels.Label{
Name: l.GetName(),
Color: l.GetColor(),
Description: l.Description,
}
}

slog.Debug("transforming to YAML complete")

out, err := yaml.Marshal(lbl)
if err != nil {
return nil, fmt.Errorf("unable to marshall YAML: %w", err)
}

return out, nil
}

func save(location string, data []byte) error {
err := os.MkdirAll(location, 0o777)
if err != nil {
return fmt.Errorf("unable to create directory: %w", err)
}

output := path.Join(location, "labels.yaml")

slog.Debug("saving to file", "location", output)

err = os.WriteFile(output, data, 0o644)
if err != nil {
return fmt.Errorf("unable to save result: %w", err)
}

return nil
}
Loading

0 comments on commit f6e5f4c

Please sign in to comment.