You've already forked dynamic-badges-action
🎉 add ability to update gist only if changed
This commit is contained in:
129
index.js
129
index.js
@@ -3,6 +3,49 @@ const http = require('https');
|
||||
|
||||
try {
|
||||
|
||||
function updatingGist(data) {
|
||||
// Perform the actual request. The user agent is required as defined in
|
||||
// https://developer.github.com/v3/#user-agent-required
|
||||
const updateGistOptions = {
|
||||
host: 'api.github.com',
|
||||
path: '/gists/' + core.getInput('gistID'),
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Content-Length': data.length,
|
||||
'User-Agent': 'Schneegans',
|
||||
'Authorization': 'token ' + core.getInput('auth'),
|
||||
}
|
||||
};
|
||||
|
||||
return doRequest(updateGistOptions, data)
|
||||
}
|
||||
|
||||
function doRequest(options, data) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const req = http.request(options, (res) => {
|
||||
res.setEncoding('utf8');
|
||||
let responseBody = '';
|
||||
|
||||
res.on('data', (chunk) => {
|
||||
responseBody += chunk;
|
||||
});
|
||||
|
||||
res.on('end', () => {
|
||||
const { statusCode, statusMessage } = res;
|
||||
resolve({ statusCode, statusMessage, body: JSON.parse(responseBody) });
|
||||
});
|
||||
});
|
||||
|
||||
req.on('error', (err) => {
|
||||
reject(err);
|
||||
});
|
||||
|
||||
req.write(data)
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
// This object will be stringified and uploaded to the gist. The
|
||||
// schemaVersion, label and message attributes are always required. All others
|
||||
// are optional and added to the content object only if they are given to the
|
||||
@@ -13,6 +56,8 @@ try {
|
||||
message: core.getInput('message')
|
||||
};
|
||||
|
||||
const updateIfChanged = core.getInput('updateIfChanged');
|
||||
|
||||
// Compute the message color based on the given inputs.
|
||||
const color = core.getInput('color');
|
||||
const valColorRange = core.getInput('valColorRange');
|
||||
@@ -64,6 +109,7 @@ try {
|
||||
const logoPosition = core.getInput('logoPosition');
|
||||
const style = core.getInput('style');
|
||||
const cacheSeconds = core.getInput('cacheSeconds');
|
||||
const filename = core.getInput('filename');
|
||||
|
||||
if (labelColor != '') {
|
||||
content.labelColor = labelColor;
|
||||
@@ -101,39 +147,72 @@ try {
|
||||
content.cacheSeconds = parseInt(cacheSeconds);
|
||||
}
|
||||
|
||||
let shouldUpdate = true;
|
||||
|
||||
// For the POST request, the above content is set as file contents for the
|
||||
// given filename.
|
||||
const request = JSON.stringify({
|
||||
files: {[core.getInput('filename')]: {content: JSON.stringify(content)}}
|
||||
files: {[filename]: {content: JSON.stringify(content)}}
|
||||
});
|
||||
|
||||
if (updateIfChanged == 'true') {
|
||||
const getGistOptions = {
|
||||
host: 'api.github.com',
|
||||
path: '/gists/' + core.getInput('gistID'),
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'User-Agent': 'Schneegans',
|
||||
'Authorization': 'token ' + core.getInput('auth'),
|
||||
}
|
||||
};
|
||||
|
||||
doRequest(getGistOptions, JSON.stringify({})).then(oldGist => {
|
||||
if (oldGist.statusCode < 200 || oldGist.statusCode >= 400) {
|
||||
// print the error, but don't fail the action
|
||||
console.log(
|
||||
'Failed to get gist, response status code: ' + oldGist.statusCode +
|
||||
', status message: ' + oldGist.statusMessage);
|
||||
}
|
||||
|
||||
if (oldGist && oldGist.body && oldGist.body.files && oldGist.body.files[filename]) {
|
||||
const oldContent = oldGist.body.files[filename].content;
|
||||
|
||||
// Perform the actual request. The user agent is required as defined in
|
||||
// https://developer.github.com/v3/#user-agent-required
|
||||
const req = http.request(
|
||||
{
|
||||
host: 'api.github.com',
|
||||
path: '/gists/' + core.getInput('gistID'),
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Content-Length': request.length,
|
||||
'User-Agent': 'Schneegans',
|
||||
'Authorization': 'token ' + core.getInput('auth'),
|
||||
if (oldContent === JSON.stringify(content)) {
|
||||
console.log(`Content did not change, not updating gist at ${filename}`);
|
||||
shouldUpdate = false;
|
||||
}
|
||||
},
|
||||
res => {
|
||||
if (res.statusCode < 200 || res.statusCode >= 400) {
|
||||
core.setFailed(
|
||||
}
|
||||
|
||||
if (shouldUpdate) {
|
||||
if (oldGist.body.files[filename]) {
|
||||
console.log(`Content changed, updating gist at ${filename}`);
|
||||
} else {
|
||||
console.log(`Content didn't exist, creating gist at ${filename}`);
|
||||
}
|
||||
|
||||
updatingGist(request).then(res => {
|
||||
if (res.statusCode < 200 || res.statusCode >= 400) {
|
||||
core.setFailed(
|
||||
'Failed to create gist, response status code: ' + res.statusCode +
|
||||
', status message: ' + res.statusMessage);
|
||||
} else {
|
||||
console.log('Success!');
|
||||
}
|
||||
});
|
||||
|
||||
req.write(request);
|
||||
req.end();
|
||||
|
||||
} else {
|
||||
console.log('Success!');
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
updatingGist(request).then(res => {
|
||||
if (res.statusCode < 200 || res.statusCode >= 400) {
|
||||
core.setFailed(
|
||||
'Failed to create gist, response status code: ' + res.statusCode +
|
||||
', status message: ' + res.statusMessage);
|
||||
} else {
|
||||
console.log('Success!');
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
core.setFailed(error);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user