Skip to content

Commit

Permalink
minor tweaks, docs updates, and adding the ability to remove labels (#36
Browse files Browse the repository at this point in the history
)
  • Loading branch information
MasonEgger authored Oct 8, 2021
1 parent 1df77db commit bd932f7
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 26 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ if you don't specify your version control system, Github or Gitlab, it will defa
```sh
hfest-repo -t <TOKEN> -u <USER> --labels
```
### The "Default Hacktoberfest run this on my stuff in GitLab" command

```sh
hfest-repo --vcs Gitlab -t <TOKEN> -u <USER> --labels
```

### The "Default Hacktoberfest run this on my stuff" command, but run as a dry run for validation

Expand Down Expand Up @@ -69,6 +74,16 @@ hfest-repo -t <TOKEN> -o <ORG> --labels
hfest-repo -t <TOKEN> -u <USER>/-o <ORG> --remove
```

### Remove Hacktoberfest topic from a user/org
```sh
hfest-repo -t <TOKEN> -u <USER>/-o <ORG> --remove
```

### Remove Hacktoberfest topic and labels from a user/org
```sh
hfest-repo -t <TOKEN> -u <USER>/-o <ORG> --labels --remove
```

### Add an arbitrary topic to a user's/organization's repos instead of the `hacktoberfest` topic
```sh
hfest-repo -t <TOKEN> -u <USER>/-o <ORG> -p fun
Expand All @@ -92,7 +107,8 @@ Flags:
-u, --user=USER Github or Gitlab user to fetch repos of
-o, --org=ORG Github org or Gitlab group to fetch repos of
-p, --topic="hacktoberfest" topic to add to repos
-r, --remove Remove hacktoberfest topic from all repos
-r, --remove Remove topic and labels from all repos. Include -l to
remove labels
-l, --labels Add spam, invalid, and hacktoberfest-accepted labels to repo
--include-forks Include forks
--include-private Include private repos
Expand Down
79 changes: 54 additions & 25 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ import (
)

var (
vcs = kingpin.Flag("vcs", "Github or Gitlab, defaults to Github").Short('V').Default("Github").String()
accessToken = kingpin.Flag("access-token", "GitHub or Gitlab API Token - if unset, attempts to use this tool's stored token of its current default context. env var: ACCESS_TOKEN").Short('t').Envar("ACCESS_TOKEN").String()
user = kingpin.Flag("user", "Github or Gitlab user to fetch repos of").Short('u').String()
org = kingpin.Flag("org", "Github org or Gitlab group to fetch repos of").Short('o').String()
vcs = kingpin.Flag("vcs", "Github or GitLab, defaults to Github").Short('V').Default("Github").String()
accessToken = kingpin.Flag("access-token", "GitHub or GitLab API Token - if unset, attempts to use this tool's stored token of its current default context. env var: ACCESS_TOKEN").Short('t').Envar("ACCESS_TOKEN").String()
user = kingpin.Flag("user", "Github or GitLab user to fetch repos of").Short('u').String()
org = kingpin.Flag("org", "GitHub org or GitLab group to fetch repos of").Short('o').String()
topic = kingpin.Flag("topic", "topic to add to repos").Short('p').Default("hacktoberfest").String()
remove = kingpin.Flag("remove", "Remove hacktoberfest topic from all repos").Short('r').Default("false").Bool()
remove = kingpin.Flag("remove", "Remove topic and labels from all repos. Include -l to remove labels").Short('r').Default("false").Bool()
labels = kingpin.Flag("labels", "Add spam, invalid, and hacktoberfest-accepted labels to repo").Short('l').Default("false").Bool()
includeForks = kingpin.Flag("include-forks", "Include forks").Default("false").Bool()
includePrivate = kingpin.Flag("include-private", "Include private repos").Default("false").Bool()
Expand Down Expand Up @@ -190,20 +190,35 @@ func main() {
"spam": "#b33a3a",
}
if *labels == true {
for label, color := range labelColors {
labelOpt := gitlab.CreateLabelOptions{Name: gitlab.String(label), Color: gitlab.String(color)}
_, _, err := client.Labels.CreateLabel(repo.ID, &labelOpt)
if err != nil {
if strings.Contains(err.Error(), "already_exists") {
continue
if *remove == true {
for label, _ := range labelColors {
labelOpt := gitlab.DeleteLabelOptions{Name: gitlab.String(label)}
_, err := client.Labels.DeleteLabel(repo.ID, &labelOpt)
if err != nil {
if strings.Contains(err.Error(), "doesnt_exist") {
continue
} else {
loggerWithFields.WithError(err).Infof("issue removing hacktoberfest labels in repo")
}
} else {
loggerWithFields.WithError(err).Infof("issue adding hacktoberfest label to repo")
loggerWithFields.WithField("label", label).Info("removing labels")
}
}
} else {
for label, color := range labelColors {
labelOpt := gitlab.CreateLabelOptions{Name: gitlab.String(label), Color: gitlab.String(color)}
_, _, err := client.Labels.CreateLabel(repo.ID, &labelOpt)
if err != nil {
if strings.Contains(err.Error(), "already_exists") {
continue
} else {
loggerWithFields.WithError(err).Infof("issue adding hacktoberfest label to repo")
}
} else {
loggerWithFields.WithField("label", label).Info("adding labels")
}
} else {
loggerWithFields.WithField("label", label).Info("adding labels")
}
}

}
} else {
loggerWithFields.WithField("topic", *topic).Infof("[dryrun] %s topic", operation)
Expand Down Expand Up @@ -328,22 +343,36 @@ func main() {
}

labelColors := map[string]string{
"hacktoberfest-accepted": "9c4668",
"hacktoberfest-accepted": "f54501",
"invalid": "ca0b00",
"spam": "b33a3a",
}
if *labels == true {

for label, color := range labelColors {
_, _, err := client.Issues.CreateLabel(ctx, *repo.Owner.Login, *repo.Name, &github.Label{Name: github.String(label), Color: github.String(color)})
if err != nil {
if strings.Contains(err.Error(), "already_exists") {
continue
if *remove == true {
for label, _ := range labelColors {
_, err := client.Issues.DeleteLabel(ctx, *repo.Owner.Login, *repo.Name, label)
if err != nil {
if strings.Contains(err.Error(), "doesnt_exist") {
continue
} else {
loggerWithFields.WithError(err).Infof("issue removing hacktoberfest labels in repo")
}
} else {
loggerWithFields.WithField("label", label).Info("removing labels")
}
}
} else {
for label, color := range labelColors {
_, _, err := client.Issues.CreateLabel(ctx, *repo.Owner.Login, *repo.Name, &github.Label{Name: github.String(label), Color: github.String(color)})
if err != nil {
if strings.Contains(err.Error(), "already_exists") {
continue
} else {
loggerWithFields.WithError(err).Infof("issue adding hacktoberfest label to repo")
}
} else {
loggerWithFields.WithError(err).Infof("issue adding hacktoberfest label to repo")
loggerWithFields.WithField("label", label).Info("adding labels")
}
} else {
loggerWithFields.WithField("label", label).Info("adding labels")
}
}
}
Expand Down

0 comments on commit bd932f7

Please sign in to comment.