Skip to content

Instantly share code, notes, and snippets.

@chamerling
Created November 14, 2018 21:44
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/a0cb00884173dff10ff0cc2013fd0948 to your computer and use it in GitHub Desktop.
Save chamerling/a0cb00884173dff10ff0cc2013fd0948 to your computer and use it in GitHub Desktop.
const axios = require('axios');
const { from, interval } = require('rxjs');
const { switchMap, pluck, flatMap, distinct, skip, share } = require('rxjs/operators');
const baseURL = process.env.GITLAB_ENDPOINT || 'https://gitlab.com';
const privateToken = process.env.GITLAB_TOKEN;
const pollingInterval = 2000;
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}`);
}
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'),
distinct(),
skip()
);
}
function userNotesCount$(shareMergeRequest$) {
return shareMergeRequest$.pipe(
pluck('user_notes_count'),
distinct(),
skip()
);
}
const newMergeRequestSubscription = newMergeRequest$.subscribe(mr => {
console.log(new Date(), 'There is a new merge request', mr.title);
const shareMergeRequest$ = mergeRequest$(mr).pipe(share());
const upvotesSubscription = upvotes$(shareMergeRequest$).subscribe(upvotes => {
console.log(`👍 on ${mr.title} (${upvotes})`);
});
const userNotesCountSubscription = userNotesCount$(shareMergeRequest$).subscribe(user_notes_count => {
console.log(`New comment on ${mr.title} (${user_notes_count})`);
});
});
(function wait() {
setTimeout(wait, 1000);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment