3

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

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

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

4

1 回答 1

3

最好的方法是结合使用 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 在 Exact Online 中作为事件提交

于 2017-03-28T09:13:04.293 回答