@@ -41,6 +41,8 @@ const (
4141 PerPageValue = 50
4242)
4343
44+ const DefaultRepoKey string = "%s_%s-default-repo"
45+
4446var validFeatures = map [string ]bool {
4547 featureIssueCreation : true ,
4648 featureIssues : true ,
@@ -127,7 +129,7 @@ func (p *Plugin) getCommand(config *Configuration) (*model.Command, error) {
127129 return & model.Command {
128130 Trigger : "github" ,
129131 AutoComplete : true ,
130- AutoCompleteDesc : "Available commands: connect, disconnect, todo, subscriptions, issue, me, mute, settings, help, about" ,
132+ AutoCompleteDesc : "Available commands: connect, disconnect, todo, subscriptions, issue, default-repo, me, mute, settings, help, about" ,
131133 AutoCompleteHint : "[command]" ,
132134 AutocompleteData : getAutocompleteData (config ),
133135 AutocompleteIconData : iconData ,
@@ -743,6 +745,110 @@ func (p *Plugin) handleIssue(_ *plugin.Context, args *model.CommandArgs, paramet
743745 }
744746}
745747
748+ func (p * Plugin ) handleDefaultRepo (c * plugin.Context , args * model.CommandArgs , parameters []string , userInfo * GitHubUserInfo ) string {
749+ if len (parameters ) == 0 {
750+ return "Invalid action. Available actions are 'set', 'get' and 'unset'."
751+ }
752+
753+ command := parameters [0 ]
754+ parameters = parameters [1 :]
755+
756+ switch {
757+ case command == "set" :
758+ return p .handleSetDefaultRepo (args , parameters , userInfo )
759+ case command == "get" :
760+ return p .handleGetDefaultRepo (args , userInfo )
761+ case command == "unset" :
762+ return p .handleUnSetDefaultRepo (args , userInfo )
763+ default :
764+ return fmt .Sprintf ("Unknown subcommand %v" , command )
765+ }
766+ }
767+
768+ func (p * Plugin ) handleSetDefaultRepo (args * model.CommandArgs , parameters []string , userInfo * GitHubUserInfo ) string {
769+ if len (parameters ) == 0 {
770+ return "Please specify a repository."
771+ }
772+
773+ repo := parameters [0 ]
774+ config := p .getConfiguration ()
775+ baseURL := config .getBaseURL ()
776+ owner , repo := parseOwnerAndRepo (repo , baseURL )
777+ if owner == "" || repo == "" {
778+ return "Please provide a valid repository"
779+ }
780+
781+ owner = strings .ToLower (owner )
782+ repo = strings .ToLower (repo )
783+
784+ if config .GitHubOrg != "" && strings .ToLower (config .GitHubOrg ) != owner {
785+ return fmt .Sprintf ("Repository is not part of the locked Github organization. Locked Github organization: %s" , config .GitHubOrg )
786+ }
787+
788+ ctx := context .Background ()
789+ githubClient := p .githubConnectUser (ctx , userInfo )
790+
791+ ghRepo , _ , err := githubClient .Repositories .Get (ctx , owner , repo )
792+ if err != nil {
793+ return "Error occurred while getting github repository details"
794+ }
795+ if ghRepo == nil {
796+ return fmt .Sprintf ("Unknown repository %s" , fullNameFromOwnerAndRepo (owner , repo ))
797+ }
798+
799+ if _ , err := p .store .Set (fmt .Sprintf (DefaultRepoKey , args .ChannelId , userInfo .UserID ), []byte (fmt .Sprintf ("%s/%s" , owner , repo ))); err != nil {
800+ return "Error occurred saving the default repo"
801+ }
802+
803+ repoLink := fmt .Sprintf ("%s%s/%s" , baseURL , owner , repo )
804+ successMsg := fmt .Sprintf ("The default repo has been set to [%s/%s](%s) for this channel" , owner , repo , repoLink )
805+
806+ return successMsg
807+ }
808+
809+ func (p * Plugin ) GetDefaultRepo (userID , channelID string ) (string , error ) {
810+ var defaultRepoBytes []byte
811+ if err := p .store .Get (fmt .Sprintf (DefaultRepoKey , channelID , userID ), & defaultRepoBytes ); err != nil {
812+ return "" , err
813+ }
814+
815+ return string (defaultRepoBytes ), nil
816+ }
817+
818+ func (p * Plugin ) handleGetDefaultRepo (args * model.CommandArgs , userInfo * GitHubUserInfo ) string {
819+ defaultRepo , err := p .GetDefaultRepo (userInfo .UserID , args .ChannelId )
820+ if err != nil {
821+ p .client .Log .Warn ("Not able to get the default repo" , "UserID" , userInfo .UserID , "ChannelID" , args .ChannelId , "Error" , err .Error ())
822+ return "Error occurred while getting the default repo"
823+ }
824+
825+ if defaultRepo == "" {
826+ return "You have not set a default repository for this channel"
827+ }
828+
829+ config := p .getConfiguration ()
830+ repoLink := config .getBaseURL () + defaultRepo
831+ return fmt .Sprintf ("The default repository is [%s](%s)" , defaultRepo , repoLink )
832+ }
833+
834+ func (p * Plugin ) handleUnSetDefaultRepo (args * model.CommandArgs , userInfo * GitHubUserInfo ) string {
835+ defaultRepo , err := p .GetDefaultRepo (userInfo .UserID , args .ChannelId )
836+ if err != nil {
837+ p .client .Log .Warn ("Not able to get the default repo" , "UserID" , userInfo .UserID , "ChannelID" , args .ChannelId , "Error" , err .Error ())
838+ return "Error occurred while getting the default repo"
839+ }
840+
841+ if defaultRepo == "" {
842+ return "You have not set a default repository for this channel"
843+ }
844+
845+ if err := p .store .Delete (fmt .Sprintf (DefaultRepoKey , args .ChannelId , userInfo .UserID )); err != nil {
846+ return "Error occurred while unsetting the repo for this channel"
847+ }
848+
849+ return "The default repository has been unset successfully"
850+ }
851+
746852func (p * Plugin ) handleSetup (_ * plugin.Context , args * model.CommandArgs , parameters []string ) string {
747853 userID := args .UserId
748854 isSysAdmin , err := p .isAuthorizedSysAdmin (userID )
@@ -912,7 +1018,7 @@ func getAutocompleteData(config *Configuration) *model.AutocompleteData {
9121018 return github
9131019 }
9141020
915- github := model .NewAutocompleteData ("github" , "[command]" , "Available commands: connect, disconnect, todo, subscriptions, issue, me, mute, settings, help, about" )
1021+ github := model .NewAutocompleteData ("github" , "[command]" , "Available commands: connect, disconnect, todo, subscriptions, issue, default-repo, me, mute, settings, help, about" )
9161022
9171023 connect := model .NewAutocompleteData ("connect" , "" , "Connect your Mattermost account to your GitHub account" )
9181024 if config .EnablePrivateRepo {
@@ -985,6 +1091,20 @@ func getAutocompleteData(config *Configuration) *model.AutocompleteData {
9851091
9861092 github .AddCommand (issue )
9871093
1094+ defaultRepo := model .NewAutocompleteData ("default-repo" , "[command]" , "Available commands: set, get, unset" )
1095+ defaultRepoSet := model .NewAutocompleteData ("set" , "[owner/repo]" , "Set the default repository for the channel" )
1096+ defaultRepoSet .AddTextArgument ("Owner/repo to set as a default repository" , "[owner/repo]" , "" )
1097+
1098+ defaultRepoGet := model .NewAutocompleteData ("get" , "" , "Get the default repository already set for the channel" )
1099+
1100+ defaultRepoDelete := model .NewAutocompleteData ("unset" , "" , "Unset the default repository set for the channel" )
1101+
1102+ defaultRepo .AddCommand (defaultRepoSet )
1103+ defaultRepo .AddCommand (defaultRepoGet )
1104+ defaultRepo .AddCommand (defaultRepoDelete )
1105+
1106+ github .AddCommand (defaultRepo )
1107+
9881108 me := model .NewAutocompleteData ("me" , "" , "Display the connected GitHub account" )
9891109 github .AddCommand (me )
9901110
0 commit comments