Skip to content

Commit

Permalink
feat: enable bitbucket pipelines during setup
Browse files Browse the repository at this point in the history
  • Loading branch information
DASPRiD committed Mar 6, 2024
1 parent 1d192d9 commit 266fad8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 12 deletions.
39 changes: 33 additions & 6 deletions src/bitbucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@ type GetRepositoryResponse = {
};

export class BitBucketClient {
public constructor(private accessToken: string) {}
public constructor(
private readonly accessToken: string,
private readonly workspace: string,
private readonly repoSlug: string,
) {}

public async getRepositoryUuid(workspace: string, repoSlug: string): Promise<string> {
const response = await fetch(`${BASE_URL}/repositories/${workspace}/${repoSlug}`, {
headers: {
authorization: `Bearer ${this.accessToken}`,
public async getRepositoryUuid(): Promise<string> {
const response = await fetch(
`${BASE_URL}/repositories/${this.workspace}/${this.repoSlug}`,
{
headers: {
authorization: `Bearer ${this.accessToken}`,
},
},
});
);

if (!response.ok) {
throw new Error("Failed to fetch repository");
Expand All @@ -21,4 +28,24 @@ export class BitBucketClient {
const body = (await response.json()) as GetRepositoryResponse;
return body.uuid;
}

public async enablePipeline(): Promise<void> {
const response = await fetch(
`${BASE_URL}/repositories/${this.workspace}/${this.repoSlug}/pipelines_config`,
{
method: "PUT",
headers: {
authorization: `Bearer ${this.accessToken}`,
"content-type": "application/json",
},
body: JSON.stringify({
enabled: true,
}),
},
);

if (!response.ok) {
throw new Error("Failed to enable pipeline");
}
}
}
21 changes: 15 additions & 6 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,23 @@ const tasks = new Listr<Context>(
{
title: "Retrieve repository UUID",
task: async (context): Promise<void> => {
const bitbucket = new BitBucketClient(context.bitbucketAccessToken);
context.repositoryUuid = await bitbucket.getRepositoryUuid(
const bitbucket = new BitBucketClient(
context.bitbucketAccessToken,
context.workspace,
context.repository,
);
context.repositoryUuid = await bitbucket.getRepositoryUuid();
},
},
{
title: "Configure pipeline environments",
task: async (context): Promise<void> => {
const bitbucket = new BitBucketClient(
context.bitbucketAccessToken,
context.workspace,
context.repository,
);
await bitbucket.enablePipeline();
},
},
{
Expand Down Expand Up @@ -343,10 +355,7 @@ const tasks = new Listr<Context>(
try {
await tasks.run();

logger.log(
ListrLogLevels.COMPLETED,
"Project creation successful, you must run the pipeline manually once.",
);
logger.log(ListrLogLevels.COMPLETED, "Project creation successful.");
} catch (error) {
logger.log(ListrLogLevels.FAILED, error as string);
}

0 comments on commit 266fad8

Please sign in to comment.