diff --git a/src/app/main.ts b/src/app/main.ts index b8d05fc..1bb3bb2 100644 --- a/src/app/main.ts +++ b/src/app/main.ts @@ -1,5 +1,5 @@ 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!"); @@ -7,4 +7,8 @@ module.exports = app => { app.on("push", async (context: Context) => { addRepoCommentsToGH(context); }); + + app.on("installation", async (context: Context) => { + addCommentsToGHonInstall(context); + }); }; diff --git a/src/lib/github/changedFilesList.ts b/src/lib/github/changedFilesList.ts index a082ed1..b80db8f 100644 --- a/src/lib/github/changedFilesList.ts +++ b/src/lib/github/changedFilesList.ts @@ -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 { const { owner, repo } = getBasicRepoProps (context); const sha = context.payload.head_commit.id; @@ -25,11 +18,11 @@ export default async function getChangedFiles(context: Context): Promise> = 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 }; }); diff --git a/src/lib/github/getFilesContent.ts b/src/lib/github/getFilesContent.ts index dc7a986..e296ee8 100644 --- a/src/lib/github/getFilesContent.ts +++ b/src/lib/github/getFilesContent.ts @@ -1,5 +1,5 @@ import fetch from "node-fetch"; -import { ModifiedFile } from "./changedFilesList"; +import { ModifiedFile } from "./types"; export interface FilesContent { content: string; diff --git a/src/lib/github/getFilesOnInstall.ts b/src/lib/github/getFilesOnInstall.ts new file mode 100644 index 0000000..d6ac7bb --- /dev/null +++ b/src/lib/github/getFilesOnInstall.ts @@ -0,0 +1,26 @@ +/** + * Gets files that have changed in the last commits + */ +import { Context, ModifiedFile, File } from "./types"; +import { getBasicRepoProps, getfileName } from "./utils"; + +export default async function getFiles(context: Context): Promise { + 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> = treeObject.tree.map( async (obj) => { + return getfileName(octokit, owner, repo, obj.sha); + }); + const arrFile = await Promise.all(filePromises); + const modifiedFiles: Array> = 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); +} diff --git a/src/lib/github/types.ts b/src/lib/github/types.ts index 8173470..8f6cb9e 100644 --- a/src/lib/github/types.ts +++ b/src/lib/github/types.ts @@ -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; +} diff --git a/src/lib/github/utils.ts b/src/lib/github/utils.ts index 41d44e9..5ee4b72 100644 --- a/src/lib/github/utils.ts +++ b/src/lib/github/utils.ts @@ -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; @@ -45,3 +44,29 @@ export function mergeFileRepoIssues(repoIssues: RepoIssue[][]): RepoIssue[] { Object.keys(groupedByIssueType).map(key => groupedByIssueType[key]); return flatten(issueGroupsList); } + +export function getModifiedFiles(octokit, array, owner, repo): Promise { + const modifiedFiles: Array> = 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 { + 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); + } + }); +} diff --git a/src/lib/index.ts b/src/lib/index.ts index b729e59..8a4a623 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -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"; @@ -26,3 +27,16 @@ export async function addRepoCommentsToGH(context: Context): Promise { return addIssuesToRepo(context, issuesToAddToRepo); } +export async function addCommentsToGHonInstall(context: Context): Promise { + // 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); +} +