Skip to content

Commit

Permalink
adds route: `/v1/github/(orgs|users)/{login_name}/projects/{project_i…
Browse files Browse the repository at this point in the history
…d_or_name}/repositories​/contributions` (closes #58) [1.2.0]
  • Loading branch information
jonasfroeller committed Nov 22, 2024
1 parent a92d409 commit e9a6871
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 38 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ bun run dev

Open <http://localhost:3000/> with your browser to see the result.

### GitHub GraphQL-Explorer

The [Github GraphQL-Explorer](https://docs.github.com/en/graphql/overview/explorer) can only be used on the GitHub docs domain, because everything else is not in GitHub's CORS policy (docs.github.com).

The window is quite small. I recommend using a [Stylus](https://chromewebstore.google.com/detail/stylus/clngdbkpkpeebahjckkjfobafhncgmne) user-css to make it larger. ([example](https://gist.github.com/jonasfroeller/c1714de2d7fb162fdef94e3f83df9d0e)).
Use a [diacritics remover](https://pteo.paranoiaworks.mobi/diacriticsremover), if you copied the graphql from somewhere else, before pasting it.

### Testing

[![cov](https://propromo-software.github.io/propromo.rest/coverage.svg)](https://github.com/propromo-software/propromo.rest/actions)
Expand Down
50 changes: 49 additions & 1 deletion src/v1/adapters/github/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1018,7 +1018,55 @@ const ACCOUNT_LEVEL_CHILDREN = (login_type: "organization" | "user") =>
tags: ["github"],
},
},
),
)
.get(
"/contributions",
async ({
fetchParams,
params: { login_name, project_id_or_name },
query,
set,
}) => {
const response =
await fetchGithubDataUsingGraphql<{
project: ProjectV2;
}>(
AccountScopeEntryRoot(
login_name,
getAllRepositoriesInProject(
project_id_or_name,
[
GITHUB_PROJECT_SCOPES.REPOSITORIES_LINKED,
],
[
{
scopeName: GITHUB_REPOSITORY_SCOPES.CONTRIBUTIONS,
pageSize: query.pageSize ?? 1,
continueAfter: query.continueAfter?.replaceAll("+", " "), // TODO: make this global (not sure, if only commits can have spaces in page hashes)
},
{
scopeName: GITHUB_REPOSITORY_SCOPES.COUNT,
pageSize: query.rootPageSize ?? 1,
continueAfter: query.rootContinueAfter,
},
] as PageSize<GITHUB_REPOSITORY_SCOPES>[],
),
login_type,
),
fetchParams.auth,
set,
fetchParams.auth_type,
);

return response;
},
{
detail: {
description: `Request repository contributions in the ${login_type} project.`,
tags: ["github"],
}
}
)
)

/**
Expand Down
139 changes: 106 additions & 33 deletions src/v1/adapters/github/scopes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,18 +440,20 @@ export class Repository extends FetcherExtended {
#doFetchMilestones = false;
#doFetchIssues = false;
#doFetchCollaborators = false;
#doFetchContributions = false;

static defaultPageSize = 10;
#rootPageSize: number;
#vulnerabilitiesPageSize: number;
#topicsPageSize: number;
#labelsPageSize: number;
#releasesPageSize: number;
#deploymentsPageSize: number;
#languagesPageSize: number;
#milestonesPageSize: number;
#issuesPageSize: number;
#collaboratorsPageSize: number;
#rootPageSize: number = Repository.defaultPageSize;
#vulnerabilitiesPageSize: number = Repository.defaultPageSize;
#topicsPageSize: number = Repository.defaultPageSize;
#labelsPageSize: number = Repository.defaultPageSize;
#releasesPageSize: number = Repository.defaultPageSize;
#deploymentsPageSize: number = Repository.defaultPageSize;
#languagesPageSize: number = Repository.defaultPageSize;
#milestonesPageSize: number = Repository.defaultPageSize;
#issuesPageSize: number = Repository.defaultPageSize;
#collaboratorsPageSize: number = Repository.defaultPageSize;
#contributionsPageSize: number = Repository.defaultPageSize;

#rootContinueAfter: string | undefined | null = null;
#vulnerabilitiesContinueAfter: string | undefined | null = null;
Expand All @@ -463,6 +465,7 @@ export class Repository extends FetcherExtended {
#milestonesContinueAfter: string | undefined | null = null;
#issuesContinueAfter: string | undefined | null = null;
#collaboratorsContinueAfter: string | undefined | null = null;
#contributionsContinueAfter: string | undefined | null = null;

#count_nodes = false;
#log = false;
Expand All @@ -474,18 +477,7 @@ export class Repository extends FetcherExtended {
) {
super(("name" in args) ? args.name : null);

this.#log = DEV_MODE;

this.#rootPageSize = Repository.defaultPageSize;
this.#vulnerabilitiesPageSize = Repository.defaultPageSize;
this.#topicsPageSize = Repository.defaultPageSize;
this.#labelsPageSize = Repository.defaultPageSize;
this.#releasesPageSize = Repository.defaultPageSize;
this.#deploymentsPageSize = Repository.defaultPageSize;
this.#languagesPageSize = Repository.defaultPageSize;
this.#milestonesPageSize = Repository.defaultPageSize;
this.#issuesPageSize = Repository.defaultPageSize;
this.#collaboratorsPageSize = Repository.defaultPageSize;
this.#log = DEV_MODE

this.#parseScopes(args.scopes);
}
Expand Down Expand Up @@ -567,6 +559,10 @@ export class Repository extends FetcherExtended {
this.#collaboratorsPageSize = ps.pageSize ?? this.#collaboratorsPageSize;
this.#collaboratorsContinueAfter = this.#validateCursor(ps.continueAfter);
break;
case GITHUB_REPOSITORY_SCOPES.CONTRIBUTIONS:
this.#doFetchContributions = true;
this.#contributionsPageSize = ps.pageSize ?? this.#contributionsPageSize;
this.#contributionsContinueAfter = this.#validateCursor(ps.continueAfter);
default:
break;
}
Expand Down Expand Up @@ -625,7 +621,8 @@ export class Repository extends FetcherExtended {
${this.#releasesBody()}
${this.#deploymentsBody()}
${this.#languagesBody()}
${this.#collaboratorsBody()}
${this.#collaboratorsBody()}
${this.#contributionsBody()}
}
`;
}
Expand All @@ -651,7 +648,8 @@ export class Repository extends FetcherExtended {
${this.#releasesBody()}
${this.#deploymentsBody()}
${this.#languagesBody()}
${this.#collaboratorsBody()}
${this.#collaboratorsBody()}
${this.#contributionsBody()}
}
}`;
}
Expand All @@ -678,8 +676,10 @@ export class Repository extends FetcherExtended {
${this.#milestonesBody(
issues_states ?? [GITHUB_MILESTONE_ISSUE_STATES.OPEN],
milestones_amount,
milestone_number,
)}
milestone_number)}
${this.#collaboratorsBody()}
${this.#contributionsBody()}
}
`;
}
Expand Down Expand Up @@ -709,8 +709,10 @@ export class Repository extends FetcherExtended {
${this.#milestonesBody(
issues_states ?? [GITHUB_MILESTONE_ISSUE_STATES.OPEN],
milestones_amount,
milestone_number,
)}
milestone_number)}
${this.#collaboratorsBody()}
${this.#contributionsBody()}
}
}`;
}
Expand Down Expand Up @@ -886,8 +888,7 @@ export class Repository extends FetcherExtended {
if (this.#log) console.info("fetching releases");

return `
releases(first: ${this.#releasesPageSize}, after: ${this.#releasesContinueAfter
}) {
releases(first: ${this.#releasesPageSize}, after: ${this.#releasesContinueAfter}) {
${this.#count_nodes ? "totalCount" : ""}
pageInfo {
Expand Down Expand Up @@ -951,8 +952,7 @@ export class Repository extends FetcherExtended {
if (this.#log) console.info("fetching deployments");

return `
deployments(first: ${this.#deploymentsPageSize}, after: ${this.#deploymentsContinueAfter
}) {
deployments(first: ${this.#deploymentsPageSize}, after: ${this.#deploymentsContinueAfter}) {
${this.#count_nodes ? "totalCount" : ""}
pageInfo {
Expand All @@ -963,7 +963,6 @@ export class Repository extends FetcherExtended {
nodes {
updatedAt
createdAt
updatedAt
description
environment
task
Expand All @@ -976,6 +975,7 @@ export class Repository extends FetcherExtended {
state
deployment {
createdAt
updatedAt
description
commit {
additions
Expand Down Expand Up @@ -1007,6 +1007,7 @@ export class Repository extends FetcherExtended {
state
deployment {
createdAt
updatedAt
description
commit {
additions
Expand Down Expand Up @@ -1217,4 +1218,76 @@ export class Repository extends FetcherExtended {

return "";
}

#contributionsBody() {
if (this.#doFetchContributions) {
if (this.#log) console.info("fetching contributions");

return `
defaultBranchRef {
target {
... on Commit {
history(first: ${this.#contributionsPageSize}, after: ${this.#contributionsContinueAfter}) {
${this.#count_nodes ? "totalCount" : ""}
pageInfo {
endCursor
hasNextPage
}
edges {
node {
... on Commit {
abbreviatedOid
oid
id
messageHeadlineHTML
messageBodyHTML
additions
deletions
changedFilesIfAvailable
commitUrl
treeUrl
committedDate
deployments(first: 1, orderBy: {direction: DESC, field: CREATED_AT}, after: null) {
totalCount
pageInfo {
endCursor
hasNextPage
}
nodes {
createdAt
updatedAt
description
latestStatus {
createdAt
updatedAt
state
description
environmentUrl
logUrl
}
}
}
}
}
}
}
}
}
}
`;
}

return "";
}
}
3 changes: 2 additions & 1 deletion src/v1/adapters/github/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export interface RestResponse<T> {
type?: string | undefined;
}

export interface GraphqlResponse<T> extends RestResponse<T> {}
export interface GraphqlResponse<T> extends RestResponse<T> { }

// in type GraphQlQueryResponse<ResponseData> of @octokit/graphql (has string as type...)
export enum GraphqlResponseErrorCode {
Expand Down Expand Up @@ -89,6 +89,7 @@ export enum GITHUB_REPOSITORY_SCOPES {
MILESTONES = "milestones",
ISSUES = "issues",
COLLABORATORS = "collaborators",
CONTRIBUTIONS = "contributions",
}

export enum GITHUB_MILESTONE_ISSUE_STATES {
Expand Down
6 changes: 3 additions & 3 deletions src/v1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export const v1 = new Elysia({ prefix: `/${V1_PATH}` })
.use(
swagger({
/* Stable: 1.17.16 */
/* Modern UI: 1.25.25 */
scalarVersion: "1.25.58",
/* Modern UI: 1.25.25, 1.25.58 */
scalarVersion: "1.25.68", // https://github.com/scalar/scalar/issues/3956
path: SWAGGER_PATH,
exclude: [
...ROOT_PATHS,
Expand All @@ -49,7 +49,7 @@ export const v1 = new Elysia({ prefix: `/${V1_PATH}` })
title: "Propromo RestAPI Documentation",
description:
"A RestAPI for the scopes of the Github GraphqlAPI, that Propromo needs (latest).",
version: "1.1.0",
version: "1.2.0",
},
tags: [
{
Expand Down

0 comments on commit e9a6871

Please sign in to comment.