atomist / automation-client

Automation client for running command and events handlers against the Atomist API.
GNU General Public License v3.0
15 stars 16 forks source link

Cannot open PR? #570

Closed domantasjurkus closed 5 years ago

domantasjurkus commented 5 years ago

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:

import { CommandHandlerRegistration, CommandListenerInvocation, execPromise, ExecPromiseError, ParametersDefinition, SpawnPromiseOptions } from '@atomist/sdm'
import { CloneOptions, configurationValue, GitCommandGitProject, GitHubRepoRef, GitProject,  logger} from '@atomist/automation-client'

const listener = async (ci: CommandListenerInvocation) => {
  const repoRef = GitHubRepoRef.from({
    owner: 'domantasjurkus',
    repo: 'change-me-atomist',
    rawApiBase: '...',
  })
  const project = await GitCommandGitProject.cloned(process.env.GHE_ATOMIST_TOKEN, repoRef)

  await project.createBranch('new-branch')
  await project.commit('new commit').catch(() => { console.log("error: nothing to commit")} )
  await project.push().catch(() => { console.log("error: did not push, everything up to date")} )
  await project.raisePullRequest('new PR', 'master').catch(() => { console.log("error: PR not raised")} )
}

export const TestCommand: CommandHandlerRegistration = {
  name: 'test',
  intent: ['test'],
  listener: listener
}

output:

