refactor: use gitdata API to push tree information (#231)

* refactor: use gitdata API to push tree information

* chore: fix build

* chore: fix build

* fix: s/contents/content

* fix: use correct tree sha

* fix: use show not log

* fix: branches are refs/heads

* chore: tmp

* fix: use trop bot user id

* fix: use co-authored-by to bypass signing requirement

* fix: strip co-authored details

* fix: strip git raw better
This commit is contained in:
Samuel Attard 2022-09-29 04:16:02 -07:00 коммит произвёл GitHub
Родитель 8884710795
Коммит ded7c687a0
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 108 добавлений и 10 удалений

Просмотреть файл

@ -1,5 +1,2 @@
# Configuration for trop
# the name and email that will be used to open the backport
tropName: Electron Bot
tropEmail: electron@github.com
tropName: trop
tropEmail: 37223003+trop[bot]@users.noreply.github.com

Просмотреть файл

@ -1,5 +1,5 @@
import { Octokit } from '@octokit/rest';
import { Context } from 'probot';
import { Context, GitHubAPI } from 'probot';
import { BackportPurpose } from './enums';
export interface RemotesOptions {
@ -23,10 +23,11 @@ export interface BackportOptions {
tempBranch: string;
patches: string[];
shouldPush: boolean;
github: GitHubAPI;
}
export interface TryBackportOptions {
context?: Context;
context: Context;
repoAccessToken: string;
purpose: BackportPurpose;
pr: Octokit.PullsGetResponse;

Просмотреть файл

@ -1,9 +1,21 @@
import * as fs from 'fs-extra';
import * as path from 'path';
import * as simpleGit from 'simple-git/promise';
import { BackportOptions } from '../interfaces';
import { log } from '../utils/log-util';
import { LogLevel } from '../enums';
const cleanRawGitString = (s: string) => {
let nS = s.trim();
if (nS.startsWith(`'`)) {
nS = nS.slice(1).trim();
}
if (nS.endsWith(`'`)) {
nS = nS.slice(0, nS.length - 1).trim();
}
return nS;
};
/**
* Runs the git commands to apply backports in a series of cherry-picked commits.
*
@ -66,7 +78,6 @@ export const backportCommitsToBranch = async (options: BackportOptions) => {
try {
await fs.writeFile(patchPath, patch, 'utf8');
await git.raw(['am', '-3', patchPath]);
await fs.remove(patchPath);
} catch (error) {
log(
'backportCommitsToBranch',
@ -76,13 +87,99 @@ export const backportCommitsToBranch = async (options: BackportOptions) => {
);
return false;
} finally {
if (await fs.pathExists(patchPath)) {
await fs.remove(patchPath);
}
}
}
// Push the commit to the target branch on the remote.
if (options.shouldPush) {
await git.push(options.targetRemote, options.tempBranch, {
'--set-upstream': null,
const appliedCommits = await git.log({
from: `target_repo/${options.targetBranch}`,
to: options.tempBranch,
});
let baseCommitSha = await git.revparse([
`target_repo/${options.targetBranch}`,
]);
const baseTree = await options.github.git.getCommit({
owner: 'electron',
repo: 'electron',
commit_sha: baseCommitSha,
});
let baseTreeSha = baseTree.data.sha;
for (const commit of [...appliedCommits.all].reverse()) {
const rawDiffTree = await git.raw([
'diff-tree',
'--no-commit-id',
'--name-only',
'-r',
commit.hash,
]);
const changedFiles = rawDiffTree
.trim()
.split('\n')
.map((s) => s.trim());
await git.checkout(commit.hash);
const newTree = await options.github.git.createTree({
base_tree: baseTreeSha,
owner: 'electron',
repo: 'electron',
tree: await Promise.all(
changedFiles.map(async (changedFile) => {
const onDiskPath = path.resolve(options.dir, changedFile);
if (!(await fs.pathExists(onDiskPath))) {
return <const>{
path: changedFile,
mode: <const>'100644',
type: 'blob',
sha: null as any,
};
}
const fileContents = await fs.readFile(onDiskPath, 'utf-8');
const stat = await fs.stat(onDiskPath);
return <const>{
path: changedFile,
mode: stat.mode === 33188 ? '100644' : '100755',
type: 'blob',
content: fileContents,
};
}),
),
});
const authorEmail = cleanRawGitString(
await git.raw(['show', '-s', "--format='%ae'", commit.hash]),
);
const authorName = cleanRawGitString(
await git.raw(['show', '-s', "--format='%an'", commit.hash]),
);
const commitMessage = cleanRawGitString(
await git.raw(['show', '-s', "--format='%B'", commit.hash]),
);
const newMessage = `${commitMessage}\n\nCo-authored-by: ${authorName} <${authorEmail}>`;
const newCommit = await options.github.git.createCommit({
owner: 'electron',
repo: 'electron',
parents: [baseCommitSha],
tree: newTree.data.sha,
message: newMessage,
});
baseTreeSha = newTree.data.sha;
baseCommitSha = newCommit.data.sha;
}
await options.github.git.createRef({
owner: 'electron',
repo: 'electron',
sha: baseCommitSha,
ref: `refs/heads/${options.tempBranch}`,
});
}

Просмотреть файл

@ -166,6 +166,7 @@ and must be performed manually.',
patches,
targetRemote: 'target_repo',
shouldPush: opts.purpose === BackportPurpose.ExecuteBackport,
github: context.github,
});
if (success) {
@ -214,6 +215,7 @@ const tryBackportSquashCommit = async (opts: TryBackportOptions) => {
patches: [patch],
targetRemote: 'target_repo',
shouldPush: opts.purpose === BackportPurpose.ExecuteBackport,
github: opts.context.github,
});
if (success) {
@ -487,6 +489,7 @@ export const backportImpl = async (
if (!success) {
const end = backportViaSquashHisto.startTimer();
success = await tryBackportSquashCommit({
context,
repoAccessToken,
purpose,
pr,