Skip to content

Commit

Permalink
Remove axios
Browse files Browse the repository at this point in the history
  • Loading branch information
cdupuis committed Feb 19, 2019
1 parent c8952ca commit 0de96c6
Show file tree
Hide file tree
Showing 18 changed files with 46 additions and 73 deletions.
13 changes: 9 additions & 4 deletions lib/api-helper/goal/chooseAndSetGoals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/

import {
Configuration,
ConfigurationAware,
guid,
HandlerContext,
logger,
Expand All @@ -31,15 +33,13 @@ import {
PreferenceStore,
PreferenceStoreFactory,
} from "../../api/context/preferenceStore";
import { StatefulPushListenerInvocation } from "../../api/dsl/goalContribution";
import { EnrichGoal } from "../../api/goal/enrichGoal";
import {
Goal,
GoalWithPrecondition,
hasPreconditions,
} from "../../api/goal/Goal";
import { Goals } from "../../api/goal/Goals";
import { SdmGoalEvent } from "../../api/goal/SdmGoalEvent";
import {
SdmGoalFulfillment,
SdmGoalFulfillmentMethod,
Expand Down Expand Up @@ -90,6 +90,7 @@ export interface ChooseAndSetGoalsRules {
preferencesFactory?: PreferenceStoreFactory;

parameterPromptFactory?: ParameterPromptFactory<any>;

}

/**
Expand All @@ -110,11 +111,12 @@ export async function chooseAndSetGoals(rules: ChooseAndSetGoalsRules,
const id = repoRefResolver.repoRefFromPush(push);
const addressChannels = addressChannelsFor(push.repo, context);
const preferences = !!preferencesFactory ? preferencesFactory(parameters.context) : NoPreferenceStore;
const configuration = (context as any as ConfigurationAware).configuration;
const goalSetId = guid();

const { determinedGoals, goalsToSave } = await determineGoals(
{ projectLoader, repoRefResolver, goalSetter, implementationMapping, enrichGoal }, {
credentials, id, context, push, addressChannels, preferences, goalSetId,
credentials, id, context, push, addressChannels, preferences, goalSetId, configuration,
});

if (goalsToSave.length > 0) {
Expand All @@ -131,6 +133,7 @@ export async function chooseAndSetGoals(rules: ChooseAndSetGoalsRules,
context,
credentials,
addressChannels,
configuration,
preferences,
goalSetId,
goalSetName: determinedGoals ? determinedGoals.name : undefined,
Expand All @@ -152,6 +155,7 @@ export async function determineGoals(rules: {
credentials: ProjectOperationCredentials,
id: RemoteRepoRef,
context: HandlerContext,
configuration: Configuration,
push: PushFields.Fragment,
addressChannels: AddressChannels,
preferences?: PreferenceStore,
Expand All @@ -161,7 +165,7 @@ export async function determineGoals(rules: {
goalsToSave: SdmGoalMessage[],
}> {
const { enrichGoal, projectLoader, repoRefResolver, goalSetter, implementationMapping } = rules;
const { credentials, id, context, push, addressChannels, goalSetId, preferences } = circumstances;
const { credentials, id, context, push, addressChannels, goalSetId, preferences, configuration } = circumstances;
return projectLoader.doWithProject({
credentials,
id,
Expand All @@ -177,6 +181,7 @@ export async function determineGoals(rules: {
push,
context,
addressChannels,
configuration,
preferences: preferences || NoPreferenceStore,
};
const determinedGoals = await chooseGoalsForPushOnProject({ goalSetter }, pli);
Expand Down
7 changes: 1 addition & 6 deletions lib/api-helper/goal/executeGoal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ import { SdmGoalState } from "../../typings/types";
import { WriteToAllProgressLog } from "../log/WriteToAllProgressLog";
import { spawnLog } from "../misc/child_process";
import { toToken } from "../misc/credentials/toToken";
import { stringifyError } from "../misc/errorPrinting";
import { reportFailureInterpretation } from "../misc/reportFailureInterpretation";
import { serializeResult } from "../misc/result";
import { ProjectListenerInvokingProjectLoader } from "../project/ProjectListenerInvokingProjectLoader";
Expand Down Expand Up @@ -113,6 +112,7 @@ export async function executeGoal(rules: { projectLoader: ProjectLoader, goalExe
id,
context,
addressChannels,
configuration,
preferences,
credentials,
goal,
Expand Down Expand Up @@ -141,11 +141,6 @@ export async function executeGoal(rules: { projectLoader: ProjectLoader, goalExe
const goalResult: ExecuteGoalResult = (await prepareGoalExecutor(implementation, inProcessGoalEvent, configuration)
(prepareGoalInvocation(goalInvocation, projectListeners))
.catch(async err => {
progressLog.write("ERROR caught: " + err.message + "\n");
progressLog.write(err.stack);
progressLog.write(sprintf("Full error object: [%s]", stringifyError(err)));
await progressLog.flush();

throw new GoalExecutionError({ where: "executing goal", cause: err });
})) || Success;
if (isFailure(goalResult)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import { filteredView } from "../misc/project/filteredView";
*/
export async function createPushImpactListenerInvocation(goalInvocation: GoalInvocation,
project: GitProject): Promise<PushImpactListenerInvocation> {
const { goalEvent, credentials, id, context, addressChannels, preferences } = goalInvocation;
const { goalEvent, credentials, id, context, addressChannels, preferences, configuration } = goalInvocation;
const smartContext = teachToRespondInEventHandler(context, ...messageDestinationsFor(goalEvent.push.repo, context));

const push = goalEvent.push;
Expand All @@ -44,6 +44,7 @@ export async function createPushImpactListenerInvocation(goalInvocation: GoalInv
return {
id,
context: smartContext,
configuration,
addressChannels,
preferences,
project,
Expand Down
1 change: 1 addition & 0 deletions lib/api-helper/listener/executeAutoInspects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ function createParametersInvocation(goalInvocation: GoalInvocation,
addressChannels: goalInvocation.addressChannels,
preferences: goalInvocation.preferences,
context: goalInvocation.context,
configuration: goalInvocation.configuration,
credentials: goalInvocation.credentials,
parameters: autoInspect.parametersInstance,
};
Expand Down
1 change: 1 addition & 0 deletions lib/api-helper/listener/executeFingerprinting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export function executeFingerprinting(fingerprinters: FingerprinterRegistration[
l({
id,
context,
configuration,
credentials,
addressChannels,
preferences,
Expand Down
1 change: 1 addition & 0 deletions lib/api-helper/listener/executeVerifyEndpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export function executeVerifyEndpoint(sdm: SdmVerification): ExecuteGoal {
id: r.id,
url: endpointGoal.url,
addressChannels: r.addressChannels,
configuration: r.configuration,
preferences: r.preferences,
context: r.context,
credentials: r.credentials,
Expand Down
3 changes: 3 additions & 0 deletions lib/api-helper/machine/handlerRegistrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// tslint:disable:max-file-line-count

import {
ConfigurationAware,
editModes,
GitHubRepoRef,
GitProject,
Expand Down Expand Up @@ -341,11 +342,13 @@ export function toCommandListenerInvocation<P>(c: CommandRegistration<P>,
const addressChannels = (msg, opts) => context.messageClient.respond(msg, opts);
const promptFor = sdm.parameterPromptFactory ? sdm.parameterPromptFactory(context) : NoParameterPrompt;
const preferences = sdm.preferenceStoreFactory ? sdm.preferenceStoreFactory(context) : NoPreferenceStore;
const configuration = (context as any as ConfigurationAware).configuration;
return {
commandName: c.name,
context,
parameters,
addressChannels,
configuration,
promptFor,
preferences,
credentials,
Expand Down
47 changes: 0 additions & 47 deletions lib/api-helper/misc/errorPrinting.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export class ProjectListenerInvokingProjectLoader implements ProjectLoader {
const pli: PushListenerInvocation = {
addressChannels: this.gi.addressChannels,
preferences: this.gi.preferences,
configuration: this.gi.configuration,
context: this.gi.context,
credentials: this.gi.credentials,
id: this.gi.id,
Expand Down
16 changes: 9 additions & 7 deletions lib/api-helper/project/fileCopy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,28 @@
*/

import {
configurationValue,
DefaultHttpClientFactory,
GitCommandGitProject,
HttpMethod,
logger,
Project,
ProjectOperationCredentials,
RemoteRepoRef,
SimpleProjectEditor,
} from "@atomist/automation-client";
import axios from "axios";
import { CodeTransform } from "../../api/registration/CodeTransform";

/**
* Add the downloaded content to the given project
* @param {string} url url of the content. Must be publicly accessible
* @param {string} path
* @return {SimpleProjectEditor}
* @param url url of the content. Must be publicly accessible
* @param path
*/
export function copyFileFromUrl(url: string, path: string): CodeTransform {
return async p => {
const response = await axios.get(url);
return p.addFile(path, response.data);
const http = configurationValue("http.client.factory", DefaultHttpClientFactory);
const response = await http.create(url).exchange<string>(url, { method: HttpMethod.Get });
return p.addFile(path, response.body);
};
}

Expand Down Expand Up @@ -75,7 +77,7 @@ export function copyFiles(donorProject: Project,
fileMappings: Array<FileMapping | string>): CodeTransform {
return async p => {
for (const m of fileMappings) {
const fm = typeof m === "string" ? {donorPath: m, recipientPath: m} : m;
const fm = typeof m === "string" ? { donorPath: m, recipientPath: m } : m;
const found = await donorProject.getFile(fm.donorPath);
if (found) {
await p.addFile(fm.recipientPath, await found.getContent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export function fakeCommandListenerInvocation<P>(opts: Partial<CommandListenerIn
commandName: opts.commandName || "test",
parameters: opts.parameters,
context: fakeContext(),
configuration: {},
addressChannels: AddressNoChannels,
promptFor: NoParameterPrompt,
preferences: NoPreferenceStore,
Expand Down
1 change: 1 addition & 0 deletions lib/api-helper/testsupport/fakePush.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export function fakePush(project?: Project, pli: Partial<PushListenerInvocation>
project: project as GitProject,
context: fakeContext(),
addressChannels: AddressNoChannels,
configuration: {},
preferences: NoPreferenceStore,
credentials: { token: "fake-token" },
...pli,
Expand Down
3 changes: 3 additions & 0 deletions lib/api/context/SdmContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import {
Configuration,
HandlerContext,
ProjectOperationCredentials,
RemoteRepoRef,
Expand Down Expand Up @@ -54,6 +55,8 @@ export interface SdmContext {
*/
preferences: PreferenceStore;

configuration: Configuration;

}

/**
Expand Down
9 changes: 6 additions & 3 deletions lib/api/goal/common/Fingerprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
* limitations under the License.
*/

import { logger } from "@atomist/automation-client";
import axios from "axios";
import {
HttpMethod,
logger,
} from "@atomist/automation-client";
import { executeFingerprinting } from "../../../api-helper/listener/executeFingerprinting";
import { FingerprintListener } from "../../listener/FingerprintListener";
import { FingerprinterRegistration } from "../../registration/FingerprinterRegistration";
Expand Down Expand Up @@ -83,7 +85,8 @@ export const SendFingerprintToAtomist: FingerprintListener = fli => {
} catch (err) {
logger.error(`Unable to serialize fingerprint: %s`, err.message);
}
return axios.post(url, payload)
const http = fli.configuration.http.client.factory.create(url);
return http.exchange(url, { method: HttpMethod.Post, body: payload })
.catch(err => {
logger.error(`Failed to send fingerprint: %s`, err.message);
return Promise.reject(err);
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

4 changes: 1 addition & 3 deletions test/api-helper/listener/relevantCodeActions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/

import { InMemoryProject } from "@atomist/automation-client";
import axios from "axios";
import * as assert from "power-assert";
import { relevantCodeActions } from "../../../lib/api-helper/listener/relevantCodeActions";
import { PushImpactListenerInvocation } from "../../../lib/api/listener/PushImpactListener";
Expand All @@ -37,8 +36,7 @@ describe("relevantCodeActions", () => {
const autofixes: AutofixRegistration = {
name: "License Fix",
transform: async p => {
const license = await axios.get("https://www.apache.org/licenses/LICENSE-2.0.txt");
return p.addFile("LICENSE", license.data);
return p.addFile("LICENSE", "Some license content");
},
};
const relevant = await relevantCodeActions([autofixes], pti);
Expand Down
3 changes: 3 additions & 0 deletions test/api/mapping/predictGoals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ export function throwingPushListenerInvocation(knownBits: Partial<PushListenerIn
get preferences(): PreferenceStore {
throw new InsufficientDataError("preferences");
},
get configuration(): PreferenceStore {
throw new InsufficientDataError("configuration");
},
get credentials(): ProjectOperationCredentials {
throw new InsufficientDataError("credentials");
},
Expand Down
1 change: 1 addition & 0 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"deprecation": true,
"eofline": true,
"forin": true,
"import-blacklist": [true, "axios"],
"import-spacing": true,
"indent": {
"options": ["spaces"]
Expand Down

0 comments on commit 0de96c6

Please sign in to comment.