Skip to content

Commit

Permalink
feat: add labels standard
Browse files Browse the repository at this point in the history
Signed-off-by: Xinwei Xiong(cubxxw-openim) <[email protected]>
  • Loading branch information
cubxxw committed Jun 20, 2023
1 parent 7d4ecdb commit 4ff27ab
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 18 deletions.
26 changes: 18 additions & 8 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,23 @@ This document provides guidelines and best practices to help you contribute effe

## 📇Topics

- [What we expect of you](#What-we-expect-of-you)
- [Code of Conduct](#Code-of-Conduct)
- [Getting Started](#Getting-Started)
- [Style and Specification](#Style-and-Specification)
- [Engage to help anything](#Engage-to-help-anything)
- [Release version](#Release-version)
- [Contact Us](#Contact-Us)
- [Contributing to github-label-syncer](#contributing-to-github-label-syncer)
- [📇Topics](#topics)
- [What we expect of you](#what-we-expect-of-you)
- [Code of ConductCode of Conduct](#code-of-conductcode-of-conduct)
- [Code and doc contribution](#code-and-doc-contribution)
- [Where should I start?](#where-should-i-start)
- [Design documents](#design-documents)
- [Getting Started](#getting-started)
- [Style and Specification](#style-and-specification)
- [Reporting security issues](#reporting-security-issues)
- [Reporting general issues](#reporting-general-issues)
- [Commit Rules](#commit-rules)
- [PR Description](#pr-description)
- [Docs Contribution](#docs-contribution)
- [Engage to help anything](#engage-to-help-anything)
- [Release version](#release-version)
- [Contact Us](#contact-us)

## What we expect of you

Expand Down Expand Up @@ -175,7 +185,7 @@ To be honest, we regard every user of github-label-synceras a very kind contribu

Since we collaborate project github-label-syncer in a distributed way, we appreciate **WELL-WRITTEN**, **DETAILED**, **EXPLICIT** issue reports. To make the communication more efficient, we wish everyone could search if your issue is an existing one in the searching list. If you find it existing, please add your details in comments under the existing issue instead of opening a brand new one.

To make the issue details as standard as possible, we setup an [ISSUE TEMPLATE](https://github.com/kubecub/github-label-syncer/tree/main/.github/ISSUE_TEMPLATE) for issue reporters. You can find three kinds of issue templates there: question, bug report and feature request. Please **BE SURE** to follow the instructions to fill fields in template.
To make the issue details as standard as possible, we setup an [ISSUE TEMPLATE](https://github.com/kubecub/.github/tree/main/.github/ISSUE_TEMPLATE) for issue reporters. You can find three kinds of issue templates there: question, bug report and feature request. Please **BE SURE** to follow the instructions to fill fields in template.

**There are a lot of cases when you could open an issue:**

Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ The labels are designed semantic and standard. We provide labels for priority, t
❯ make help # show help
❯ make build # build binary exporter and syncer
echo 'export PATH=$PATH:/path/to/_output/platforms/linux/amd64/' | tee -a ~/.zshrc;source ~/.zshrc

```

</details>
Expand All @@ -90,8 +89,10 @@ Comment in an issue:
<details>
<summary>Use</summary>

You can set your own `GITHUB_TOKEN` via env or via the `export GITHUB_TOKEN` environment variable, or use one of the default tokens we provide, which is `TOEKN` for our free automated [🤖 robot](https://github.com/kubbot)

```bash
❯ ./_output/platforms/linux/amd64/exporter --help
❯ ./_output/bin/platforms/linux/amd64/exporter --help
usage: exporter [<flags>] <owner> <repo>


Expand All @@ -100,6 +101,10 @@ Flags:
-y, --[no-]yaml Use the YAML format.
-j, --[no-]json Use the JSON format.
-t, --[no-]table Use the table format.
-x, --[no-]xml Use the XML format.
--[no-]toml Use the TOML format.
--[no-]ini Use the INI format.
--[no-]csv Use the CSV format.

Args:
<owner> Owner of the repository.
Expand Down
49 changes: 44 additions & 5 deletions cmd/exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"fmt"
"log"
"os"
"runtime/debug"

"github.com/alecthomas/kingpin/v2"
Expand All @@ -23,14 +24,20 @@ var (
yaml = kingpin.Flag("yaml", "Use the YAML format.").Short('y').Bool()
json = kingpin.Flag("json", "Use the JSON format.").Short('j').Bool()
table = kingpin.Flag("table", "Use the table format.").Short('t').Bool()
xml = kingpin.Flag("xml", "Use the XML format.").Short('x').Bool()

// TODO: Add support for these formats.
xml = kingpin.Flag("xml", "Use the XML format.").Short('x').Bool()
toml = kingpin.Flag("toml", "Use the TOML format.").Bool()
ini = kingpin.Flag("ini", "Use the INI format.").Bool()
csv = kingpin.Flag("csv", "Use the CSV format.").Bool()
)

var (
// New flags.
file = kingpin.Flag("file", "Export labels to file.").Short('f').String()
token = kingpin.Flag("token", "GitHub token.").Envar("GITHUB_TOKEN").String()
)

var (
goVersion = "unknown" // Populated by goreleaser during build

Expand Down Expand Up @@ -67,7 +74,15 @@ func main() {
if err != nil {
log.Fatal(err)
}
fmt.Println(string(b))
if *file != "" {
err = os.WriteFile(*file, b, 0644)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Labels exported to %s\n", *file)
} else {
fmt.Println(string(b))
}
return
}

Expand All @@ -76,7 +91,15 @@ func main() {
if err != nil {
log.Fatal(err)
}
fmt.Println(string(b))
if *file != "" {
err = os.WriteFile(*file, b, 0644)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Labels exported to %s\n", *file)
} else {
fmt.Println(string(b))
}
return
}

Expand All @@ -85,7 +108,15 @@ func main() {
if err != nil {
log.Fatal(err)
}
fmt.Println(string(b))
if *file != "" {
err = os.WriteFile(*file, b, 0644)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Labels exported to %s\n", *file)
} else {
fmt.Println(string(b))
}
return
}

Expand All @@ -94,7 +125,15 @@ func main() {
if err != nil {
log.Fatal(err)
}
fmt.Println(string(b))
if *file != "" {
err = os.WriteFile(*file, b, 0644)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Labels exported to %s\n", *file)
} else {
fmt.Println(string(b))
}
return
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require (
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1
golang.org/x/oauth2 v0.8.0
golang.org/x/sync v0.2.0
gopkg.in/ini.v1 v1.67.0
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
sigs.k8s.io/yaml v1.3.0
Expand Down Expand Up @@ -67,6 +68,5 @@ require (
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
k8s.io/klog v1.0.0 // indirect
)
2 changes: 2 additions & 0 deletions pkg/exporter/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package exporter
import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
Expand All @@ -23,6 +24,7 @@ func NewClient() (*githubClient, error) {
envPath := filepath.Join(".", ".env")
if _, err := os.Stat(envPath); os.IsNotExist(err) {
envTemplatePath := filepath.Join(".", ".env.template")
fmt.Println("envPath: ", envPath)
if _, err := os.Stat(envTemplatePath); os.IsNotExist(err) {
return nil, errors.New("missing .env.template file")
}
Expand Down
56 changes: 54 additions & 2 deletions pkg/exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ package exporter

import (
"bytes"
"encoding/csv"
"encoding/json"
"encoding/xml"
"fmt"

"github.com/olekukonko/tablewriter"
"gopkg.in/ini.v1"
"sigs.k8s.io/yaml"
)

Expand All @@ -31,14 +35,17 @@ type LabelsToObject interface {
}

func LabelsToJSON(labels []*Label) ([]byte, error) {
fmt.Println("hits labels to json")
return json.Marshal(labels)
}

func LabelsToYAML(labels []*Label) ([]byte, error) {
fmt.Println("hits labels to yaml")
return yaml.Marshal(labels)
}

func LabelsToTable(labels []*Label) ([]byte, error) {
fmt.Println("hits labels to table")
labelRows := make([][]string, 0, len(labels))
for _, l := range labels {
labelRows = append(labelRows, []string{l.Name, l.Description, l.Color})
Expand All @@ -54,17 +61,62 @@ func LabelsToTable(labels []*Label) ([]byte, error) {
}

func LabelsToXML(labels []*Label) ([]byte, error) {
return nil, nil
fmt.Println("hits labels to xml")
type XMLLabels struct {
XMLName xml.Name `xml:"labels"`
Labels []*Label `xml:"label"`
}
xmlLabels := &XMLLabels{Labels: labels}
return xml.MarshalIndent(xmlLabels, "", " ")
}

func LabelsToTOML(labels []*Label) ([]byte, error) {
fmt.Println("hits labels to toml")
type TOMLLabel struct {
Name string `toml:"name"`
Description string `toml:"description"`
Color string `toml:"color"`
}
type TOMLLabels struct {
Labels []*TOMLLabel `toml:"label"`
}
tomlLabels := &TOMLLabels{}
for _, label := range labels {
tomlLabels.Labels = append(tomlLabels.Labels, &TOMLLabel{
Name: label.Name,
Description: label.Description,
Color: label.Color,
})
}
return nil, nil
}

func LabelsToINI(labels []*Label) ([]byte, error) {
fmt.Println("hits labels to ini")
cfg := ini.Empty()
for _, label := range labels {
section, err := cfg.NewSection(label.Name)
if err != nil {
return nil, err
}
section.NewKey("description", label.Description)
section.NewKey("color", label.Color)
}
return nil, nil
}

func LabelsToCSV(labels []*Label) ([]byte, error) {
return nil, nil
fmt.Println("hits labels to csv")
b := &bytes.Buffer{}
w := csv.NewWriter(b)
if err := w.Write([]string{"Name", "Description", "Color"}); err != nil {
return nil, err
}
for _, label := range labels {
if err := w.Write([]string{label.Name, label.Description, label.Color}); err != nil {
return nil, err
}
}
w.Flush()
return b.Bytes(), nil
}

0 comments on commit 4ff27ab

Please sign in to comment.