2019-06-27T15:26:45.781Z [m:6912] [debug] Atomist automation client startup completed in 2.061s
2019-06-27T15:27:10.618Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:002] [debug] Incoming command invocation '{"command":"test","parameters":[{"name":"github://user_token?scopes=repo,user:email,read:user","value":"not.a.real.token"},{"name":"_","value":["test"]},{"name":"$0","value":"/usr/local/bin/atomist"}],"mapped_parameters":[{"name":"slackTeam","value":"local"},{"name":"github://user_token?scopes=repo,user:email,read:user","value":"not.a.real.token"},{"name":"_","value":["test"]},{"name":"$0","value":"/usr/local/bin/atomist"}],"secrets":[{"uri":"github://user_token?scopes=repo,user:email,read:user","value":"n**************n"}],"correlation_id":"atomist-cli-10000-general-1561649230603","api_version":"1","team":{"id":"local","name":"DOMAJURK02M"}}'
2019-06-27T15:27:10.619Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:003] [info ] Invoking command handler 'test'
2019-06-27T15:27:10.624Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:008] [info ] Directory info: {"path":"/var/folders/zc/kkwmmtkj3m15jzmpl__lx6tw0000gp/T/atm-6912-6912joaQNa7SKEYX","type":"empty-directory","transient":true,"provenance":"created with tmp, keep = false"}
2019-06-27T15:27:10.625Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:009] [debug] Cloning repo with owner 'domantasjurkus', name 'change-me-atomist', branch 'undefined', sha 'undefined' and options '{"keep":false,"alwaysDeep":false}'
2019-06-27T15:27:10.625Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:009] [debug] Cloning repo 'https://github.paradoxa.net/domantasjurkus/change-me-atomist.git' in '/var/folders/zc/kkwmmtkj3m15jzmpl__lx6tw0000gp/T/atm-6912-6912joaQNa7SKEYX'
2019-06-27T15:27:10.627Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:011] [debug] Running: /Users/domantasjurkus/csnit/team-pr-sdm ==> git 'clone' 'https://github.paradoxa.net/domantasjurkus/change-me-atomist.git' '/var/folders/zc/kkwmmtkj3m15jzmpl__lx6tw0000gp/T/atm-6912-6912joaQNa7SKEYX' '--depth' '1'
2019-06-27T15:27:10.632Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:016] [debug] Spawned PID 6914: /Users/domantasjurkus/csnit/team-pr-sdm ==> git 'clone' 'https://github.paradoxa.net/domantasjurkus/change-me-atomist.git' '/var/folders/zc/kkwmmtkj3m15jzmpl__lx6tw0000gp/T/atm-6912-6912joaQNa7SKEYX' '--depth' '1'
2019-06-27T15:27:11.231Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:615] [debug] Child process exit with code 0 and signal null: /Users/domantasjurkus/csnit/team-pr-sdm ==> git 'clone' 'https://github.paradoxa.net/domantasjurkus/change-me-atomist.git' '/var/folders/zc/kkwmmtkj3m15jzmpl__lx6tw0000gp/T/atm-6912-6912joaQNa7SKEYX' '--depth' '1'
2019-06-27T15:27:11.232Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:616] [debug] Child process close with code 0 and signal null: /Users/domantasjurkus/csnit/team-pr-sdm ==> git 'clone' 'https://github.paradoxa.net/domantasjurkus/change-me-atomist.git' '/var/folders/zc/kkwmmtkj3m15jzmpl__lx6tw0000gp/T/atm-6912-6912joaQNa7SKEYX' '--depth' '1'
2019-06-27T15:27:11.232Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:616] [debug] Running: /var/folders/zc/kkwmmtkj3m15jzmpl__lx6tw0000gp/T/atm-6912-6912joaQNa7SKEYX ==> git 'checkout' 'HEAD' '--'
2019-06-27T15:27:11.236Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:620] [debug] Spawned PID 6923: /var/folders/zc/kkwmmtkj3m15jzmpl__lx6tw0000gp/T/atm-6912-6912joaQNa7SKEYX ==> git 'checkout' 'HEAD' '--'
2019-06-27T15:27:11.249Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:633] [debug] Child process exit with code 0 and signal null: /var/folders/zc/kkwmmtkj3m15jzmpl__lx6tw0000gp/T/atm-6912-6912joaQNa7SKEYX ==> git 'checkout' 'HEAD' '--'
2019-06-27T15:27:11.249Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:633] [debug] Child process close with code 0 and signal null: /var/folders/zc/kkwmmtkj3m15jzmpl__lx6tw0000gp/T/atm-6912-6912joaQNa7SKEYX ==> git 'checkout' 'HEAD' '--'
2019-06-27T15:27:11.250Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:634] [debug] Clone succeeded with URL 'https://github.paradoxa.net/domantasjurkus/change-me-atomist.git'
2019-06-27T15:27:11.250Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:635] [debug] Created GitProject
2019-06-27T15:27:11.251Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:635] [debug] Running: /var/folders/zc/kkwmmtkj3m15jzmpl__lx6tw0000gp/T/atm-6912-6912joaQNa7SKEYX ==> git 'branch' 'new-branch'
2019-06-27T15:27:11.255Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:639] [debug] Spawned PID 6924: /var/folders/zc/kkwmmtkj3m15jzmpl__lx6tw0000gp/T/atm-6912-6912joaQNa7SKEYX ==> git 'branch' 'new-branch'
2019-06-27T15:27:11.264Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:648] [debug] Child process exit with code 0 and signal null: /var/folders/zc/kkwmmtkj3m15jzmpl__lx6tw0000gp/T/atm-6912-6912joaQNa7SKEYX ==> git 'branch' 'new-branch'
2019-06-27T15:27:11.264Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:648] [debug] Child process close with code 0 and signal null: /var/folders/zc/kkwmmtkj3m15jzmpl__lx6tw0000gp/T/atm-6912-6912joaQNa7SKEYX ==> git 'branch' 'new-branch'
2019-06-27T15:27:11.265Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:649] [debug] Running: /var/folders/zc/kkwmmtkj3m15jzmpl__lx6tw0000gp/T/atm-6912-6912joaQNa7SKEYX ==> git 'checkout' 'new-branch' '--'
2019-06-27T15:27:11.268Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:652] [debug] Spawned PID 6925: /var/folders/zc/kkwmmtkj3m15jzmpl__lx6tw0000gp/T/atm-6912-6912joaQNa7SKEYX ==> git 'checkout' 'new-branch' '--'
2019-06-27T15:27:11.279Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:663] [debug] Child process exit with code 0 and signal null: /var/folders/zc/kkwmmtkj3m15jzmpl__lx6tw0000gp/T/atm-6912-6912joaQNa7SKEYX ==> git 'checkout' 'new-branch' '--'
2019-06-27T15:27:11.279Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:663] [debug] Child process close with code 0 and signal null: /var/folders/zc/kkwmmtkj3m15jzmpl__lx6tw0000gp/T/atm-6912-6912joaQNa7SKEYX ==> git 'checkout' 'new-branch' '--'
2019-06-27T15:27:11.279Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:663] [debug] Running: /var/folders/zc/kkwmmtkj3m15jzmpl__lx6tw0000gp/T/atm-6912-6912joaQNa7SKEYX ==> git 'add' '.'
2019-06-27T15:27:11.284Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:668] [debug] Spawned PID 6926: /var/folders/zc/kkwmmtkj3m15jzmpl__lx6tw0000gp/T/atm-6912-6912joaQNa7SKEYX ==> git 'add' '.'
2019-06-27T15:27:11.299Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:683] [debug] Child process exit with code 0 and signal null: /var/folders/zc/kkwmmtkj3m15jzmpl__lx6tw0000gp/T/atm-6912-6912joaQNa7SKEYX ==> git 'add' '.'
2019-06-27T15:27:11.300Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:684] [debug] Child process close with code 0 and signal null: /var/folders/zc/kkwmmtkj3m15jzmpl__lx6tw0000gp/T/atm-6912-6912joaQNa7SKEYX ==> git 'add' '.'
2019-06-27T15:27:11.300Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:684] [debug] Running: /var/folders/zc/kkwmmtkj3m15jzmpl__lx6tw0000gp/T/atm-6912-6912joaQNa7SKEYX ==> git 'commit' '-m' 'new commit'
2019-06-27T15:27:11.305Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:689] [debug] Spawned PID 6927: /var/folders/zc/kkwmmtkj3m15jzmpl__lx6tw0000gp/T/atm-6912-6912joaQNa7SKEYX ==> git 'commit' '-m' 'new commit'
2019-06-27T15:27:11.315Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:699] [debug] Child process exit with code 1 and signal null: /var/folders/zc/kkwmmtkj3m15jzmpl__lx6tw0000gp/T/atm-6912-6912joaQNa7SKEYX ==> git 'commit' '-m' 'new commit'
2019-06-27T15:27:11.315Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:699] [debug] Child process close with code 1 and signal null: /var/folders/zc/kkwmmtkj3m15jzmpl__lx6tw0000gp/T/atm-6912-6912joaQNa7SKEYX ==> git 'commit' '-m' 'new commit'
2019-06-27T15:27:11.316Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:700] [error] Child process 6927 exited with non-zero status 1: /var/folders/zc/kkwmmtkj3m15jzmpl__lx6tw0000gp/T/atm-6912-6912joaQNa7SKEYX ==> git 'commit' '-m' 'new commit'

