Skip to content

Commit

Permalink
Organization: get Copilot seat assignments
Browse files Browse the repository at this point in the history
Retrieves the list of Copilot basics when the organization
has Copilot for Business attachments.

Do note that this API only works to page, slowly, at scale to
a point - after 400 pages I am not able to get results due to
a hard cap of sorts here, so need to rely on other methods to
get the info.
  • Loading branch information
jeffwilcox committed Nov 3, 2023
1 parent d8195d4 commit 4471969
Show file tree
Hide file tree
Showing 6 changed files with 264 additions and 46 deletions.
9 changes: 9 additions & 0 deletions business/githubApps/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ export function getAppPurposeId(purpose: AppPurposeTypes) {
return id;
}

export function tryGetAppPurposeAppConfiguration(purpose: AppPurposeTypes, organizationName: string) {
if (
(purpose as ICustomAppPurpose).isCustomAppPurpose === true &&
(purpose as ICustomAppPurpose).getForOrganizationName
) {
return (purpose as ICustomAppPurpose).getForOrganizationName(organizationName);
}
}

export class GitHubAppPurposes {
private static _instance: GitHubAppPurposes = new GitHubAppPurposes();

Expand Down
14 changes: 14 additions & 0 deletions business/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import { ConfigGitHubTemplates } from '../config/github.templates.types';
import { GitHubTokenManager } from './githubApps/tokenManager';
import { OrganizationProjects } from './projects';
import { OrganizationDomains } from './domains';
import { OrganizationCopilot } from './organizationCopilot';

interface IGetMembersParameters {
org: string;
Expand Down Expand Up @@ -219,6 +220,8 @@ export class Organization {
private _projects: OrganizationProjects;
private _domains: OrganizationDomains;

private _copilot: OrganizationCopilot;

id: number;
uncontrolled: boolean;

Expand Down Expand Up @@ -351,6 +354,17 @@ export class Organization {
return this._projects;
}

get copilot() {
if (!this._copilot) {
this._copilot = new OrganizationCopilot(
this,
this._getSpecificAuthorizationHeader.bind(this),
this._operations
);
}
return this._copilot;
}

get domains() {
if (!this._domains) {
this._domains = new OrganizationDomains(
Expand Down
68 changes: 68 additions & 0 deletions business/organizationCopilot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// Copyright (c) Microsoft.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

import {
IGetAuthorizationHeader,
IOperationsInstance,
IPagedCacheOptions,
IPurposefulGetAuthorizationHeader,
throwIfNotGitHubCapable,
} from '../interfaces';
import type { CollectionCopilotSeatsOptions } from '../lib/github/collections';
import { AppPurpose, AppPurposeTypes } from './githubApps';
import { CacheDefault, getMaxAgeSeconds, getPageSize } from './operations/core';
import { Organization } from './organization';

export type CopilotSeatData = {
assignee: {
avatar_url: string;
id: number;
login: string;
};
created_at: string;
updated_at: string;
last_activity_at: string;
last_activity_editor: string;
};

export class OrganizationCopilot {
constructor(
private organization: Organization,
private getSpecificAuthorizationHeader: IPurposefulGetAuthorizationHeader,
private operations: IOperationsInstance
) {}

async getSeatActivity(
options?: IPagedCacheOptions,
appPurpose: AppPurposeTypes = AppPurpose.Data
): Promise<CopilotSeatData[]> {
options = options || {};
const operations = throwIfNotGitHubCapable(this.operations);
const getAuthorizationHeader = this.getSpecificAuthorizationHeader.bind(
this,
appPurpose
) as IGetAuthorizationHeader;
const github = operations.github;
const parameters: CollectionCopilotSeatsOptions = {
org: this.organization.name,
per_page: getPageSize(operations),
};
const caching = {
maxAgeSeconds: getMaxAgeSeconds(operations, CacheDefault.orgMembersStaleSeconds, options),
backgroundRefresh: true,
pageRequestDelay: options.pageRequestDelay,
};
if (options && options.backgroundRefresh === false) {
caching.backgroundRefresh = false;
}
(caching as any).pageLimit = 10;
const seats = (await github.collections.getOrganizationCopilotSeats(
getAuthorizationHeader,
parameters,
caching
)) as CopilotSeatData[];
return seats;
}
}
Loading

0 comments on commit 4471969

Please sign in to comment.