Skip to content

Commit

Permalink
feat: basic pr opening (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomKristie committed Jul 15, 2020
1 parent 96901bf commit 2be3e59
Show file tree
Hide file tree
Showing 5 changed files with 681 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/github-handler/index.ts
@@ -1,2 +1,3 @@
export * from './fork-handler';
export {branch} from './branch-handler';
export * from './pr-handler';
57 changes: 57 additions & 0 deletions src/github-handler/pr-handler.ts
@@ -0,0 +1,57 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import {BranchDomain, Description, Logger, Octokit, RepoDomain} from '../types';

const DEFAULT_PRIMARY = 'master';

/**
* Create a GitHub PR on the upstream organization's repo
* Throws an error if the GitHub API fails
* @param {Logger} logger The logger instance
* @param {Octokit} octokit The authenticated octokit instance
* @param {RepoDomain} upstream The upstream repository
* @param {BranchDomain} origin The remote origin information that contains the origin branch
* @param {Description} description The pull request title and detailed description
* @param {boolean} maintainersCanModify Whether or not maintainers can modify the pull request. Default is true
* @param {string} upstreamPrimary The upstream repository's primary branch. Default is master.
* @returns {Promise<void>}
*/
async function openPR(
logger: Logger,
octokit: Octokit,
upstream: RepoDomain,
origin: BranchDomain,
description: Description,
maintainersCanModify = true,
upstreamPrimary: string = DEFAULT_PRIMARY
): Promise<void> {
// TODO - autosync fork, update branch, and re-apply changes
const pullResponseData = (
await octokit.pulls.create({
owner: upstream.owner,
repo: origin.repo,
title: description.title,
head: `${origin.owner}:${origin.branch}`,
base: upstreamPrimary,
body: description.body,
maintainer_can_modify: maintainersCanModify,
})
).data;
logger.info(
`Successfully opened pull request available at url: ${pullResponseData.url}.`
);
}

export {openPR};
17 changes: 17 additions & 0 deletions src/types/index.ts
Expand Up @@ -28,6 +28,21 @@ interface RepoDomain {
owner: string;
}

/**
* The domain for a branch
*/
interface BranchDomain extends RepoDomain {
branch: string;
}

/**
* The descriptive properties for any entity
*/
interface Description {
title: string;
body: string;
}

/**
* The user parameter for GitHub data needed for creating a PR
*/
Expand Down Expand Up @@ -61,6 +76,8 @@ interface GitHubContext {
}

export {
BranchDomain,
Description,
Files,
GitHubContextParam,
GitHubContext,
Expand Down

0 comments on commit 2be3e59

Please sign in to comment.