-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into DOP-4020-docsets
- Loading branch information
Showing
23 changed files
with
381 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Octokit } from '@octokit/rest'; | ||
|
||
let client: Octokit; | ||
|
||
export function getOctokitClient(): Octokit { | ||
if (client) return client; | ||
|
||
try { | ||
const { GITHUB_BOT_PASSWORD } = process.env; | ||
|
||
if (!GITHUB_BOT_PASSWORD) throw new Error('GITHUB_BOT_PASSWORD is not defined'); | ||
|
||
client = new Octokit({ auth: GITHUB_BOT_PASSWORD }); | ||
return client; | ||
} catch (error) { | ||
console.error('ERROR! Failed to create Octokit client. Is GITHUB_BOT_PASSWORD defined?', error); | ||
throw error; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { getSnootyDirSet } from './utils/path-utils'; | ||
import { GitCommitInfo } from './types/github-types'; | ||
import { getProjectDirFromPath } from './services/get-paths'; | ||
|
||
interface FileUpdatePayload { | ||
ownerName: string; | ||
repoName: string; | ||
commitSha: string; | ||
updatedFilePaths: string[]; | ||
} | ||
|
||
/** | ||
* Retrieves the path of project directories. This is determined | ||
* by finding the nearest parent directory that has a snooty.toml file | ||
* for a given updated file path from a commit. | ||
* @param repoName Name of the repository to check. | ||
* @param ownerName Name of the owner of the repository. | ||
* @param commitSha The Git commit SHA that contains the changed files. | ||
* @param updatedFilePaths An array of all of the changed files (added, removed, modified) | ||
* from the commit. The method `getUpdatedFilePaths` in the `src/monorepo/utils/path-utils.ts | ||
* can be used to parse these paths from a GitHub `Commit` object. | ||
* @returns An array of all the project paths that need to be built. | ||
*/ | ||
export async function getMonorepoPaths({ | ||
repoName, | ||
ownerName, | ||
commitSha, | ||
updatedFilePaths, | ||
}: FileUpdatePayload): Promise<string[]> { | ||
const commitInfo: GitCommitInfo = { | ||
ownerName, | ||
repoName, | ||
commitSha, | ||
}; | ||
|
||
const snootyDirSet = await getSnootyDirSet(commitInfo); | ||
|
||
const projects = updatedFilePaths.map((path) => getProjectDirFromPath(path, snootyDirSet)); | ||
|
||
// remove empty strings and remove duplicated values | ||
return Array.from(new Set(projects.filter((dir) => !!dir))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { SNOOTY_TOML_FILENAME } from '../utils/monorepo-constants'; | ||
|
||
/** | ||
* This function returns the project path for a given file change from a docs repository | ||
* within the monorepo. This function supports nested projects. | ||
* @param path An added/modified/removed file path from a commit e.g. server-docs/source/index.rst | ||
* @param commitInfo Contains information | ||
* @returns The closest file path that contains a snooty.toml, relative to the path parameter. | ||
*/ | ||
export function getProjectDirFromPath(path: string, snootyDirSet: Set<string>): string { | ||
const pathArray = path.split('/'); | ||
if (pathArray.length === 0) { | ||
console.warn('WARNING! Empty path found: ', path); | ||
return ''; | ||
} | ||
|
||
/** | ||
* If the changed file is the snooty.toml file, we know that we | ||
* are in the project's root directory. We can join the original | ||
* pathArray to get the project path since the snooty.toml has been removed. | ||
*/ | ||
const changedFile = pathArray.pop(); | ||
|
||
if (changedFile === SNOOTY_TOML_FILENAME) return pathArray.join('/'); | ||
|
||
while (pathArray.length > 0) { | ||
const currDir = pathArray.join('/'); | ||
|
||
if (snootyDirSet.has(currDir)) return currDir; | ||
|
||
pathArray.pop(); | ||
} | ||
|
||
console.warn(`WARNING! No snooty.toml found for the given path: ${path}`); | ||
return ''; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
interface DirectoryConfig { | ||
snooty_toml?: string; | ||
source?: string; | ||
} | ||
|
||
interface RepoConfig { | ||
repoName: string; | ||
deployable: boolean; | ||
branches: BranchConfig[]; | ||
} | ||
|
||
interface BranchConfig { | ||
gitBranchName: string; | ||
} | ||
|
||
// TODO: Populate these more. For DOP-3911, they are | ||
// being added for testing purposes. | ||
export interface DocSetEntry { | ||
project: string; | ||
prefix: string; | ||
bucket: string; | ||
url: string; | ||
directories?: DirectoryConfig; | ||
repos?: RepoConfig[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface GitCommitInfo { | ||
commitSha: string; | ||
ownerName: string; | ||
repoName: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const SNOOTY_TOML_FILENAME = 'snooty.toml'; | ||
export const MONOREPO_NAME = 'docs-monorepo'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Commit } from '@octokit/webhooks-types'; | ||
import { getOctokitClient } from '../../clients/githubClient'; | ||
import { GitCommitInfo } from '../types/github-types'; | ||
import { SNOOTY_TOML_FILENAME } from './monorepo-constants'; | ||
|
||
/** | ||
* Creates a `Set` of all `snooty.toml` paths within the monorepo. | ||
* The function retrieves the monorepo's tree structure from GitHub. | ||
*/ | ||
export async function getSnootyDirSet({ commitSha, ownerName, repoName }: GitCommitInfo): Promise<Set<string>> { | ||
try { | ||
const client = getOctokitClient(); | ||
|
||
// getting the repository tree for a given commit SHA. This returns an object | ||
// with the property `tree` that is a flat array of all files in the repository. | ||
// The tree array contains objects that hold the file path. | ||
// Unlike the contents API for repositories, the actual file content is not returned. | ||
const { data } = await client.request('GET /repos/{owner}/{repo}/git/trees/{tree_sha}', { | ||
owner: ownerName, | ||
repo: repoName, | ||
tree_sha: commitSha, | ||
recursive: 'true', | ||
}); | ||
|
||
const snootyTomlDirs = data.tree | ||
.filter((treeNode) => !!treeNode.path?.includes(SNOOTY_TOML_FILENAME)) | ||
.map((treeNode) => { | ||
// casting the `treeNode.path` from `(string | undefined)` to `string` since the filter will ensure that the result | ||
// only includes treeNode.path values that are defined and include snooty.toml | ||
// in the path i.e. we will not have `undefined` as a value in the resulting array. | ||
const path = treeNode.path as string; | ||
|
||
// the - 1 is to remove the trailing slash | ||
return path.slice(0, path.length - SNOOTY_TOML_FILENAME.length - 1); | ||
}); | ||
|
||
const snootyDirSet = new Set(snootyTomlDirs); | ||
|
||
return snootyDirSet; | ||
} catch (error) { | ||
console.error( | ||
`ERROR! Unable to retrieve tree for SHA: ${commitSha} owner name: ${ownerName} repo name: ${repoName}`, | ||
error | ||
); | ||
throw error; | ||
} | ||
} | ||
|
||
export const getUpdatedFilePaths = (commit: Commit): string[] => | ||
commit.modified.concat(commit.added).concat(commit.removed); |
Oops, something went wrong.