aws-lambda - 在 Exact Online ERP 中注册 AWS GIT 代码推送事件

标签 aws-lambda aws-codecommit exact-online invantive-sql

当开发人员通过 GIT 推送到 AWS codecommit checkin 代码时,我想在我们的 ERP 包 Exact Online 中注册一个事件。这允许项目经理从 ERP 包中审查提交。

AWS Codecommit 仅支持通过 SNS 和 Lambda 触发;没有批处理文件的连接。我一直在玩弄 AWS Lambda 并设法将事件从 AWS Codecommit 发布到 Slack,但对于 Exact Online 来说似乎更难。

如何将代码提交的 GIT 事件发布到 Exact Online 中的业务对象?

最佳答案

最好的方法是结合使用 AWS Lambda 和 Invantive Data Access Point,使用如下脚本:

console.log('Loading function codecommit2slack.');

const aws = require('aws-sdk');
const codecommit = new aws.CodeCommit({ apiVersion: '2015-04-13', region: 'eu-west-1' });
const querystring = require('querystring');

const https = require('https');
const url = require('url');
//
// To get the slack hook url, go into slack admin and create a new "Incoming Webhook" integration.
//
const slack_url = 'https://hooks.slack.com/services/SECRET/STUFF';
const slack_req_opts = url.parse(slack_url);
slack_req_opts.method = 'POST';
slack_req_opts.headers = {'Content-Type': 'application/json'};

exports.handler = function(event, context) 
{
  console.log('Run codecommit2slack.');
  (event.Records || []).forEach(function (rec) 
  {
    var details = rec.codecommit.references[0];
    var commitId = details.commit;
    var ref = details.ref;
    var repository = rec.eventSourceARN.split(":")[5];

    console.log("Repo " + repository + ", commit ID " + commitId + " on " + ref);

    var params = 
    {   commitId: commitId
    ,   repositoryName: repository
    };

    codecommit.getCommit
    (   params
    ,   function(err, data) 
        {
            if (err) console.log(err, err.stack); // an error occurred
            else     
            {
                var commitMessage = data.commit.message;
                var authorName = data.commit.author.name;
                var committerName = data.commit.committer.name;

                console.log(commitMessage);

                var postData = querystring.stringify
                (
                    {   'connection': 'PUBLIC\\Exact Online (nl)'
                    ,   'format': 'Xml'
                    ,   'query': "insert into events(description, enddate, notes, startdate, status) values ('" + repository + ": " + commitMessage.replace(/[^\x20-\x7E]/gmi, "") + "', sysdate, '" + committerName + " / " + commitId + "', sysdate, 50)"
                    }
                )
                ;

                var daphttpoptions =
                {   host: "data-access-point.com"
                ,   port: 443
                ,   path: '/eol/stable/dap/Results'
                ,   auth: 'EXACT-ONLINE-USER' + ":" + 'EXACT-ONLINE-PASSWORD'
                ,   method: 'POST'
                ,   headers:
                    {   'Content-Type': 'application/x-www-form-urlencoded'
                    ,   'Content-Length': Buffer.byteLength(postData)
                    }
                }

                var dapreq = https.request
                ( daphttpoptions
                , function (res) 
                    {
                        if (res.statusCode === 200) 
                        {
                            console.log('posted to DAP');
                            context.succeed('posted to DAP');
                        } 
                        else 
                        {
                            console.log('post to DAP failed with status code: ' + res.statusCode);
                            context.fail('status code: ' + res.statusCode);
                        }
                    }
                );

                dapreq.on
                ( 'error'
                , function(e) 
                    {
                        console.log('problem with DAP request: ' + e.message);
                        context.fail(e.message);
                    }
                );

                //
                // Send to Data Access Point.
                //
                dapreq.write(postData);

                dapreq.end();

                var req = https.request
                ( slack_req_opts
                , function (res) 
                    {
                        if (res.statusCode === 200) 
                        {
                            console.log('posted to slack');
                            context.succeed('posted to slack');
                        } 
                        else 
                        {
                            console.log('post to slack failed with status code: ' + res.statusCode);
                            context.fail('status code: ' + res.statusCode);
                        }
                    }
                );

                req.on
                ( 'error'
                , function(e) 
                    {
                        console.log('problem with Slack request: ' + e.message);
                        context.fail(e.message);
                    }
                );

                //
                // Send to development-audit channel.
                //
                req.write(JSON.stringify({username: committerName, as_user: true, text: commitMessage + " on " + repository + " (ID: " + commitId + ", ref " + ref + ")", channel: '#development-audit'}));

                req.end();
            }
        }
    );

  });
};

console.log('Finished loading function codecommit2slack.');

此脚本还包括一个到 Slack 的帖子。第一版代码基于 https://gist.github.com/vgeshel/1dba698aed9e8b39a464 ,谢谢。

Exact Online 中的结果看起来像这样,但当然您也可以使用它为每个新提交或标记创建例如文章或序列号:

GIT commits in Exact Online as events

关于aws-lambda - 在 Exact Online ERP 中注册 AWS GIT 代码推送事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43065388/

相关文章:

Eclipse:尝试克隆 CodeCommit 存储库并收到以下错误: "...git-upload-pack not permitted on..."

exact-online - 如何使用 Invantive Query Tool 从 Exact Online 仅下载我的采购发票文件?

api - 准确的 API 响应语言

node.js - Amazon Lambda 上来自 ffmpeg 的 SIGSEGV

node.js - 如何达到 AWS Lambda 并发执行限制?

amazon-web-services - 如何将一个 AWS CodeCommit 存储库 fork 到另一个 CodeCommit 存储库?

exact-online - 如何通过 SalesInvoicesExploded 优化 SerialNumbers 的性能?

AWS EC2 上的 Node.js RESTful API 服务器与 AWS API 网关

aws-lambda - 如何在不使用不可靠的城镇时钟的情况下安排重复的 lambda 执行

git - 针对 AWS CodeCommits 运行 git clone 时出现 403 错误