Skip to content

Commit

Permalink
Retrieve userAccountId when connecting Confluence (#3387)
Browse files Browse the repository at this point in the history
  • Loading branch information
flvndvd authored Jan 23, 2024
1 parent bf47395 commit 23b68e2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
4 changes: 4 additions & 0 deletions connectors/src/connectors/confluence/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { ConnectorsAPIErrorResponse } from "@dust-tt/types";
import { confluenceConfig } from "@connectors/connectors/confluence/lib/config";
import {
getConfluenceCloudInformation,
getConfluenceUserAccountId,
listConfluenceSpaces,
} from "@connectors/connectors/confluence/lib/confluence_api";
import type { ConfluenceSpaceType } from "@connectors/connectors/confluence/lib/confluence_client";
Expand Down Expand Up @@ -52,6 +53,8 @@ export async function createConfluenceConnector(
return new Err(new Error("Confluence access token is invalid"));
}

const userAccountId = await getConfluenceUserAccountId(confluenceAccessToken);

const { id: cloudId, url: cloudUrl } = confluenceCloudInformation;
try {
const connector = await sequelize_conn.transaction(async (transaction) => {
Expand All @@ -70,6 +73,7 @@ export async function createConfluenceConnector(
cloudId,
connectorId: connector.id,
url: cloudUrl,
userAccountId,
},
{ transaction }
);
Expand Down
8 changes: 8 additions & 0 deletions connectors/src/connectors/confluence/lib/confluence_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ export async function getConfluenceCloudInformation(accessToken: string) {
}
}

export async function getConfluenceUserAccountId(accessToken: string) {
const client = new ConfluenceClient(accessToken);

const userAccount = await client.getUserAccount();

return userAccount.account_id;
}

async function fetchConfluenceConfiguration(connectorId: ModelId) {
const confluenceConfig = await ConfluenceConfiguration.findOne({
where: {
Expand Down
11 changes: 11 additions & 0 deletions connectors/src/connectors/confluence/lib/confluence_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ const ConfluenceListPagesCodec = t.type({
}),
});

const ConfluenceUserProfileCodec = t.intersection([
t.type({
account_id: t.string,
}),
CatchAllCodec,
]);

function extractCursorFromLinks(links: { next?: string }): string | null {
if (!links.next) {
return null;
Expand Down Expand Up @@ -182,4 +189,8 @@ export class ConfluenceClient {
ConfluencePageWithBodyCodec
);
}

async getUserAccount() {
return this.request("/me", ConfluenceUserProfileCodec);
}
}
10 changes: 9 additions & 1 deletion connectors/src/lib/models/confluence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class ConfluenceConfiguration extends Model<

declare cloudId: string;
declare url: string;
declare userAccountId: string;

declare connectorId: ForeignKey<Connector["id"]>;
}
Expand Down Expand Up @@ -46,11 +47,18 @@ ConfluenceConfiguration.init(
type: DataTypes.STRING,
allowNull: false,
},
userAccountId: {
type: DataTypes.STRING,
allowNull: false,
},
},
{
sequelize: sequelize_conn,
modelName: "confluence_configurations",
indexes: [{ fields: ["connectorId"], unique: true }],
indexes: [
{ fields: ["connectorId"], unique: true },
{ fields: ["userAccountId"] },
],
}
);
Connector.hasOne(ConfluenceConfiguration);
Expand Down

0 comments on commit 23b68e2

Please sign in to comment.