error: nothing to commit
2019-06-27T15:27:11.317Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:701] [debug] Running: /var/folders/zc/kkwmmtkj3m15jzmpl__lx6tw0000gp/T/atm-6912-6912joaQNa7SKEYX ==> git 'push' '--set-upstream' 'origin' 'new-branch'
2019-06-27T15:27:11.320Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:704] [debug] Spawned PID 6928: /var/folders/zc/kkwmmtkj3m15jzmpl__lx6tw0000gp/T/atm-6912-6912joaQNa7SKEYX ==> git 'push' '--set-upstream' 'origin' 'new-branch'
2019-06-27T15:27:11.738Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:1122] [debug] Child process exit with code 1 and signal null: /var/folders/zc/kkwmmtkj3m15jzmpl__lx6tw0000gp/T/atm-6912-6912joaQNa7SKEYX ==> git 'push' '--set-upstream' 'origin' 'new-branch'
2019-06-27T15:27:11.738Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:1122] [debug] Child process close with code 1 and signal null: /var/folders/zc/kkwmmtkj3m15jzmpl__lx6tw0000gp/T/atm-6912-6912joaQNa7SKEYX ==> git 'push' '--set-upstream' 'origin' 'new-branch'
2019-06-27T15:27:11.738Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:1122] [error] Child process 6928 exited with non-zero status 1: /var/folders/zc/kkwmmtkj3m15jzmpl__lx6tw0000gp/T/atm-6912-6912joaQNa7SKEYX ==> git 'push' '--set-upstream' 'origin' 'new-branch'
To https://github.paradoxa.net/domantasjurkus/change-me-atomist.git
 ! [rejected]        new-branch -> new-branch (fetch first)
