diff --git a/.github/workflows/project-automation.yml b/.github/workflows/project-automation.yml new file mode 100644 index 0000000..542c4f2 --- /dev/null +++ b/.github/workflows/project-automation.yml @@ -0,0 +1,18 @@ +name: Project automation + +on: + schedule: + - cron: "0 0 * * *" + workflow_dispatch: {} + +jobs: + sync-project-issues: + name: Add issue/pull requests to project + runs-on: ubuntu-latest + env: + GH_PROJECT_TOKEN: ${{ secrets.GH_PROJECT_TOKEN }} + steps: + - uses: actions/checkout@v2 + - run: | + npm install @actions/github date-fns + node scripts/project-automation.js diff --git a/scripts/project-automation.js b/scripts/project-automation.js new file mode 100644 index 0000000..02af36b --- /dev/null +++ b/scripts/project-automation.js @@ -0,0 +1,70 @@ +const github = require('@actions/github'); +const subDays = require('date-fns/subDays'); + +// read from secrets +const GITHUB_TOKEN = process.env.GH_PROJECT_TOKEN; + +if (!GITHUB_TOKEN) + throw new Error("no token provided"); + +// read from secrets +const config = { + owner:"metosin", + ownerType: "organization", + projectNumber: 4, + datesBack: 7 +}; + +const repos = ["malli", "reitit"]; + +const octokit = github.getOctokit(GITHUB_TOKEN); + +const syncRepoIssuesToProject = async (config, repo) => { + const projectResult = await octokit.graphql(` + query { + ${config.ownerType}(login: "${config.owner}"){ + projectV2(number: ${config.projectNumber}) { + id + } + } + }`); + + const projectId = projectResult[config.ownerType].projectV2.id; + + const openIssuesAndPullRequests = await octokit.rest.issues.listForRepo( + { + owner: config.owner, + repo: repo, + state: 'open', + since: subDays(new Date(), config.datesBack).toISOString() + }); + + for (issue of openIssuesAndPullRequests.data) { + + console.log(`Add ${repo}/#${issue.number} ${issue.title}`); + + try { + const result = await octokit.graphql(` + mutation { + addProjectV2ItemById(input: {projectId: "${projectId}" contentId: "${issue.node_id}"}) { + item { + id + } + } + }`); + } catch (error){ + console.log("Error while syncing issue"); + console.log(issue); + console.log(error); + } + } +}; + +const syncRepos = async (config, repos) => { + for (repo of repos) { + await syncRepoIssuesToProject(config, repo); + } +}; + +// run the sync +syncRepos(config, repos);