Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch necessary git refs #162

Merged
merged 5 commits into from
Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .github/workflows/backport.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ jobs:
)
steps:
- uses: actions/checkout@v2
with:
# Required to find all branches
fetch-depth: 0
- name: Create backport PRs
uses: zeebe-io/backport-action@master
with:
Expand Down
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ jobs:
if: github.event.pull_request.merged
steps:
- uses: actions/checkout@v2
with:
# Required to find all branches
fetch-depth: 0
- name: Create backport PRs
uses: zeebe-io/backport-action@master
with:
Expand Down Expand Up @@ -107,9 +104,6 @@ jobs:
)
steps:
- uses: actions/checkout@v2
with:
# Required to find all branches
fetch-depth: 0
- name: Create backport PRs
uses: zeebe-io/backport-action@master
with:
Expand Down
34 changes: 33 additions & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions src/backport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ export class Backport {
`Detected labels on PR: ${labels.map((label) => label.name)}`
);

if (!someLabelIn(labels).matches(this.config.labels.pattern)) {
console.log(
`Nothing to backport: none of the labels match the backport pattern '${this.config.labels.pattern.source}'`
);
return; // nothing left to do here
}

await git.fetch(`refs/pull/${pull_number}/head`, this.config.pwd);

for (const label of labels) {
console.log(`Working on label ${label.name}`);

Expand All @@ -86,6 +95,8 @@ export class Backport {
const target = match[1];
console.log(`Found target in label: ${target}`);

await git.fetch(target, this.config.pwd);

try {
const branchname = `backport-${pull_number}-to-${target}`;

Expand Down Expand Up @@ -289,3 +300,17 @@ export class Backport {
return dedent`Successfully created backport PR #${pr_number} for \`${target}\`.`;
}
}

/**
* Helper method for label arrays to check that it matches a particular pattern
*
* @param labels an array of labels
* @returns a 'curried' function to easily test for a matching a label
*/
function someLabelIn(labels: { name: string }[]): {
matches: (pattern: RegExp) => boolean;
} {
return {
matches: (pattern) => labels.some((l) => pattern.test(l.name)),
};
}
15 changes: 15 additions & 0 deletions src/git.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import execa from "execa";

/**
* Fetches a ref from origin
*
* @param ref the sha, branchname, etc to fetch
* @param pwd the root of the git repository
*/
export async function fetch(ref: string, pwd: string) {
const { exitCode } = await git("fetch", ["origin", ref], pwd);
if (exitCode !== 0) {
throw new Error(
`'git fetch origin ${ref}' failed with exit code ${exitCode}`
);
}
}

/**
* Performs the backport
*
Expand Down
13 changes: 13 additions & 0 deletions src/test/backport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ describe("the backport action", () => {
await backport.run();
expect(mockedDefaultGithub.createComment).toHaveBeenCalledTimes(0);
});

it("does not fetch the pull request's head", async () => {
await backport.run();
expect(mockedGit.fetch).toHaveBeenCalledTimes(0);
});
});

describe("given a payload for a PR with backport label", () => {
Expand Down Expand Up @@ -89,6 +94,14 @@ describe("the backport action", () => {
beforeEach(() => {
mockedGit.performBackport.mockResolvedValue(0);
});
it("fetches the pull request's head", async () => {
await backport.run();
expect(mockedGit.fetch).toHaveBeenCalledWith(
"refs/pull/1347/head",
config.pwd
);
expect(mockedGit.fetch).toHaveBeenCalledWith("stable/0.25", config.pwd);
});
it("pushes the commits to origin", async () => {
mockedGit.push.mockResolvedValue(0);
await backport.run();
Expand Down