Skip to content

Instantly share code, notes, and snippets.

@wisetc
Created August 30, 2019 02:45
Show Gist options
  • Select an option

  • Save wisetc/99d280e3d20920e2fdfff22c03a4223d to your computer and use it in GitHub Desktop.

Select an option

Save wisetc/99d280e3d20920e2fdfff22c03a4223d to your computer and use it in GitHub Desktop.
nodejs fetch request with git data.
// save_build.prod.js
// @format
const util = require('util');
const exec = util.promisify(require('child_process').exec);
async function getCommitMessage() {
const { stdout } = await exec(
'echo $(git log -1 --pretty="%B") | tr -d "\n"'
);
return stdout;
}
async function getAuthor() {
const { stdout } = await exec(
'echo $(git log -1 --pretty="%an <%ae>") | tr -d "\n"'
);
return stdout;
}
async function getHash() {
const { stdout } = await exec(
'echo $(git log -1 --pretty="%H") | tr -d "\n"'
);
return stdout;
}
async function getPostBody() {
const content = await getCommitMessage();
const creator = await getAuthor();
const version = await getHash();
const body = {
content,
creator,
product: 'dingtalk',
version,
};
let ret = '';
for (const k in body) {
let value = encodeURIComponent(body[k]);
ret += `&${k}=${value}`;
}
return ret.slice(1);
}
async function saveBuild() {
const postBody = await getPostBody();
const url = process.env.BUILD_LOGGER_URL;
const shellCmd = `curl "${url}" -d "${postBody}"`;
console.log(shellCmd);
const { stdout, stderr } = await exec(shellCmd);
stdout && console.log(stdout);
stderr && console.log(stderr);
return stdout;
}
saveBuild();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment