Skip to content
This repository was archived by the owner on Apr 13, 2020. It is now read-only.

Commit cf34c23

Browse files
evanlouiemtarng
authored andcommitted
Fixed missing Config() fallback for create-revision command (#94)
Added the missing `--personal-access-token` and `--org-name` fallback for the `create-revision` command. Flags still take precedent, but if they are not present, the values expressed in spk-config.yaml will be used.
1 parent 77aeee4 commit cf34c23

File tree

3 files changed

+37
-33
lines changed

3 files changed

+37
-33
lines changed

src/commands/service/create-revision.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import commander from "commander";
22
import { join } from "path";
3-
import { Bedrock } from "../../config";
3+
import { Bedrock, Config } from "../../config";
44
import { createPullRequest } from "../../lib/git/azure";
55
import { getCurrentBranch, getOriginUrl } from "../../lib/gitutils";
66
import { logger } from "../../logger";
@@ -38,7 +38,11 @@ export const createServiceRevisionCommandDecorator = (
3838
.option("")
3939
.action(async opts => {
4040
try {
41-
const { orgName, personalAccessToken } = opts;
41+
const { azure_devops } = Config();
42+
const {
43+
orgName = azure_devops && azure_devops.org,
44+
personalAccessToken = azure_devops && azure_devops.access_token
45+
} = opts;
4246
let { description, remoteUrl, sourceBranch, title } = opts;
4347

4448
////////////////////////////////////////////////////////////////////////

src/lib/git/azure.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { IAzureDevOpsOpts, PullRequest } from ".";
99
import { Config } from "../../config";
1010
import { logger } from "../../logger";
1111
import { azdoUrl } from "../azdoutil";
12+
import { getOriginUrl } from "../gitutils";
1213

1314
////////////////////////////////////////////////////////////////////////////////
1415
// State
@@ -49,15 +50,14 @@ const generatePRUrl = async (
4950
* Authenticates using config and credentials from global config and returns
5051
* an Azure DevOps Git API
5152
*/
52-
export const GitAPI = async (opts: IAzureDevOpsOpts = {}) => {
53+
export const GitAPI = async (opts: IAzureDevOpsOpts = {}): Promise<IGitApi> => {
5354
// Load the gitApi if it has not been initialized
5455
if (typeof gitApi === "undefined") {
5556
// Load config from opts and fallback to spk config
56-
const config = Config();
57+
const { azure_devops } = Config();
5758
const {
58-
personalAccessToken = config.azure_devops &&
59-
config.azure_devops.access_token,
60-
orgName = config.azure_devops && config.azure_devops.org
59+
personalAccessToken = azure_devops && azure_devops.access_token,
60+
orgName = azure_devops && azure_devops.org
6161
} = opts;
6262

6363
// PAT and devops URL are required
@@ -128,17 +128,15 @@ export const createPullRequest: PullRequest = async (
128128
const gitOrigin: string =
129129
originPushUrl.length > 0
130130
? originPushUrl
131-
: await promisify(exec)("git config --get remote.origin.url")
132-
.then(out => out.stdout.trim())
133-
.catch(err => {
134-
logger.error(err);
135-
logger.error(
136-
`Error parsing remote origin from git client, run 'git config --get remote.origin.url' for more information`
137-
);
138-
return Promise.reject(
139-
Error(`No remote origin found in the current git repository`)
140-
);
141-
});
131+
: await getOriginUrl().catch(err => {
132+
logger.error(err);
133+
logger.error(
134+
`Error parsing remote origin from git client, run 'git config --get remote.origin.url' for more information`
135+
);
136+
return Promise.reject(
137+
Error(`No remote origin found in the current git repository`)
138+
);
139+
});
142140

143141
// fetch all repositories associated with the personal access token
144142
const gitAPI = await GitAPI(options);

src/lib/gitutils.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { exec } from "./shell";
55
/**
66
* Gets the current working branch.
77
*/
8-
export const getCurrentBranch = async () => {
8+
export const getCurrentBranch = async (): Promise<string> => {
99
try {
1010
const branch = await exec("git", ["rev-parse", "--abbrev-ref", "HEAD"]);
1111
return branch;
@@ -23,15 +23,15 @@ export const getCurrentBranch = async () => {
2323
export const checkoutBranch = async (
2424
branchName: string,
2525
createNewBranch: boolean
26-
) => {
26+
): Promise<void> => {
2727
try {
2828
if (createNewBranch) {
2929
await exec("git", ["checkout", "-b", `${branchName}`]);
3030
} else {
3131
await exec("git", ["checkout", `${branchName}`]);
3232
}
3333
} catch (_) {
34-
throw new Error(`Unable to checkout git branch ${branchName}: ` + _);
34+
throw Error(`Unable to checkout git branch ${branchName}: ` + _);
3535
}
3636
};
3737

@@ -40,11 +40,11 @@ export const checkoutBranch = async (
4040
*
4141
* @param branchName
4242
*/
43-
export const deleteBranch = async (branchName: string) => {
43+
export const deleteBranch = async (branchName: string): Promise<void> => {
4444
try {
4545
await exec("git", ["branch", "-D", `${branchName}`]);
4646
} catch (_) {
47-
throw new Error(`Unable to delete git branch ${branchName}: ` + _);
47+
throw Error(`Unable to delete git branch ${branchName}: ` + _);
4848
}
4949
};
5050

@@ -54,12 +54,15 @@ export const deleteBranch = async (branchName: string) => {
5454
* @param directory
5555
* @param branchName
5656
*/
57-
export const commitDir = async (directory: string, branchName: string) => {
57+
export const commitDir = async (
58+
directory: string,
59+
branchName: string
60+
): Promise<void> => {
5861
try {
5962
await exec("git", ["add", `${directory}`]);
6063
await exec("git", ["commit", "-m", `Adding new service: ${branchName}`]);
6164
} catch (_) {
62-
throw new Error(
65+
throw Error(
6366
`Unable to commit changes in ${directory} to git branch ${branchName}: ` +
6467
_
6568
);
@@ -71,18 +74,18 @@ export const commitDir = async (directory: string, branchName: string) => {
7174
*
7275
* @param branchName
7376
*/
74-
export const pushBranch = async (branchName: string) => {
77+
export const pushBranch = async (branchName: string): Promise<void> => {
7578
try {
7679
await exec("git", ["push", "-u", "origin", `${branchName}`]);
7780
} catch (_) {
78-
throw new Error(`Unable to push git branch ${branchName}: ` + _);
81+
throw Error(`Unable to push git branch ${branchName}: ` + _);
7982
}
8083
};
8184

8285
/**
8386
* Gets the origin url.
8487
*/
85-
export const getOriginUrl = async () => {
88+
export const getOriginUrl = async (): Promise<string> => {
8689
try {
8790
const originUrl = await exec("git", [
8891
"config",
@@ -92,9 +95,8 @@ export const getOriginUrl = async () => {
9295
logger.debug(`Got git origin url ${originUrl}`);
9396
return originUrl;
9497
} catch (_) {
95-
throw new Error(`Unable to get git origin URL.: ` + _);
98+
throw Error(`Unable to get git origin URL.: ` + _);
9699
}
97-
return "";
98100
};
99101

100102
/**
@@ -109,7 +111,7 @@ export const getPullRequestLink = async (
109111
baseBranch: string,
110112
newBranch: string,
111113
originUrl: string
112-
) => {
114+
): Promise<string> => {
113115
try {
114116
const gitComponents = GitUrlParse(originUrl);
115117
if (gitComponents.resource.includes("dev.azure.com")) {
@@ -125,7 +127,7 @@ export const getPullRequestLink = async (
125127
return "Could not determine origin repository, or it is not a supported provider. Please check for the newly pushed branch and open a PR manually.";
126128
}
127129
} catch (_) {
128-
throw new Error(
130+
throw Error(
129131
`"Could not determine git provider, or it is not a supported type.": ` + _
130132
);
131133
}
@@ -134,7 +136,7 @@ export const getPullRequestLink = async (
134136
export const checkoutCommitPushCreatePRLink = async (
135137
newBranchName: string,
136138
directory: string
137-
) => {
139+
): Promise<void> => {
138140
try {
139141
const currentBranch = await getCurrentBranch();
140142
try {

0 commit comments

Comments
 (0)