MCPcopy Create free account
hub / github.com/bytebase/bytebase / ensureAppInstalledForUser

Method ensureAppInstalledForUser

backend/plugin/webhook/teams/app.go:311–348  ·  view source on GitHub ↗

ensureAppInstalledForUser ensures the Teams app is installed for a user. Returns the installation ID.

(ctx context.Context, userAADID, teamsAppID string)

Source from the content-addressed store, hash-verified

309// ensureAppInstalledForUser ensures the Teams app is installed for a user.
310// Returns the installation ID.
311func (p *provider) ensureAppInstalledForUser(ctx context.Context, userAADID, teamsAppID string) (string, error) {
312 // First, check if the app is already installed.
313 listURL := fmt.Sprintf("https://graph.microsoft.com/v1.0/users/%s/teamwork/installedApps?$filter=teamsApp/externalId%%20eq%%20'%s'&$expand=teamsApp", userAADID, p.clientID)
314
315 b, err := p.doGraphRequest(ctx, http.MethodGet, listURL, nil)
316 if err == nil {
317 var resp teamsAppInstallationsResponse
318 if err := json.Unmarshal(b, &resp); err == nil && len(resp.Value) > 0 {
319 return resp.Value[0].ID, nil
320 }
321 }
322
323 // App not installed, install it.
324 installURL := fmt.Sprintf("https://graph.microsoft.com/v1.0/users/%s/teamwork/installedApps", userAADID)
325 installBody := fmt.Sprintf(`{"teamsApp@odata.bind":"https://graph.microsoft.com/v1.0/appCatalogs/teamsApps/%s"}`, teamsAppID)
326
327 _, err = p.doGraphRequest(ctx, http.MethodPost, installURL, []byte(installBody))
328 if err != nil {
329 return "", errors.Wrapf(err, "failed to install Teams app for user")
330 }
331
332 // Retrieve the installation ID.
333 b, err = p.doGraphRequest(ctx, http.MethodGet, listURL, nil)
334 if err != nil {
335 return "", errors.Wrapf(err, "failed to get installation after install")
336 }
337
338 var resp teamsAppInstallationsResponse
339 if err := json.Unmarshal(b, &resp); err != nil {
340 return "", errors.Wrapf(err, "failed to unmarshal installation response")
341 }
342
343 if len(resp.Value) == 0 {
344 return "", errors.Errorf("installation not found after installing app")
345 }
346
347 return resp.Value[0].ID, nil
348}
349
350// getChatIDForUser gets the chat ID for a user's app installation.
351func (p *provider) getChatIDForUser(ctx context.Context, userAADID, installationID string) (string, error) {

Callers 1

sendMessageMethod · 0.95

Calls 3

doGraphRequestMethod · 0.95
UnmarshalMethod · 0.80
ErrorfMethod · 0.80

Tested by

no test coverage detected