Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server Simple support to sync users from SG over to Ayon #76

Merged
merged 9 commits into from
Dec 2, 2024
1 change: 1 addition & 0 deletions frontend/dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<body>
<h1>ShotGrid Sync</h1>
<button id="sync-users-button" class="sync-users-button" onclick="syncUsers();">Sync Users</button>
<p>
From this page you can Import projects from ShotGrid or trigger a project Sync!<br>
In order for the Synchronization to work, a <b><code>ShotgridProcessor</code> <a target="_parent" href="/services">service</a> must be running</b>, this page will only create <a target="_parent" href="/events"> events (with the topic: "shotgrid.event.project.sync") </a> for the processor to handle.
Expand Down
138 changes: 137 additions & 1 deletion frontend/dist/shotgrid-addon.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,142 @@ const populateTable = async () => {
}


const syncUsers = async () => {
/* Get all the Users from AYON and Shotgrid, then populate the table with their info
and a button to Synchronize if they pass the requirements */
ayonUsers = await getAyonUsers();
sgUsers = await getShotgridUsers();

sgUsers.forEach((sg_user) => {
let already_exists = false
ayonUsers.forEach((user) => {
if (sg_user.login == user.name) {
already_exists = true
}
})
if (!already_exists) {
createNewUserInAyon(sg_user.login, sg_user.email, sg_user.name)
}
})
}


const getShotgridUsers = async () => {
/* Query Shotgrid for all active users. */
const sgBaseUrl = `${addonSettings.shotgrid_server.replace(/\/+$/, '')}/api/v1`
sgAuthToken = await axios
.post(`${sgBaseUrl}/auth/access_token`, {
client_id: addonSettings.shotgrid_script_name,
jakubjezek001 marked this conversation as resolved.
Show resolved Hide resolved
client_secret: addonSettings.shotgrid_api_key,
grant_type: "client_credentials",
}, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json'
}
})
.then((result) => result.data.access_token)
.catch((error) => {
console.log("Unable to Acquire the Shotgrid Authorization Token!")
console.log(error)
});

sgUsers = await axios
.get(`${sgBaseUrl}/entity/human_users?fields=*`, {
headers: {
'Authorization': `Bearer ${sgAuthToken}`,
'Accept': 'application/json'
}
})
.then((result) => result.data.data)
.catch((error) => {
console.log("Unable to Fetch Shotgrid Users!")
console.log(error)
});

var sgUsersConformed = []

users_to_ignore = ["dummy", "root", "support"]
if (sgUsers) {
sgUsers.forEach((sg_user) => {
if (
sg_user.attributes.sg_status_list == "act" &&
!users_to_ignore.some(item => sg_user.attributes.email.includes(item))
) {
sgUsersConformed.push({
"login": sg_user.attributes.login,
"name": sg_user.attributes.name,
"email": sg_user.attributes.email,
})
}
});
}
return sgUsersConformed;
}


const getAyonUsers = async () => {
/* Query AYON for all existing users. */
ayonUsers = await axios({
url: '/graphql',
headers: {"Authorization": `Bearer ${accessToken}`},
method: 'post',
data: {
query: `
query ActiveUsers {
users {
edges {
node {
attrib {
email
fullName
}
active
name
}
}
}
}
`
}
}).then((result) => result.data.data.users.edges);

var ayonUsersConformed = []

if (ayonUsers) {
ayonUsers.forEach((user) => {
ayonUsersConformed.push({
"name": user.node.name,
"email": user.node.attrib.email,
"fullName": user.node.attrib.fullName,
})
})
}
return ayonUsersConformed
}


const createNewUserInAyon = async (login, email, name) => {
call_result_paragraph = document.getElementById("call-result");

response = await ayonAPI
.put("/api/users/" + login, {
"active": true,
"attrib": {
"fullName": name,
"email": email,
},
"password": login,
})
.then((result) => result)
.catch((error) => {
console.log("Unable to create user in AYON!")
console.log(error)
call_result_paragraph.innerHTML = `Unable to create user in AYON! ${error}`
});
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems this api call is obsolete or I am not sure why is the console erroring it

image

@martastain can you have a look into this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm yeah I'm not sure, my syncs still work fine, I just synced a few more differences I had on our fork

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So some changes needed to be done so we could merge this. Please review following PR on your fork @fabiaserra fabiaserra#1. This should probably interfere with your user base since we are only normalizing users which are having email in login. Also I had removed the password and active flag since it is active by default. The original SG login was added into user.data.sg_login.



const getShotgridProjects = async () => {
/* Query Shotgrid for all existing projects. */
const sgBaseUrl = `${addonSettings.shotgrid_server.replace(/\/+$/, '')}/api/v1`
Expand Down Expand Up @@ -159,7 +295,7 @@ const getShotgridProjects = async () => {

if (sgProjects) {
sgProjects.forEach((project) => {
sgProjectsConformed.push({
sgProjectsConformed.push({
"name": project.attributes.name,
"code": project.attributes[`${addonSettings.shotgrid_project_code_field}`],
"shotgridId": project.id,
Expand Down