Skip to content

Commit

Permalink
Merged by Peril
Browse files Browse the repository at this point in the history
Adds a create/update label function to the github utils func
  • Loading branch information
peril-staging[bot] authored Aug 13, 2018
2 parents acf9b5d + fbbcc1c commit a4c420f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

## Master

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

# 3.8.4

- Exposes some internals on module resolution to Peril - [@orta][]
Expand Down
13 changes: 13 additions & 0 deletions source/danger.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,19 @@ interface GitHubUtilsDSL {
content: string,
config: { title: string; open: boolean; owner: string; repo: string }
) => Promise<string>

/**
* An API for creating, or setting a label to an issue. Usable from Peril
* by adding an additional param for settings about a repo.
*
* @param {obj} labelConfig The config for the label
* @param {obj | undefined} Optional: the config for the issue
* @returns {Promise<undefined>} No return value.
*/
createOrAddLabel: (
labelConfig: { name: string; color: string; description: string },
repoConfig?: { owner: string; repo: string; id: number }
) => Promise<void>
}

/**
Expand Down
13 changes: 13 additions & 0 deletions source/dsl/GitHubDSL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ export interface GitHubUtilsDSL {
content: string,
config: { title: string; open: boolean; owner: string; repo: string }
) => Promise<string>

/**
* An API for creating, or setting a label to an issue. Usable from Peril
* by adding an additional param for settings about a repo.
*
* @param {obj} labelConfig The config for the label
* @param {obj | undefined} Optional: the config for the issue
* @returns {Promise<undefined>} No return value.
*/
createOrAddLabel: (
labelConfig: { name: string; color: string; description: string },
repoConfig?: { owner: string; repo: string; id: number }
) => Promise<void>
}

/**
Expand Down
31 changes: 31 additions & 0 deletions source/platforms/github/GitHubUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,41 @@ const utils = (pr: GitHubPRDSL, api: GitHub): GitHubUtilsDSL => {
return sentence(hrefs)
}

const createOrAddLabel = async (
labelConfig: { name: string; color: string; description: string },
repoConfig?: { owner: string; repo: string; id: number }
) => {
// 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)

// Create the label if it doesn't exist yet
if (!mergeOnGreen) {
await api.issues.createLabel({
owner: config.owner,
repo: config.repo,
name: labelConfig.name,
color: labelConfig.color,
description: labelConfig.description,
})
}

// Then add the label
await api.issues.addLabels({
owner: config.owner,
repo: config.owner,
number: config.id,
labels: [labelConfig.name],
})
}

return {
fileLinks,
fileContents: fileContentsGenerator(api, pr.head.repo.full_name, pr.head.ref),
createUpdatedIssueWithID: createUpdatedIssueWithIDGenerator(api),
createOrAddLabel,
}
}

Expand Down

0 comments on commit a4c420f

Please sign in to comment.