Skip to content

Commit

Permalink
Repository invitations list support
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffwilcox committed Oct 6, 2023
1 parent 10233a2 commit e55f5ca
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 4 deletions.
1 change: 1 addition & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,7 @@
"shortcutting",
"shouldrestore",
"showids",
"shuf",
"Sida",
"signin",
"signoff",
Expand Down
2 changes: 2 additions & 0 deletions api/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import routeNews from './news';
import routeCrossOrganizationPeople from './people';
import routeCrossOrganizationRepos from './repos';
import routeCrossOrganizationTeams from './teams';
import routeUsers from './users';

const router: Router = Router();

Expand Down Expand Up @@ -63,6 +64,7 @@ router.use('/signout', routeSession);
router.use('/people', routeCrossOrganizationPeople);
router.use('/repos', routeCrossOrganizationRepos);
router.use('/teams', routeCrossOrganizationTeams);
router.use('/users', routeUsers);
router.use('/news', routeNews);

const dynamicStartupInstance = getCompanySpecificDeployment();
Expand Down
4 changes: 2 additions & 2 deletions api/client/people.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { type GitHubSimpleAccount, type ICorporateLink, ReposAppRequest } from '
import JsonPager from './jsonPager';
import getCompanySpecificDeployment from '../../middleware/companySpecificDeployment';

import RouteGetPerson from './person';
import { getPerson as routeGetPerson } from './person';
import { equivalentLegacyPeopleSearch } from './peopleSearch';

const router: Router = Router();
Expand All @@ -37,7 +37,7 @@ interface IOrganizationMembershipAccount {
[id: string]: GitHubSimpleAccount;
}

router.get('/:login', RouteGetPerson);
router.get('/:login', routeGetPerson);

router.get(
'/',
Expand Down
4 changes: 3 additions & 1 deletion api/client/person.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { IGraphEntry } from '../../lib/graphProvider';
import { jsonError } from '../../middleware';
import { getProviders } from '../../transitional';

export default asyncHandler(async (req: ReposAppRequest, res: Response, next: NextFunction) => {
const getPerson = asyncHandler(async (req: ReposAppRequest, res: Response, next: NextFunction) => {
const providers = getProviders(req);
const { operations, queryCache, graphProvider } = providers;
const login = req.params.login as string;
Expand Down Expand Up @@ -77,3 +77,5 @@ export default asyncHandler(async (req: ReposAppRequest, res: Response, next: Ne
return next(jsonError(`login ${login} error: ${error}`, 500));
}
});

export { getPerson };
6 changes: 6 additions & 0 deletions business/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ export class Account {
case AccountJsonFormat.GitHub: {
return basic;
}
case AccountJsonFormat.GitHubExtended: {
const cloneEntity = Object.assign({}, this._originalEntity || {});
delete (cloneEntity as any).cost;
delete (cloneEntity as any).headers;
return cloneEntity;
}
case AccountJsonFormat.GitHubDetailedWithLink: {
const cloneEntity = Object.assign({}, this._originalEntity || {});
delete (cloneEntity as any).cost;
Expand Down
40 changes: 40 additions & 0 deletions business/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import { RepositoryActions } from './repositoryActions';
import { RepositoryPullRequest } from './repositoryPullRequest';
import { ErrorHelper } from '../transitional';
import { augmentInertiaPreview, RepositoryProject } from './repositoryProject';
import { RepositoryInvitation } from './repositoryInvitation';

interface IRepositoryMoments {
created?: moment.Moment;
Expand Down Expand Up @@ -1057,6 +1058,39 @@ export class Repository {
return collaborators;
}

async listCollaboratorInvitations(cacheOptions?: IPagedCacheOptions): Promise<RepositoryInvitation[]> {
cacheOptions = cacheOptions || {};
const operations = throwIfNotGitHubCapable(this._operations);
const github = operations.github;
const parameters = {
owner: this.organization.name,
repo: this.name,
per_page: getPageSize(operations),
};
if (!cacheOptions.maxAgeSeconds) {
cacheOptions.maxAgeSeconds = getMaxAgeSeconds(
operations,
CacheDefault.orgRepoCollaboratorsStaleSeconds
);
}
if (cacheOptions.backgroundRefresh === undefined) {
cacheOptions.backgroundRefresh = true;
}
const invitationEntities = await github.collections.getRepoInvitations(
this.authorize(AppPurpose.Data),
parameters,
cacheOptions
);
const invitations = common.createInstances<RepositoryInvitation>(
this,
invitationFromEntity,
invitationEntities
);
invitationEntities?.cost && ((invitations as any).cost = invitationEntities.cost);
invitationEntities?.headers && ((invitations as any).headers = invitationEntities.headers);
return invitations;
}

async addCollaborator(
username: string,
permission: GitHubRepositoryPermission
Expand Down Expand Up @@ -1945,3 +1979,9 @@ function collaboratorPermissionFromEntity(entity) {
const permission = new Collaborator(entity);
return permission;
}

function invitationFromEntity(entity) {
// 'this' is bound for this function to be a private method
const invitation = new RepositoryInvitation(this, entity);
return invitation;
}
1 change: 1 addition & 0 deletions interfaces/github/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

export enum AccountJsonFormat {
GitHub = 'github',
GitHubExtended = 'github+extended',
UplevelWithLink = 'github+link',
GitHubDetailedWithLink = 'detailed+link',
}
Expand Down
24 changes: 23 additions & 1 deletion lib/github/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ import { IRestResponse, flattenData } from './core';
import { CompositeApiContext, CompositeIntelligentEngine } from './composite';
import { Collaborator } from '../../business/collaborator';
import { Team } from '../../business/team';
import { IPagedCacheOptions, IGetAuthorizationHeader, IDictionary } from '../../interfaces';
import {
IPagedCacheOptions,
IGetAuthorizationHeader,
IDictionary,
GitHubRepositoryPermission,
} from '../../interfaces';
import { RestLibrary } from '.';
import { sleep } from '../../utils';
import GitHubApplication from '../../business/application';
import { RepositoryPrimaryProperties } from '../../business/primaryProperties';
import { RepositoryInvitation } from '../../business/repositoryInvitation';

export interface IGetAppInstallationsParameters {
app_id: string;
Expand Down Expand Up @@ -57,6 +63,7 @@ const teamDetailsToCopy = Team.PrimaryProperties;
const memberDetailsToCopy = Collaborator.PrimaryProperties;
const appInstallDetailsToCopy = GitHubApplication.PrimaryInstallationProperties;
const contributorsDetailsToCopy = [...Collaborator.PrimaryProperties, 'contributions'];
const repoInviteDetailsToCopy = RepositoryInvitation.PrimaryProperties;

const teamPermissionsToCopy = [
'id',
Expand Down Expand Up @@ -294,6 +301,21 @@ export class RestCollections {
);
}

getRepoInvitations(
token: string | IGetAuthorizationHeader,
options,
cacheOptions: IPagedCacheOptions
): Promise<any> {
return this.generalizedCollectionWithFilter(
'repoInvitations',
'repos.listInvitations',
repoInviteDetailsToCopy,
token,
options,
cacheOptions
);
}

getRepoBranches(
token: string | IGetAuthorizationHeader,
options,
Expand Down
4 changes: 4 additions & 0 deletions utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export function daysInMilliseconds(days: number): number {
return 1000 * 60 * 60 * 24 * days;
}

export function dateToDateString(date: Date) {
return date.toISOString().substr(0, 10);
}

export function stringOrNumberAsString(value: any) {
if (typeof value === 'number') {
return (value as number).toString();
Expand Down

0 comments on commit e55f5ca

Please sign in to comment.