|
| 1 | +const github = require('@actions/github'); |
| 2 | +const subDays = require('date-fns/subDays'); |
| 3 | + |
| 4 | +// read from secrets |
| 5 | +const GITHUB_TOKEN = process.env.GH_PROJECT_TOKEN; |
| 6 | + |
| 7 | +if (!GITHUB_TOKEN) |
| 8 | + throw new Error("no token provided"); |
| 9 | + |
| 10 | +// read from secrets |
| 11 | +const config = { |
| 12 | + owner:"metosin", |
| 13 | + ownerType: "organization", |
| 14 | + projectNumber: 4, |
| 15 | + datesBack: 7 |
| 16 | +}; |
| 17 | + |
| 18 | +const repos = ["malli", "reitit"]; |
| 19 | + |
| 20 | +const octokit = github.getOctokit(GITHUB_TOKEN); |
| 21 | + |
| 22 | +const syncRepoIssuesToProject = async (config, repo) => { |
| 23 | + const projectResult = await octokit.graphql(` |
| 24 | + query { |
| 25 | + ${config.ownerType}(login: "${config.owner}"){ |
| 26 | + projectV2(number: ${config.projectNumber}) { |
| 27 | + id |
| 28 | + } |
| 29 | + } |
| 30 | + }`); |
| 31 | + |
| 32 | + const projectId = projectResult[config.ownerType].projectV2.id; |
| 33 | + |
| 34 | + const openIssuesAndPullRequests = await octokit.rest.issues.listForRepo( |
| 35 | + { |
| 36 | + owner: config.owner, |
| 37 | + repo: repo, |
| 38 | + state: 'open', |
| 39 | + since: subDays(new Date(), config.datesBack).toISOString() |
| 40 | + }); |
| 41 | + |
| 42 | + for (issue of openIssuesAndPullRequests.data) { |
| 43 | + |
| 44 | + console.log(`Add ${repo}/#${issue.number} ${issue.title}`); |
| 45 | + |
| 46 | + try { |
| 47 | + const result = await octokit.graphql(` |
| 48 | + mutation { |
| 49 | + addProjectV2ItemById(input: {projectId: "${projectId}" contentId: "${issue.node_id}"}) { |
| 50 | + item { |
| 51 | + id |
| 52 | + } |
| 53 | + } |
| 54 | + }`); |
| 55 | + } catch (error){ |
| 56 | + console.log("Error while syncing issue"); |
| 57 | + console.log(issue); |
| 58 | + console.log(error); |
| 59 | + } |
| 60 | + } |
| 61 | +}; |
| 62 | + |
| 63 | +const syncRepos = async (config, repos) => { |
| 64 | + for (repo of repos) { |
| 65 | + await syncRepoIssuesToProject(config, repo); |
| 66 | + } |
| 67 | +}; |
| 68 | + |
| 69 | +// run the sync |
| 70 | +syncRepos(config, repos); |
0 commit comments