You've already forked dynamic-badges-action
🔀 Merge pull request #17 from MishaKav/add-update-if-changed
This commit is contained in:
@@ -86,6 +86,7 @@ Parameter | Description
|
|||||||
`logoPosition` | The position of the logo.
|
`logoPosition` | The position of the logo.
|
||||||
`style` | The style like "flat" or "social".
|
`style` | The style like "flat" or "social".
|
||||||
`cacheSeconds` | The cache lifetime in seconds (must be greater than 300).
|
`cacheSeconds` | The cache lifetime in seconds (must be greater than 300).
|
||||||
|
`updateIfChanged` | Default is `false`. If `true` will update the gist only if content changed.
|
||||||
|
|
||||||
### Color Range Parameters (optional)
|
### Color Range Parameters (optional)
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,10 @@ inputs:
|
|||||||
message:
|
message:
|
||||||
description: 'The right text of the badge'
|
description: 'The right text of the badge'
|
||||||
required: true
|
required: true
|
||||||
|
updateIfChanged:
|
||||||
|
description: 'If true will update the gist only if content changed'
|
||||||
|
default: 'false'
|
||||||
|
required: false
|
||||||
labelColor:
|
labelColor:
|
||||||
description: 'The left color of the badge'
|
description: 'The left color of the badge'
|
||||||
required: false
|
required: false
|
||||||
|
|||||||
129
index.js
129
index.js
@@ -3,6 +3,49 @@ const http = require('https');
|
|||||||
|
|
||||||
try {
|
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
|
// This object will be stringified and uploaded to the gist. The
|
||||||
// schemaVersion, label and message attributes are always required. All others
|
// 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
|
// are optional and added to the content object only if they are given to the
|
||||||
@@ -13,6 +56,8 @@ try {
|
|||||||
message: core.getInput('message')
|
message: core.getInput('message')
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const updateIfChanged = core.getInput('updateIfChanged');
|
||||||
|
|
||||||
// Compute the message color based on the given inputs.
|
// Compute the message color based on the given inputs.
|
||||||
const color = core.getInput('color');
|
const color = core.getInput('color');
|
||||||
const valColorRange = core.getInput('valColorRange');
|
const valColorRange = core.getInput('valColorRange');
|
||||||
@@ -64,6 +109,7 @@ try {
|
|||||||
const logoPosition = core.getInput('logoPosition');
|
const logoPosition = core.getInput('logoPosition');
|
||||||
const style = core.getInput('style');
|
const style = core.getInput('style');
|
||||||
const cacheSeconds = core.getInput('cacheSeconds');
|
const cacheSeconds = core.getInput('cacheSeconds');
|
||||||
|
const filename = core.getInput('filename');
|
||||||
|
|
||||||
if (labelColor != '') {
|
if (labelColor != '') {
|
||||||
content.labelColor = labelColor;
|
content.labelColor = labelColor;
|
||||||
@@ -101,39 +147,72 @@ try {
|
|||||||
content.cacheSeconds = parseInt(cacheSeconds);
|
content.cacheSeconds = parseInt(cacheSeconds);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let shouldUpdate = true;
|
||||||
|
|
||||||
// For the POST request, the above content is set as file contents for the
|
// For the POST request, the above content is set as file contents for the
|
||||||
// given filename.
|
// given filename.
|
||||||
const request = JSON.stringify({
|
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
|
if (oldContent === JSON.stringify(content)) {
|
||||||
// https://developer.github.com/v3/#user-agent-required
|
console.log(`Content did not change, not updating gist at ${filename}`);
|
||||||
const req = http.request(
|
shouldUpdate = false;
|
||||||
{
|
|
||||||
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'),
|
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
res => {
|
|
||||||
if (res.statusCode < 200 || res.statusCode >= 400) {
|
if (shouldUpdate) {
|
||||||
core.setFailed(
|
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 +
|
'Failed to create gist, response status code: ' + res.statusCode +
|
||||||
', status message: ' + res.statusMessage);
|
', status message: ' + res.statusMessage);
|
||||||
} else {
|
} else {
|
||||||
console.log('Success!');
|
console.log('Success!');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
req.write(request);
|
});
|
||||||
req.end();
|
} 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) {
|
} catch (error) {
|
||||||
core.setFailed(error);
|
core.setFailed(error);
|
||||||
}
|
}
|
||||||
|
|||||||
2
package-lock.json
generated
2
package-lock.json
generated
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "dynamic-badges-action",
|
"name": "dynamic-badges-action",
|
||||||
"version": "1.0.0",
|
"version": "1.5.0",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "dynamic-badges-action",
|
"name": "dynamic-badges-action",
|
||||||
"version": "1.0.0",
|
"version": "1.5.0",
|
||||||
"description": "A GitHub Action which uses shields.io to create custom badges and uploads the to a gist.",
|
"description": "A GitHub Action which uses shields.io to create custom badges and uploads the to a gist.",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@@ -12,7 +12,7 @@
|
|||||||
},
|
},
|
||||||
"keywords": [],
|
"keywords": [],
|
||||||
"author": "",
|
"author": "",
|
||||||
"license": "ISC",
|
"license": "MIT",
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/Schneegans/dynamic-badges-action/issues"
|
"url": "https://github.com/Schneegans/dynamic-badges-action/issues"
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user