sync_labels

package
v0.0.0-...-4603220 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 9, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Cmd = &cobra.Command{
	Use:   "sync-labels",
	Short: "Sync the labels",
	Long: `
examples:

  GH_TOKEN=<GH_TOKEN> github-adm sync-labels --base-url https://github.ibm.com/api/v3 --upload-url https://uploads.github.ibm.com/ --org org --repo repo --manifest labels.json --delete-out-of-sync`,
	SilenceUsage: true,
	RunE: func(cmd *cobra.Command, args []string) error {
		globalOpt := pkg.GlobalOptions
		opt := pkg.SyncLabelsOptions
		klog.Infof("Manifest file is : %s", opt.Manifest)
		manifest, err := ioutil.ReadFile(opt.Manifest)
		if err != nil {
			return err
		}
		var labels []Label
		err = json.Unmarshal(manifest, &labels)
		if err != nil {
			return err
		}
		klog.Infof("manifest: %v", labels)
		gh, err := client.NewGithub(globalOpt.BaseURL, globalOpt.UploadURL, globalOpt.ApiKey)
		if err != nil {
			return err
		}
		repoLabels, err := gh.ListLabels(opt.Org, opt.Repo)
		if err != nil {
			return err
		}

		klog.Infof("Deleting the labels from the repo which is not sync with the manifest file")
		var wg sync.WaitGroup
		for i := range repoLabels {
			if l := searchManifestLabels(*repoLabels[i].Name, labels); l == nil {
				klog.Infof("out of sync: label not found in the manifest, %s action taken on the label: %s", DeleteOutOfSyncActions[opt.DeleteOutOfSync], *repoLabels[i].Name)
				if opt.DeleteOutOfSync {
					wg.Add(1)
					go func(wg *sync.WaitGroup, label string) {
						defer wg.Done()
						klog.Infof("deleting label: %s", label)
						resp, err := gh.DeleteLabel(opt.Org, opt.Repo, url.PathEscape(label))
						if err != nil {
							klog.Errorf("failed to delete label: %s, err: %v", label, err)
							return
						}
						klog.Infof("deleted label: %s, %v %v", label, resp.StatusCode, resp.Body)
					}(&wg, *repoLabels[i].Name)
				}
			}
		}

		klog.Infof("Deleting the labels from the repo which is not sync with the manifest file.. DONE")

		klog.Infof("Adding any missing label from manifest file to repository")
		for i := range labels {
			if l := searchGitHubLabels(labels[i].Name, repoLabels); l == nil {
				klog.Infof("out of sync: label not found in the github repo, create action taken on the label: %s", labels[i].Name)
				wg.Add(1)
				label := github.Label{
					Name:  &labels[i].Name,
					Color: &labels[i].Color,
				}
				go func(wg *sync.WaitGroup, label *github.Label) {
					defer wg.Done()
					klog.Infof("adding label: %s", label)
					_, _, err := gh.CreateLabel(opt.Org, opt.Repo, label)
					if err != nil {
						klog.Errorf("failed to delete label: *repoLabels[i].Name, err: %v", err)
					}
					klog.Infof("added label: %s", label)
				}(&wg, &label)
			}
		}
		klog.Infof("Adding any missing label from manifest file to repository... DONE")

		wg.Wait()
		return nil
	},
}
View Source
var DeleteOutOfSyncActions = map[bool]string{
	// contains filtered or unexported fields
}

Functions

This section is empty.

Types

type Label

type Label struct {
	Name        string `json:"name"`
	Color       string `json:"color"`
	Description string `json:"description,omitempty"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL