artifact-pr-notify
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -7,4 +7,4 @@ { | ||
"dependencies": { | ||
"artifact-pr-notify": "^1.0.1" | ||
"artifact-pr-notify": "^1.0.2" | ||
} | ||
} |
135
lib/index.js
@@ -38,90 +38,61 @@ "use strict"; | ||
var _this = this; | ||
var axios = require("axios"); | ||
var Octokit = require("@octokit/rest"); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var slack_notify_1 = require("./slack-notify"); | ||
var get_artifact_1 = require("./get-artifact"); | ||
var github_notify_1 = require("./github-notify"); | ||
// used for local test | ||
require("dotenv").config(); | ||
module.exports = function (targetPath) { return __awaiter(_this, void 0, void 0, function () { | ||
var _a, CIRCLE_PULL_REQUEST, CIRCLE_BUILD_NUM, GITHUB_TOKEN, CIRCLE_TOKEN, CIRCLE_PROJECT_USERNAME, CIRCLE_PROJECT_REPONAME, pullRequestId; | ||
var _a, CIRCLE_PULL_REQUEST, CIRCLE_BUILD_NUM, GITHUB_TOKEN, CIRCLE_TOKEN, CIRCLE_PROJECT_USERNAME, CIRCLE_PROJECT_REPONAME, SLACK_WEBHOOK, circleciApiUrl, artifactUrl, pullRequestId; | ||
return __generator(this, function (_b) { | ||
_a = process.env, CIRCLE_PULL_REQUEST = _a.CIRCLE_PULL_REQUEST, CIRCLE_BUILD_NUM = _a.CIRCLE_BUILD_NUM, GITHUB_TOKEN = _a.GITHUB_TOKEN, CIRCLE_TOKEN = _a.CIRCLE_TOKEN, CIRCLE_PROJECT_USERNAME = _a.CIRCLE_PROJECT_USERNAME, CIRCLE_PROJECT_REPONAME = _a.CIRCLE_PROJECT_REPONAME; | ||
if (!CIRCLE_PROJECT_USERNAME) { | ||
console.error("Cannot find project username"); | ||
return [2 /*return*/]; | ||
switch (_b.label) { | ||
case 0: | ||
_a = process.env, CIRCLE_PULL_REQUEST = _a.CIRCLE_PULL_REQUEST, CIRCLE_BUILD_NUM = _a.CIRCLE_BUILD_NUM, GITHUB_TOKEN = _a.GITHUB_TOKEN, CIRCLE_TOKEN = _a.CIRCLE_TOKEN, CIRCLE_PROJECT_USERNAME = _a.CIRCLE_PROJECT_USERNAME, CIRCLE_PROJECT_REPONAME = _a.CIRCLE_PROJECT_REPONAME, SLACK_WEBHOOK = _a.SLACK_WEBHOOK; | ||
// Validation | ||
if (!CIRCLE_PROJECT_USERNAME) { | ||
console.error("Cannot find project username"); | ||
return [2 /*return*/]; | ||
} | ||
if (!CIRCLE_PROJECT_REPONAME) { | ||
console.error("Cannot find project reponame"); | ||
return [2 /*return*/]; | ||
} | ||
if (!CIRCLE_PULL_REQUEST) { | ||
console.error("Cannot find pull request ID"); | ||
return [2 /*return*/]; | ||
} | ||
if (!CIRCLE_BUILD_NUM) { | ||
console.error("Cannot find build number"); | ||
return [2 /*return*/]; | ||
} | ||
if (!CIRCLE_TOKEN) { | ||
console.error("The environment variable CIRCLE_TOKEN must be required"); | ||
return [2 /*return*/]; | ||
} | ||
circleciApiUrl = "https://circleci.com/api/v1.1/project/github/" + CIRCLE_PROJECT_USERNAME + "/" + CIRCLE_PROJECT_REPONAME + "/" + CIRCLE_BUILD_NUM + "/artifacts?circle-token=" + CIRCLE_TOKEN; | ||
return [4 /*yield*/, get_artifact_1.getArtifactUrl(circleciApiUrl, targetPath)]; | ||
case 1: | ||
artifactUrl = _b.sent(); | ||
// Slack | ||
if (SLACK_WEBHOOK) { | ||
slack_notify_1.notifySlack(SLACK_WEBHOOK, artifactUrl); | ||
} | ||
else { | ||
console.log("Slack webhook is not set or invalid"); | ||
} | ||
// Github | ||
if (GITHUB_TOKEN) { | ||
pullRequestId = CIRCLE_PULL_REQUEST.split("/").pop(); | ||
if (!pullRequestId) { | ||
console.error("Invalid Pull Request Id"); | ||
return [2 /*return*/]; | ||
} | ||
github_notify_1.notifyGithubPr(CIRCLE_PROJECT_USERNAME, CIRCLE_PROJECT_REPONAME, pullRequestId, GITHUB_TOKEN, artifactUrl); | ||
} | ||
else { | ||
console.log("Github Token is not set or invalid"); | ||
} | ||
return [2 /*return*/]; | ||
} | ||
if (!CIRCLE_PROJECT_REPONAME) { | ||
console.error("Cannot find project reponame"); | ||
return [2 /*return*/]; | ||
} | ||
if (!CIRCLE_PULL_REQUEST) { | ||
console.error("Cannot find pull request ID"); | ||
return [2 /*return*/]; | ||
} | ||
if (!CIRCLE_BUILD_NUM) { | ||
console.error("Cannot find build number"); | ||
return [2 /*return*/]; | ||
} | ||
if (!GITHUB_TOKEN) { | ||
console.error("The environment variable GITHUB_TOKEN must be required"); | ||
return [2 /*return*/]; | ||
} | ||
if (!CIRCLE_TOKEN) { | ||
console.error("The environment variable CIRCLE_TOKEN must be required"); | ||
return [2 /*return*/]; | ||
} | ||
pullRequestId = CIRCLE_PULL_REQUEST.split("/").pop(); | ||
axios | ||
.get("https://circleci.com/api/v1.1/project/github/" + CIRCLE_PROJECT_USERNAME + "/" + CIRCLE_PROJECT_REPONAME + "/" + CIRCLE_BUILD_NUM + "/artifacts?circle-token=" + CIRCLE_TOKEN) | ||
.then(function (_a) { | ||
var data = _a.data; | ||
return data; | ||
}) | ||
.then(function (artifacts) { | ||
return artifacts.filter(function (artifact) { | ||
return artifact.path.includes(targetPath); | ||
})[0]; | ||
}) | ||
.then(function (artifact) { | ||
if (!artifact) { | ||
throw new Error("Cannot find any artifacts with: " + targetPath); | ||
} | ||
/** | ||
// Slack | ||
if (SLACK_WEBHOOK && SLACK_CHANNEL) { | ||
try { | ||
fetch(SLACK_WEBHOOK, { | ||
method: "post", | ||
body: JSON.stringify({ | ||
unfurl_links: 0, | ||
username: name, | ||
channel: channel || "", | ||
attachments: [ | ||
{ | ||
title: CIRCLE_PULL_REQUEST, | ||
title_link: CIRCLE_PULL_REQUEST || null, | ||
text: `Storybook can be viewed here:\n${artifact.url}`, | ||
ts: new Date().getTime() / 1000 | ||
} | ||
] | ||
}) | ||
}); | ||
} catch (e) { | ||
console.log("error:", e); | ||
} | ||
} else { | ||
console.log("slack webhook or slack channel is not set"); | ||
} | ||
**/ | ||
var octokit = new Octokit({ auth: "token " + GITHUB_TOKEN }); | ||
return octokit.issues.createComment({ | ||
owner: CIRCLE_PROJECT_USERNAME, | ||
repo: CIRCLE_PROJECT_REPONAME, | ||
number: pullRequestId, | ||
body: "Storybook can be viewed here:\n" + artifact.url | ||
}); | ||
}) | ||
.then(console.log) | ||
.catch(console.error); | ||
return [2 /*return*/]; | ||
}); | ||
}); }; |
{ | ||
"name": "artifact-pr-notify", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "tool to notify artifact to pr", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
101
src/index.ts
@@ -1,3 +0,4 @@ | ||
const axios = require("axios"); | ||
const Octokit = require("@octokit/rest"); | ||
import { notifySlack } from "./slack-notify"; | ||
import { getArtifactUrl } from "./get-artifact"; | ||
import { notifyGithubPr } from "./github-notify"; | ||
@@ -14,7 +15,8 @@ // used for local test | ||
CIRCLE_PROJECT_USERNAME, | ||
CIRCLE_PROJECT_REPONAME | ||
// SLACK_WEBHOOK, | ||
// SLACK_CHANNEL | ||
CIRCLE_PROJECT_REPONAME, | ||
SLACK_WEBHOOK | ||
} = process.env; | ||
// Validation | ||
if (!CIRCLE_PROJECT_USERNAME) { | ||
@@ -34,2 +36,3 @@ console.error("Cannot find project username"); | ||
} | ||
if (!CIRCLE_BUILD_NUM) { | ||
@@ -39,6 +42,3 @@ console.error("Cannot find build number"); | ||
} | ||
if (!GITHUB_TOKEN) { | ||
console.error("The environment variable GITHUB_TOKEN must be required"); | ||
return; | ||
} | ||
if (!CIRCLE_TOKEN) { | ||
@@ -49,64 +49,29 @@ console.error("The environment variable CIRCLE_TOKEN must be required"); | ||
// Github | ||
const circleciApiUrl = `https://circleci.com/api/v1.1/project/github/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/${CIRCLE_BUILD_NUM}/artifacts?circle-token=${CIRCLE_TOKEN}`; | ||
const artifactUrl = await getArtifactUrl(circleciApiUrl, targetPath); | ||
const pullRequestId = CIRCLE_PULL_REQUEST.split("/").pop(); | ||
// Slack | ||
if (SLACK_WEBHOOK) { | ||
notifySlack(SLACK_WEBHOOK, artifactUrl); | ||
} else { | ||
console.log("Slack webhook is not set or invalid"); | ||
} | ||
interface AxiosResponse { | ||
data: string[]; | ||
// Github | ||
if (GITHUB_TOKEN) { | ||
const pullRequestId = CIRCLE_PULL_REQUEST.split("/").pop(); | ||
if (!pullRequestId) { | ||
console.error("Invalid Pull Request Id"); | ||
return; | ||
} | ||
notifyGithubPr( | ||
CIRCLE_PROJECT_USERNAME, | ||
CIRCLE_PROJECT_REPONAME, | ||
pullRequestId, | ||
GITHUB_TOKEN, | ||
artifactUrl | ||
); | ||
} else { | ||
console.log("Github Token is not set or invalid"); | ||
} | ||
axios | ||
.get( | ||
`https://circleci.com/api/v1.1/project/github/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/${CIRCLE_BUILD_NUM}/artifacts?circle-token=${CIRCLE_TOKEN}` | ||
) | ||
.then(({ data }: AxiosResponse) => data) | ||
.then( | ||
(artifacts: Array<any>) => | ||
artifacts.filter((artifact: any) => | ||
artifact.path.includes(targetPath) | ||
)[0] | ||
) | ||
.then((artifact: any) => { | ||
if (!artifact) { | ||
throw new Error(`Cannot find any artifacts with: ${targetPath}`); | ||
} | ||
/** | ||
// Slack | ||
if (SLACK_WEBHOOK && SLACK_CHANNEL) { | ||
try { | ||
fetch(SLACK_WEBHOOK, { | ||
method: "post", | ||
body: JSON.stringify({ | ||
unfurl_links: 0, | ||
username: name, | ||
channel: channel || "", | ||
attachments: [ | ||
{ | ||
title: CIRCLE_PULL_REQUEST, | ||
title_link: CIRCLE_PULL_REQUEST || null, | ||
text: `Storybook can be viewed here:\n${artifact.url}`, | ||
ts: new Date().getTime() / 1000 | ||
} | ||
] | ||
}) | ||
}); | ||
} catch (e) { | ||
console.log("error:", e); | ||
} | ||
} else { | ||
console.log("slack webhook or slack channel is not set"); | ||
} | ||
**/ | ||
const octokit = new Octokit({ auth: `token ${GITHUB_TOKEN}` }); | ||
return octokit.issues.createComment({ | ||
owner: CIRCLE_PROJECT_USERNAME, | ||
repo: CIRCLE_PROJECT_REPONAME, | ||
number: pullRequestId, | ||
body: `Artifact can be viewed here:\n${artifact.url}` | ||
}); | ||
}) | ||
.then(console.log) | ||
.catch(console.error); | ||
}; |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
47417
17
424
1