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

Redmine #7

Merged
merged 12 commits into from
Jan 17, 2024
73 changes: 73 additions & 0 deletions src/appmixer/redmine/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Redmine

## Generate module

```sh
appmixer init openapi ./redmine/openapi.yml ./redmine/
```

## Manual Updates

Apply the following manual updates since those are not covered by the OpenAPI generator.

### auth.js

Replace the whole content of the file with:

```
'use strict';

module.exports = {

type: 'apiKey',

definition: {

auth: {
apiKey: {
type: 'text',
name: 'API Key',
tooltip: 'Enter your API key.'
},
'url': { type: 'text', name: 'url', tooltip: 'Redmine URL' }
},

replaceVariables(context, str) {
Object.keys(this.auth).forEach(variableName => {
str = str.replaceAll('{' + variableName + '}', context[variableName]);
});
return str;
},

async requestProfileInfo(context) {
const method = 'GET';
const url = '/my/account.json';
const baseUrl = context.url;
const options = { method: method, url: baseUrl + url };
options.headers = {
'X-Redmine-API-Key': '{apiKey}'
};
options.headers = JSON.parse(this.replaceVariables(context, JSON.stringify(options.headers)));
const { data } = await context.httpRequest(options);
// Return string in format: admin - abc***
const info = `${data.user.login} - ${data.user.api_key.slice(0, 3)}***`
return { info };
},

accountNameFromProfileInfo: 'info',

async validate(context) {
const method = 'GET';
const url = '/my/account.json';
const baseUrl = context.url;
const options = { method: method, url: baseUrl + url };
options.headers = {
'X-Redmine-API-Key': '{apiKey}'
};
options.headers = JSON.parse(this.replaceVariables(context, JSON.stringify(options.headers)));
await context.httpRequest(options);
return true;
}
}
};
```
55 changes: 55 additions & 0 deletions src/appmixer/redmine/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict';

module.exports = {

type: 'apiKey',

definition: {

auth: {
apiKey: {
type: 'text',
name: 'API Key',
tooltip: 'Enter your API key.'
},
'url': { type: 'text', name: 'url', tooltip: 'Redmine URL' }
},

replaceVariables(context, str) {
Object.keys(this.auth).forEach(variableName => {
str = str.replaceAll('{' + variableName + '}', context[variableName]);
});
return str;
},

async requestProfileInfo(context) {
const method = 'GET';
const url = '/my/account.json';
const baseUrl = context.url;
const options = { method: method, url: baseUrl + url };
options.headers = {
'X-Redmine-API-Key': '{apiKey}'
};
options.headers = JSON.parse(this.replaceVariables(context, JSON.stringify(options.headers)));
const { data } = await context.httpRequest(options);
// Return string in format: admin - abc***
const info = `${data.user.login} - ${data.user.api_key.slice(0, 3)}***`;
return { info };
},

accountNameFromProfileInfo: 'info',

async validate(context) {
const method = 'GET';
const url = '/my/account.json';
const baseUrl = context.url;
const options = { method: method, url: baseUrl + url };
options.headers = {
'X-Redmine-API-Key': '{apiKey}'
};
options.headers = JSON.parse(this.replaceVariables(context, JSON.stringify(options.headers)));
await context.httpRequest(options);
return true;
}
}
};
9 changes: 9 additions & 0 deletions src/appmixer/redmine/bundle.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "appmixer.redmine",
"version": "1.0.0",
"changelog": {
"1.0.0": [
"Redmine"
]
}
}
91 changes: 91 additions & 0 deletions src/appmixer/redmine/core/IssueCreate/IssueCreate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
'use strict';

const lib = require('../../lib');

module.exports = {

receive: async function(context) {

const { data } = await this.httpRequest(context);

return context.sendJson(data, 'out');
},

httpRequest: async function(context) {

// eslint-disable-next-line no-unused-vars
const input = context.messages.in.content;

let url = lib.getBaseUrl(context) + '/issues.json';

const headers = {};

const inputMapping = {
'issue.project_id': input['issue|project_id'],
'issue.tracker_id': input['issue|tracker_id'],
'issue.status_id': input['issue|status_id'],
'issue.priority_id': input['issue|priority_id'],
'issue.subject': input['issue|subject'],
'issue.description': input['issue|description'],
'issue.category_id': input['issue|category_id'],
'issue.fixed_version_id': input['issue|fixed_version_id'],
'issue.assigned_to_id': input['issue|assigned_to_id'],
'issue.parent_issue_id': input['issue|parent_issue_id'],
'issue.custom_fields': input['issue|custom_fields'],
'issue.watcher_user_ids': input['issue|watcher_user_ids'],
'issue.is_private': input['issue|is_private'],
'issue.estimated_hours': input['issue|estimated_hours']
};
let requestBody = {};
lib.setProperties(requestBody, inputMapping);

headers['X-Redmine-API-Key'] = context.auth.apiKey;

const req = {
url: url,
method: 'POST',
data: requestBody,
headers: headers
};

try {
const response = await context.httpRequest(req);
const log = {
step: 'http-request-success',
request: {
url: req.url,
method: req.method,
headers: req.headers,
data: req.data
},
response: {
data: response.data,
status: response.status,
statusText: response.statusText,
headers: response.headers
}
};
await context.log(log);
return response;
} catch (err) {
const log = {
step: 'http-request-error',
request: {
url: req.url,
method: req.method,
headers: req.headers,
data: req.data
},
response: err.response ? {
data: err.response.data,
status: err.response.status,
statusText: err.response.statusText,
headers: err.response.headers
} : undefined
};
await context.log(log);
throw err;
}
}

};
Loading