Skip to content

Commit

Permalink
Revision f7c410a
Browse files Browse the repository at this point in the history
  • Loading branch information
qawolf-team committed Mar 28, 2024
1 parent bff724e commit e813e37
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 9 deletions.
87 changes: 82 additions & 5 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34173,6 +34173,7 @@ const getTagsFromEnvironment_1 = __nccwpck_require__(3743);
const createEnvironmentAction = async ({ branch, headRepoFullName, qawolfApiKey, qaWolfTeamId, pr, variables, log, baseEnvironmentId, }) => {
log.info("Creating environment for pull request...");
const environmentId = await (0, findOrCreateEnvironment_1.findOrCreateEnvironment)({
baseEnvironmentId,
branch,
log,
pr,
Expand Down Expand Up @@ -34329,7 +34330,7 @@ exports.findOrCreateEnvironment = void 0;
const tslib_1 = __nccwpck_require__(36);
const axios_1 = tslib_1.__importDefault(__nccwpck_require__(8924));
const constants_1 = __nccwpck_require__(8749);
async function findOrCreateEnvironment({ qawolfApiKey, branch, pr, qaWolfTeamId, log, }) {
async function findOrCreateEnvironment({ qawolfApiKey, branch, pr, qaWolfTeamId, log, baseEnvironmentId, }) {
const retrievalResponse = await axios_1.default.post(constants_1.qawolfGraphQLEndpoint, {
query: `
query Environments($where: EnvironmentWhereInput) {
Expand Down Expand Up @@ -34361,12 +34362,13 @@ async function findOrCreateEnvironment({ qawolfApiKey, branch, pr, qaWolfTeamId,
log.info(`Environment already exists with ID: ${environmentId}`);
return environmentId;
}
const response = await axios_1.default.post(constants_1.qawolfGraphQLEndpoint, {
const creationResponse = await axios_1.default.post(constants_1.qawolfGraphQLEndpoint, {
operationName: "createEnvironment",
query: `
mutation createEnvironment($name: String!, $teamId: String!) {
createEnvironment(name: $name, team_id: $teamId) {
id
branchId
}
}
`,
Expand All @@ -34380,11 +34382,86 @@ async function findOrCreateEnvironment({ qawolfApiKey, branch, pr, qaWolfTeamId,
"Content-Type": "application/json",
},
});
log.debug(`Environment response: ${JSON.stringify(response.data)}`);
if (!response.data.data.createEnvironment.id) {
log.debug(`Environment response: ${JSON.stringify(creationResponse.data)}`);
if (!creationResponse.data.data.createEnvironment.id) {
throw Error("Environment ID not found in response");
}
return response.data.data.createEnvironment.id;
const multiBranchResponse = await axios_1.default.post(constants_1.qawolfGraphQLEndpoint, {
query: `
query teamBranches($teamId: String!) {
teamBranches(where: { teamId: { equals: $teamId }}) {
id
environments{
id
}
}
}
`,
variables: {
teamId: qaWolfTeamId,
},
}, {
headers: {
Authorization: `Bearer ${qawolfApiKey}`,
"Content-Type": "application/json",
},
});
const hasMultipleBranches = multiBranchResponse.data.data.teamBranches.length > 1;
if (!hasMultipleBranches) {
return creationResponse.data.data.createEnvironment.id;
}
const sourceEnvironment = await axios_1.default.post(constants_1.qawolfGraphQLEndpoint, {
query: `
query EnvironmentWithBranch($where: EnvironmentWhereUniqueInput!) {
environment(where: $where) {
id
branchId
}
}
`,
variables: {
where: {
id: baseEnvironmentId,
},
},
}, {
headers: {
Authorization: `Bearer ${qawolfApiKey}`,
"Content-Type": "application/json",
},
});
const baseBranchId = sourceEnvironment.data.data.environment.branchId;
const targetBranchId = creationResponse.data.data.createEnvironment.branchId;
if (!baseBranchId) {
throw Error("Base branch ID not found in response");
}
log.info(`Promoting workflows from branch ${baseBranchId} to ${targetBranchId}`);
const promotionResponse = await axios_1.default.post(constants_1.qawolfGraphQLEndpoint, {
query: `
mutation PromoteWorkflowsToBranch(
$sourceTeamBranchId: String!
$targetTeamBranchId: String!
) {
promoteWorkflowsToBranch(
sourceTeamBranchId: $sourceTeamBranchId
targetTeamBranchId: $targetTeamBranchId
)
}
`,
variables: {
sourceTeamBranchId: baseBranchId,
targetTeamBranchId: targetBranchId,
},
}, {
headers: {
Authorization: `Bearer ${qawolfApiKey}`,
"Content-Type": "application/json",
},
});
if (!promotionResponse.data.data) {
throw new Error("Promotion failed");
}
return creationResponse.data.data.createEnvironment.id;
}
exports.findOrCreateEnvironment = findOrCreateEnvironment;

Expand Down
1 change: 1 addition & 0 deletions src/createEnvironmentAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const createEnvironmentAction = async ({
}: CreateEnvironmentActionArgs) => {
log.info("Creating environment for pull request...");
const environmentId = await findOrCreateEnvironment({
baseEnvironmentId,
branch,
log,
pr,
Expand Down
127 changes: 123 additions & 4 deletions src/findOrCreateEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { type LogHelper } from "./handleOperation";
export interface EnvironmentCreationResponse {
data: {
createEnvironment: {
branchId: string;
id: string;
};
};
Expand All @@ -20,6 +21,7 @@ export interface EnvironmentRetrievalResponse {
}

type FindOrCreateEnvironmentParams = {
baseEnvironmentId?: string;
branch: string;
log: LogHelper;
pr?: {
Expand All @@ -36,6 +38,7 @@ export async function findOrCreateEnvironment({
pr,
qaWolfTeamId,
log,
baseEnvironmentId,
}: FindOrCreateEnvironmentParams): Promise<string> {
const retrievalResponse = await axios.post<EnvironmentRetrievalResponse>(
qawolfGraphQLEndpoint,
Expand Down Expand Up @@ -74,14 +77,15 @@ export async function findOrCreateEnvironment({
return environmentId;
}

const response = await axios.post<EnvironmentCreationResponse>(
const creationResponse = await axios.post<EnvironmentCreationResponse>(
qawolfGraphQLEndpoint,
{
operationName: "createEnvironment",
query: `
mutation createEnvironment($name: String!, $teamId: String!) {
createEnvironment(name: $name, team_id: $teamId) {
id
branchId
}
}
`,
Expand All @@ -98,11 +102,126 @@ export async function findOrCreateEnvironment({
},
);

log.debug(`Environment response: ${JSON.stringify(response.data)}`);
log.debug(`Environment response: ${JSON.stringify(creationResponse.data)}`);

if (!response.data.data.createEnvironment.id) {
if (!creationResponse.data.data.createEnvironment.id) {
throw Error("Environment ID not found in response");
}

return response.data.data.createEnvironment.id;
const multiBranchResponse = await axios.post<{
data: {
teamBranches: {
environments: {
id: string;
}[];
id: string;
}[];
};
}>(
qawolfGraphQLEndpoint,
{
query: `
query teamBranches($teamId: String!) {
teamBranches(where: { teamId: { equals: $teamId }}) {
id
environments{
id
}
}
}
`,
variables: {
teamId: qaWolfTeamId,
},
},
{
headers: {
Authorization: `Bearer ${qawolfApiKey}`,
"Content-Type": "application/json",
},
},
);

const hasMultipleBranches =
multiBranchResponse.data.data.teamBranches.length > 1;

if (!hasMultipleBranches) {
return creationResponse.data.data.createEnvironment.id;
}

const sourceEnvironment = await axios.post<{
data: {
environment: {
branchId: string;
id: string;
};
};
}>(
qawolfGraphQLEndpoint,
{
query: `
query EnvironmentWithBranch($where: EnvironmentWhereUniqueInput!) {
environment(where: $where) {
id
branchId
}
}
`,
variables: {
where: {
id: baseEnvironmentId,
},
},
},
{
headers: {
Authorization: `Bearer ${qawolfApiKey}`,
"Content-Type": "application/json",
},
},
);

const baseBranchId = sourceEnvironment.data.data.environment.branchId;
const targetBranchId = creationResponse.data.data.createEnvironment.branchId;

if (!baseBranchId) {
throw Error("Base branch ID not found in response");
}

log.info(
`Promoting workflows from branch ${baseBranchId} to ${targetBranchId}`,
);

const promotionResponse = await axios.post(
qawolfGraphQLEndpoint,
{
query: `
mutation PromoteWorkflowsToBranch(
$sourceTeamBranchId: String!
$targetTeamBranchId: String!
) {
promoteWorkflowsToBranch(
sourceTeamBranchId: $sourceTeamBranchId
targetTeamBranchId: $targetTeamBranchId
)
}
`,
variables: {
sourceTeamBranchId: baseBranchId,
targetTeamBranchId: targetBranchId,
},
},
{
headers: {
Authorization: `Bearer ${qawolfApiKey}`,
"Content-Type": "application/json",
},
},
);

if (!promotionResponse.data.data) {
throw new Error("Promotion failed");
}

return creationResponse.data.data.createEnvironment.id;
}

0 comments on commit e813e37

Please sign in to comment.