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

Business logic for versioning service #4

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
115 changes: 115 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@

# Created by https://www.toptal.com/developers/gitignore/api/node
# Edit at https://www.toptal.com/developers/gitignore?templates=node

### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
out

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# End of https://www.toptal.com/developers/gitignore/api/node
153 changes: 153 additions & 0 deletions api/githubapi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
const githubApi = require("../config/githubConfig");
const config = require("../config/config");
const utils = require("./utils");

/**
* Propose a change
* @param {Object} user - user proposing the change
* @param {Object} practice - json practice object containing all fields
*
* @description
* <ul>
* <li> Encode JSON Data </li>
* <li> Retrieve Last Commit from Master </li>
* <li> Create a new branch using last commit as head </li>
* <li> Check to see if content already exists in repo (i.e. is this an update to an existing practice or creation of a new practice) </li>
* <li> Create or update file contents on the branch </li>
* <li> Make PR from branch to master </li>
* <li> Label PR with the practice slug </li>
* <li> Associate User with PR in Content-Api-Service </li>
* </ul>
*
*/

exports.proposeChange = async function proposeChange(user, practice) {
const encodedPractice = Buffer.from(
JSON.stringify(practice, null, 2)
).toString("base64");

const ref = utils.createRefString(user, practice);
const lastCommitSha = await utils.getLatestCommitOnMaster();
const newBranchResponse = await utils.createNewBranch(ref, lastCommitSha);

const existingPracticeSha = await utils.checkIfExistingPractice(
practice.slug
);

const fileUploadResponse = await githubApi.repos.createOrUpdateFileContents({
owner: config.org,
repo: config.repo,
path: practice.slug + ".json",
...(existingPracticeSha !== null && { sha: existingPracticeSha }),
branch: ref,
message: "Proposed Change to " + practice.title + " by " + user.username,
content: encodedPractice,
committer: {
name: user.username,
email: user.email,
},
author: {
name: user.username,
email: user.email,
},
});

const {
data: { number },
} = await githubApi.pulls.create({
owner: config.org,
repo: config.repo,
title: "PR to " + practice.title + " by " + user.username,
head: ref,
base: config.base,
});

await utils.addSlugToPullRequest(practice.slug, number);

return number;
};

/**
* Get the proposed change and content
* @param {int} pullRequestNumber - pull request associated with change
*
* @description
* <ul>
* <li> Get Last Commit on Pull Request </li>
* <li> Get Practice on Pull Request from Label </li>
* <li> Retrieve JSON using Raw Github Link </li>
* </ul>
*
*/

exports.getProposedChange = async function getProposedChange(
pullRequestNumber
) {
const { data } = await utils.getPullRequest(pullRequestNumber);
const commitSha = data.head.sha;
const practice_slug = utils.extractPracticeFromLabelsOnPullRequest(
data.labels
);
return utils.getPracticeFromCommit(practice_slug, commitSha);
};

/**
*
* Admin approves a PR
*
* @param {int} pullRequestNumber - pull request associated with change
* @description
* <ul>
* <li> Verify that the PR is mergeable </li>
* <li> Merge the PR </li>
* <li> Delete the branch associated with the PR </li>
* </ul>
*
* Things to Consider
* 1. Do we need to sync latest changes with Strapi?
*/
exports.approveChange = async function approveChange(pullRequestNumber) {
const { data } = await utils.getPullRequest(pullRequestNumber);
if (data.mergeable) {
await utils.mergePullRequest(pullRequestNumber);
await utils.deleteBranch(data.head.ref);
return true;
}

return false;
};

/**
*
* Admin rejects a PR
*
* @param {int} pullRequestNumber - pull request associated with change
* @description
* <ul>
* <li> Update the PR to be closed </li>
* <li> Delete the branch associated with the PR </li>
* </ul>
*
*/
exports.rejectChange = async function rejectChange(pullRequestNumber) {
const { data } = await utils.getPullRequest(pullRequestNumber);
await utils.closePullRequest(pullRequestNumber);
await utils.deleteBranch(data.head.ref);
return true;
};


/**
*
* Admin gets all proposed changes
*
* @description
* <ul>
* <li> get all PRs with an Open state </li>
* </ul>
*
*/
exports.getAllProposedChanges = async function getAllProposedChanges() {
const {data} = await utils.getAllPullRequests();
return data;
};
53 changes: 53 additions & 0 deletions api/resolvers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const githubapi = require("./githubapi");
const utils = require("./utils");

exports.proposedChanges = async function proposedChanges() {
const proposedChanges = await githubapi.getAllProposedChanges();

const returnVal = proposedChanges.map((proposedChange) => {
const slug = exports.getPracticeSlug(proposedChange);
return {
number: proposedChange.number,
practiceSlug: slug,
};
});

return returnVal;

};




exports.getPracticeSlug = function getPracticeSlug(proposedChange) {
try {
return utils.extractPracticeFromLabelsOnPullRequest(proposedChange.labels);
} catch (err) {
//console.log(err);
}

return "unknown";
};

exports.getMergeableStatus = async function getMergeableStatus(proposedChange) {
return await utils.determineMergeableStatus(proposedChange.number);
};

exports.getProposedChange = async function getProposedChange(
pullRequestNumber
) {
return githubapi.getProposedChange(pullRequestNumber);
};

exports.getDiffFromPullRequest = async function getDiffFromPullRequest(
pullRequestNumber
) {
const diffValue = await utils.getDiffFromPullRequest(pullRequestNumber);
return { diff: diffValue };
};

exports.proposeChange = async function proposeChange(user, practice) {
console.log(practice);
const data = await githubapi.proposeChange(user, practice);
return { response: "done" };
};
Loading