Skip to content
This repository has been archived by the owner on Apr 7, 2022. It is now read-only.

Commit

Permalink
Add dropdown to view a previous commit on a PR
Browse files Browse the repository at this point in the history
This lets you see previous commit results as they were shown on the HUD when that commit was the tip via a dropdown of commit hashes. This is a step toward fixing #103 but not completely there since there's no way to see re-runs on the same commit yet.
  • Loading branch information
driazati committed Nov 5, 2021
1 parent 47ea70a commit 1be86e8
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 8 deletions.
113 changes: 106 additions & 7 deletions src/PrDisplay.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Editor from "@monaco-editor/react";
import ReactMarkdown from "react-markdown";
import { filterLog, registerLogLanguage } from "./pr/logs.js";
import CircleCICard from "./pr/CircleCICard.js";

import Spin from "./Spin.js";
import { BsFillCaretRightFill, BsFillCaretDownFill } from "react-icons/bs";

import { GoPrimitiveDot, GoCircleSlash, GoCheck, GoX } from "react-icons/go";
Expand All @@ -30,6 +30,24 @@ function isOnDevelopmentHost() {
);
}

function getCommitsForPrQuery(user, repo, number) {
return `
{
repository(name: "${repo}", owner: "${user}") {
pullRequest(number: ${number}) {
commits(last: 100) {
nodes {
commit {
oid
}
}
}
}
}
}
`;
}

function getPrQuery(user, repo, number) {
return `
{
Expand All @@ -50,6 +68,8 @@ function getPrQuery(user, repo, number) {
commits(last: 1) {
nodes {
commit {
messageHeadline
oid
status {
contexts {
description
Expand Down Expand Up @@ -159,7 +179,7 @@ export default class PrDisplay extends Component {
}

componentDidMount() {
this.update().catch((error) => {
this.update({}).catch((error) => {
console.error(error);
this.setState({ error_message: error.toString() });
});
Expand Down Expand Up @@ -208,7 +228,7 @@ export default class PrDisplay extends Component {
}
}

async update() {
async update(params) {
// Set some global persistent state to redirect back to this window for log
// ins
localStorage.setItem("last_redirect", window.location.href);
Expand Down Expand Up @@ -237,15 +257,39 @@ export default class PrDisplay extends Component {
let commit = undefined;
if (this.isPr()) {
// Fetch the PR's info from GitHub's GraphQL API
let pr_result = await github.graphql(
getPrQuery(this.props.user, this.props.repo, this.props.pr_number)
);
pr = pr_result.repository.pullRequest;
let commitResponse = null;
if (params && params.selectedCommit) {
const selectedCommit = params.selectedCommit;
commitResponse = await github.graphql(
getCommitQuery(this.props.user, this.props.repo, selectedCommit)
);
}
let [prResult, prCommits] = await Promise.all([
github.graphql(
getPrQuery(this.props.user, this.props.repo, this.props.pr_number)
),
github.graphql(
getCommitsForPrQuery(
this.props.user,
this.props.repo,
this.props.pr_number
)
),
]);
pr = prResult.repository.pullRequest;
pr.allCommits = prCommits.repository.pullRequest.commits.nodes
.map((n) => n.commit.oid)
.reverse();
if (pr === null || pr === undefined) {
this.state.error_message = "Failed to fetch PR " + this.props.pr_number;
this.setState(this.state);
return;
}
if (commitResponse) {
pr.commits.nodes = [
{ commit: commitResponse.repository.object.history.nodes[0] },
];
}
commit = pr.commits.nodes[0].commit;
} else {
let commitResponse = await github.graphql(
Expand All @@ -259,6 +303,7 @@ export default class PrDisplay extends Component {
}
commit = commitResponse.repository.object.history.nodes[0];
}
console.log(commit);

// The GraphQL API doesn't have any types for artifacts (at least as far as
// I can tell), so we have to fall back to iterating through them all via
Expand Down Expand Up @@ -305,6 +350,7 @@ export default class PrDisplay extends Component {
commit: commit,
runs: workflow_runs,
statuses: statuses,
loadingNewCommit: false,
});

// Go through all the runs and check if there is a prefix for the workflow
Expand Down Expand Up @@ -873,6 +919,58 @@ export default class PrDisplay extends Component {
return cards;
}

renderCommitSelector() {
if (
!this.state.pr ||
!this.state.pr.allCommits ||
this.state.pr.allCommits.length === 0
) {
return null;
}
let items = [];
for (const oid of this.state.pr.allCommits) {
items.push(
<option key={`oid-${oid}`} value={oid}>
{oid.substring(0, 7)}
</option>
);
}
let loading = null;
if (this.state.loadingNewCommit) {
loading = (
<span style={{ marginLeft: "10px" }}>
<Spin text="Loading" />
</span>
);
}
console.log(this.state.commit);
return (
<div style={{ margin: "4px" }}>
<span>
Commit:{" "}
<a
href={`https://github.com/pytorch/pytorch/commit/${this.state.commit.oid}`}
>
{this.state.commit.messageHeadline}
</a>
</span>
<select
style={{
marginLeft: "10px",
borderRadius: "4px",
}}
onChange={async (e) => {
this.setState({ loadingNewCommit: true });
await this.update({ selectedCommit: e.target.value });
}}
>
{items}
</select>
{loading}
</div>
);
}

render() {
let runs = [];
let groups = {
Expand Down Expand Up @@ -1042,6 +1140,7 @@ export default class PrDisplay extends Component {
{loading}

{this.renderDocPreviewButton()}
{this.renderCommitSelector()}
{report}
<div>{displayRuns}</div>
</div>
Expand Down
10 changes: 9 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,14 @@ async function github_graphql_raw(query) {
if (result.status !== 200) {
throw new Error(`Error fetching data from GitHub: ${await result.text()}`);
}
return await result.json();

const data = await result.json();
if (data.errors) {
throw new Error(
`Error in GraphQL response: ${JSON.stringify(data.errors)}`
);
}
return data;
}

async function github_graphql(query) {
Expand Down Expand Up @@ -109,6 +116,7 @@ export async function github_raw(url) {
Authorization: "token " + pat,
},
});

return result;
}

Expand Down

0 comments on commit 1be86e8

Please sign in to comment.