Parcourir la source

Adds commit count for GitHub

Splits get commit branches into 2 methods
main
Eric Amodio il y a 2 ans
Parent
révision
efc567b860
3 fichiers modifiés avec 154 ajouts et 73 suppressions
  1. +1
    -1
      .vscode/queries.github-graphql-nb
  2. +111
    -62
      src/premium/github/github.ts
  3. +42
    -10
      src/premium/github/githubGitProvider.ts

+ 1
- 1
.vscode/queries.github-graphql-nb
Fichier diff supprimé car celui-ci est trop grand
Voir le fichier


+ 111
- 62
src/premium/github/github.ts Voir le fichier

@ -699,105 +699,155 @@ export class GitHubApi {
}
@debug<GitHubApi['getCommitBranches']>({ args: { 0: '<token>' } })
async getCommitBranches(
token: string,
owner: string,
repo: string,
ref: string,
date: Date,
branch?: string,
): Promise<string[]> {
async getCommitBranches(token: string, owner: string, repo: string, ref: string, date: Date): Promise<string[]> {
const cc = Logger.getCorrelationContext();
if (branch) {
interface QueryResult {
repository: {
ref: {
interface QueryResult {
repository: {
refs: {
nodes: {
name: string;
target: {
history: {
nodes: { oid: string }[];
};
};
};
}[];
};
}
try {
const query = `query getCommitBranch(
};
}
try {
const query = `query getCommitBranches(
$owner: String!
$repo: String!
$ref: String!
$since: GitTimestamp!
$until: GitTimestamp!
) {
repository(owner: $owner, name: $repo) {
ref(qualifiedName: $ref) {
target {
... on Commit {
history(first: 3, since: $since until: $until) {
nodes { oid }
refs(first: 20, refPrefix: "refs/heads/", orderBy: { field: TAG_COMMIT_DATE, direction: DESC }) {
nodes {
name
target {
... on Commit {
history(first: 3, since: $since until: $until) {
nodes { oid }
}
}
}
}
}
}
}`;
const rsp = await this.graphql<QueryResult>(token, query, {
owner: owner,
repo: repo,
ref: `refs/heads/${branch}`,
since: date.toISOString(),
until: date.toISOString(),
});
const rsp = await this.graphql<QueryResult>(token, query, {
owner: owner,
repo: repo,
since: date.toISOString(),
until: date.toISOString(),
});
const nodes = rsp?.repository?.ref.target.history.nodes;
if (nodes == null) return [];
const nodes = rsp?.repository?.refs?.nodes;
if (nodes == null) return [];
const branches = [];
const branches = [];
for (const commit of nodes) {
for (const branch of nodes) {
for (const commit of branch.target.history.nodes) {
if (commit.oid === ref) {
branches.push(branch);
branches.push(branch.name);
break;
}
}
}
return branches;
} catch (ex) {
return this.handleRequestError<string[]>(ex, cc, []);
return branches;
} catch (ex) {
return this.handleRequestError<string[]>(ex, cc, []);
}
}
@debug<GitHubApi['getCommitCount']>({ args: { 0: '<token>' } })
async getCommitCount(token: string, owner: string, repo: string, ref: string): Promise<number | undefined> {
const cc = Logger.getCorrelationContext();
interface QueryResult {
repository: {
ref: {
target: {
history: { totalCount: number };
};
};
};
}
try {
const query = `query getCommitCount(
$owner: String!
$repo: String!
$ref: String!
) {
repository(owner: $owner, name: $repo) {
ref(qualifiedName: $ref) {
target {
... on Commit {
history(first: 1) {
totalCount
}
}
}
}
}
}`;
const rsp = await this.graphql<QueryResult>(token, query, {
owner: owner,
repo: repo,
ref: ref,
});
const count = rsp?.repository?.ref?.target.history.totalCount;
return count;
} catch (ex) {
return this.handleRequestError(ex, cc, undefined);
}
}
@debug<GitHubApi['getCommitOnBranch']>({ args: { 0: '<token>' } })
async getCommitOnBranch(
token: string,
owner: string,
repo: string,
branch: string,
ref: string,
date: Date,
): Promise<string[]> {
const cc = Logger.getCorrelationContext();
interface QueryResult {
repository: {
refs: {
nodes: {
name: string;
target: {
history: {
nodes: { oid: string }[];
};
ref: {
target: {
history: {
nodes: { oid: string }[];
};
}[];
};
};
};
}
try {
const query = `query getCommitBranches(
const query = `query getCommitOnBranch(
$owner: String!
$repo: String!
$ref: String!
$since: GitTimestamp!
$until: GitTimestamp!
) {
repository(owner: $owner, name: $repo) {
refs(first: 20, refPrefix: "refs/heads/", orderBy: { field: TAG_COMMIT_DATE, direction: DESC }) {
nodes {
name
target {
... on Commit {
history(first: 3, since: $since until: $until) {
nodes { oid }
}
ref(qualifiedName: $ref) {
target {
... on Commit {
history(first: 3, since: $since until: $until) {
nodes { oid }
}
}
}
@ -807,21 +857,20 @@ export class GitHubApi {
const rsp = await this.graphql<QueryResult>(token, query, {
owner: owner,
repo: repo,
ref: `refs/heads/${branch}`,
since: date.toISOString(),
until: date.toISOString(),
});
const nodes = rsp?.repository?.refs?.nodes;
const nodes = rsp?.repository?.ref.target.history.nodes;
if (nodes == null) return [];
const branches = [];
for (const branch of nodes) {
for (const commit of branch.target.history.nodes) {
if (commit.oid === ref) {
branches.push(branch.name);
break;
}
for (const commit of nodes) {
if (commit.oid === ref) {
branches.push(branch);
break;
}
}

+ 42
- 10
src/premium/github/githubGitProvider.ts Voir le fichier

@ -763,14 +763,27 @@ export class GitHubGitProvider implements GitProvider, Disposable {
try {
const { metadata, github, session } = await this.ensureRepositoryContext(repoPath);
const branches = await github.getCommitBranches(
session?.accessToken,
metadata.repo.owner,
metadata.repo.name,
ref,
options?.commitDate,
options?.branch,
);
let branches;
if (options?.branch) {
branches = await github.getCommitOnBranch(
session?.accessToken,
metadata.repo.owner,
metadata.repo.name,
options?.branch,
ref,
options?.commitDate,
);
} else {
branches = await github.getCommitBranches(
session?.accessToken,
metadata.repo.owner,
metadata.repo.name,
ref,
options?.commitDate,
);
}
return branches;
} catch (ex) {
Logger.error(ex, cc);
@ -780,8 +793,27 @@ export class GitHubGitProvider implements GitProvider, Disposable {
}
@log()
async getCommitCount(_repoPath: string, _ref: string): Promise<number | undefined> {
return undefined;
async getCommitCount(repoPath: string, ref: string): Promise<number | undefined> {
if (repoPath == null) return undefined;
const cc = Logger.getCorrelationContext();
try {
const { metadata, github, session } = await this.ensureRepositoryContext(repoPath);
const count = await github.getCommitCount(
session?.accessToken,
metadata.repo.owner,
metadata.repo.name,
ref,
);
return count;
} catch (ex) {
Logger.error(ex, cc);
debugger;
return undefined;
}
}
@log()

Chargement…
Annuler
Enregistrer