Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Message formatting changes #35

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The following notification destinations are supported:
| ---------- | --------------------------------------------------------------------------------- |
| smtpServer | email |
| webHooks | Webhook, the $msg variable can be used to insert the docker-notify update message |
| twitter | twitter API, $msg variable can be used for tweet text |

## Setup

Expand Down Expand Up @@ -116,6 +117,16 @@ The `config.json` looks like the following:
"httpBody": {
"text": "$msg"
}
},
{
"twitter": {
"rasaAlerts": {
"appKey": "your_app_api_key",
"appSecret": "your_app_api_secret",
"accessToken": "your_app_access_token",
"accessSecret": "your_app_access_secret",
"message": "$msg"
}
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "docker-notify",
"version": "1.1.0",
"version": "1.3.0",
"description": "",
"main": "index.js",
"scripts": {
Expand All @@ -12,7 +12,8 @@
"ajv": "^8.6.3",
"ajv-formats": "^2.1.1",
"axios": "^0.24.0",
"nodemailer": "^6.7.0"
"nodemailer": "^6.7.0",
"twitter-api-v2": "^1.6.4"
},
"devDependencies": {
"eslint": "^7.32.0",
Expand Down
28 changes: 24 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const dockerAPI = new (require('./DockerAPI').DockerAPI)();
const { TwitterApi } = require('twitter-api-v2');
const mailService = require('./mailService');
const Cache = require('./Cache');
const schema = require('./schema.json');
Expand Down Expand Up @@ -57,8 +58,8 @@ const notifyServices = config.notifyServices;
// these things are: smtp-server referenced in notifyJob is existing and
// webhooks referenced in notifyJob is existing
if (!notifyServices.every((o) => o.actions.every((o2) => o2.type === 'webHook' ? config.webHooks[o2.instance] :
o2.type === 'mailHook' ? config.smtpServer[o2.instance] : false))){
logger.error('Mail/Smtp Hooks that are referenced are not defined!');
o2.type === 'mailHook' ? config.smtpServer[o2.instance] : o2.type === 'twitter' ? config.twitter[o2.instance] : false))) {
logger.error('Mail/Smtp/twitter Hooks that are referenced are not defined!');
process.exit(3);
}

Expand Down Expand Up @@ -216,7 +217,7 @@ const checkForUpdates = function () {
const message = webHook.httpBody;
Object.keys(message).forEach((key) => {
if (typeof message[key] == 'string') {
message[key] = message[key].replace('$msg', 'Docker image \'' + o.updatedString + '\' was updated:\n' + JSON.stringify(o.job.image));
message[key] = message[key].replace('$msg', 'Docker image \'' + o.updatedString + '\' was updated:\nhttps://hub.docker.com/r/' + o.updatedString.split(':')[0] + '/tags');
}
});

Expand All @@ -234,7 +235,26 @@ const checkForUpdates = function () {
}
else if (o2.type == 'mailHook'){
mailHookSend(o2.instance, o2.recipient, o.updatedString, 'Docker image \'' + o.updatedString + '\' was updated:\n'
+ JSON.stringify(o.job.image, null, 2));
+ 'https://hub.docker.com/r/' + o.updatedString.split(':')[0] + '/tags');
}
else if (o2.type == 'twitter') {
const twitter = config.twitter[o2.instance];
const twitterClient = new TwitterApi({
appKey: twitter.appKey,
appSecret: twitter.appSecret,
accessToken: twitter.accessToken,
accessSecret: twitter.accessSecret,
});

(async () => {
try {
const message = twitter.message.replace('$msg', 'Docker image \'' + o.updatedString + '\' was updated');
await twitterClient.v1.tweet(message);
} catch (err) {
logger.error('tweet error: ', err);
logger.error('tweet error: ', err.data.errors.message);
}
})();
}
else {
logger.error('Trying to execute an unknown hook(' + o2.type + '), falling back to printing to console');
Expand Down
60 changes: 60 additions & 0 deletions src/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,28 @@
"recipient"
],
"additionalProperties": false
},
{
"type": "object",
"description": "A twitter instance consists of the reference in the twitter Array and the type specified as twitter",
"properties": {
"type": {
"type": "string",
"description": "As we are creating a twitter API instance, this can only be twitter",
"enum": [
"twitter"
]
},
"instance": {
"type": "string",
"description": "A reference to a object in the list of webhooks (Reference to id)"
}
},
"required": [
"type",
"instance"
],
"additionalProperties": false
}
]
},
Expand Down Expand Up @@ -164,6 +186,44 @@
}
},
"additionalProperties": false
},
"twitter": {
"type": "object",
"patternProperties": {
"^..*$": {
"type": "object",
"properties": {
"appKey": {
"type": "string",
"minLength": 10
},
"appSecret": {
"type": "string",
"minLength": 20
},
"accessToken": {
"type": "string",
"minLength": 10
},
"accessSecret": {
"type": "string",
"minLength": 20
},
"message": {
"type": "string",
"default": "$msg"
}
},
"required": [
"appKey",
"appSecret",
"accessToken",
"accessSecret"
],
"additionalProperties": false
}
},
"additionalProperties": false
}
},
"required": [
Expand Down