Skip to content

Commit

Permalink
Merge pull request #23 from argoproj-labs/marcb/handle-app-diff
Browse files Browse the repository at this point in the history
WIP- adding api to handle app diff with specific name
  • Loading branch information
marcb1 authored Feb 10, 2020
2 parents bd879ae + 05671cb commit fa7f003
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/argo-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ export class ArgoAPI {
return responseJson;
}

public async fetchDirForAppWithName(appName) {
const url = "http://" + this.serverIP + "/api/v1/applications/" + appName;
const responseJson = await this.fetchHttp(url, this.token);

if (!responseJson.hasOwnProperty("metadata") || !responseJson.hasOwnProperty("spec") || !responseJson["spec"].hasOwnProperty("source") ||
!responseJson["spec"]["source"].hasOwnProperty("path")) {
return {};
}
return responseJson["spec"]["source"]["path"];
}

private async fetchAllApplications() {
const url = "http://" + this.serverIP + "/api/v1/applications?fields=" + this.fetchAllAppsFilter;
const responseJson = await this.fetchHttp(url, this.token);
Expand Down
9 changes: 8 additions & 1 deletion src/argo-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const BotCommand = "argo";
// supported actions, must be prefixed by BotCommand for example 'argo unlock'
const BotActions = Object.freeze({Unlock: "unlock", Diff: "diff", Sync: "sync", Preview: "preview", Info: "info", History: "history", Rollback: "rollback", Help: "help"});
// supported diff flags
const BotDiffActions = Object.freeze({AutoSync: "--auto-sync", All: "--all", Dir: "--dir", DirShort: "-d"});
const BotDiffActions = Object.freeze({AutoSync: "--auto-sync", All: "--all", Dir: "--dir", DirShort: "-d", App: "--app", AppShort: "-a"});

// help string for actions
const diffHelp = `
Expand All @@ -20,6 +20,8 @@ supported flags:
--auto-sync: diffs all apps with auto sync enabled against yaml files/helm charts on current branch
--dir [dir name] or -d [dir name]: diffs all yaml files/helm charts in a specific directory against what's deployed in GKE
will look in subdirectories i.e 'argo diff -d /abc' will also check attempt to diff all deployments in 'abc' dir
--app [app name] or -a [app name]: diffs all yaml manifests/helm charts for a specific app against what's deployed in GKE
will look in subdirectories where argo app is deployed i.e 'argo app diff -a app1' will diff against git repo where app1 manifests reside
--all: diffs all apps on current branch against what's deployed in GKE (default behavior)`;

const BotHelp = Object.freeze({Diff: diffHelp,
Expand Down Expand Up @@ -162,6 +164,11 @@ export class ArgoBot {
this.appContext.log("Received diff command with" + BotDiffActions.Dir);
jsonResponse = await this.argoAPI.fetchAppsWithDirectory(arr[3]);
return await this.handleDiff(jsonResponse);
} else if (arr[2] && (arr[2] === BotDiffActions.App || arr[2] === BotDiffActions.AppShort) && arr[3]) {
this.appContext.log("Received diff command with" + BotDiffActions.App);
const appDir = await this.argoAPI.fetchDirForAppWithName(arr[3]);
jsonResponse = {items: [{metadata: { name: arr[3]}, spec: { source: { path: appDir } } }] };
return await this.handleDiff(jsonResponse);
} else if (arr[2]) {
// if arr[2] is not empty, then it's not a valid diff arg, notify user
this.appContext.log("Received unsupported diff command");
Expand Down
34 changes: 34 additions & 0 deletions test/argo.diff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,4 +306,38 @@ describe("argo-cd-bot", () => {

await probot.receive({name: "issue_comment", payload: autoSyncPayload})
})

test("diff comment posted on PR with --app flag", async() => {
nock("https://api.github.com")
.post("/app/installations/2/access_tokens")
.reply(200, {token: "test"})

// test constants
const branch = "newBranch"
const appDiff = "===== App Diff ===="
const appName = "app1"
const appDir = "projects/app1"

nock("https://api.github.com").get("/repos/robotland/test/pulls").reply(200, {"data": {"number": 109, "head": { "ref": branch}}})
nock("https://api.github.com").get("/repos/robotland/test/pulls").reply(200, {"data": {"number": 109, "head": { "ref": branch, "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e", "repo": { "id": 1296269, "node_id": "MDEwOlJlcG9zaXRvcnkxMjk2MjY5", "name": "Hello-World", "full_name": "octocat/Hello-World", "owner": { "login": "octocat" }}}}});
// bot should post status check on PR
nock("https://api.github.com").post("/repos/octocat/Hello-World/statuses/6dcb09b5b57875f334f61aebed695e2e4193db5e", /success/).reply(200)

const child_process = require("child_process")
const execStub = sandbox.stub(child_process, "exec")
// first exec, will fork script to clone repo
execStub.onCall(0).yields(false)

nock("http://" + argoCDServer).get("/api/v1/applications/" + appName)
.reply(200, {"metadata": {}, "spec": {"source": { "path": appDir } } })
execStub.onCall(1).yields(false, appDiff)

// regex match post body should match diff produced by API
nock("https://api.github.com").post("/repos/robotland/test/issues/109/comments", /===== App Diff ====/).reply(200)
nock("https://api.github.com").post("/repos/robotland/test/issues/109/comments", /If Auto-sync is enabled just merge this PR to deploy the above changes/).reply(200)

let diffPayload = JSON.parse(JSON.stringify(payloadPr1))
diffPayload["comment"]["body"] = "argo diff --app " + appName
await probot.receive({name: "issue_comment", payload: diffPayload})
})
})

0 comments on commit fa7f003

Please sign in to comment.