Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: get repository contributors last activities #82

Merged
merged 2 commits into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ console.log(utils.location);

const scanner = await github.downloadAndExtract("NodeSecure.scanner");
console.log(scanner.location);

const contributors = await github.getContributorsLastActivities("NodeSecure", "scanner");
console.log(contributors);
```

## API
Expand Down Expand Up @@ -74,8 +77,24 @@ export interface DownloadResult {
organization: string;
}

export interface GetContributorsLastActivities {
token?: string;
}

export interface GetContributorsLastActivitiesResult {
[key: string]: {
repository: string;
actualRepo: boolean,
lastActivity: string;
}[];
}
export function download(repo: string, options?: DownloadOptions): Promise<DownloadResult>;
export function downloadAndExtract(repo: string, options?: ExtractOptions): Promise<DownloadResult>;
export function getContributorsLastActivities(
owner: string,
repository: string,
options?: GetContributorsLastActivities
): Promise<GetContributorsLastActivitiesResult | null>;
export function setToken(githubToken: string): void;
```

Expand Down
17 changes: 17 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,23 @@ export interface DownloadResult {
organization: string;
}

export interface GetContributorsLastActivities {
token?: string;
}

export interface GetContributorsLastActivitiesResult {
[key: string]: {
repository: string;
actualRepo: boolean,
lastActivity: string;
}[];
}

export function download(repo: string, options?: DownloadOptions): Promise<DownloadResult>;
export function downloadAndExtract(repo: string, options?: ExtractOptions): Promise<DownloadResult>;
export function getContributorsLastActivities(
owner: string,
repository: string,
options?: GetContributorsLastActivities
): Promise<GetContributorsLastActivitiesResult | null>;
export function setToken(githubToken: string): void;
72 changes: 72 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import httpie from "@myunisoft/httpie";

// CONSTANTS
const kGithubURL = new URL("https://github.com/");
const kGithubApi = new URL("https://api.github.com/");
const kDefaultBranch = "main";

// VARS
Expand Down Expand Up @@ -78,6 +79,77 @@ export async function downloadAndExtract(repository, options = Object.create(nul
return result;
}

export async function getContributorsLastActivities(owner, repository, options = Object.create(null)) {
if (typeof owner !== "string") {
throw new TypeError(`owner must be a string, but got ${owner}`);
}

if (typeof repository !== "string") {
throw new TypeError(`repository must be a string, but got ${repository}`);
}

const { token } = options;

const contributorsUrl = new URL(`repos/${owner}/${repository}/contributors`, kGithubApi);

try {
const { data } = await httpie.get(contributorsUrl, {
headers: {
"User-Agent": "NodeSecure",
Authorization: typeof token === "string" ? `token ${token}` : GITHUB_TOKEN
},
maxRedirections: 1
});

const contributors = data.filter((contributor) => !/bot/.test(contributor.login))
.map((contributor) => hydrateLastActivities(contributor.login, owner, repository));

const lastActivities = new Map(await Promise.all(contributors));

return lastActivities;
}
catch (error) {
return null;
}
}

async function hydrateLastActivities(contributor, owner, repository) {
const formatedRepositoryName = `${owner}/${repository}`;

const eventsUrl = new URL(`users/${contributor}/events`, kGithubApi);

const { data } = await httpie.get(eventsUrl, {
headers: {
"User-Agent": "NodeSecure",
Authorization: typeof token === "string" ? `token ${token}` : GITHUB_TOKEN
},
maxRedirections: 1
});

function getLastEvents(events) {
const lastEvent = events[0];
const lastRelatedEvent = events.find((event) => event.repo.name === formatedRepositoryName);

if (lastEvent.repo.name === formatedRepositoryName) {
return [lastEvent];
}

return lastRelatedEvent ?
[lastEvent, lastRelatedEvent] : [lastEvent];
}

return [contributor,
[...getLastEvents(data)]
.map((event) => {
return {
repository: event.repo.name,
actualRepo: event.repo.name === formatedRepositoryName,
lastActivity: event.created_at
};
})
];
}

export function setToken(githubToken) {
GITHUB_TOKEN = githubToken;
}
45 changes: 45 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,48 @@ test("teardown", async() => {
}
}
});

test("get contributors last activites for NodeSecure/scanner", async(tape) => {
const contributors = await github.getContributorsLastActivities("NodeSecure", "scanner");

tape.true(contributors.has("fraxken"));
});

test("getContributorsLastActivities must throw: repository must be a string, but got `repository`", async(tape) => {
tape.plan(2);

const repository = 1;

try {
await github.getContributorsLastActivities("My-fake-owner", repository);
}
catch (error) {
tape.strictEqual(error.name, "TypeError");
tape.strictEqual(error.message, `repository must be a string, but got ${repository}`);
}

tape.end();
});

test("getContributorsLastActivities must throw: owner must be a string, but got `owner`", async(tape) => {
tape.plan(2);

const owner = 1;

try {
await github.getContributorsLastActivities(owner, "my-fake-repository");
}
catch (error) {
tape.strictEqual(error.name, "TypeError");
tape.strictEqual(error.message, `owner must be a string, but got ${owner}`);
}

tape.end();
});

test("getContributorsLastActivities must not throw & return null when he can't find a repository", async(tape) => {
const contributors = await github.getContributorsLastActivities("my-fake-owner", "my-fake-repository");

tape.is(contributors, null);
tape.end();
});