Skip to content

Instantly share code, notes, and snippets.

@RajatVaryani
Last active December 3, 2019 08:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RajatVaryani/a8121661a112b6fc41d5ea13975e724f to your computer and use it in GitHub Desktop.
Save RajatVaryani/a8121661a112b6fc41d5ea13975e724f to your computer and use it in GitHub Desktop.
Initial version of Proposed changes
// ------------------ Proposed changes in helpers ----------------------------
var rootCommand *cobra.Command
var lock sync.Mutex
func (p *HelpersImpl) RegisterCobraSlashCommand(c *cobra.Command, mcmd *model.Command) error {
rootCommand = c
if err := p.API.RegisterCommand(mcmd); err != nil {
return err
}
return nil
}
func (p *HelpersImpl) ExecuteCommand(args *model.CommandArgs) (*model.CommandResponse, *model.AppError) {
lock.Lock()
defer lock.Unlock()
fields := strings.Fields(args.Command)
parameters := fields[1:]
rootCommand.SetArgs(parameters)
if err := rootCommand.Execute(); err != nil {
return nil, &model.AppError{Message: err.Error()}
}
// We won't be able to alter the CommandResponse depending on the subcommand
return &model.CommandResponse{}, nil
}
// ------------------ Changes in Plugin struct----------------------------
type Plugin struct {
plugin.MattermostPlugin
// githubPermalinkRegex is used to parse github permalinks in post messages.
githubPermalinkRegex *regexp.Regexp
BotUserID string
// configurationLock synchronizes access to the configuration.
configurationLock sync.RWMutex
// configuration is the active plugin configuration. Consult getConfiguration and
// setConfiguration for usage.
configuration *configuration
context *plugin.Context
args *model.CommandArgs
}
// ------------------ Changes while registering the command----------------------------
func (p *Plugin) connect(command *cobra.Command, args []string) error {
config := p.API.GetConfig()
if config.ServiceSettings.SiteURL == nil {
p.postCommandResponse(p.args, "Encountered an error connecting to GitHub.")
return nil
}
p.postCommandResponse(p.args, fmt.Sprintf("[Click here to link your GitHub account.](%s/plugins/github/oauth/connect)", *config.ServiceSettings.SiteURL))
return nil
}
func (p *Plugin) OnActivate() error {
config := p.getConfiguration()
if err := config.IsValid(); err != nil {
return err
}
rootCommand := &cobra.Command{Use: "/github"}
command := &cobra.Command{Use: "connect", RunE: p.connect}
rootCommand.AddCommand(command)
p.Helpers.RegisterCobraSlashCommand(rootCommand, getCommand())
}
// ------------------------------ While executing the command-----------------------------
func (p *Plugin) ExecuteCommand(c *plugin.Context, args *model.CommandArgs) (*model.CommandResponse, *model.AppError) {
p.Args = args
p.Context = c
p.Helpers.ExecuteCommand(args)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment