Skip to content

Commit

Permalink
Merge pull request #30 from danger/tests_ok
Browse files Browse the repository at this point in the history
Add some test structure for the github class + flow typed definition
  • Loading branch information
orta committed Nov 5, 2016
2 parents e8636a7 + 1fbe5bc commit b396e91
Show file tree
Hide file tree
Showing 14 changed files with 397 additions and 49 deletions.
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

// Add your own contribution below

* Started turning this into a real project by adding tests - orta
* `danger.pr` -> `danger.github.pr`, I've also created interfaces for them - orta

### 0.0.5-0.0.10

* Changes some files casing, added some logs, a bit of error reporting, and verifying everything works through npm - orta
Expand Down
15 changes: 0 additions & 15 deletions dangerfile.js
Original file line number Diff line number Diff line change
@@ -1,15 +0,0 @@
// @flow

// import danger from "danger"
import { danger, fail } from "./source/danger"

// warn on changes in Package.json and not in shrinkwrap
const hasChangelog = danger.git.modified_files.includes("changelog.md")
if (!hasChangelog) {
fail("No Changelog changes!")
}

// warn on changelog
// console.log(danger)
// console.log(danger.git)
// console.log(danger.git.created_files)
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
"prepublish": "npm run build",
"build": "babel source --out-dir distribution --source-maps",
"buildwatch": "babel source --watch --out-dir distribution",
"link": "npm run build ; chmod +x distribution/commands/danger.js ; npm link"
"link": "npm run build ; chmod +x distribution/commands/danger.js ; npm link",
"export-flowtype": "node scripts/create-flow-typed-export.js"

},
"repository": {
"type": "git",
Expand Down
55 changes: 55 additions & 0 deletions scripts/create-flow-typed-export.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
var fs = require("fs")
var fileOutput = ""

fs.readdir("source/dsl", (err, files) => {
if (err) { return console.log("Could not read DSL folder") }
for (var file of files) {
// Sometimes they have more stuff, in those cases
// offer a way to crop the file.
const content = fs.readFileSync(`source/dsl/${file}`).toString()
if (content.includes("/* END FLOWTYPE")) {
fileOutput += content.split("/* END FLOWTYPE")[0]
} else {
fileOutput += content
}
}

// The definition of all the exposed vars is inside
// the Dangerfile.js file.
const allDangerfile = fs.readFileSync("source/runner/Dangerfile.js").toString()
const moduleContext = allDangerfile.split("BEGIN FLOWTYPE EXPORT */")[1].split("/* END FLOWTYPE")[0]

// we need to add either `declare function` or `declare var` to the interface
const context = moduleContext.split("\n").map((line) => {
if ((line.length === 0) || (line.includes("*"))) { return line }
if (line.includes("(")) { return " declare function " + line.trim() }
if (line.includes(":")) { return " declare var " + line.trim() }
}).join("\n")

fileOutput += `
declare module "danger" {
declare module.exports: {
${context}
};
}
`
// Remove all JS-y bits
fileOutput = fileOutput.split("\n").filter((line) => {
return !line.startsWith("import type") &&
!line.startsWith('"use strict"') &&
!line.startsWith("// @flow") &&
!line.includes("* @type ")
}).join("\n")

// We don't export in the definitions files
fileOutput = fileOutput.replace(/export interface/gi, "interface")

// Remove any 2 line breaks
fileOutput = fileOutput.replace(/\n\s*\n/g, "\n")

// This is so you can get it for this repo 👍
fs.writeFileSync("flow-typed/npm/danger_v0.x.x.js", fileOutput)

console.log("Awesome - shipped to flow-typed/npm/danger_v0.x.x.js")
console.log("This should get sent to the main repo.")
})
1 change: 0 additions & 1 deletion source/ci_source/ci_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
export type Env = any;

/** The shape of an object that represents an individual CI */

export interface CISource {
/** The name, mainly for showing errors */
env: string,
Expand Down
5 changes: 4 additions & 1 deletion source/commands/danger-run.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ program
const source = getCISourceForEnv(process.env)
if (!source) {
console.log("Could not find a CI source for this run")
// Check for ENV["CI"] and wanr they might want a local command instead?
process.exitCode = 1
}

Expand All @@ -29,6 +30,8 @@ if (source && source.isPR) {
if (platform) {
console.log(`OK, looks good ${source.name} on ${platform.name}`)
const exec = new Executor(source, platform)
exec.run()
exec.setup()
const results = exec.run()
exec.handleResults(results)
}
}
31 changes: 27 additions & 4 deletions source/dsl/DangerDSL.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,37 @@
// @flow
"use strict"

import type { GitDSL } from "../dsl/Git"
import type { GitDSL } from "../dsl/GitDSL"
import type { GitHubDSL } from "../dsl/GitHubDSL"

export default class DangerDSL {
/**
* The Danger DSL provides the metadata for introspection
* in order to create your own rules.
*/
export interface DangerDSLType {
/**
* Details specific to the git changes within the code changes.
* Currently, this is just the raw file paths that have been
* added, removed or modified.
*/
git: GitDSL;
/**
* The GitHub metadata.
* Currently, this is just the raw PR information.
*/
github: GitHubDSL;
}

/* END FLOWTYPE EXPORT */

export class DangerDSL {
git: GitDSL
pr: any
github: GitHubDSL

constructor(pr: any, git: GitDSL) {
this.git = git
this.pr = pr
this.github = {
pr
}
}
}
21 changes: 21 additions & 0 deletions source/dsl/GitDSL.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// @flow
"use strict"

export interface GitDSL {
/**
* Filepaths with changes relative to the git root
* @type {string[]}
*/
modified_files: string[],
/**
* Newly created filepaths relative to the git root
* @type {string[]}
*/
created_files: string[],
/**
* Removed filepaths relative to the git root
* @type {string[]}
*/
deleted_files: string[]
}

180 changes: 180 additions & 0 deletions source/dsl/GitHubDSL.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
// @flow
"use strict"

export interface GitHubDSL {
/**
* The PR metadata for a code review session
* @type {GitHubPRDSL}
*/
pr: GitHubPRDSL
}

/**
* A GitHub user account
*/
export interface GitHubUser {
/**
* Generic UUID
* @type {number}
*/
id: number,
/**
* The handle for the user/org
* @type {string}
*/
login: string,
/**
* Whether the user is an org, or a user
* @type {string}
*/
type: "User" | "Organization"
}

/**
* A GitHub Repo
*/
export interface GitHubRepo {
/**
* Generic UUID
* @type {number}
*/
id: number,

/**
* The name of the repo, e.g. "Danger-JS"
* @type {string}
*/
name: string,

/**
* The full name of the owner + repo, e.g. "Danger/Danger-JS"
* @type {string}
*/
full_name: string,

/**
* The owner of the repo
* @type {GitHubUser}
*/
owner: GitHubUser,

/**
* Is the repo publicly accessible?
* @type {bool}
*/
private: bool,

/**
* The textual description of the repo
* @type {string}
*/
description: string,

/**
* Is the repo a fork?
* @type {bool}
*/
fork: false
}

export interface GitHubMergeRef {
/**
* The human display name for the merge reference, e.g. "artsy:master"
* @type {string}
*/
label: string,

/**
* The reference point for the merge, e.g. "master"
* @type {string}
*/
ref: string,

/**
* The reference point for the merge, e.g. "704dc55988c6996f69b6873c2424be7d1de67bbe"
* @type {string}
*/
sha: string,

/**
* The user that owns the merge reference e.g. "artsy"
* @type {string}
*/
user: GitHubUser
}

export interface GitHubPRDSL {
/**
* The UUID for the PR
* @type {number}
*/
number: number,

/**
* The state for the PR
* @type {string}
*/
state: "closed" | "open" | "locked" | "merged",

/**
* Has the PR been locked to contributors only?
* @type {boolean}
*/
locked: boolean,

/**
* The title of the PR
* @type {string}
*/
title: string,

/**
* The markdown body message of the PR
* @type {string}
*/
body: string,

/**
* ISO6801 Date string for when PR was created
* @type {string}
*/
created_at: string,

/**
* ISO6801 Date string for when PR was updated
* @type {string}
*/
updated_at: string,

/**
* optional ISO6801 Date string for when PR was closed
* @type {string}
*/
closed_at: ?string,

/**
* Optional ISO6801 Date string for when PR was merged.
* Danger probably shouldn't be running in this state.
* @type {string}
*/
merged_at: ?string,

/**
* Merge reference for the _other_ repo.
* @type {GitHubMergeRef}
*/
head: GitHubMergeRef,

/**
* Merge reference for _this_ repo.
* @type {GitHubMergeRef}
*/
base: GitHubMergeRef,

/**
* The User who submitted the PR
* @type {GitHubUser}
*/
user: GitHubUser
}

9 changes: 0 additions & 9 deletions source/dsl/git.js

This file was deleted.

0 comments on commit b396e91

Please sign in to comment.