Skip to content
Open
6 changes: 5 additions & 1 deletion src/app/main.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { Context } from "../lib/github/types";
import { addRepoCommentsToGH } from "../lib";
import { addRepoCommentsToGH, addCommentsToGHonInstall } from "../lib";

module.exports = app => {
app.log("Cheers, the app runs on a server!");

app.on("push", async (context: Context) => {
addRepoCommentsToGH(context);
});

app.on("installation", async (context: Context) => {
addCommentsToGHonInstall(context);
});
};
15 changes: 4 additions & 11 deletions src/lib/github/changedFilesList.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
/**
* Gets files that have changed during a commit
*/
import { Context } from "./types";
import { Context, ModifiedFile } from "./types";
import { getBasicRepoProps } from "./utils";

export interface ModifiedFile {
name: string;
downloadUrl: string;
htmlUrl: string;
author: string;
}

export default async function getChangedFiles(context: Context): Promise<ModifiedFile[]> {
const { owner, repo } = getBasicRepoProps (context);
const sha = context.payload.head_commit.id;
Expand All @@ -25,11 +18,11 @@ export default async function getChangedFiles(context: Context): Promise<Modifie
const modifiedFiles: Array<Promise<ModifiedFile>> =
result.data.files.map(async(file) => {
const name = file.filename;
const content = await octokit.repos.getContent({owner, repo, path: name});
const content = await octokit.repos.getContents({owner, repo, path: name});
return {
name,
htmlUrl: content.data.html_url,
downloadUrl: content.data.download_url,
htmlUrl: content.html_url,
downloadUrl: content.download_url,
author
};
});
Expand Down
2 changes: 1 addition & 1 deletion src/lib/github/getFilesContent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fetch from "node-fetch";
import { ModifiedFile } from "./changedFilesList";
import { ModifiedFile } from "./types";

export interface FilesContent {
content: string;
Expand Down
26 changes: 26 additions & 0 deletions src/lib/github/getFilesOnInstall.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Gets files that have changed in the last commits
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe comment should change. I think it should be something like Get all repo files.

*/
import { Context, ModifiedFile, File } from "./types";
import { getBasicRepoProps, getfileName } from "./utils";

export default async function getFiles(context: Context): Promise<ModifiedFile[]> {
const { owner, repo } = getBasicRepoProps (context);
const sha = context.payload.head_commit.id;
const octokit = context.github;
const treeObject = await octokit.gitdata.getTree({owner, repo, sha, recursive: 5});
const filePromises: Array<Promise<File>> = treeObject.tree.map( async (obj) => {
return getfileName(octokit, owner, repo, obj.sha);
});
const arrFile = await Promise.all(filePromises);
const modifiedFiles: Array<Promise<ModifiedFile>> = arrFile.map( async (objFile: File) => {
const content = await octokit.repos.getContents({owner, repo, path: objFile.name});
return {
name: objFile.name,
htmlUrl: content.html_url,
downloadUrl: content.download_url,
author: owner
};
});
return Promise.all(modifiedFiles);
}
12 changes: 12 additions & 0 deletions src/lib/github/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,15 @@ export interface GHIssue {
number: number;
title: string;
}

export interface ModifiedFile {
name: string;
downloadUrl: string;
htmlUrl: string;
author: string;
}

export interface File {
name: string;
downloadUrl: string;
}
29 changes: 27 additions & 2 deletions src/lib/github/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
*/
import { groupBy, flatten, prop } from "ramda";

import { Context, Issue, RepoProps, GHIssue } from "./types";
import { Context, Issue, RepoProps, GHIssue, ModifiedFile, File } from "./types";
import { RepoIssue } from "../parser";


export function getBasicRepoProps (context: Context): RepoProps {
const owner = context.payload.repository.owner.login;
const repo = context.payload.repository.name;
Expand Down Expand Up @@ -45,3 +44,29 @@ export function mergeFileRepoIssues(repoIssues: RepoIssue[][]): RepoIssue[] {
Object.keys(groupedByIssueType).map(key => groupedByIssueType[key]);
return flatten<RepoIssue>(issueGroupsList);
}

export function getModifiedFiles(octokit, array, owner, repo): Promise<ModifiedFile[]> {
const modifiedFiles: Array<Promise<ModifiedFile>> = array.data.files.map(async(file) => {
const name = file.filename;
const content = await octokit.repos.getContent({owner, repo, path: name});
return { name, htmlUrl: content.data.html_url, downloadUrl: content.data.download_url};
});
return Promise.all(modifiedFiles);
}

export async function getfileName(
octokit,
owner: string,
repo: string,
sha: string
): Promise<File> {
const objFiles = await octokit.gitdata.getTree({owner, repo, sha});
if (!objFiles) return;
return objFiles.tree.map( async (obj) => {
if (obj.type !== "tree") {
return {name: obj.path, downloadUrl: obj.url};
} else {
return await getfileName(octokit, owner, repo, obj.sha);
}
});
}
14 changes: 14 additions & 0 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import getChangedFiles from "../lib/github/changedFilesList";
import getFilesContent, { FilesContent } from "./github/getFilesContent";
import getNewIssuesFromSource from "./github/getNewIssue";
import addIssuesToRepo from "./github/addIssuesToRepo";
import getFiles from "../lib/github/getFilesOnInstall";
import parseData from "./parser";

import { mergeFileRepoIssues } from "./github/utils";
Expand All @@ -26,3 +27,16 @@ export async function addRepoCommentsToGH(context: Context): Promise<void> {
return addIssuesToRepo(context, issuesToAddToRepo);
}

export async function addCommentsToGHonInstall(context: Context): Promise<void> {
// get changed files on install
const changedFiles = await getFiles(context);
// get file data
const filesContent: FilesContent[] = await getFilesContent(changedFiles); // error handling

const filesRepoIssues = filesContent.map(parseData);
// merge into a single set of FIXME and TODO issues
const repoIssues = mergeFileRepoIssues(filesRepoIssues);
const issuesToAddToRepo = await getNewIssuesFromSource(context, repoIssues);
return addIssuesToRepo(context, issuesToAddToRepo);
}