Skip to content

Instantly share code, notes, and snippets.

@chamerling
Created November 14, 2018 22:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chamerling/08f8762d3845874d834ffd95aeb987c1 to your computer and use it in GitHub Desktop.
Save chamerling/08f8762d3845874d834ffd95aeb987c1 to your computer and use it in GitHub Desktop.
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