Created
November 14, 2018 22:46
-
-
Save chamerling/08f8762d3845874d834ffd95aeb987c1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require('axios'); | |
const { from, interval, combineLatest } = require('rxjs'); | |
const { switchMap, pluck, flatMap, distinct, skipWhile, share } = require('rxjs/operators'); | |
const baseURL = process.env.GITLAB_ENDPOINT || 'https://gitlab.com'; | |
const privateToken = process.env.GITLAB_TOKEN; | |
const pollingInterval = 2000; | |
const UPVOTES = 2; | |
const client = axios.create({ baseURL, headers: { 'Private-Token': privateToken }}); | |
function fetchMergeRequests() { | |
return client.get('/api/v4/merge_requests?state=opened'); | |
} | |
function getMergeRequest({project_id, iid}) { | |
return client.get(`/api/v4/projects/${project_id}/merge_requests/${iid}`); | |
} | |
function merge({project_id, iid}) { | |
return client.put(`/api/v4/projects/${project_id}/merge_requests/${iid}/merge`); | |
} | |
const myMergeRequests$ = interval(pollingInterval).pipe( | |
switchMap(() => from(fetchMergeRequests())), | |
pluck('data') | |
); | |
const newMergeRequest$ = myMergeRequests$.pipe( | |
flatMap(mr => mr), | |
distinct(mr => mr.id) | |
); | |
function mergeRequest$(mr) { | |
return interval(pollingInterval).pipe( | |
switchMap(() => from(getMergeRequest(mr))), | |
pluck('data') | |
); | |
} | |
function upvotes$(shareMergeRequest) { | |
return shareMergeRequest.pipe( | |
pluck('upvotes'), | |
skipWhile(upvotes => upvotes < UPVOTES) | |
); | |
} | |
function mergeStatus$(shareMergeRequest$) { | |
return shareMergeRequest$.pipe( | |
pluck('merge_status'), | |
skipWhile(mergeStatus => mergeStatus !== 'can_be_merged') | |
); | |
} | |
const newMergeRequestSubscription = newMergeRequest$.subscribe(mr => { | |
console.log(new Date(), 'There is a new merge request', mr.title); | |
const shareMergeRequest$ = mergeRequest$(mr).pipe(share()); | |
const merginatorSubscription$ = combineLatest(upvotes$(shareMergeRequest$), mergeStatus$(shareMergeRequest$)).subscribe(() => { | |
console.log(`MERGINATOR is about to merge ${mr.title}`) | |
merge(mr).then(() => { | |
console.log('MERGINATOR 💪'); | |
merginatorSubscription$.unsubscribe(); | |
}).catch(err => { | |
console.log('MERGINATOR should call Chuck Norris', err); | |
}) | |
}); | |
}); | |
(function wait() { | |
setTimeout(wait, 1000); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment