Skip to content

Commit

Permalink
Part-2: Implementing Cron Scheduler to daily update GitHub repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
furknyavuz committed Apr 25, 2019
1 parent ca948ac commit acee592
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 1 deletion.
4 changes: 3 additions & 1 deletion config/env.config.js
@@ -1,5 +1,7 @@
module.exports = {
"port": process.env.PORT || 3000,
"environment": "dev",
"mongoDbUri": process.env.MONGODB_URI || "mongodb://localhost/github-consumer"
"mongoDbUri": process.env.MONGODB_URI || "mongodb://localhost/github-consumer",
"githubEndpoint": "https://api.github.com/graphql",
"githubAccessToken": process.env.GITHUB_ACCESS_TOKEN
};
12 changes: 12 additions & 0 deletions controller/cron.controller.js
@@ -0,0 +1,12 @@
const RepositoryController = require('./repository.controller');
const CronJob = require('cron').CronJob;

function updateDaily() {
RepositoryController.updateRepositories();
}

exports.startCronJobs = function () {
new CronJob('0 0 * * *', function () {
updateDaily()
}, null, true, 'UTC');
};
115 changes: 115 additions & 0 deletions controller/repository.controller.js
@@ -1,4 +1,9 @@
const RepositoryModel = require('../model/repository.model');
const Axios = require('axios');
const Config = require('../config/env.config');

const GITHUB_API_URL = Config.githubEndpoint;
const GITHUB_ACCESS_TOKEN = Config.githubAccessToken;

exports.insert = (req, res) => {
RepositoryModel.create(req.body)
Expand Down Expand Up @@ -37,3 +42,113 @@ exports.deleteById = (req, res) => {
res.status(204).send({});
});
};

// Cron job Update methods

async function updateDatabase(responseData, owner, name) {

let createdAt = '';
let resourcePath = '';
let tagName = '';
let releaseDescription = '';
let homepageUrl = '';
let repositoryDescription = '';
let avatarUrl = '';

if (responseData.repository.releases) {

createdAt = responseData.repository.releases.nodes[0].createdAt;
resourcePath = responseData.repository.releases.nodes[0].resourcePath;
tagName = responseData.repository.releases.nodes[0].tagName;
releaseDescription = responseData.repository.releases.nodes[0].description;
homepageUrl = responseData.repository.homepageUrl;
repositoryDescription = responseData.repository.description;

if (responseData.organization && responseData.organization.avatarUrl) {
avatarUrl = responseData.organization.avatarUrl;
} else if (responseData.user && responseData.user.avatarUrl) {
avatarUrl = responseData.user.avatarUrl;
}

const repositoryData = {
owner: owner,
name: name,
createdAt: createdAt,
resourcePath: resourcePath,
tagName: tagName,
releaseDescription: releaseDescription,
homepageUrl: homepageUrl,
repositoryDescription: repositoryDescription,
avatarUrl: avatarUrl
};

await RepositoryModel.findByOwnerAndName(owner, name)
.then((oldGitHubRelease) => {
if (!oldGitHubRelease[0]) {
RepositoryModel.create(repositoryData);
} else {
RepositoryModel.patchById(oldGitHubRelease[0].id, repositoryData);
}
console.log(`Updated latest release: http://github.com${repositoryData.resourcePath}`);
});
}
}

async function getLatestRelease(repository) {

const owner = repository.owner;
const name = repository.name;

console.log(`Getting latest release for: http://github.com/${owner}/${name}`);

const query = `
query {
organization(login: "${owner}") {
avatarUrl
}
user(login: "${owner}") {
avatarUrl
}
repository(owner: "${owner}", name: "${name}") {
homepageUrl
description
releases(first: 1, orderBy: {field: CREATED_AT, direction: DESC}) {
nodes {
createdAt
resourcePath
tagName
description
}
}
}
}`;

const jsonQuery = JSON.stringify({query});

const headers = {
'User-Agent': 'Release Tracker',
'Authorization': `Bearer ${GITHUB_ACCESS_TOKEN}`
};

await Axios.post(GITHUB_API_URL, jsonQuery, {headers: headers}
).then((response) => {
return updateDatabase(response.data.data, owner, name);
});
}

async function asyncUpdate() {

await RepositoryModel.list().then((array) => {
const promises = array.map(getLatestRelease);

return Promise.all(promises);
});
}

exports.updateRepositories = async function update() {
console.log('GitHub Repositories Update Started');

await asyncUpdate().then(() => {
console.log('GitHub Repositories Update Finished');
});
};
2 changes: 2 additions & 0 deletions index.js
Expand Up @@ -3,6 +3,7 @@ const express = require('express');
const bodyParser = require('body-parser');

const Router = require('./config/routes.config');
const CronController = require('./controller/cron.controller');

const app = express();

Expand All @@ -21,6 +22,7 @@ app.use(function (req, res, next) {

app.use(bodyParser.json());
Router.routesConfig(app);
CronController.startCronJobs();

app.listen(config.port, function () {
console.log('app listening at port %s', config.port);
Expand Down
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -18,7 +18,9 @@
},
"homepage": "https://github.com/furknyavuz/sample-github-api-consumer-nodejs#readme",
"dependencies": {
"axios": "^0.18.0",
"body-parser": "1.7.0",
"cron": "^1.7.0",
"express": "^4.8.7",
"moment": "^2.17.1",
"moment-timezone": "^0.5.13",
Expand Down

0 comments on commit acee592

Please sign in to comment.