Skip to content

Commit

Permalink
Merge pull request gitgitgadget#1054 from webstech/update-prs
Browse files Browse the repository at this point in the history
Move updateOpenPrs code to CIHelper
  • Loading branch information
dscho authored Jul 19, 2022
2 parents 7fbe7df + 55f7794 commit 357c94e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 64 deletions.
72 changes: 72 additions & 0 deletions lib/ci-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { commitExists, git, emptyTreeName } from "./git";
import { GitNotes } from "./git-notes";
import { GitGitGadget, IGitGitGadgetOptions } from "./gitgitgadget";
import { GitHubGlue, IGitHubUser, IPRCommit, IPullRequestInfo } from "./github-glue";
import { toPrettyJSON } from "./json-util";
import { MailArchiveGitHelper } from "./mail-archive-helper";
import { MailCommitMapping } from "./mail-commit-mapping";
import { IMailMetadata } from "./mail-metadata";
Expand Down Expand Up @@ -767,6 +768,77 @@ GitGitGadget needs an email address to Cc: you on your contribution, so that you
return await mailArchiveGit.processMails(prFilter);
}

public async updateOpenPrs(): Promise<boolean> {
const options = await this.getGitGitGadgetOptions();
let optionsChanged = false;

if (!options.openPRs) {
options.openPRs = {};
optionsChanged = true;
}

if (!options.activeMessageIDs) {
options.activeMessageIDs = {};
optionsChanged = true;
}

const handledPRs = new Set<string>();
const handledMessageIDs = new Set<string>();

for (const repositoryOwner of this.config.repo.owners) {
const pullRequests = await this.github.getOpenPRs(repositoryOwner);

for (const pr of pullRequests) {
const meta = await this.getPRMetadata(pr.pullRequestURL);

if (!meta) {
console.log(`No meta found for ${pr.pullRequestURL}`);
continue;
}

const url: string = pr.pullRequestURL;
handledPRs.add(url);

if (meta.coverLetterMessageId && options.openPRs[url] === undefined) {
options.openPRs[url] = meta.coverLetterMessageId;
optionsChanged = true;
}

if (meta.baseCommit && meta.headCommit) {
for (const rev of await this.getOriginalCommitsForPR(meta)) {
const messageID = await this.notes.getLastCommitNote(rev);
handledMessageIDs.add(messageID);
if (messageID && options.activeMessageIDs[messageID] === undefined) {
options.activeMessageIDs[messageID] = rev;
optionsChanged = true;
}
}
}
}
}

for (const url in options.openPRs) {
if (!handledPRs.has(url)) {
delete options.openPRs[url];
optionsChanged = true;
}
}

for (const messageID in options.activeMessageIDs) {
if (!handledMessageIDs.has(messageID)) {
delete options.activeMessageIDs[messageID];
optionsChanged = true;
}
}

if (optionsChanged) {
console.log(`Changed options:\n${toPrettyJSON(options)}`);
await this.notes.set("", options, true);
}

return optionsChanged;
}

private async getPRInfo(prKey: pullRequestKey): Promise<IPullRequestInfo> {
const pr = await this.github.getPRInfo(prKey);

Expand Down
66 changes: 2 additions & 64 deletions script/misc-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,70 +81,8 @@ const commandOptions = commander.opts<ICommanderOptions>();
process.exit(1);
}

const gitHub = new GitHubGlue(ci.workDir, config.repo.owner, config.repo.name);

const options = await ci.getGitGitGadgetOptions();
let optionsChanged = false;
if (!options.openPRs) {
options.openPRs = {};
optionsChanged = true;
}
if (!options.activeMessageIDs) {
options.activeMessageIDs = {};
optionsChanged = true;
}

const handledPRs = new Set<string>();
const handledMessageIDs = new Set<string>();
for (const repositoryOwner of config.repo.owners) {
const pullRequests = await gitHub.getOpenPRs(repositoryOwner);
for (const pr of pullRequests) {
const meta = await ci.getPRMetadata(pr.pullRequestURL);
if (!meta) {
console.log(`No meta found for ${pr.pullRequestURL}`);
continue;
}

const url: string = pr.pullRequestURL;
handledPRs.add(url);
if (meta.coverLetterMessageId &&
options.openPRs[url] === undefined) {
options.openPRs[url] = meta.coverLetterMessageId;
optionsChanged = true;
}

if (meta.baseCommit && meta.headCommit) {
for (const rev of await ci.getOriginalCommitsForPR(meta)) {
const messageID = await ci.notes.getLastCommitNote(rev);
handledMessageIDs.add(messageID);
if (messageID &&
options.activeMessageIDs[messageID] === undefined) {
options.activeMessageIDs[messageID] = rev;
optionsChanged = true;
}
}
}
}
}

for (const url in options.openPRs) {
if (!handledPRs.has(url)) {
delete options.openPRs[url];
optionsChanged = true;
}
}

for (const messageID in options.activeMessageIDs) {
if (!handledMessageIDs.has(messageID)) {
delete options.activeMessageIDs[messageID];
optionsChanged = true;
}
}

if (optionsChanged) {
console.log(`Changed options:\n${toPrettyJSON(options)}`);
await ci.notes.set("", options, true);
}
const result = await ci.updateOpenPrs();
console.log(`Updated notes: ${result}`);
} else if (command === "update-commit-mappings") {
if (commander.args.length !== 1) {
process.stderr.write(`${command}: does not accept arguments\n`);
Expand Down

0 comments on commit 357c94e

Please sign in to comment.