wip
This commit is contained in:
committed by
mrugesh mohapatra
parent
0e5a338ac2
commit
5c926dc2dd
@ -1,4 +1,5 @@
|
||||
const debug = require("debug")("probot:presolver");
|
||||
const Presolver = require('./lib/presolver')
|
||||
/*const Presolver = {
|
||||
(github, { owner, repo, logger = console, ...config }) {
|
||||
//constructor(github, { owner, repo, logger = console, ...config }) {
|
||||
@ -22,6 +23,7 @@ const debug = require("debug")("probot:presolver");
|
||||
|
||||
//}
|
||||
|
||||
|
||||
async function probotPlugin(robot) {
|
||||
const events = [
|
||||
"pull_request.opened",
|
||||
@ -30,32 +32,38 @@ async function probotPlugin(robot) {
|
||||
"pull_request.reopened"
|
||||
];
|
||||
|
||||
robot.on(events, presolve);
|
||||
robot.on(events, presolve.bind(null, robot));
|
||||
}
|
||||
async function _getClashingRanges(context, pullRequest) {
|
||||
/*async function _getClashingRanges(context, pullRequest) {
|
||||
const repo = pullRequest.base.repo;
|
||||
//console.log(pullRequest)
|
||||
const owner = repo.owner;
|
||||
const number = pullRequest.number;
|
||||
|
||||
console.log(context)
|
||||
const prs = (await context.github.pullRequests.get({ owner, repo }))
|
||||
.data || [];
|
||||
prs.forEach(function(pr){
|
||||
const files = pr.files()
|
||||
console.log(files)
|
||||
})
|
||||
}
|
||||
async function presolver(context, pullRequest) {
|
||||
}*/
|
||||
/*async function presolver(context, pullRequest) {
|
||||
//Object.assign(this.pullRequest, pullRequest);
|
||||
//const { owner, repo } = this.config;
|
||||
//const number = this.pullRequest.number;
|
||||
|
||||
const state = await _getClashingRanges(context, pullRequest);
|
||||
|
||||
}*/
|
||||
async function presolve(app, context) {
|
||||
const presolver = forRepository(context);
|
||||
const pullRequest = getPullRequest(context);
|
||||
return presolver.presolve(pullRequest);
|
||||
}
|
||||
|
||||
function forRepository(context) {
|
||||
const config = Object.assign({}, context.repo({ logger: debug }));
|
||||
return new Presolver(context.github, config);
|
||||
return new Presolver(context, config);
|
||||
}
|
||||
|
||||
function getPullRequest(context) {
|
||||
@ -63,13 +71,6 @@ function getPullRequest(context) {
|
||||
}
|
||||
|
||||
|
||||
async function presolve(context) {
|
||||
//const presolved = //forRepository(context);
|
||||
const pullRequest = getPullRequest(context);
|
||||
//presolver.presolve(pullRequest);
|
||||
console.log(context);
|
||||
return presolver(context, pullRequest);
|
||||
}
|
||||
module.exports = probotPlugin;
|
||||
/*module.exports = async app => {
|
||||
// Your code here
|
||||
|
6
probot/presolver/lib/defaults.js
Normal file
6
probot/presolver/lib/defaults.js
Normal file
@ -0,0 +1,6 @@
|
||||
module.exports = {
|
||||
labelPRConflict: {
|
||||
name: "PR: potential-conflict",
|
||||
color: "c2e0c6"
|
||||
}
|
||||
};
|
89
probot/presolver/lib/presolver.js
Normal file
89
probot/presolver/lib/presolver.js
Normal file
@ -0,0 +1,89 @@
|
||||
class Presolver {
|
||||
constructor(context, { owner, repo, logger = console, ...config }) {
|
||||
this.context = context;
|
||||
this.github = context.github;
|
||||
this.logger = logger;
|
||||
this.config = Object.assign({}, require("./defaults"), config || {}, {
|
||||
owner,
|
||||
repo
|
||||
});
|
||||
this.pullRequest = {};
|
||||
this.conflictingFiles = [];
|
||||
}
|
||||
|
||||
async presolve(pullRequest) {
|
||||
Object.assign(this.pullRequest, pullRequest);
|
||||
const { owner, repo } = this.config;
|
||||
const number = this.pullRequest.number;
|
||||
await this._ensurePresolverLabelExists();
|
||||
const state = await this._getState();
|
||||
/*switch(state) {
|
||||
case Presolver.STATE.CONFLICT:
|
||||
case Presolver.STATE.NOCONFLICT:
|
||||
default:
|
||||
throw new Error('Undefined state');*/
|
||||
|
||||
}
|
||||
async _getState() {
|
||||
const files = await this.github.pullRequests.getFiles(this.context.issue());
|
||||
console.log(files)
|
||||
const {owner, repo} = this.config;
|
||||
const prs = await this.github.pullRequests.getAll({ owner, repo })
|
||||
.data || [];
|
||||
//let state;
|
||||
console.log(prs)
|
||||
await this._getConflictingFiles(prs, files);
|
||||
/*if (conflictingFiles.length) {
|
||||
return Presolver.STATE.CONFLICT;
|
||||
} else {
|
||||
return Presolver.STATE.NOCONFLICT;
|
||||
}*/
|
||||
return;
|
||||
// ... this.pullRequest.
|
||||
/*
|
||||
const prs = (await context.github.pullRequests.get({ owner, repo }))
|
||||
.data || [];
|
||||
prs.forEach(function(pr){
|
||||
const files = pr.files()
|
||||
console.log(files)
|
||||
})
|
||||
*/
|
||||
}
|
||||
|
||||
async _getConflictingFiles(prs, files) {
|
||||
prs.forEach(function(pr){
|
||||
var prFiles = pr.getFiles();
|
||||
prFiles.forEach(function(file){
|
||||
files.forEach(function(f){
|
||||
console.log(f, file)
|
||||
if (f === file) {
|
||||
this.conflictingFiles.push(file)
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
async _ensurePresolverLabelExists() {
|
||||
const label = this.config.labelPRConflict;
|
||||
await this._createLabel(label);
|
||||
}
|
||||
|
||||
async _createLabel(labelObj) {
|
||||
const { owner, repo } = this.config;
|
||||
|
||||
return this.github.issues
|
||||
.getLabels({ owner, repo, name: labelObj.name })
|
||||
.catch(() => {
|
||||
console.log(labelObj)
|
||||
return this.github.issues.createLabel({
|
||||
owner,
|
||||
repo,
|
||||
name: labelObj.name,
|
||||
color: labelObj.color
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Presolver
|
@ -23,6 +23,7 @@
|
||||
"probot": "^7.2.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"expect": "^23.6.0",
|
||||
"jest": "^22.4.3",
|
||||
"nock": "^10.0.0",
|
||||
"nodemon": "^1.17.2",
|
||||
|
@ -1,5 +1,5 @@
|
||||
{
|
||||
"action": "labeled",
|
||||
"action": "reopened",
|
||||
"number": 13,
|
||||
"pull_request": {
|
||||
"url": "https://api.github.com/repos/tbushman/pu/pulls/13",
|
||||
@ -35,7 +35,7 @@
|
||||
},
|
||||
"body": "",
|
||||
"created_at": "2018-11-01T03:06:36Z",
|
||||
"updated_at": "2018-11-01T03:24:19Z",
|
||||
"updated_at": "2018-11-11T09:11:01Z",
|
||||
"closed_at": null,
|
||||
"merged_at": null,
|
||||
"merge_commit_sha": "fc696e1a288b07f66318e775fdd9ea619122d09e",
|
||||
@ -329,9 +329,9 @@
|
||||
},
|
||||
"author_association": "OWNER",
|
||||
"merged": false,
|
||||
"mergeable": true,
|
||||
"rebaseable": true,
|
||||
"mergeable_state": "clean",
|
||||
"mergeable": null,
|
||||
"rebaseable": null,
|
||||
"mergeable_state": "unknown",
|
||||
"merged_by": null,
|
||||
"comments": 0,
|
||||
"review_comments": 0,
|
||||
@ -341,14 +341,6 @@
|
||||
"deletions": 6,
|
||||
"changed_files": 1
|
||||
},
|
||||
"label": {
|
||||
"id": 511502933,
|
||||
"node_id": "MDU6TGFiZWw1MTE1MDI5MzM=",
|
||||
"url": "https://api.github.com/repos/tbushman/pu/labels/enhancement",
|
||||
"name": "enhancement",
|
||||
"color": "84b6eb",
|
||||
"default": true
|
||||
},
|
||||
"repository": {
|
||||
"id": 77999410,
|
||||
"node_id": "MDEwOlJlcG9zaXRvcnk3Nzk5OTQxMA==",
|
@ -1,25 +1,42 @@
|
||||
const nock = require('nock')
|
||||
// Requiring our app implementation
|
||||
const myProbotApp = require('..')
|
||||
const expect = require('expect');
|
||||
const { Probot } = require('probot')
|
||||
const prSuccessEvent = require('./events/pullRequests.opened')
|
||||
const probotPlugin = require('..')
|
||||
// Requiring our fixtures
|
||||
const payload = require('./fixtures/pullRequests.labeled')
|
||||
const issueCreatedBody = { body: 'Thanks for contributing!' }
|
||||
//const payload = require('./fixtures/pullRequests.labeled')
|
||||
//const issueCreatedBody = { body: 'Thanks for contributing!' }
|
||||
// const url = (process.env.NODE_ENV === 'production' ? 'https://api.github.com' : 'https://smee.io/Vq0IH8tsXTuCp6kM')
|
||||
nock.disableNetConnect()
|
||||
//nock.disableNetConnect()
|
||||
|
||||
describe('My Probot app', () => {
|
||||
describe('Presolver', () => {
|
||||
let probot, github
|
||||
|
||||
beforeEach(() => {
|
||||
probot = new Probot({})
|
||||
// Load our app into probot
|
||||
let app = probot.load(myProbotApp)
|
||||
let app = probot.load(probotPlugin)
|
||||
// This is an easy way to mock out the GitHub API
|
||||
// https://probot.github.io/docs/testing/
|
||||
github = {
|
||||
issues: {
|
||||
createComment: jest.fn().mockReturnValue(Promise.resolve({}))
|
||||
createComment: jest.fn().mockReturnValue(Promise.resolve({})),
|
||||
addLabels: jest.fn(),
|
||||
getLabels: jest.fn().mockImplementation(() => Promise.resolve([])),
|
||||
createLabel: jest.fn()
|
||||
},
|
||||
repos: {
|
||||
getContent: () => Promise.resolve({data: Buffer.from(`
|
||||
issueOpened: Message
|
||||
pullRequestOpened: Message
|
||||
`).toString('base64')})
|
||||
},
|
||||
pullRequests: {
|
||||
getFiles: jest.fn().mockImplementation(() => ({
|
||||
data: [
|
||||
{filename: 'test.txt'}
|
||||
]
|
||||
})),
|
||||
getAll: jest.fn().mockResolvedValue({ data: [prSuccessEvent] })
|
||||
}
|
||||
}
|
||||
app.auth = () => Promise.resolve(github)
|
||||
@ -28,21 +45,9 @@ describe('My Probot app', () => {
|
||||
})
|
||||
|
||||
test('creates a comment when an issue is opened', async () => {
|
||||
/*nock(url)
|
||||
.post('/app/installations/421598/access_tokens')
|
||||
.reply(200, { token: 'test' })*/
|
||||
|
||||
// Test that a comment is posted
|
||||
/*nock('https://api.github.com')
|
||||
.post('/repos/hiimbex/testing-things/issues/1/comments', (body) => {
|
||||
expect(body).toMatchObject(issueCreatedBody)
|
||||
return true
|
||||
})
|
||||
.reply(200)*/
|
||||
|
||||
// Receive a webhook event
|
||||
await probot.receive({name: 'pull_request.opened', payload: payload})
|
||||
expect(github.issues.createComment).toHaveBeenCalled()
|
||||
await probot.receive({name: 'pull_request.opened', payload: prSuccessEvent})
|
||||
expect(github.issues.createLabel).toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -1494,6 +1494,18 @@ expect@^22.4.0:
|
||||
jest-message-util "^22.4.3"
|
||||
jest-regex-util "^22.4.3"
|
||||
|
||||
expect@^23.6.0:
|
||||
version "23.6.0"
|
||||
resolved "https://registry.yarnpkg.com/expect/-/expect-23.6.0.tgz#1e0c8d3ba9a581c87bd71fb9bc8862d443425f98"
|
||||
integrity sha512-dgSoOHgmtn/aDGRVFWclQyPDKl2CQRq0hmIEoUAuQs/2rn2NcvCWcSCovm6BLeuB/7EZuLGu2QfnR+qRt5OM4w==
|
||||
dependencies:
|
||||
ansi-styles "^3.2.0"
|
||||
jest-diff "^23.6.0"
|
||||
jest-get-type "^22.1.0"
|
||||
jest-matcher-utils "^23.6.0"
|
||||
jest-message-util "^23.4.0"
|
||||
jest-regex-util "^23.3.0"
|
||||
|
||||
express-async-errors@^3.0.0:
|
||||
version "3.1.1"
|
||||
resolved "https://registry.yarnpkg.com/express-async-errors/-/express-async-errors-3.1.1.tgz#6053236d61d21ddef4892d6bd1d736889fc9da41"
|
||||
@ -2538,6 +2550,16 @@ jest-diff@^22.4.0, jest-diff@^22.4.3:
|
||||
jest-get-type "^22.4.3"
|
||||
pretty-format "^22.4.3"
|
||||
|
||||
jest-diff@^23.6.0:
|
||||
version "23.6.0"
|
||||
resolved "https://registry.yarnpkg.com/jest-diff/-/jest-diff-23.6.0.tgz#1500f3f16e850bb3d71233408089be099f610c7d"
|
||||
integrity sha512-Gz9l5Ov+X3aL5L37IT+8hoCUsof1CVYBb2QEkOupK64XyRR3h+uRpYIm97K7sY8diFxowR8pIGEdyfMKTixo3g==
|
||||
dependencies:
|
||||
chalk "^2.0.1"
|
||||
diff "^3.2.0"
|
||||
jest-get-type "^22.1.0"
|
||||
pretty-format "^23.6.0"
|
||||
|
||||
jest-docblock@^22.4.0, jest-docblock@^22.4.3:
|
||||
version "22.4.3"
|
||||
resolved "http://registry.npmjs.org/jest-docblock/-/jest-docblock-22.4.3.tgz#50886f132b42b280c903c592373bb6e93bb68b19"
|
||||
@ -2605,6 +2627,15 @@ jest-matcher-utils@^22.4.0, jest-matcher-utils@^22.4.3:
|
||||
jest-get-type "^22.4.3"
|
||||
pretty-format "^22.4.3"
|
||||
|
||||
jest-matcher-utils@^23.6.0:
|
||||
version "23.6.0"
|
||||
resolved "https://registry.yarnpkg.com/jest-matcher-utils/-/jest-matcher-utils-23.6.0.tgz#726bcea0c5294261a7417afb6da3186b4b8cac80"
|
||||
integrity sha512-rosyCHQfBcol4NsckTn01cdelzWLU9Cq7aaigDf8VwwpIRvWE/9zLgX2bON+FkEW69/0UuYslUe22SOdEf2nog==
|
||||
dependencies:
|
||||
chalk "^2.0.1"
|
||||
jest-get-type "^22.1.0"
|
||||
pretty-format "^23.6.0"
|
||||
|
||||
jest-message-util@^22.4.0, jest-message-util@^22.4.3:
|
||||
version "22.4.3"
|
||||
resolved "http://registry.npmjs.org/jest-message-util/-/jest-message-util-22.4.3.tgz#cf3d38aafe4befddbfc455e57d65d5239e399eb7"
|
||||
@ -2615,6 +2646,17 @@ jest-message-util@^22.4.0, jest-message-util@^22.4.3:
|
||||
slash "^1.0.0"
|
||||
stack-utils "^1.0.1"
|
||||
|
||||
jest-message-util@^23.4.0:
|
||||
version "23.4.0"
|
||||
resolved "https://registry.yarnpkg.com/jest-message-util/-/jest-message-util-23.4.0.tgz#17610c50942349508d01a3d1e0bda2c079086a9f"
|
||||
integrity sha1-F2EMUJQjSVCNAaPR4L2iwHkIap8=
|
||||
dependencies:
|
||||
"@babel/code-frame" "^7.0.0-beta.35"
|
||||
chalk "^2.0.1"
|
||||
micromatch "^2.3.11"
|
||||
slash "^1.0.0"
|
||||
stack-utils "^1.0.1"
|
||||
|
||||
jest-mock@^22.4.3:
|
||||
version "22.4.3"
|
||||
resolved "http://registry.npmjs.org/jest-mock/-/jest-mock-22.4.3.tgz#f63ba2f07a1511772cdc7979733397df770aabc7"
|
||||
@ -2623,6 +2665,11 @@ jest-regex-util@^22.1.0, jest-regex-util@^22.4.3:
|
||||
version "22.4.3"
|
||||
resolved "http://registry.npmjs.org/jest-regex-util/-/jest-regex-util-22.4.3.tgz#a826eb191cdf22502198c5401a1fc04de9cef5af"
|
||||
|
||||
jest-regex-util@^23.3.0:
|
||||
version "23.3.0"
|
||||
resolved "https://registry.yarnpkg.com/jest-regex-util/-/jest-regex-util-23.3.0.tgz#5f86729547c2785c4002ceaa8f849fe8ca471bc5"
|
||||
integrity sha1-X4ZylUfCeFxAAs6qj4Sf6MpHG8U=
|
||||
|
||||
jest-resolve-dependencies@^22.1.0:
|
||||
version "22.4.3"
|
||||
resolved "http://registry.npmjs.org/jest-resolve-dependencies/-/jest-resolve-dependencies-22.4.3.tgz#e2256a5a846732dc3969cb72f3c9ad7725a8195e"
|
||||
@ -3717,6 +3764,14 @@ pretty-format@^22.4.0, pretty-format@^22.4.3:
|
||||
ansi-regex "^3.0.0"
|
||||
ansi-styles "^3.2.0"
|
||||
|
||||
pretty-format@^23.6.0:
|
||||
version "23.6.0"
|
||||
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-23.6.0.tgz#5eaac8eeb6b33b987b7fe6097ea6a8a146ab5760"
|
||||
integrity sha512-zf9NV1NSlDLDjycnwm6hpFATCGl/K1lt0R/GdkAK2O5LN/rwJoB+Mh93gGJjut4YbmecbfgLWVGSTCr0Ewvvbw==
|
||||
dependencies:
|
||||
ansi-regex "^3.0.0"
|
||||
ansi-styles "^3.2.0"
|
||||
|
||||
private@^0.1.8:
|
||||
version "0.1.8"
|
||||
resolved "https://registry.yarnpkg.com/private/-/private-0.1.8.tgz#2381edb3689f7a53d653190060fcf822d2f368ff"
|
||||
|
Reference in New Issue
Block a user