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

Added metrics mock-up #23

Open
wants to merge 1 commit into
base: master
from
Open
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -31,7 +31,7 @@ New contributors are always welcome!

Need to contact other collaborators in this project? Feel free to [join our slack group!](https://join.slack.com/t/githubdashboard/shared_invite/enQtNDcxMTM5OTMyNjExLWFmYTE1NTFiMzkyMzU0ZmRjMjI0YjI1OTVkMDk0MTUyZmJlMjM2NGUzODQ1YjZmZDVkMzkxYzgzYjM1MjI1ZGI)

## Liscense
## License

This comment has been minimized.

Copy link
@humphd

humphd Dec 14, 2018

Unrelated changes should go in their own PRs. Do one thing per PR.


This project is licensed under the MIT License - see our [LICENSE.md](https://github.com/deepanjali19/GitHub-Dashboard/blob/master/LICENSE) file for details

@@ -0,0 +1,81 @@
const octokit = require('@octokit/rest')()
const fs = require('fs');

var gitToken = "thisToken"

This comment has been minimized.

Copy link
@humphd

humphd Dec 14, 2018

This needs to come from an environment variable or the like, you can't leave it like this. Also, you're not using it below, which you should.


octokit.repos.getForOrg({
org: 'octokit',
type: 'public'
})

var targetProjectRoot = "C:/Users/SHAWN/data/"

This comment has been minimized.

Copy link
@humphd

humphd Dec 14, 2018

This can't be hard coded, once again I'd suggest an environment variable. That way each dev, or the server, can use whatever they want. process.env gives you access to these.

var github = { "owner": "deepanjali19", "repo": "GitHub-Dashboard", "branch": "master" }

downloadGitHubRepo(github, targetProjectRoot)

async function downloadGitHubRepo(github, targetDirectory) {

This comment has been minimized.

Copy link
@humphd

humphd Dec 14, 2018

An async function needs to return a Promise. You should add a return on line 22 to achieve this.

console.log(`Installing GitHub Repo ${github.owner}\\${github.repo}`)
var repo = github.repo;
var path = ''
var owner = github.owner
var ref = github.commit ? github.commit : (github.tag ? github.tag : (github.branch ? github.branch : 'master'))
processGithubDirectory(owner, repo, ref, path, path, targetDirectory)
}

const binaryExtensions = ['png', 'jpg', 'tiff', 'wav', 'mp3', 'doc', 'pdf']
var maxSize = 1000000;
function processGithubDirectory(owner, repo, ref, path, sourceRoot, targetRoot) {

This comment has been minimized.

Copy link
@humphd

humphd Dec 14, 2018

Above you are using async/await style, and here using Promise.then() chaining. I'd suggest picking one or the other. async/await is probably best, so this code could be changed to:

try {
  const result = await octokit.repos.getContent({ "owner": owner, "repo": repo, "path": path, "ref": ref });
  // use result here...
catch (err) {
 // handle errors
}
octokit.repos.getContent({ "owner": owner, "repo": repo, "path": path, "ref": ref })
.then(result => {
var targetDir = targetRoot + path
// check if exists
checkDirectorySync(targetDir)
result.data.forEach(item => {
if (item.type == "dir") {
processGithubDirectory(owner, repo, ref, item.path, sourceRoot, targetRoot)
}
// if directory
if (item.type == "file") {

if (item.size > maxSize) {
var sha = item.sha
octokit.gitdata.getBlob({ "owner": owner, "repo": repo, "sha": item.sha }
).then(result => {
var target = `${targetRoot + item.path}`
fs.writeFile(target
, Buffer.from(result.data.content, 'base64').toString('utf8'), function (err, data) { })

This comment has been minimized.

Copy link
@humphd

humphd Dec 14, 2018

Couple things here. fs.writeFile doesn't return data on the callback, just an err. Also, you can get rid of the Buffer.from(...), and just write the Buffer directly (doesn't need to be a string, you can write the raw bytes). Finally, you need to check on err in your callback, in case the write fails.

})
.catch((error) => { console.log("ERROR BIGGA" + error) })
return;
}
octokit.repos.getContent({ "owner": owner, "repo": repo, "path": item.path, "ref": ref })
.then(result => {
var target = `${targetRoot + item.path}`
if (binaryExtensions.includes(item.path.slice(-3))) {

This comment has been minimized.

Copy link
@humphd

humphd Dec 14, 2018

Prefer to use the path module and extname().

fs.writeFile(target
, Buffer.from(result.data.content, 'base64'), function (err, data) { reportFile(item, target) })

This comment has been minimized.

Copy link
@humphd

humphd Dec 14, 2018

Deal with err case in your callback.

}

else

This comment has been minimized.

Copy link
@humphd

humphd Dec 14, 2018

Odd indenting here.

fs.writeFile(target

This comment has been minimized.

Copy link
@humphd

humphd Dec 14, 2018

fs.writeFile(target, Buffer.from(result.data.content, 'base64'), function (err) {
  if (err) {
    return console.error(`Error writing content to disk: ${err.message}`);
  }
 reportFile(item, target);
});
, Buffer.from(result.data.content, 'base64').toString('utf8'), function (err, data) { if (!err) reportFile(item, target); else console.log('Fuotje ' + err) })

})
.catch((error) => { console.log("ERROR " + error) })
}
})
}).catch((error) => { console.log("ERROR " + error) })
}

function reportFile(item, target) {
console.log(`- installed ${item.name} (${item.size} bytes )in ${target}`)
}

function checkDirectorySync(directory) {
try {
fs.statSync(directory);

This comment has been minimized.

Copy link
@humphd

humphd Dec 14, 2018

I would advise against mixing sync with the rest of your async fs stuff above.

} catch (e) {
fs.mkdirSync(directory);
console.log("Created directory: " + directory)
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.