Skip to content
This repository was archived by the owner on Jan 16, 2023. It is now read-only.

Commit

Permalink
major functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
samiliak committed Dec 23, 2019
1 parent 0d3d5ce commit b8a7a96
Show file tree
Hide file tree
Showing 2 changed files with 180 additions and 9 deletions.
94 changes: 94 additions & 0 deletions api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
module.exports = { getGists, getGistById, createGist, updateGist };

const https = require('https');

const commonOptions = {
headers: {
'User-Agent': 'actions-pipeline',
'Authorization': `token ${ process.env.INPUT_GIST_TOKEN }`
},
hostname: "api.github.com",
port: 443,
};

function getGists() {
return new Promise((resolve, reject) => {
const options = {
...commonOptions,
path: "/gists",
method: 'GET',
};

sendRequest({ options, resolve, reject });
});
}

function getGistById(gistId) {
return new Promise((resolve, reject) => {
const options = {
...commonOptions,
path: `/gists/${gistId}`,
method: 'GET',
};

sendRequest({ options, resolve, reject });
});
}

function createGist(parameters) {
return new Promise((resolve, reject) => {
const options = {
...commonOptions,
headers: {
...commonOptions.headers,
'Content-Type': 'application/json'
},
path: '/gists',
method: 'POST'
};

sendRequest({ body: JSON.stringify(parameters), options, resolve, reject });
});
}

function updateGist(gistId, parameters) {
return new Promise((resolve, reject) => {
const options = {
...commonOptions,
headers: {
...commonOptions.headers,
'Content-Type': 'application/json'
},
path: `/gists/${gistId}`,
method: 'PATCH'
};

sendRequest({ body: JSON.stringify(parameters), options, resolve, reject });
});
}

function sendRequest({ options, body, resolve, reject }) {
const request = https.request(options, responseHandler(resolve));

request.on('error', (error) => {
reject(error);
});

if (body) {
request.write(body);
}
request.end()
}

function responseHandler(resolve) {
return (response) => {
let body = '';
response.setEncoding('UTF-8');
response.on('data', (chunk) => {
body += chunk;
});
response.on('end', () => {
resolve(JSON.parse(body))
})
}
}
95 changes: 86 additions & 9 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,93 @@
const { env } = process;
const api = require('./api');

const GITHUB_CONTEXT = JSON.parse(env.INPUT_GITHUB_INFO)
const GIST_TOKEN = env.INPUT_GIST_TOKEN
const githubContext = JSON.parse(process.env.INPUT_GITHUB_INFO);
const gistName = 'build-version-state';
const commonGistParameters = {
description: gistName,
public: false,
};

const { event_name: eventName } = GITHUB_CONTEXT;
const branch = getBranchName(githubContext);
const now = new Date(Date.now());
const date = `${now.getFullYear().toString().slice(-2)}-${now.getMonth()+1}-${now.getDate()}`;

switch (eventName) {
case 'pull_request':
pullRequestAction(GITHUB_CONTEXT);
getBuildNumber(branch)
.then(buildNumber => {
switch (branch.split('/')[0]){
case 'master':
return `${date}.${buildNumber}`;
case 'feature':
return `PR${githubContext.event.number}.${buildNumber}`;
case 'release':
const currentVersion = branch.replace('release/', '');
return `${currentVersion}-hotfix.${buildNumber}`;
case 'bugfix':
return `PR${githubContext.event.number}.${buildNumber}`;
case 'hotfix':
return `PR${githubContext.event.number}.${buildNumber}`;
default:
console.error('Unsupported branch name' + branch);
process.exit(1);
}
})
.then(version => {
console.log(`::set-env name=VERSION::${version}`)
console.log(`::set-output name=version::${version}`)
});

async function getBuildNumber(branch) {
const gistList = await api.getGists();
const state = gistList.find(gist => gist.description === gistName);

if (!state) {
const gistCreationParameters = patchGistFile({ [branch]: 1 });
await api.createGist(gistCreationParameters);
return 1;
}

const stateGist = await api.getGistById(state.id);

if (!Object.keys(stateGist.files).includes(process.env.GITHUB_REPOSITORY)){
await api.updateGist(stateGist.id, patchGistFile({ [branch]: 1 }));

return 1;
}

const repositoryVersions = JSON.parse(stateGist.files[process.env.GITHUB_REPOSITORY].content);

if (!Object.keys(repositoryVersions).includes(branch)) {
repositoryVersions[branch] = 1;
await api.updateGist(stateGist.id, patchGistFile(repositoryVersions));

return repositoryVersions[branch];
}

++repositoryVersions[branch];
await api.updateGist(stateGist.id, patchGistFile(repositoryVersions));

return repositoryVersions[branch]
}

function patchGistFile(fileContent) {
return {
...commonGistParameters,
files: {
[process.env.GITHUB_REPOSITORY]: {
content: JSON.stringify(fileContent)
}
},
};
}

function pullRequestAction(context) {
console.log('This is PR : ', context.event.number);
function getBranchName(context) {
switch(context.event_name) {
case 'pull_request':
return context.head_ref;
case 'push':
return context.ref.replace('refs/heads/', '');
default:
console.log('Something went wrong :( Unsupported Github event type');
process.exit(1);
break;
}
}

0 comments on commit b8a7a96

Please sign in to comment.