Skip to content

Commit

Permalink
feat: run copybara with github token auth
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyasbhat0 committed Jul 16, 2024
1 parent 3c5d41d commit dcd6819
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 18 deletions.
14 changes: 7 additions & 7 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
name: "Copybara Action 2"
description: "Transform and move code between repositories. Start with ZERO config and 100% customizable."
author: "Romain Barissat"
name: "Action Copybara"
description: "Transform and move code between repositories"
author: "Sudoblock"

inputs:
ssh_key:
description: "SSH public key."
required: true
required: false

access_token:
description: "Personal access token with `repo` permissions. Always required on *destination*. Required on *SoT* only if the variable `sot_branch` below is left empty and to decide between **push** and **init** workflows."
required: false
required: true

sot_repo:
description: "Source repository (Source of Truth)."
Expand Down Expand Up @@ -97,7 +97,7 @@ inputs:
copybara_image:
description: "Copybara Docker image to run."
required: false
default: "olivr/copybara"
default: "shreyasbhat0/copybara"

copybara_image_tag:
description: "Copybara Docker image tag to use."
Expand All @@ -115,7 +115,7 @@ inputs:
default: "yes"

runs:
using: "node12"
using: "node20"
main: "dist/index.js"

branding:
Expand Down
6 changes: 4 additions & 2 deletions src/copybara.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export class CopyBara {
}

public async run(workflow: string, copybaraOptions: string[], ref: string | number = ""): Promise<number> {

console.log(copybaraOptions);
switch (workflow) {
case "init":
return this.exec(
Expand All @@ -32,9 +34,9 @@ export class CopyBara {
public static getConfig(workflow: string, config: CopybaraConfig): string {
this.validateConfig(config, workflow);
return copyBaraSky(
`[email protected]:${config.sot.repo}.git`,
config.sot.repo,
config.sot.branch,
`[email protected]:${config.destination.repo}.git`,
config.destination.repo,
config.destination.branch,
config.committer,
"file:///usr/src/app",
Expand Down
2 changes: 0 additions & 2 deletions src/copybaraAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ export class CopybaraAction {

async saveConfigFiles() {
core.debug("Save config files");
await hostConfig.saveSshKey(this.config.sshKey);
await hostConfig.saveAccessToken(this.config.accessToken);
await hostConfig.saveCommitter(this.config.committer);
await hostConfig.saveKnownHosts(this.config.knownHosts);
Expand All @@ -165,6 +164,5 @@ export class CopybaraAction {
}

interface CopybaraActionConfig extends CopybaraConfig {
sshKey: string;
accessToken: string;
}
4 changes: 0 additions & 4 deletions src/hostConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ export class hostConfig {
return this.save(this.gitCredentialsPath, `https://user:${accessToken}@github.com`);
}

static async saveSshKey(sshKey: string): Promise<void> {
return this.save(this.sshKeyPath, sshKey);
}

static async saveKnownHosts(knownHosts: string): Promise<void> {
return this.save(this.knownHostsPath, `${this.githubKnownHost}\n${knownHosts}`);
}
Expand Down
13 changes: 10 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ import { exit } from "./exit";

const action = new CopybaraAction({
// Credentials
sshKey: core.getInput("ssh_key", { required: true }),
accessToken: core.getInput("access_token"),

// Common config
sot: {
repo: core.getInput("sot_repo"),
repo: createAuthenticatedUrl(core.getInput("access_token"),core.getInput("sot_repo")),
branch: core.getInput("sot_branch"),
},
destination: {
repo: core.getInput("destination_repo"),
repo: createAuthenticatedUrl(core.getInput("access_token"),core.getInput("destination_repo")),
branch: core.getInput("destination_branch"),
},
committer: core.getInput("committer"),
Expand Down Expand Up @@ -57,3 +56,11 @@ if (!core.isDebug()) {
core.debug("BEWARE: Debug mode is on, this could result in this action succeeding while it didn't. Check the logs.");
action.run().then(exit);
}

function createAuthenticatedUrl(repourl:string,access_token:string): string{
const url = new URL(repourl);
url.username = 'oauth';
url.password = access_token;
return url.toString();

}

0 comments on commit dcd6819

Please sign in to comment.