Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmycallin committed Apr 2, 2024
1 parent 2b8df5e commit cd603f9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 19 deletions.
52 changes: 36 additions & 16 deletions actions/ftrack-sync/ftrack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,26 @@ let _session: Session;

export function getSession() {
if (!_session) {
_session = new Session(
process.env.FTRACK_URL!,
process.env.FTRACK_LOGIN_EMAIL!,
process.env.FTRACK_API_KEY!,
{
additionalHeaders: { "ftrack-bulk": true },
autoConnectEventHub: false,
},
);
_session = createUserSession(process.env.FTRACK_LOGIN_EMAIL!, true);
}
return _session;
}

export function createUserSession(
userEmail: string,
dontStoreAsActivity = false,
) {
return new Session(
process.env.FTRACK_URL!,
userEmail,
process.env.FTRACK_API_KEY!,
{
additionalHeaders: { "ftrack-bulk": dontStoreAsActivity },
autoConnectEventHub: false,
},
);
}

export type ProductsAttribute = { key: "products"; value: string[] };
export type ReleaseNoteAttribute = { key: "release_note"; value: string };
export type InternalChangeAttribute = {
Expand Down Expand Up @@ -103,15 +110,28 @@ export async function ensureReleaseTagExists(
return tagExists;
}

export async function ensureChangeRequestExists(release: Release) {
async function getUserFromGithubUsername(username: string) {
return (
await getSession().query(
`select email from User where custom_attributes.key is 'github_user' and custom_attributes.value is ${username}`,
)
).data[0];
}

export async function ensureChangeRequestExists(
release: Release,
actor: string,
) {
const changeRequestName = `Deploy ${release.name}`;
const changeRequestExists = (
await getSession().query(
`select id from Change where name is "${changeRequestName}"`,
)
).data[0];
if (!changeRequestExists) {
const result = await getSession().create("Change", {
const user = await getUserFromGithubUsername(actor);
const userSession = createUserSession(user.email);
const result = await userSession.create("Change", {
name: changeRequestName,
parent_id: "f9497d8b-3026-46b4-96f4-a47aa9a73804",
project_id: "dc34b754-79e8-11e3-b4d0-040102b7e101",
Expand Down Expand Up @@ -140,37 +160,37 @@ export async function ensureChangeRequestExists(release: Release) {
}),
);
customAttributes.push(
getSession().create("CustomAttributeValue", {
userSession.create("CustomAttributeValue", {
configuration_id: RELEVANT_DOCUMENTS_CONFIGURATION_ID,
entity_id: changeRequestId,
value:
"https://github.com/ftrackhq/ftrack-server/releases/tag/4.13.7\nhttps://docs.google.com/document/d/1z_gbXx9vMKho80UKYEW6d5POcFrdzdngeCtxibH90qw/edit#heading=h.lmq93bry7zi4",
}),
);
customAttributes.push(
getSession().create("CustomAttributeValue", {
userSession.create("CustomAttributeValue", {
configuration_id: BACKOUT_PROCEDURE_CONFIGURATION_ID,
entity_id: changeRequestId,
value:
"https://docs.google.com/document/d/1z_gbXx9vMKho80UKYEW6d5POcFrdzdngeCtxibH90qw/edit#heading=h.notdwrv0mb75",
}),
);
customAttributes.push(
getSession().create("CustomAttributeValue", {
userSession.create("CustomAttributeValue", {
configuration_id: RISK_RATING_CONFIGURATION_ID,
entity_id: changeRequestId,
value: "low",
}),
);
customAttributes.push(
getSession().create("CustomAttributeValue", {
userSession.create("CustomAttributeValue", {
configuration_id: APPROVAL_REQUIREMENT_CONFIGURATION_ID,
entity_id: changeRequestId,
value: "peer",
}),
);
customAttributes.push(
getSession().create("CustomAttributeValue", {
userSession.create("CustomAttributeValue", {
configuration_id: IMPACT_CONFIGURATION_ID,
entity_id: changeRequestId,
value: "low",
Expand Down
10 changes: 7 additions & 3 deletions actions/ftrack-sync/pre_deploy.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { ensureReleaseTagExists, ensureChangeRequestExists } from "./ftrack.js";
import { checkEnvironment } from "./utils.js";

export async function preDeploy(repo: string, tagName: string) {
export async function preDeploy(repo: string, tagName: string, actor: string) {
const releaseTag = await ensureReleaseTagExists(repo, tagName);
await ensureChangeRequestExists(releaseTag);
await ensureChangeRequestExists(releaseTag, actor);
}

async function main() {
console.log("Github payload:\n", process.env.GITHUB_PAYLOAD);
const githubPayload = JSON.parse(process.env.GITHUB_PAYLOAD!);
await preDeploy(githubPayload.event.repository.name, githubPayload.ref_name);
await preDeploy(
githubPayload.event.repository.name,
githubPayload.ref_name,
githubPayload.actor,
);
}

if (import.meta.url === `file://${process.argv[1]}`) {
Expand Down

0 comments on commit cd603f9

Please sign in to comment.