error: failed to push some refs to 'https://github.paradoxa.net/domantasjurkus/change-me-atomist.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

2019-06-27T15:27:11.739Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:1123] [error] Unable to push 'git "push" "--set-upstream" "origin" "new-branch"': Child process 6928 exited with non-zero status 1: /var/folders/zc/kkwmmtkj3m15jzmpl__lx6tw0000gp/T/atm-6912-6912joaQNa7SKEYX ==> git 'push' '--set-upstream' 'origin' 'new-branch'
To https://github.paradoxa.net/domantasjurkus/change-me-atomist.git
 ! [rejected]        new-branch -> new-branch (fetch first)
error: failed to push some refs to 'https://github.paradoxa.net/domantasjurkus/change-me-atomist.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

error: did not push, everything up to date
2019-06-27T15:27:11.740Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:1124] [error] Error executing command 'test': Only token auth supported
2019-06-27T15:27:11.758Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:1142] [error] Error: Only token auth supported
    at headers (/Users/domantasjurkus/csnit/team-pr-sdm/node_modules/@atomist/automation-client/lib/operations/common/GitHubRepoRef.ts:139:15)
    at GitHubRepoRef.raisePullRequest (/Users/domantasjurkus/csnit/team-pr-sdm/node_modules/@atomist/automation-client/lib/operations/common/GitHubRepoRef.ts:103:24)
    at GitCommandGitProject.raisePullRequest (/Users/domantasjurkus/csnit/team-pr-sdm/node_modules/@atomist/automation-client/lib/project/git/GitCommandGitProject.ts:192:24)
    at Object.<anonymous> (/Users/domantasjurkus/csnit/team-pr-sdm/lib/team/test/test.ts:15:17)
    at Generator.next (<anonymous>)
    at fulfilled (/Users/domantasjurkus/csnit/team-pr-sdm/lib/team/test/test.js:4:58)
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:229:7)
2019-06-27T15:27:11.761Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:1145] [error] Only token auth supported
2019-06-27T15:27:11.781Z [m:6912:b4e2c49f-2393-47e7-982d-7f846c773ee1:DOMAJURK02M:test:1165] [info ] Finished invocation of command 'test': {"code":1,"correlation_id":"atomist-cli-10000-general-1561649230603","invocation_id":"b4e2c49f-2393-47e7-982d-7f846c773ee1","message":"Only token auth supported"}
cdupuis commented 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:

cdupuis commented 5 years ago

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 SDMsProjectLoader` 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.

domantasjurkus commented 5 years ago

Cheers! The branch name was the problem :)

ipcrm commented 5 years ago

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.

domantasjurkus commented 5 years ago

Can I create an account without an @atomist.com email? Can't seem to find a way to register for the workspace.

ipcrm commented 5 years ago

@domantasjurkus I'll check on that right now, that should not be the case and I'll get it resolved.

ipcrm commented 5 years ago

@domantasjurkus Please use https://join.atomist.com to join - sorry about that.