-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |