Skip to content

Commit

Permalink
Merge pull request #4 from lfre/add-slugify-and-environment-url
Browse files Browse the repository at this point in the history
Fixes issue with private repos, adds slugify for string macros and environment_url for deployments
  • Loading branch information
lfre authored Aug 20, 2019
2 parents fd9fee6 + ef7ac4d commit 54449c7
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 247 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,15 @@ There are 3 required fields: `baseUrl`, `ci`, and `type`. Lightkeeper is a budge
| Macro | Details |
| ---- | ------ |
| `{pr_number}` | The Pull Request Number. |
| `{branch}` | The branch name. |
| `{repo}` | A slugified repo name. |
| `{branch}` | A slugified branch name. |
| `{commit_hash}` | The full commit SHA. |
| `{commit_hash:n}` | A trimmed SHA, where `n` is a digit. |
| *`{target_url}` | The target url from the Github Response. |
| **`{environment_url}` | The url from the Deployment status. |

\* Available for statuses and deployments.
\** Available for deployments.

- Replace `ci` and `type` for your CI tool. Examples:

Expand Down Expand Up @@ -121,7 +124,7 @@ There are 3 required fields: `baseUrl`, `ci`, and `type`. Lightkeeper is a budge

```json
{
"baseUrl": "{target_url}",
"baseUrl": "{environment_url}",
"ci": "now",
"type": "deployment"
}
Expand All @@ -131,6 +134,10 @@ There are 3 required fields: `baseUrl`, `ci`, and `type`. Lightkeeper is a budge
> If you're unsure about the name of your CI tool, it's the name displayed under `Developer` in the application page:
> https://github.com/apps/[app-name]

**NOTE:** These providers and their settings can change. If the provided `ci` name above does not work, use the full visible name:

![](https://raw.githubusercontent.com/wiki/lfre/lightkeeper/images/full-ci-name.png)

## Configuration

Visit the [wiki](https://github.com/lfre/lightkeeper/wiki/Configuration-Options) for a full list of configuration options.
Expand Down
27 changes: 23 additions & 4 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,20 @@ async function onRequestedCheck(context) {
async function onDeployment(context) {
const {
deployment_status: {
id: status_id,
state,
creator: { login },
target_url,
environment
},
deployment: { sha: headSha },
deployment: { id: deployment_id, sha: headSha },
installation: { node_id: installationNode }
} = context.payload;

// skip for started or failed statuses
if (state !== 'success' || !headSha || environment !== 'staging') return;

const pullNumber = await getPullRequestNumber(context.github, headSha);
const pullNumber = await getPullRequestNumber(context, headSha);

if (!pullNumber) return;

Expand All @@ -112,12 +113,30 @@ async function onDeployment(context) {
})
);

// retrieve the `environment_url`
// TODO: Switch to `repos.getDeploymentStatus` when correct header is added
const {
data: { environment_url }
} = await context.github.request(
'GET /repos/:owner/:repo/deployments/:deployment_id/statuses/:status_id ',
context.repo({
deployment_id,
status_id,
headers: {
accept: 'application/vnd.github.ant-man-preview+json'
}
})
);

await run(
context,
null,
{ pullNumber, headBranch, headSha, installationNode },
isValidCheck([login], 'deployment'),
{ '{target_url}': target_url }
{
'{target_url}': target_url,
'{environment_url}': environment_url
}
);
}

Expand All @@ -138,7 +157,7 @@ async function onStatus(context) {

if (state !== 'success' || !headSha) return;

const pullNumber = await getPullRequestNumber(context.github, headSha);
const pullNumber = await getPullRequestNumber(context, headSha);

if (!pullNumber) return;

Expand Down
7 changes: 6 additions & 1 deletion app/session.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const slugify = require('@sindresorhus/slugify');
const { homepage } = require('../package.json');
const { getStats, processBudgets, processCategories, processLightWallet } = require('./budgets');
const Status = require('./status');
Expand Down Expand Up @@ -77,10 +78,14 @@ class Session {
this.logger.error('Runner setup failed', err);
return;
}

const { repo } = context.repo();

// set up the url formatter
try {
this.urlFormatter = urlFormatter(baseUrl, {
'{branch}': headBranch,
'{repo}': slugify(repo),
'{branch}': slugify(headBranch),
'{commit_hash}': headSha,
'{pr_number}': pullNumber,
...macros
Expand Down
33 changes: 19 additions & 14 deletions app/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,27 @@ ${linebreak}

/**
* Finds the pull request number from a commit hash
* @param {object} github Octokit github client
* @param {object} context The github event contextt
* @param {string} headSha The commit hash in the PR
*/
async function getPullRequestNumber(github, headSha) {
const { data: { items = [] } = {} } = await github.search.issuesAndPullRequests({
q: `SHA=${headSha}`,
per_page: 1
async function getPullRequestNumber(context, headSha) {
const {
data: pullRequests = []
} = await context.github.repos.listPullRequestsAssociatedWithCommit(
context.repo({
commit_sha: headSha,
per_page: 100
})
);

let pullNumber = null;

pullRequests.some(({ state, number }) => {
if (state === 'closed') return false;
pullNumber = number;
return true;
});

if (!items.length) return null;

// find the pr number
const { number: pullNumber, state } = items.pop();

if (state === 'closed') return null;

return pullNumber;
}

Expand Down Expand Up @@ -110,14 +115,14 @@ function parseConfig(config = {}) {

function urlFormatter(baseUrl, macros = {}) {
const macroReplacer = replaceMacros(macros);
const base = new URL(macroReplacer(baseUrl)).href;
const { href: base, pathname: basePath = '' } = new URL(macroReplacer(baseUrl));
return url => {
if (!url || url === base) return base;

if (url.startsWith('http')) {
return new URL(macroReplacer(url)).href;
}
return macroReplacer(resolve(base, url));
return macroReplacer(resolve(base, resolve(basePath, url.replace(/^\/+/, ''))));
};
}

Expand Down
6 changes: 3 additions & 3 deletions lighthouse/package-lock.json

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

Loading

1 comment on commit 54449c7

@vercel
Copy link

@vercel vercel bot commented on 54449c7 Aug 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.