Skip to content

Commit

Permalink
feat: add PR link as a resource when PR is merged
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaagrav committed Nov 28, 2022
1 parent 0c2e5cb commit 9f9829c
Show file tree
Hide file tree
Showing 11 changed files with 972 additions and 52 deletions.
868 changes: 829 additions & 39 deletions dist/index.js

Large diffs are not rendered by default.

30 changes: 23 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@actions/github": "^5.1.1",
"@vercel/ncc": "^0.34.0",
"dotenv": "^16.0.3",
"node-fetch": "^3.3.0"
"node-fetch": "^3.3.0",
"uuid": "^9.0.0"
}
}
50 changes: 50 additions & 0 deletions src/api/create-resource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { v4 as uuidv4 } from "uuid";
import fetch from "node-fetch";
import core from "@actions/core";
import dotenv from "dotenv";

dotenv.config();

const ATLAN_INSTANCE_URL =
core.getInput("ATLAN_INSTANCE_URL") || process.env.ATLAN_INSTANCE_URL;
const ATLAN_API_TOKEN =
core.getInput("ATLAN_API_TOKEN") || process.env.ATLAN_API_TOKEN;

export default async function createResource(guid, name, link) {
var myHeaders = {
Authorization: `Bearer ${ATLAN_API_TOKEN}`,
"Content-Type": "application/json",
};

var raw = JSON.stringify({
entities: [
{
typeName: "Link",
attributes: {
qualifiedName: uuidv4(),
name,
link,
tenantId: "default",
},
relationshipAttributes: {
asset: {
guid,
},
},
},
],
});

var requestOptions = {
method: "POST",
headers: myHeaders,
body: raw,
};

var response = await fetch(
`${ATLAN_INSTANCE_URL}/api/meta/entity/bulk`,
requestOptions
).then((e) => e.json());

return response;
}
1 change: 1 addition & 0 deletions src/api/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default as getDownstreamAssets } from "./get-downstream-assets.js";
export { default as getAsset } from "./get-asset.js";
export { default as createResource } from "./create-resource.js";
8 changes: 4 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import dotenv from "dotenv";
import core from "@actions/core";
import github from "@actions/github";

import { printDownstreamAssets } from "./main/index.js";
import { printDownstreamAssets, setResourceOnAsset } from "./main/index.js";

dotenv.config();

Expand All @@ -12,15 +12,15 @@ async function run() {
const { context } = github;
const octokit = github.getOctokit(GITHUB_TOKEN);
const { pull_request } = context.payload;
const { state } = pull_request;
console.log(pull_request);
const { state, merged } = pull_request;

switch (state) {
case "open":
await printDownstreamAssets({ octokit, context });
break;
case "closed":
console.log(pull_request);
if (merged) await setResourceOnAsset({ octokit, context });
break;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/main/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default as printDownstreamAssets } from "./print-downstream-assets.js";
export { default as setResourceOnAsset } from "./set-resource-on-asset.js";
2 changes: 2 additions & 0 deletions src/main/print-downstream-assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
export default async function printDownstreamAssets({ octokit, context }) {
const changedFiles = await getChangedFiles(octokit, context);

if (changedFiles.length === 0) return;

changedFiles.forEach(async ({ name, filePath }) => {
const assetName = await getAssetName(octokit, context, name, filePath);
const asset = await getAsset({ name: assetName });
Expand Down
44 changes: 44 additions & 0 deletions src/main/set-resource-on-asset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { getAsset, createResource } from "../api/index.js";
import {
createCustomComment,
getChangedFiles,
getAssetName,
} from "../utils/index.js";

export default async function setResourceOnAsset({ octokit, context }) {
const changedFiles = await getChangedFiles(octokit, context);
const { pull_request } = context.payload;

if (changedFiles.length === 0) return;

changedFiles.forEach(async ({ name, filePath }) => {
const assetName = await getAssetName(octokit, context, name, filePath);
const asset = await getAsset({ name: assetName });

if (!asset) return;

const { guid: modelGuid } = asset;
const { guid: tableAssetGuid } = asset.attributes.sqlAsset;

await createResource(
modelGuid,
"Pull Request on GitHub",
pull_request.html_url
);
await createResource(
tableAssetGuid,
"Pull Request on GitHub",
pull_request.html_url
);
});

const comment = await createCustomComment(
octokit,
context,
`🎊 Congrats on the merge!
This pull request has been added as a resource to all the assets modified. ✅
`
);
console.log(comment);
}
12 changes: 12 additions & 0 deletions src/utils/create-comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,15 @@ export default async function createComment(
if (IS_DEV) return comment;
return octokit.rest.issues.createComment(commentObj);
}

export async function createCustomComment(octokit, context, content) {
const { pull_request } = context.payload;
const commentObj = {
...context.repo,
issue_number: pull_request.number,
body: content,
};

if (IS_DEV) return content;
return octokit.rest.issues.createComment(commentObj);
}
5 changes: 4 additions & 1 deletion src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ export {
getCertificationImage,
} from "./get-image-url.js";
export { default as hostedImages } from "./hosted-images.js";
export { default as createComment } from "./create-comment.js";
export {
default as createComment,
createCustomComment,
} from "./create-comment.js";
export {
getFileContents,
getChangedFiles,
Expand Down

0 comments on commit 9f9829c

Please sign in to comment.