|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const path = require('path'); |
| 4 | +const fs = require('fs'); |
| 5 | +const GitHubApi = require('github'); |
| 6 | +const github = new GitHubApi(); |
| 7 | + |
| 8 | + |
| 9 | +/** CONFIGURATION: change these things if you want to tweak how the runs are made. */ |
| 10 | + |
| 11 | +/** Path to the local ng-material. By default based on the location of this script. */ |
| 12 | +const localRepo = path.resolve(__dirname, '..'); |
| 13 | + |
| 14 | +/** Where to write the output from the presubmit script. */ |
| 15 | +const logDir = '/tmp/v1-pr-presubmit-logs'; |
| 16 | + |
| 17 | +/** |
| 18 | + * The presubmit script to use (can change this if you want to use a locally modified script). |
| 19 | + * The default path is stored in an environment variable because it references an internal-Google |
| 20 | + * location. |
| 21 | + */ |
| 22 | +const presubmitScript = path.join(process.env.MAT_PRESUBMIT_DIR, 'sync-material.sh'); |
| 23 | + |
| 24 | +/** Time to start presubmits. */ |
| 25 | +const startTime = '9:30 pm'; |
| 26 | + |
| 27 | +/** Number of minutes between presubmit runs. */ |
| 28 | +const intervalMinutes = 20; |
| 29 | + |
| 30 | +/** Instead of querying github for PR numbers, manually provide the PR numbers to be presubmit */ |
| 31 | +const explicitPullRequests = []; |
| 32 | + |
| 33 | +/** END OF CONFIGURATION. */ |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +/** Number of intervals that have scheduled tasks already. */ |
| 38 | +let presubmitCount = 0; |
| 39 | + |
| 40 | +if (explicitPullRequests.length) { |
| 41 | + writeScheduleScript(explicitPullRequests.map(n => ({number: n}))); |
| 42 | +} else { |
| 43 | + // Fetch PRs that are merge-ready but not merge-safe |
| 44 | + github.search.issues({ |
| 45 | + per_page: 100, |
| 46 | + q: 'repo:angular/material is:open type:pr label:"pr: merge ready" -label:"pr: merge safe"', |
| 47 | + }, (error, response) => { |
| 48 | + if (response) { |
| 49 | + writeScheduleScript(response.data.items); |
| 50 | + } else { |
| 51 | + console.error('Fetching merge-ready PRs failed.'); |
| 52 | + console.error(error); |
| 53 | + } |
| 54 | + }); |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | +function writeScheduleScript(prs) { |
| 59 | + let script = |
| 60 | + `#!/bin/bash \n\n` + |
| 61 | + `mkdir -p ${logDir} \n\n` + |
| 62 | + `# Be sure you have no locally modified files in your git client before running this. \n\n`; |
| 63 | + |
| 64 | + // Generate a command for each file to be piped into the `at` command, scheduling it to run at |
| 65 | + // a later time. |
| 66 | + for (const pr of prs) { |
| 67 | + script += |
| 68 | + `echo '(` + |
| 69 | + `cd ${localRepo} ; ` + |
| 70 | + `${presubmitScript} presubmit ${pr.number} --global 2>&1 > ${logDir}/pr-${pr.number}.txt ` + |
| 71 | + `)' | ` + |
| 72 | + `at ${startTime} today + ${intervalMinutes * presubmitCount} min ` + |
| 73 | + `\n`; |
| 74 | + |
| 75 | + presubmitCount++; |
| 76 | + } |
| 77 | + |
| 78 | + fs.writeFileSync(path.join(localRepo, 'dist', 'schedule-presubmit.sh'), script, 'utf-8'); |
| 79 | + |
| 80 | + console.log('schedule-presubmit.sh written to dist'); |
| 81 | + console.log('Be sure to prodaccess overnight, that you have no locally modified files, ' + |
| 82 | + 'and push_tag has been run'); |
| 83 | +} |
0 commit comments