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

feat(cli): Create pull request via Github api and access token. #2838

Merged
merged 2 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions packages/cli/src/cli/github/github.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
import { Env, LogType } from '@basemaps/shared';
import { execFileSync } from 'child_process';
import { Octokit } from '@octokit/core';
import { Api } from '@octokit/plugin-rest-endpoint-methods/dist-types/types.js';
import { restEndpointMethods } from '@octokit/plugin-rest-endpoint-methods';

export class Github {
repo: string;
org: string;
logger: LogType;
repoName: string;
octokit: Api;

constructor(repo: string, logger: LogType) {
this.repo = repo;
this.logger = logger;
const [org, repoName] = repo.split('/');
if (org == null || repoName == null) throw new Error(`Badly formatted repo name: ${repo}`);
this.repoName = repoName;

const token = Env.get(Env.GitHubToken);
if (token == null) throw new Error('Please set up github token environment variable.');
this.octokit = restEndpointMethods(new Octokit({ auth: token }));
}

isOk = (s: number): boolean => s >= 200 && s <= 299;

/**
* Clone the repository
*
Expand Down Expand Up @@ -72,4 +83,24 @@ export class Github {
this.logger.info({ repository: this.repo }, 'GitHub: Push');
execFileSync('git', ['push', 'origin', 'HEAD'], { cwd: this.repoName }).toString().trim();
}

/**
* Create pull request
* This needs to use github API to create Pull request by access token.
*
*/
async createPullRequests(branch: string, title: string, draft: boolean): Promise<number> {
// Create pull request from the give head
const response = await this.octokit.rest.pulls.create({
owner: this.org,
repo: this.repoName,
title,
head: branch,
base: 'master',
draft,
});
if (!this.isOk(response.status)) throw new Error('Failed to create pull request.');
this.logger.info({ branch, url: response.data.html_url }, 'GitHub: Create Pull Request');
return response.data.number;
}
}
2 changes: 2 additions & 0 deletions packages/cli/src/cli/github/make.cog.pr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export class MakeCogGithub extends Github {
const message = `config(raster): Add imagery ${this.imagery} to ${filename} config file.`;
this.commit(message);
this.push();
await this.createPullRequests(branch, message, false);
}

/**
Expand Down Expand Up @@ -143,6 +144,7 @@ export class MakeCogGithub extends Github {
const message = `config(vector): Update the ${this.imagery} to ${filename} config file.`;
this.commit(message);
this.push();
await this.createPullRequests(branch, message, false);
}

/**
Expand Down