@@ -81,11 +81,6 @@ func AddMetadataToIssueParams(client *api.Client, baseRepo ghrepo.Interface, par
8181 return nil
8282 }
8383
84- var err error
85- if tb .Assignees , err = ReplaceAtMeLogin (tb .Assignees , client , baseRepo ); err != nil {
86- return err
87- }
88-
8984 if err := fillMetadata (client , baseRepo , tb ); err != nil {
9085 return err
9186 }
@@ -196,17 +191,47 @@ func quoteValueForQuery(v string) string {
196191 return v
197192}
198193
199- // ReplaceAtMeLogin iterates over the list of specified login names, replacing
200- // any "@me" mentions with the current user LoginName.
201- func ReplaceAtMeLogin (logins []string , client * api.Client , repo ghrepo.Interface ) ([]string , error ) {
202- for i , u := range logins {
203- if strings .EqualFold (u , "@me" ) {
204- login , err := api .CurrentLoginName (client , repo .RepoHost ())
205- if err != nil {
206- return logins , fmt .Errorf ("@me resolve: failed obtaining user id: %w" , err )
207- }
208- logins [i ] = login
194+ // MeReplacer resolves usages of `@me` to the handle of the currently logged in user.
195+ type MeReplacer struct {
196+ apiClient * api.Client
197+ hostname string
198+ login string
199+ }
200+
201+ func NewMeReplacer (apiClient * api.Client , hostname string ) * MeReplacer {
202+ return & MeReplacer {
203+ apiClient : apiClient ,
204+ hostname : hostname ,
205+ }
206+ }
207+
208+ func (r * MeReplacer ) currentLogin () (string , error ) {
209+ if r .login != "" {
210+ return r .login , nil
211+ }
212+ login , err := api .CurrentLoginName (r .apiClient , r .hostname )
213+ if err != nil {
214+ return "" , fmt .Errorf ("failed resolving `@me` to your user handle: %w" , err )
215+ }
216+ r .login = login
217+ return login , nil
218+ }
219+
220+ func (r * MeReplacer ) Replace (handle string ) (string , error ) {
221+ if handle == "@me" {
222+ return r .currentLogin ()
223+ }
224+ return handle , nil
225+ }
226+
227+ func (r * MeReplacer ) ReplaceSlice (handles []string ) ([]string , error ) {
228+ res := make ([]string , len (handles ))
229+ for i , h := range handles {
230+ var err error
231+ res [i ], err = r .Replace (h )
232+ if err != nil {
233+ return nil , err
209234 }
210235 }
211- return logins , nil
236+ return res , nil
212237}
0 commit comments