You've already forked dynamic-badges-action
📝 Add some comments
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
BasedOnStyle: Google
|
BasedOnStyle: Google
|
||||||
|
ColumnLimit: 90
|
||||||
AlignConsecutiveAssignments: true
|
AlignConsecutiveAssignments: true
|
||||||
KeepEmptyLinesAtTheStartOfBlocks: true
|
KeepEmptyLinesAtTheStartOfBlocks: true
|
||||||
90
index.js
90
index.js
@@ -1,34 +1,14 @@
|
|||||||
|
//////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// This file is part of the Dynamic Badges Action //
|
||||||
|
// It may be used under the terms of the MIT license. See the LICENSE file for details. //
|
||||||
|
// Copyright: (c) 2020 Simon Schneegans //
|
||||||
|
//////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
const core = require('@actions/core');
|
const core = require('@actions/core');
|
||||||
const http = require('https');
|
const http = require('https');
|
||||||
|
|
||||||
try {
|
// Performs an HTTP request and returns a Promise accordingly. See docs of
|
||||||
|
// http.request() for the available options.
|
||||||
function updateGist(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'),
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
doRequest(updateGistOptions, data).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!');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function doRequest(options, data) {
|
function doRequest(options, data) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const req = http.request(options, res => {
|
const req = http.request(options, res => {
|
||||||
@@ -54,10 +34,39 @@ try {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// This object will be stringified and uploaded to the gist. The
|
// This uses the method above to update a gist with the given data. The user agent is
|
||||||
// schemaVersion, label and message attributes are always required. All others
|
// required as defined in https://developer.github.com/v3/#user-agent-required
|
||||||
// are optional and added to the content object only if they are given to the
|
function updateGist(data) {
|
||||||
// action.
|
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'),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
doRequest(updateGistOptions, data).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!');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// We wrap the entire action in a try / catch block so we can set it to "failed" if
|
||||||
|
// something goes wrong.
|
||||||
|
try {
|
||||||
|
|
||||||
|
// 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 action.
|
||||||
let content = {
|
let content = {
|
||||||
schemaVersion: 1,
|
schemaVersion: 1,
|
||||||
label: core.getInput('label'),
|
label: core.getInput('label'),
|
||||||
@@ -158,9 +167,14 @@ try {
|
|||||||
const request =
|
const request =
|
||||||
JSON.stringify({files: {[filename]: {content: JSON.stringify(content)}}});
|
JSON.stringify({files: {[filename]: {content: JSON.stringify(content)}}});
|
||||||
|
|
||||||
|
// If "forceUpdate" is set to true, we can simply update the gist. If not, we have to
|
||||||
|
// get the gist data and compare it to the new value before.
|
||||||
if (core.getBooleanInput('forceUpdate')) {
|
if (core.getBooleanInput('forceUpdate')) {
|
||||||
updateGist(request);
|
updateGist(request);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
|
// Get the old gist.
|
||||||
const getGistOptions = {
|
const getGistOptions = {
|
||||||
host: 'api.github.com',
|
host: 'api.github.com',
|
||||||
path: '/gists/' + core.getInput('gistID'),
|
path: '/gists/' + core.getInput('gistID'),
|
||||||
@@ -174,7 +188,7 @@ try {
|
|||||||
|
|
||||||
doRequest(getGistOptions, JSON.stringify({})).then(oldGist => {
|
doRequest(getGistOptions, JSON.stringify({})).then(oldGist => {
|
||||||
if (oldGist.statusCode < 200 || oldGist.statusCode >= 400) {
|
if (oldGist.statusCode < 200 || oldGist.statusCode >= 400) {
|
||||||
// print the error, but don't fail the action
|
// print the error, but don't fail the action.
|
||||||
console.log(
|
console.log(
|
||||||
'Failed to get gist, response status code: ' + oldGist.statusCode +
|
'Failed to get gist, response status code: ' + oldGist.statusCode +
|
||||||
', status message: ' + oldGist.statusMessage);
|
', status message: ' + oldGist.statusMessage);
|
||||||
@@ -182,22 +196,20 @@ try {
|
|||||||
|
|
||||||
let shouldUpdate = true;
|
let shouldUpdate = true;
|
||||||
|
|
||||||
if (oldGist && oldGist.body && oldGist.body.files &&
|
if (oldGist && oldGist.body && oldGist.body.files && oldGist.body.files[filename]) {
|
||||||
oldGist.body.files[filename]) {
|
|
||||||
const oldContent = oldGist.body.files[filename].content;
|
const oldContent = oldGist.body.files[filename].content;
|
||||||
|
|
||||||
if (oldContent === JSON.stringify(content)) {
|
if (oldContent === JSON.stringify(content)) {
|
||||||
console.log(
|
console.log(`Content did not change, not updating gist at ${filename}.`);
|
||||||
`Content did not change, not updating gist at ${filename}`);
|
|
||||||
shouldUpdate = false;
|
shouldUpdate = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (shouldUpdate) {
|
if (shouldUpdate) {
|
||||||
if (oldGist.body.files[filename]) {
|
if (oldGist.body.files[filename]) {
|
||||||
console.log(`Content changed, updating gist at ${filename}`);
|
console.log(`Content changed, updating gist at ${filename}.`);
|
||||||
} else {
|
} else {
|
||||||
console.log(`Content didn't exist, creating gist at ${filename}`);
|
console.log(`Content didn't exist, creating gist at ${filename}.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
updateGist(request);
|
updateGist(request);
|
||||||
|
|||||||
Reference in New Issue
Block a user