Closed domantasjurkus closed 5 years ago
Thanks for raising this.
I'll simplified the code a little bit. Here is what works for me:
import {
GitCommandGitProject,
GitHubRepoRef,
} from "@atomist/automation-client";
import {
CommandHandlerRegistration,
formatDate,
} from "@atomist/sdm";
import { configure } from "@atomist/sdm-core";
const listener = async () => {
const repoRef = GitHubRepoRef.from({
owner: "sdm-org",
repo: "cd-api-01",
});
const project = await GitCommandGitProject.cloned({ token: process.env.GHE_ATOMIST_TOKEN }, repoRef);
await project.createBranch(`new-branch-${formatDate()}`);
await project.addFile(Date.now().toString(), "I was here");
await project.commit("new commit");
await project.push();
await project.raisePullRequest("new PR", "master");
};
export const TestCommand: CommandHandlerRegistration = {
name: "test",
intent: ["test"],
listener,
};
There are 2 issues I can see in your code:
GitCommandGitProject.cloned(process.env.GHE_ATOMIST_TOKEN, repoRef)
: The first argument is supposed to be of type ProjectCredentials
and in the GitHub case TokenCredentials
. So please pass the token with { token: process.env.GHE_ATOMIST_TOKEN }
project.createBranch('new-branch')
won't check if the branch exists already. If you want to re-use a potentially existing branch, you have to checkout that branch first. Or alternatively use a generated branch name like I did above.Here is a code example that achieves the same PR by using a CodeTransform
that takes the owner and repo name as parameters:
import { editModes } from "@atomist/automation-client";
import {
CodeTransform,
CodeTransformRegistration,
} from "@atomist/sdm";
import { configure } from "@atomist/sdm-core";
const transform: CodeTransform = async project => {
await project.addFile(Date.now().toString(), "I was here");
};
export const TestCommand: CodeTransformRegistration = {
name: "test",
intent: ["test"],
transform,
transformPresentation: () => new editModes.PullRequest("new PR", "master"),
};
export const configuration = configure(async sdm => {
sdm.addCodeTransformCommand(TestCommand);
});
Alternatively, if you need to hardcode the owner and repo name, you can still use the SDMs
ProjectLoader` infrastructure to load the repo. This has the added benefit that you get cleanup etc out of the box. Here is the modified example:
import { GitHubRepoRef } from "@atomist/automation-client";
import {
CommandHandlerRegistration,
CommandListener,
formatDate,
SoftwareDeliveryMachine,
} from "@atomist/sdm";
import { configure } from "@atomist/sdm-core";
function listener(sdm: SoftwareDeliveryMachine): CommandListener {
return async ci => {
const repoRef = GitHubRepoRef.from({
owner: "sdm-org",
repo: "cd-api-01",
});
await sdm.configuration.sdm.projectLoader.doWithProject({
...ci,
id: repoRef,
credentials: { token: process.env.GHE_ATOMIST_TOKEN },
readOnly: false,
}, async project => {
await project.createBranch(`new-branch-${formatDate()}`);
await project.addFile(Date.now().toString(), "I was here");
await project.commit("new commit");
await project.push();
await project.raisePullRequest("new PR", "master");
});
};
}
export function testCommand(sdm: SoftwareDeliveryMachine): CommandHandlerRegistration {
return {
name: "test",
intent: ["test"],
listener: listener(sdm),
};
}
export const configuration = configure(async sdm => {
sdm.addCommand(testCommand(sdm));
});
Please let use know if this is helpful at all.
Cheers! The branch name was the problem :)
Hi @domantasjurkus thanks for opening this; I'd also encourage you to join our community slack (atomist-community.slack.com). I'd love to hear what you're working on and we can provide some more assistance there.
Can I create an account without an @atomist.com email? Can't seem to find a way to register for the workspace.
@domantasjurkus I'll check on that right now, that should not be the case and I'll get it resolved.
@domantasjurkus Please use https://join.atomist.com to join - sorry about that.
Not sure if this is an issue with the library, but I'm unable to open PRs after cloning and and committing changes.
The code:
output: