Skip to content

Commit

Permalink
Fixes a bug in adding a new label to a repo
Browse files Browse the repository at this point in the history
  • Loading branch information
orta committed Aug 28, 2018
1 parent a4c420f commit 5d62ea3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 29 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

## Master

# 3.8.5 - 3.87

- Adds a function to handle creating or adding a label on a PR/Issue. Works with both Danger and Peril:
`danger.github.createOrAddLabel` - [@orta][]

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "danger",
"version": "3.8.4",
"version": "3.8.7",
"description": "Unit tests for Team Culture",
"main": "distribution/danger.js",
"typings": "distribution/danger.d.ts",
Expand Down
16 changes: 12 additions & 4 deletions source/platforms/github/GitHubUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,18 @@ const utils = (pr: GitHubPRDSL, api: GitHub): GitHubUtilsDSL => {
// Create or re-use an existing label
const config = repoConfig || { owner: pr.base.repo.owner.login, repo: pr.base.repo.name, id: pr.number }

const existingLabels = await api.issues.getLabels({ owner: config.owner, repo: config.repo })
const mergeOnGreen = existingLabels.data.find((l: any) => l.name == labelConfig.name)
d("Checking for existing labels")
let label: any = null
try {
const existingLabels = await api.issues.getLabels({ owner: config.owner, repo: config.repo })
label = existingLabels.data.find((l: any) => l.name == labelConfig.name)
} catch (e) {
d("api.issues.getLabels gave an error", e)
}

// Create the label if it doesn't exist yet
if (!mergeOnGreen) {
if (!label) {
d("no label found, creating a new one for this repo")
await api.issues.createLabel({
owner: config.owner,
repo: config.repo,
Expand All @@ -51,10 +58,11 @@ const utils = (pr: GitHubPRDSL, api: GitHub): GitHubUtilsDSL => {
})
}

d("adding a label to this pr")
// Then add the label
await api.issues.addLabels({
owner: config.owner,
repo: config.owner,
repo: config.repo,
number: config.id,
labels: [labelConfig.name],
})
Expand Down
27 changes: 3 additions & 24 deletions source/runner/Executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,6 @@ export class Executor {
public readonly options: ExecutorOptions
) {}

/** TODO: Next two functions aren't used in Danger, are they used in Peril? */

/** Mainly just a dumb helper because I can't do
* async functions in danger-run.js
* @param {string} file the path to run Danger from
* @returns {Promise<DangerResults>} The results of the Danger run
*/
async setupAndRunDanger(file: string) {
const runtimeEnv = await this.setupDanger()
return await this.runDanger(file, runtimeEnv)
}

/**
* Runs all of the operations for a running just Danger
* @returns {DangerfileRuntimeEnv} A runtime environment to run Danger in
*/
async setupDanger(): Promise<DangerContext> {
const dsl = await jsonDSLGenerator(this.platform)
const realDSL = await jsonToDSL(dsl)
const context = contextForDanger(realDSL)
return await this.runner.createDangerfileRuntimeEnvironment(context)
}

/**
* Runs all of the operations for a running just Danger
* @param {string} file the filepath to the Dangerfile
Expand Down Expand Up @@ -124,7 +101,9 @@ export class Executor {
async handleResults(results: DangerResults, git: GitDSL) {
validateResults(results)

this.d(`Got Results back, current settings`, this.options)
this.d("Got results back:", results)
this.d(`Evaluator settings`, this.options)

if (this.options.stdoutOnly || this.options.jsonOnly) {
await this.handleResultsPostingToSTDOUT(results)
} else {
Expand Down

0 comments on commit 5d62ea3

Please sign in to comment.