Skip to content

Commit

Permalink
Show warnings about unmerged parent pull requests (#18)
Browse files Browse the repository at this point in the history
* Show dependency warning under PR body

* add feature to readme
  • Loading branch information
rikukissa authored Dec 30, 2017
1 parent e6d5967 commit 186e989
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 7 deletions.
Binary file added .github/warning.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
|<img alt="Only relevant changes visible" src="./.github/only-relevant.png" height="200px" />|
|--|

<p align="center">
<strong>Automatic warnings on sequential pull requests</strong>
</p>

|<img alt="Automatic warnings on sequential pull requests" src="./.github/warning.png" />|
|--|


## Workflow

Expand Down
68 changes: 68 additions & 0 deletions src/features/merge-warning.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { h } from "dom-chef";
import { getPullRequests } from "../api";
import {
createIdForPullRequest,
getBasePullRequestWithStackerInfo
} from "../lib/base";
import { IStackerContext } from "../lib/context";
import { getBodyTextarea } from "../lib/dom";
import { getLocation, getPullRequestURL } from "../lib/location";
import { getStackerInfo } from "../lib/prInfo";

function getTextareaStackerInfo() {
const $textarea = getBodyTextarea();
return $textarea && getStackerInfo($textarea.value);
}
const WARNING_ID = "stacker-merge-warning";
export default async function initialize(context: IStackerContext) {
const $comment = document.querySelector(".comment-body");

const location = getLocation(context.location);

const pullRequests = await getPullRequests(context.accessToken)(
location.ownerLogin,
location.repoName
);

const stackerInfo = getTextareaStackerInfo();
const basePR =
stackerInfo && getBasePullRequestWithStackerInfo(stackerInfo, pullRequests);

// Handle existing warning
const $warning = document.getElementById(WARNING_ID);
if ($warning) {
if (!basePR) {
// Clear existing warning
$warning.remove();
return;
}

if (
createIdForPullRequest(basePR) ===
$warning.getAttribute("data-stacker-pr")
) {
// Keep existing warning
return;
}
}

if (!(stackerInfo && $comment && $comment.firstElementChild)) {
return;
}

if (!basePR) {
return;
}

$comment.insertBefore(
<p id={WARNING_ID} data-stacker-pr={createIdForPullRequest(basePR)}>
⚠️ &nbsp;This pull request depends on{" "}
<strong>
<a href={getPullRequestURL(basePR)} target="_blank">
#{basePR.number} {basePR.title}
</a>
</strong>. Consider merging it before merging this one.
</p>,
$comment.firstElementChild.nextSibling
);
}
11 changes: 4 additions & 7 deletions src/features/parent-pr-select/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getBasePullRequestWithStackerInfo
} from "../../lib/base";
import { IStackerContext } from "../../lib/context";
import { getBodyTextarea } from "../../lib/dom";
import {
getLocation,
isNewPullRequestView,
Expand All @@ -22,19 +23,15 @@ function updateTextareaValue(newParent: IGithubPullRequest) {
const newStackerInfo = {
baseBranch: createIdForPullRequest(newParent)
};
const $textarea = document.querySelector(
'textarea[name="pull_request[body]"]'
) as HTMLTextAreaElement;

const $textarea = getBodyTextarea();
if ($textarea) {
$textarea.value = updateStackerInfo($textarea.value, newStackerInfo);
}
}

function getTextareaStackerInfo() {
const $textarea = document.querySelector(
'textarea[name="pull_request[body]"]'
) as HTMLTextAreaElement | null;

const $textarea = getBodyTextarea();
return $textarea && getStackerInfo($textarea.value);
}

Expand Down
2 changes: 2 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { UnauthorizedError } from "./api";
import diffSelect from "./features/diff-select";
import fadeOutUnrelatedCommits from "./features/fade-out-unrelated-commits";
import mergeWarning from "./features/merge-warning";
import parentPRSelect from "./features/parent-pr-select";
import showStackingInList from "./features/show-stacking-in-list";
import { setConfig } from "./lib/config";
Expand All @@ -17,6 +18,7 @@ async function run() {
await showStackingInList(context);
await parentPRSelect(context);
await fadeOutUnrelatedCommits(context);
await mergeWarning(context);
} catch (err) {
if (err instanceof UnauthorizedError) {
const token = window.prompt("Please enter an access token");
Expand Down
11 changes: 11 additions & 0 deletions src/lib/dom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function getBodyTextarea(): HTMLTextAreaElement | null {
return document.querySelector(
'textarea[name="pull_request[body]"]'
) as HTMLTextAreaElement | null;
}

export function getBodyTextareaValue(): string | null {
const $textarea = getBodyTextarea();

return $textarea && $textarea.value;
}
8 changes: 8 additions & 0 deletions src/lib/location.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { IGithubPullRequest } from "../api";

export interface ILocation {
ownerLogin: string;
repoName: string;
Expand Down Expand Up @@ -44,3 +46,9 @@ export function getOwner(location: Location) {
export function getRepo(location: Location) {
return getLocation(location).repoName;
}

export function getPullRequestURL(pullRequest: IGithubPullRequest) {
return `/${pullRequest.base.repo.owner.login}/${
pullRequest.base.repo.name
}/pull/${pullRequest.number}`;
}

0 comments on commit 186e989

Please sign in to comment.