github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cmd/step/pr/step_pr_labels.go (about) 1 package pr 2 3 import ( 4 "fmt" 5 "os" 6 "regexp" 7 "strconv" 8 "strings" 9 10 "github.com/olli-ai/jx/v2/pkg/cmd/helper" 11 "github.com/olli-ai/jx/v2/pkg/util" 12 13 "github.com/pkg/errors" 14 "github.com/spf13/cobra" 15 16 "github.com/jenkins-x/jx-logging/pkg/log" 17 "github.com/olli-ai/jx/v2/pkg/cmd/opts" 18 "github.com/olli-ai/jx/v2/pkg/cmd/templates" 19 ) 20 21 // DefaultPrefix for all PR labels environment keys 22 const DefaultPrefix = "JX_PR_LABELS" 23 24 // StepPRLabelsOptions holds the options for the cmd 25 type StepPRLabelsOptions struct { 26 *opts.CommonOptions 27 28 Dir string 29 Prefix string 30 PullRequest string 31 } 32 33 var ( 34 labelLong = templates.LongDesc(` 35 Creates environment variables from the labels in a pull request. 36 37 Environment variables are prefixed per default with ` + DefaultPrefix + `. 38 You can use the '--prefix' argument to set a different prefix. 39 `) 40 41 labelExample = templates.Examples(` 42 # List all labels of a given pull-request 43 jx step pr labels 44 45 # List all labels of a given pull-request using a custom prefix 46 jx step pr --prefix PRL 47 48 # List all labels of a given pull-request using a custom pull-request number 49 jx step pr --pr PR-34 50 jx step pr --pr 34 51 52 `) 53 ) 54 55 // NewCmdStepPRLabels creates the new cmd 56 func NewCmdStepPRLabels(commonOpts *opts.CommonOptions) *cobra.Command { 57 options := &StepPRLabelsOptions{ 58 CommonOptions: commonOpts, 59 } 60 cmd := &cobra.Command{ 61 Use: "labels", 62 Short: "List all labels of a given pull-request", 63 Long: labelLong, 64 Example: labelExample, 65 Run: func(cmd *cobra.Command, args []string) { 66 options.Cmd = cmd 67 options.Args = args 68 err := options.Run() 69 helper.CheckErr(err) 70 }, 71 } 72 cmd.Flags().StringVarP(&options.PullRequest, "pr", "", "", "Git Pull Request number") 73 cmd.Flags().StringVarP(&options.Prefix, "prefix", "p", "", "Environment variable prefix") 74 return cmd 75 } 76 77 // Run implements the execution 78 func (o *StepPRLabelsOptions) Run() error { 79 gitInfo, provider, _, err := o.CreateGitProvider(o.Dir) 80 if err != nil { 81 return err 82 } 83 if provider == nil { 84 return fmt.Errorf("No Git provider could be found. Are you in a directory containing a `.git/config` file?") 85 } 86 87 if o.PullRequest == "" { 88 o.PullRequest = strings.TrimPrefix(os.Getenv(util.EnvVarBranchName), "PR-") 89 } 90 91 if o.Prefix == "" { 92 o.Prefix = DefaultPrefix 93 } 94 95 prNum, err := strconv.Atoi(o.PullRequest) 96 if err != nil { 97 log.Logger().Warn("Unable to convert PR " + o.PullRequest + " to a number") 98 } 99 100 pr, err := provider.GetPullRequest(gitInfo.Organisation, gitInfo, prNum) 101 if err != nil { 102 return errors.Wrapf(err, "failed to find PullRequest %d", prNum) 103 } 104 105 reg, err := regexp.Compile("[^a-zA-Z0-9]+") 106 if err != nil { 107 return errors.Wrapf(err, "failed to create regex %v", reg) 108 } 109 110 for _, v := range pr.Labels { 111 envKey := reg.ReplaceAllString(*v.Name, "_") 112 log.Logger().Infof("%v_%v='%v'", o.Prefix, strings.ToUpper(envKey), *v.Name) 113 } 114 return nil 115 }